ogr 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +3 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +19 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ogr/graphs/breadth_first_search.rb +66 -0
- data/lib/ogr/graphs/depth_first_search.rb +40 -0
- data/lib/ogr/graphs/digraph.rb +29 -0
- data/lib/ogr/graphs/edge.rb +13 -0
- data/lib/ogr/graphs/edge_bag.rb +29 -0
- data/lib/ogr/graphs/graph.rb +105 -0
- data/lib/ogr/graphs/graph_as_list.rb +78 -0
- data/lib/ogr/graphs/graph_as_matrix.rb +67 -0
- data/lib/ogr/graphs/graph_as_tri_matrix.rb +28 -0
- data/lib/ogr/version.rb +3 -0
- data/lib/ogr.rb +18 -0
- data/ogr.gemspec +37 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 83cf5471104a54bfb6ed4f87203f21af7b6c0dc1
|
4
|
+
data.tar.gz: ecf312736e5b4a2a950fed9157d563625ff2d266
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c6a05934278805233e4a72479bd1aa6e1f8d172a25a5498da9c714acf61b9391723205d7a105a2910b87821408d3b9fc9c909ed1bb56fc2d72c23c463c7a5dc
|
7
|
+
data.tar.gz: 1c56b4b496bb8c60b237599c7c8d06597f5620fda246804f9b097114997ff1a4662b19d0d8e519252c750d6e9f3ddf51e3e2647cc09a6c00d0ecbfafc99253d3
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 knife
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# Ogr
|
2
|
+
|
3
|
+
General graph library for Ruby. Provides sparse(or dense), directed(or undirected) and weighted graphs for Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem install ogr
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'ogr'
|
15
|
+
graph = Ogr::Graph.new
|
16
|
+
```
|
17
|
+
|
18
|
+
To not have to type "Ogr::" before each class, use:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
include Ogr
|
22
|
+
graph = Graph.new
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
## Graph
|
27
|
+
|
28
|
+
Creating new Graph
|
29
|
+
|
30
|
+
First define edges:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
edges = []
|
34
|
+
edges << Edge.new('Jim','Bob')
|
35
|
+
edges << Edge.new('Jim','Tom')
|
36
|
+
edges << Edge.new('Bob','Jack')
|
37
|
+
edges << Edge.new('Tom','Bob')
|
38
|
+
```
|
39
|
+
or
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
edges = [
|
43
|
+
['Jim','Bob'],
|
44
|
+
['Jim','Tom'],
|
45
|
+
['Bob','Jack'],
|
46
|
+
['Tom','Bob']
|
47
|
+
]
|
48
|
+
```
|
49
|
+
|
50
|
+
New undirected graph (implemented as adjency list)
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
graph = Graph.new(edges)
|
54
|
+
```
|
55
|
+
|
56
|
+
New undirected dense graph (implemented as triangular matrix)
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
graph = Graph.create(edges)
|
60
|
+
```
|
61
|
+
or
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
graph = Graph.new(edges, :tri_matrix)
|
65
|
+
|
66
|
+
```
|
67
|
+
|
68
|
+
Examples:
|
69
|
+
```ruby
|
70
|
+
graph.vertex_size #=> 4
|
71
|
+
graph.degree("Bob") #=> 3
|
72
|
+
graph.edge?("Bob", "Tom") #=> true
|
73
|
+
graph.edge?("Tom", "Jack") #=> false
|
74
|
+
graph.add_edge(Edge.new("Bob", "Kate"))
|
75
|
+
graph.remove("Bob", "Jack")
|
76
|
+
graph.neighbors('Tom') #=> ["Bob", "Jim"]
|
77
|
+
```
|
78
|
+
|
79
|
+
Iterating
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
graph.each_edge{ |e| p e }
|
83
|
+
graph.each_vertex{ |v| p v }
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
## Digraph
|
88
|
+
|
89
|
+
Directed Weighted Graph
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
edges = []
|
93
|
+
edges << Edge.new(:A, :C, 5)
|
94
|
+
edges << Edge.new(:A, :D, 3)
|
95
|
+
edges << Edge.new(:A, :G, 14)
|
96
|
+
edges << Edge.new(:C, :E, 3)
|
97
|
+
edges << Edge.new(:C, :F, 2)
|
98
|
+
edges << Edge.new(:D, :C, 11)
|
99
|
+
edges << Edge.new(:D, :E, 7)
|
100
|
+
edges << Edge.new(:D, :G, 6)
|
101
|
+
edges << Edge.new(:G, :E, 7)
|
102
|
+
edges << Edge.new(:E, :B, 5)
|
103
|
+
edges << Edge.new(:G, :B, 6)
|
104
|
+
edges << Edge.new(:F, :B, 7)
|
105
|
+
```
|
106
|
+
|
107
|
+
New directed graph (implemented as adjency list)
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
wdigraph = Digraph.new(edges)
|
111
|
+
```
|
112
|
+
|
113
|
+
New directed dense graph (implemented as matrix)
|
114
|
+
```ruby
|
115
|
+
wdigraph = Digraph.create(edges)
|
116
|
+
```
|
117
|
+
or
|
118
|
+
```ruby
|
119
|
+
wdigraph = Digraph.new(edges, :matrix)
|
120
|
+
```
|
121
|
+
|
122
|
+
Examples
|
123
|
+
```ruby
|
124
|
+
wdigraph.get_edge(:D, :C).weight #=> 11
|
125
|
+
wdigraph.degree(:E) #=> 4
|
126
|
+
wdigraph.in_degree(:E) #=> 3
|
127
|
+
wdigraph.out_degree(:E) #=> 1
|
128
|
+
```
|
129
|
+
|
130
|
+
## Searching
|
131
|
+
|
132
|
+
Breadth First Search:
|
133
|
+
```ruby
|
134
|
+
BreadthFirstSearch.new(wdigraph).search(:A) #=> [:A, :C, :D, :G, :E, :F, :B]
|
135
|
+
```
|
136
|
+
|
137
|
+
Depth First Search:
|
138
|
+
```ruby
|
139
|
+
DepthFirstSearch.new(wdigraph).search(:A) #=> [:A, :G, :B, :E, :D, :C, :F]
|
140
|
+
`
|
141
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
Rake::TestTask.new do |t|
|
12
|
+
t.name = 'test:performance'
|
13
|
+
t.libs << 'test'
|
14
|
+
t.test_files = FileList['test/performance/*test.rb']
|
15
|
+
t.verbose = true
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Run tests'
|
19
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'ogr'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Class implements Breadth First Search in graphs
|
3
|
+
class BreadthFirstSearch
|
4
|
+
attr_accessor :parents, :visited, :distance
|
5
|
+
private :parents=, :visited=, :distance=
|
6
|
+
|
7
|
+
def initialize(graph)
|
8
|
+
@graph = graph
|
9
|
+
@colors = {}
|
10
|
+
@parents = {}
|
11
|
+
@visited = []
|
12
|
+
@distance = {}
|
13
|
+
@to_visit = SimpleQueue.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def search(s)
|
17
|
+
# TODO: Check if source exists in graph
|
18
|
+
reset!
|
19
|
+
visit_source(s)
|
20
|
+
until to_visit.empty?
|
21
|
+
v = to_visit.dequeue
|
22
|
+
visit_neighbors(v)
|
23
|
+
colors[v] = :black
|
24
|
+
visited << (block_given? ? yield(v) : v)
|
25
|
+
end
|
26
|
+
visited
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_accessor :graph, :to_visit, :colors
|
32
|
+
|
33
|
+
def reset!
|
34
|
+
self.visited = []
|
35
|
+
graph.each_vertex do |v|
|
36
|
+
colors[v] = :white
|
37
|
+
parents[v] = nil
|
38
|
+
distance[v] = Float::INFINITY
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def visit_source(s)
|
43
|
+
colors[s] = :white
|
44
|
+
parents[s] = nil
|
45
|
+
distance[s] = 0
|
46
|
+
to_visit.enqueue s
|
47
|
+
end
|
48
|
+
|
49
|
+
def visit_node(v, from)
|
50
|
+
colors[v] = :grey
|
51
|
+
parents[v] = from
|
52
|
+
distance[v] = distance[from] + 1
|
53
|
+
to_visit.enqueue v
|
54
|
+
end
|
55
|
+
|
56
|
+
def visit_neighbors(u)
|
57
|
+
graph.neighbors(u).each do |v|
|
58
|
+
visit_node(v, u) if not_visited?(v)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def not_visited?(v)
|
63
|
+
colors[v] == :white
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Class implements Breadth First Search in graphs
|
3
|
+
class DepthFirstSearch
|
4
|
+
attr_accessor :parents, :visited, :distance, :marked
|
5
|
+
private :parents=, :visited=, :distance=, :marked=
|
6
|
+
|
7
|
+
def initialize(graph)
|
8
|
+
@graph = graph
|
9
|
+
@parents = {}
|
10
|
+
@marked = {}
|
11
|
+
@visited = []
|
12
|
+
@distance = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def search(u, &block)
|
16
|
+
reset!
|
17
|
+
dfs(u, &block)
|
18
|
+
visited
|
19
|
+
end
|
20
|
+
|
21
|
+
def dfs(v, from = nil, &block)
|
22
|
+
marked[v] = true
|
23
|
+
visited << (block_given? ? yield(v) : v)
|
24
|
+
parents[v] = from
|
25
|
+
graph.neighbors(v).reverse_each do |u|
|
26
|
+
dfs(u, v, &block) unless marked[u]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
attr_reader :graph
|
33
|
+
|
34
|
+
def reset!
|
35
|
+
self.visited = []
|
36
|
+
self.parents = {}
|
37
|
+
self.marked = {}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Class implements directed graph
|
3
|
+
class Digraph < Graph
|
4
|
+
def initialize(edges, store = :list)
|
5
|
+
@map = IndexedSet.new
|
6
|
+
case store
|
7
|
+
when :list
|
8
|
+
@g = GraphAsList.new
|
9
|
+
when :matrix
|
10
|
+
@g = GraphAsMatrix.new
|
11
|
+
end
|
12
|
+
add_edges(edges)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.new_dense(args)
|
16
|
+
new(args, :matrix)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns number of incoming edges to given vertex.
|
20
|
+
def in_degree(x)
|
21
|
+
@g.degree index(x), :in
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns number of outcoming edges to given vertex.
|
25
|
+
def out_degree(x)
|
26
|
+
@g.degree index(x), :out
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Bag for Edge objects
|
3
|
+
class EdgeBag
|
4
|
+
include Enumerable
|
5
|
+
def initialize
|
6
|
+
@store = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(x, weight)
|
10
|
+
@store.push v: x, weight: weight
|
11
|
+
end
|
12
|
+
|
13
|
+
def remove(x)
|
14
|
+
@store.delete_if { |edge| edge[:v] == x }
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(x)
|
18
|
+
@store.find { |edge| edge[:v] == x }
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
@store.each(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def size
|
26
|
+
@store.size
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Graph class
|
3
|
+
class Graph
|
4
|
+
# Create new graph from array of edges. Second parameter determines
|
5
|
+
# graph internal implementation: :list (Adjency List), :tri_matrix (Triangular
|
6
|
+
# Matrix), :matrix (Matrix).
|
7
|
+
def initialize(edges = nil, store = :list)
|
8
|
+
@map = IndexedSet.new
|
9
|
+
case store
|
10
|
+
when :list
|
11
|
+
@g = GraphAsList.new(directed: false)
|
12
|
+
when :matrix
|
13
|
+
@g = GraphAsMatrix.new
|
14
|
+
when :tri_matrix
|
15
|
+
@g = GraphAsTriMatrix.new
|
16
|
+
end
|
17
|
+
|
18
|
+
add_edges(edges) if edges
|
19
|
+
end
|
20
|
+
|
21
|
+
# Create new graph from array of edges. Internally graph will be represented
|
22
|
+
# by Triangular Matrix.
|
23
|
+
def self.new_dense(edges)
|
24
|
+
new(edges, :tri_matrix)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Adds new edge to graph.
|
28
|
+
def add(x, y, weight = 1)
|
29
|
+
@g.add(push(x), push(y), weight)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Adds new edges to graph.
|
33
|
+
def add_edges(edges)
|
34
|
+
if edges[0].is_a? Array
|
35
|
+
edges.each { |e| add(e[0], e[1]) }
|
36
|
+
else
|
37
|
+
edges.each { |e| add(e.from, e.to, e.weight) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Adds new edge to graph.
|
42
|
+
def add_edge(edge)
|
43
|
+
add_edges([edge])
|
44
|
+
end
|
45
|
+
|
46
|
+
# Removes connection between vertex x and y.
|
47
|
+
def remove(x, y)
|
48
|
+
@g.remove(index(x), index(y))
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns all neighbors for given vertex.
|
52
|
+
def neighbors(x)
|
53
|
+
@g.neighbors(index(x)).map { |i| vertexes[i] }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns vertex degree.
|
57
|
+
def degree(x)
|
58
|
+
@g.degree index(x)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Checks if two elements are connected.
|
62
|
+
def edge?(x, y)
|
63
|
+
@g.edge?(index(x), index(y))
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns Edge(x,y) if exist.
|
67
|
+
def get_edge(x, y)
|
68
|
+
return nil unless edge?(x, y)
|
69
|
+
w = @g.get_edge(index(x), index(y))
|
70
|
+
Edge.new(x, y, w) if w
|
71
|
+
end
|
72
|
+
|
73
|
+
def vc
|
74
|
+
(0...vertexes.size)
|
75
|
+
end
|
76
|
+
|
77
|
+
def vertexes
|
78
|
+
@map.to_a
|
79
|
+
end
|
80
|
+
|
81
|
+
def vertex_size
|
82
|
+
vertexes.size
|
83
|
+
end
|
84
|
+
|
85
|
+
# Vertex iterator
|
86
|
+
def each_vertex
|
87
|
+
vc.each { |v| yield vertexes[v] }
|
88
|
+
end
|
89
|
+
|
90
|
+
# Edge iterator
|
91
|
+
def each_edge(&block)
|
92
|
+
@g.each_edge(vertexes, &block)
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def index(x)
|
98
|
+
@map.index(x)
|
99
|
+
end
|
100
|
+
|
101
|
+
def push(x)
|
102
|
+
@map.push(x)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Graph implemented as Adjency List
|
3
|
+
class GraphAsList
|
4
|
+
def initialize(opts = {})
|
5
|
+
@store = []
|
6
|
+
@directed = opts[:directed].nil? ? true : false
|
7
|
+
end
|
8
|
+
|
9
|
+
def undirected?
|
10
|
+
!@directed
|
11
|
+
end
|
12
|
+
|
13
|
+
# Adds new edges to graph.
|
14
|
+
def add(x, y, weight)
|
15
|
+
@store[x] ||= EdgeBag.new
|
16
|
+
@store[x].add(y, weight)
|
17
|
+
@store[y] ||= EdgeBag.new if undirected?
|
18
|
+
@store[y].add(x, weight) if undirected?
|
19
|
+
end
|
20
|
+
|
21
|
+
# Removes conection between vertex x and y.
|
22
|
+
def remove(x, y)
|
23
|
+
@store[x].remove(y)
|
24
|
+
@store[y].remove(x) if undirected?
|
25
|
+
end
|
26
|
+
|
27
|
+
# Checks if two elements are connected.
|
28
|
+
def edge?(x, y)
|
29
|
+
connected?(x, y)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns Edge(x,y) if exist.
|
33
|
+
def get_edge(x, y)
|
34
|
+
edge = get(x, y)
|
35
|
+
edge[:weight] if edge
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns all neighbors for given vertex.
|
39
|
+
def neighbors(x)
|
40
|
+
@store[x].map { |edge| edge[:v] }
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns vertex degree. Second parameter determines direction - :in incoming
|
44
|
+
# edges, :out - outcoming edges, :all - incoming and outcoming edges.
|
45
|
+
def degree(x, direction = :all)
|
46
|
+
r = Hash.new(0)
|
47
|
+
vc = (0..@store.size)
|
48
|
+
vc.each do |i|
|
49
|
+
r[:in] += 1 if connected?(i, x)
|
50
|
+
r[:out] += 1 if connected?(x, i)
|
51
|
+
end
|
52
|
+
r[:all] = r[:in] + r[:out]
|
53
|
+
return r[direction] / 2 if undirected?
|
54
|
+
r[direction]
|
55
|
+
end
|
56
|
+
|
57
|
+
def each_edge(vertexes)
|
58
|
+
@store.each_with_index do |list, i|
|
59
|
+
list.each do |vertex|
|
60
|
+
j = vertex[:v]
|
61
|
+
next if i > j && undirected?
|
62
|
+
w = get_edge(i, j)
|
63
|
+
yield Edge.new(vertexes[i], vertexes[j], w) if w
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def get(x, y)
|
71
|
+
@store[x] && @store[x].get(y)
|
72
|
+
end
|
73
|
+
|
74
|
+
def connected?(x, y)
|
75
|
+
!get(x, y).nil?
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Graph implemented as Matrix
|
3
|
+
class GraphAsMatrix
|
4
|
+
def initialize
|
5
|
+
@store = Array2D.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(x, y, weight)
|
9
|
+
@store[x, y] = weight
|
10
|
+
end
|
11
|
+
|
12
|
+
# Removes conection between vertex x and y.
|
13
|
+
def remove(x, y)
|
14
|
+
@store[x, y] = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
# Checks if two elements are connected.
|
18
|
+
def edge?(x, y)
|
19
|
+
connected?(x, y)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns Edge(x,y) if exist.
|
23
|
+
def get_edge(x, y)
|
24
|
+
@store[x, y]
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns all neighbors for given vertex.
|
28
|
+
def neighbors(v)
|
29
|
+
n = []
|
30
|
+
vertexes.each { |i| n << i if connected?(v, i) }
|
31
|
+
n
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns vertex degree. Second parameter determines direction - :in incoming
|
35
|
+
# edges, :out - outcoming edges, :all - incoming and outcoming edges.
|
36
|
+
def degree(x, direction = :all)
|
37
|
+
r = Hash.new(0)
|
38
|
+
vertexes.each do |i|
|
39
|
+
r[:in] += 1 if connected?(i, x)
|
40
|
+
r[:out] += 1 if connected?(x, i)
|
41
|
+
end
|
42
|
+
r[:all] = r[:in] + r[:out]
|
43
|
+
r[direction]
|
44
|
+
end
|
45
|
+
|
46
|
+
# Edge iterator
|
47
|
+
def each_edge(vertexes)
|
48
|
+
size = vertexes.size
|
49
|
+
(0...size).each do |v0|
|
50
|
+
(0...size).each do |v1|
|
51
|
+
weight = @store[v0, v1]
|
52
|
+
yield Edge.new(vertexes[v0], vertexes[v1], weight) if weight
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def connected?(x, y)
|
60
|
+
@store[x, y] > 0
|
61
|
+
end
|
62
|
+
|
63
|
+
def vertexes
|
64
|
+
(0..@store.size)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Ogr
|
2
|
+
# Graph implemented as Triangular Matrix
|
3
|
+
class GraphAsTriMatrix < GraphAsMatrix
|
4
|
+
def initialize
|
5
|
+
@store = TriMatrix.new(0)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Returns vertex degree.
|
9
|
+
def degree(x)
|
10
|
+
sum = 0
|
11
|
+
vc = (0..@store.size)
|
12
|
+
vc.each do |i|
|
13
|
+
sum += @store[x, i] if @store[x, i]
|
14
|
+
end
|
15
|
+
sum
|
16
|
+
end
|
17
|
+
|
18
|
+
# Edge iterator
|
19
|
+
def each_edge(vertexes)
|
20
|
+
size = vertexes.size
|
21
|
+
(0...size).each do |v0|
|
22
|
+
(0...v0).each do |v1|
|
23
|
+
yield Edge.new(vertexes[v0], vertexes[v1], @store[v0, v1]) if connected?(v0, v1)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/ogr/version.rb
ADDED
data/lib/ogr.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'ds'
|
2
|
+
|
3
|
+
require 'ogr/version'
|
4
|
+
|
5
|
+
require 'ogr/graphs/edge'
|
6
|
+
require 'ogr/graphs/graph_as_matrix'
|
7
|
+
require 'ogr/graphs/graph_as_list'
|
8
|
+
require 'ogr/graphs/graph_as_tri_matrix'
|
9
|
+
require 'ogr/graphs/edge_bag'
|
10
|
+
require 'ogr/graphs/graph'
|
11
|
+
require 'ogr/graphs/digraph'
|
12
|
+
require 'ogr/graphs/breadth_first_search'
|
13
|
+
require 'ogr/graphs/depth_first_search'
|
14
|
+
|
15
|
+
# Main module namespace
|
16
|
+
module Ogr
|
17
|
+
include DS
|
18
|
+
end
|
data/ogr.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ogr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ogr'
|
8
|
+
spec.version = Ogr::VERSION
|
9
|
+
spec.authors = ['knife']
|
10
|
+
spec.email = ['satre@o2.pl']
|
11
|
+
|
12
|
+
spec.summary = 'Graphs implemented in Ruby'
|
13
|
+
spec.description = 'Graphs library for Ruby'
|
14
|
+
spec.homepage = 'https://github.com/knife/ogr'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host',
|
18
|
+
# or delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.add_runtime_dependency 'ds', '~> 0.0'
|
33
|
+
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ogr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- knife
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ds
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Graphs library for Ruby
|
70
|
+
email:
|
71
|
+
- satre@o2.pl
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/ogr.rb
|
86
|
+
- lib/ogr/graphs/breadth_first_search.rb
|
87
|
+
- lib/ogr/graphs/depth_first_search.rb
|
88
|
+
- lib/ogr/graphs/digraph.rb
|
89
|
+
- lib/ogr/graphs/edge.rb
|
90
|
+
- lib/ogr/graphs/edge_bag.rb
|
91
|
+
- lib/ogr/graphs/graph.rb
|
92
|
+
- lib/ogr/graphs/graph_as_list.rb
|
93
|
+
- lib/ogr/graphs/graph_as_matrix.rb
|
94
|
+
- lib/ogr/graphs/graph_as_tri_matrix.rb
|
95
|
+
- lib/ogr/version.rb
|
96
|
+
- ogr.gemspec
|
97
|
+
homepage: https://github.com/knife/ogr
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.8
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Graphs implemented in Ruby
|
121
|
+
test_files: []
|
122
|
+
has_rdoc:
|