gratr19 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +335 -0
- data/examples/graph_self.rb +54 -0
- data/examples/module_graph.jpg +0 -0
- data/examples/module_graph.rb +12 -0
- data/examples/self_graph.jpg +0 -0
- data/examples/visualize.jpg +0 -0
- data/examples/visualize.rb +8 -0
- data/install.rb +49 -0
- data/lib/gratr.rb +42 -0
- data/lib/gratr/adjacency_graph.rb +230 -0
- data/lib/gratr/base.rb +34 -0
- data/lib/gratr/biconnected.rb +116 -0
- data/lib/gratr/chinese_postman.rb +123 -0
- data/lib/gratr/common.rb +74 -0
- data/lib/gratr/comparability.rb +92 -0
- data/lib/gratr/digraph.rb +115 -0
- data/lib/gratr/digraph_distance.rb +185 -0
- data/lib/gratr/dot.rb +90 -0
- data/lib/gratr/edge.rb +145 -0
- data/lib/gratr/graph.rb +314 -0
- data/lib/gratr/graph_api.rb +82 -0
- data/lib/gratr/import.rb +44 -0
- data/lib/gratr/labels.rb +103 -0
- data/lib/gratr/maximum_flow.rb +107 -0
- data/lib/gratr/rdot.rb +332 -0
- data/lib/gratr/search.rb +422 -0
- data/lib/gratr/strong_components.rb +127 -0
- data/lib/gratr/undirected_graph.rb +153 -0
- data/lib/gratr/version.rb +6 -0
- data/lib/priority-queue/benchmark/dijkstra.rb +171 -0
- data/lib/priority-queue/compare_comments.rb +49 -0
- data/lib/priority-queue/ext/priority_queue/CPriorityQueue/extconf.rb +2 -0
- data/lib/priority-queue/lib/priority_queue.rb +14 -0
- data/lib/priority-queue/lib/priority_queue/c_priority_queue.rb +1 -0
- data/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb +46 -0
- data/lib/priority-queue/lib/priority_queue/ruby_priority_queue.rb +525 -0
- data/lib/priority-queue/setup.rb +1551 -0
- data/lib/priority-queue/test/priority_queue_test.rb +371 -0
- data/tests/TestBiconnected.rb +53 -0
- data/tests/TestChinesePostman.rb +53 -0
- data/tests/TestComplement.rb +54 -0
- data/tests/TestDigraph.rb +333 -0
- data/tests/TestDigraphDistance.rb +138 -0
- data/tests/TestDot.rb +75 -0
- data/tests/TestEdge.rb +171 -0
- data/tests/TestInspection.rb +57 -0
- data/tests/TestMultiEdge.rb +57 -0
- data/tests/TestNeighborhood.rb +64 -0
- data/tests/TestProperties.rb +160 -0
- data/tests/TestSearch.rb +277 -0
- data/tests/TestStrongComponents.rb +85 -0
- data/tests/TestTriagulated.rb +137 -0
- data/tests/TestUndirectedGraph.rb +219 -0
- metadata +152 -0
@@ -0,0 +1,219 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
+
# Copyright (c) 2002,2004,2005 by Horst Duchene
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
6
|
+
# are permitted provided that the following conditions are met:
|
7
|
+
#
|
8
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
9
|
+
# this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer in the documentation
|
12
|
+
# and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
14
|
+
# may be used to endorse or promote products derived from this software
|
15
|
+
# without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
18
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
21
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
23
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
24
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
25
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#++
|
28
|
+
|
29
|
+
|
30
|
+
require 'test/unit'
|
31
|
+
require 'gratr/import'
|
32
|
+
|
33
|
+
class TestUndirectedGraph < Test::Unit::TestCase # :nodoc:
|
34
|
+
|
35
|
+
def setup
|
36
|
+
@single = UndirectedGraph[1,2, 2,3, 3,4, 4,4, 1,2, 2,3]
|
37
|
+
@dups = UndirectedPseudoGraph[1,2, 2,3, 3,4, 4,4, 1,2, 2,3]
|
38
|
+
@loops = UndirectedMultiGraph[1,2, 2,3, 3,4, 4,4, 1,2, 2,3]
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_new
|
42
|
+
assert_equal UndirectedGraph[1,2, 2,3, 3,4, 4,4], @single
|
43
|
+
assert_equal UndirectedPseudoGraph[1,2, 2,3, 3,4, 4,4, 1,2, 2,3], @dups
|
44
|
+
assert_equal UndirectedMultiGraph[1,2, 2,3, 3,4, 4,4, 1,2, 2,3], @loops
|
45
|
+
assert_raise(ArgumentError) {UndirectedGraph.new(:bomb)}
|
46
|
+
assert_raise(ArgumentError) {UndirectedGraph.new(1)}
|
47
|
+
assert_equal @single, UndirectedGraph.new(@single)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_edges
|
51
|
+
assert @single.edges.include?(Edge[1,2])
|
52
|
+
assert @single.edges.include?(Edge[2,3])
|
53
|
+
assert @single.edges.include?(Edge[3,4])
|
54
|
+
assert !@single.edges.include?(Edge[4,4])
|
55
|
+
assert @loops.edges.include?(MultiEdge[4,4])
|
56
|
+
assert @single.edges.include?(Edge[1,2])
|
57
|
+
assert @single.edges.include?(Edge[2,3])
|
58
|
+
assert !@single.edges.include?(Edge[1,3])
|
59
|
+
assert @single.edge?(2,3)
|
60
|
+
assert !@single.edge?(1,4)
|
61
|
+
assert @single.edge?(Edge[1,2])
|
62
|
+
assert !@single.add_edge!(5,5).edge?(5,5)
|
63
|
+
assert !@dups.add_edge!(5,5).edge?(5,5)
|
64
|
+
assert @loops.add_edge!(5,5).edge?(5,5)
|
65
|
+
assert !@single.remove_edge!(5,5).edge?(5,5)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_vertices
|
69
|
+
assert_equal [1,2,3,4], @single.vertices.sort
|
70
|
+
assert_equal [1,2,3,4,5], @single.add_vertex!(5).sort
|
71
|
+
assert_equal [1,2,4,5], @single.remove_vertex!(3).sort
|
72
|
+
assert !@single.vertex?(3)
|
73
|
+
assert !@single.edge?(2,3)
|
74
|
+
assert !@single.edge?(3,4)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_properties
|
78
|
+
assert !@single.directed?
|
79
|
+
assert @single.empty? == false
|
80
|
+
assert UndirectedGraph.new.empty? == true
|
81
|
+
assert_equal 4, @single.size
|
82
|
+
assert_equal 4, @dups.size
|
83
|
+
assert_equal 4, @single.num_vertices
|
84
|
+
assert_equal 4, @dups.num_vertices
|
85
|
+
assert_equal 3, @single.num_edges
|
86
|
+
assert_equal 6, @loops.num_edges
|
87
|
+
assert_equal 5, @dups.num_edges
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_merge
|
91
|
+
@dups.merge(@single)
|
92
|
+
assert_equal 8, @dups.num_edges
|
93
|
+
assert_equal [1,2,3,4], @dups.vertices.sort
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_operators
|
97
|
+
result = @single + Edge[3,2]
|
98
|
+
assert_equal 4, @single.size
|
99
|
+
assert_equal 3, @single.num_edges
|
100
|
+
assert_equal 4, result.size
|
101
|
+
assert_equal 3, result.num_edges
|
102
|
+
|
103
|
+
result = @single + 5
|
104
|
+
assert_equal 4, @single.size
|
105
|
+
assert_equal 3, @single.num_edges
|
106
|
+
assert_equal 5, result.size
|
107
|
+
assert_equal 3, result.num_edges
|
108
|
+
|
109
|
+
result = @single - Edge[4,4]
|
110
|
+
assert_equal 4, @single.size
|
111
|
+
assert_equal 3, @single.num_edges
|
112
|
+
assert_equal 4, result.size
|
113
|
+
assert_equal 3, result.num_edges
|
114
|
+
|
115
|
+
result = @single - 4
|
116
|
+
assert_equal 4, @single.size
|
117
|
+
assert_equal 3, @single.num_edges
|
118
|
+
assert_equal 3, result.size
|
119
|
+
assert_equal 2, result.num_edges
|
120
|
+
|
121
|
+
@single << Edge[6,1]
|
122
|
+
assert_equal 5, @single.size
|
123
|
+
assert_equal 4, @single.num_edges
|
124
|
+
assert @single.edge?(6,1)
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_complement
|
128
|
+
complement = @single.complement
|
129
|
+
assert [1,2,3,4], complement.vertices.sort
|
130
|
+
assert !complement.edge?(1,1)
|
131
|
+
assert complement.edge?(1,3)
|
132
|
+
assert complement.edge?(1,4)
|
133
|
+
assert !complement.edge?(2,2)
|
134
|
+
assert complement.edge?(2,4)
|
135
|
+
assert complement.edge?(3,1)
|
136
|
+
assert !complement.edge?(3,3)
|
137
|
+
assert complement.edge?(4,1)
|
138
|
+
assert complement.edge?(4,2)
|
139
|
+
assert 7, complement.num_edges
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_induced_subgraph
|
143
|
+
induced = @single.induced_subgraph([1,2])
|
144
|
+
assert [1,2], induced.vertices.sort
|
145
|
+
assert induced.edge?(1,2)
|
146
|
+
assert 1, induced.num_edges
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_include
|
150
|
+
assert @single.include?(4)
|
151
|
+
assert @dups.include?(4)
|
152
|
+
assert !@dups.include?(5)
|
153
|
+
assert !@single.include?(5)
|
154
|
+
assert @single.include?(Edge[1,2])
|
155
|
+
assert @dups.include?(Edge[1,2])
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_adjacent
|
159
|
+
|
160
|
+
assert @single.adjacent?(2, Edge[1,2])
|
161
|
+
assert_equal [2], @single.adjacent(1)
|
162
|
+
|
163
|
+
assert_equal [Edge[1,2]], @single.adjacent(1, :type=>:edges)
|
164
|
+
assert_equal [Edge[1,2]], @single.adjacent(1, :type=>:edges, :direction=> :out)
|
165
|
+
assert_equal [Edge[1,2],Edge[2,3]], @single.adjacent(2, :type=>:edges, :direction=> :in).sort
|
166
|
+
assert_equal [Edge[1,2],Edge[2,3]], @single.adjacent(2, :type=>:edges, :direction=> :all).sort
|
167
|
+
|
168
|
+
assert_equal [MultiEdge[1,2]]*2, @dups.adjacent(1, :type=>:edges)
|
169
|
+
assert_equal [MultiEdge[1,2]]*2, @dups.adjacent(1, :type=>:edges, :direction=> :out)
|
170
|
+
assert_equal ([MultiEdge[1,2]]*2 + [MultiEdge[2,3]]*2), @dups.adjacent(2, :type=>:edges, :direction=> :in).sort
|
171
|
+
assert_equal ([MultiEdge[1,2]]*2 + [MultiEdge[2,3]]*2), @dups.adjacent(2, :type=>:edges, :direction=> :all).sort
|
172
|
+
|
173
|
+
assert_equal [2], @single.adjacent(1, :type=>:vertices)
|
174
|
+
assert_equal [2], @single.adjacent(1, :type=>:vertices, :direction=> :out)
|
175
|
+
assert_equal [1,3], @single.adjacent(2, :type=>:vertices, :direction=> :in)
|
176
|
+
assert_equal [1,3], @single.adjacent(2, :type=>:vertices, :direction=> :all)
|
177
|
+
|
178
|
+
assert_equal [2,3], @single.adjacent(Edge[2,3], :type=>:vertices)
|
179
|
+
assert_equal [2,3], @single.adjacent(Edge[2,3], :type=>:vertices, :direction=> :out)
|
180
|
+
assert_equal [2,3], @single.adjacent(Edge[2,3], :type=>:vertices, :direction=> :in)
|
181
|
+
assert_equal [2,3], @single.adjacent(Edge[2,3], :type=>:vertices, :direction=> :all)
|
182
|
+
|
183
|
+
assert_equal [Edge[1,2],Edge[3,4]], @single.adjacent(Edge[2,3], :type=>:edges).sort
|
184
|
+
assert_equal [Edge[1,2],Edge[3,4]], @single.adjacent(Edge[2,3], :type=>:edges, :direction=> :out).sort
|
185
|
+
assert_equal [Edge[1,2],Edge[3,4]], @single.adjacent(Edge[2,3], :type=>:edges, :direction=> :in).sort
|
186
|
+
assert_equal [Edge[1,2],Edge[3,4]], @single.adjacent(Edge[2,3], :type=>:edges, :direction=> :all).sort
|
187
|
+
assert_equal ([MultiEdge[1,2]]*2 + [MultiEdge[3,4]]), @dups.adjacent(MultiEdge[2,3], :type=>:edges).sort
|
188
|
+
assert_equal ([MultiEdge[1,2]]*2 + [MultiEdge[3,4]]), @dups.adjacent(MultiEdge[2,3], :type=>:edges, :direction=>:out).sort
|
189
|
+
assert_equal ([MultiEdge[1,2]]*2 + [MultiEdge[3,4]]), @dups.adjacent(MultiEdge[2,3], :type=>:edges, :direction=>:in).sort
|
190
|
+
assert_equal ([MultiEdge[1,2]]*2+[MultiEdge[3,4]]), @dups.adjacent(MultiEdge[2,3], :type=>:edges, :direction=> :all).sort
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_neighborhood
|
194
|
+
assert_equal [2], @single.neighborhood(1).sort
|
195
|
+
assert_equal [1,3], @single.neighborhood(2).sort
|
196
|
+
assert_equal [Edge[1,2], Edge[3,4]], @single.neighborhood(Edge[2,3]).sort
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_degree
|
200
|
+
assert_equal 1, @single.in_degree(1)
|
201
|
+
assert_equal 2, @single.in_degree(2)
|
202
|
+
assert_equal 1, @single.in_degree(4)
|
203
|
+
assert_equal 1, @single.out_degree(1)
|
204
|
+
assert_equal 2, @single.out_degree(2)
|
205
|
+
assert_equal 1, @single.out_degree(4)
|
206
|
+
assert_equal 0, @single.add_vertex!(6).out_degree(6)
|
207
|
+
assert_equal 0, @single.add_vertex!(7).in_degree(7)
|
208
|
+
assert_equal 2, @single.add_edge!(4,2).out_degree(4)
|
209
|
+
assert_equal 3, @single.in_degree(2)
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_include
|
213
|
+
assert @single.include?(2)
|
214
|
+
assert !@single.include?(23)
|
215
|
+
assert @single.include?(Edge[1,2])
|
216
|
+
assert !@single.include?(Edge[1,4])
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gratr19
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 7
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 4
|
10
|
+
version: 0.4.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Shawn Garbett
|
14
|
+
- Ankur Sethi
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-03-28 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: |
|
24
|
+
GRATR is a framework for graph data structures and algorithms.
|
25
|
+
|
26
|
+
This library is a fork of RGL. This version utilizes
|
27
|
+
Ruby blocks and duck typing to greatly simplfy the code. It also supports
|
28
|
+
export to DOT format for display as graphics.
|
29
|
+
|
30
|
+
GRATR currently contains a core set of algorithm patterns:
|
31
|
+
|
32
|
+
* Breadth First Search
|
33
|
+
* Depth First Search
|
34
|
+
* A* Search
|
35
|
+
* Floyd-Warshall
|
36
|
+
* Best First Search
|
37
|
+
* Djikstra's Algorithm
|
38
|
+
* Lexicographic Search
|
39
|
+
|
40
|
+
The algorithm patterns by themselves do not compute any meaningful quantities
|
41
|
+
over graphs, they are merely building blocks for constructing graph
|
42
|
+
algorithms. The graph algorithms in GRATR currently include:
|
43
|
+
|
44
|
+
* Topological Sort
|
45
|
+
* Strongly Connected Components
|
46
|
+
* Transitive Closure
|
47
|
+
* Rural Chinese Postman
|
48
|
+
* Biconnected
|
49
|
+
|
50
|
+
email:
|
51
|
+
- shawn@garbett.org
|
52
|
+
- ankursethi108@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
58
|
+
- README
|
59
|
+
files:
|
60
|
+
- install.rb
|
61
|
+
- README
|
62
|
+
- lib/gratr/adjacency_graph.rb
|
63
|
+
- lib/gratr/base.rb
|
64
|
+
- lib/gratr/biconnected.rb
|
65
|
+
- lib/gratr/chinese_postman.rb
|
66
|
+
- lib/gratr/common.rb
|
67
|
+
- lib/gratr/comparability.rb
|
68
|
+
- lib/gratr/digraph.rb
|
69
|
+
- lib/gratr/digraph_distance.rb
|
70
|
+
- lib/gratr/dot.rb
|
71
|
+
- lib/gratr/edge.rb
|
72
|
+
- lib/gratr/graph.rb
|
73
|
+
- lib/gratr/graph_api.rb
|
74
|
+
- lib/gratr/import.rb
|
75
|
+
- lib/gratr/labels.rb
|
76
|
+
- lib/gratr/maximum_flow.rb
|
77
|
+
- lib/gratr/rdot.rb
|
78
|
+
- lib/gratr/search.rb
|
79
|
+
- lib/gratr/strong_components.rb
|
80
|
+
- lib/gratr/undirected_graph.rb
|
81
|
+
- lib/gratr/version.rb
|
82
|
+
- lib/gratr.rb
|
83
|
+
- lib/priority-queue/benchmark/dijkstra.rb
|
84
|
+
- lib/priority-queue/compare_comments.rb
|
85
|
+
- lib/priority-queue/ext/priority_queue/CPriorityQueue/extconf.rb
|
86
|
+
- lib/priority-queue/lib/priority_queue/c_priority_queue.rb
|
87
|
+
- lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
|
88
|
+
- lib/priority-queue/lib/priority_queue/ruby_priority_queue.rb
|
89
|
+
- lib/priority-queue/lib/priority_queue.rb
|
90
|
+
- lib/priority-queue/setup.rb
|
91
|
+
- lib/priority-queue/test/priority_queue_test.rb
|
92
|
+
- tests/TestBiconnected.rb
|
93
|
+
- tests/TestChinesePostman.rb
|
94
|
+
- tests/TestComplement.rb
|
95
|
+
- tests/TestDigraph.rb
|
96
|
+
- tests/TestDigraphDistance.rb
|
97
|
+
- tests/TestDot.rb
|
98
|
+
- tests/TestEdge.rb
|
99
|
+
- tests/TestInspection.rb
|
100
|
+
- tests/TestMultiEdge.rb
|
101
|
+
- tests/TestNeighborhood.rb
|
102
|
+
- tests/TestProperties.rb
|
103
|
+
- tests/TestSearch.rb
|
104
|
+
- tests/TestStrongComponents.rb
|
105
|
+
- tests/TestTriagulated.rb
|
106
|
+
- tests/TestUndirectedGraph.rb
|
107
|
+
- examples/graph_self.rb
|
108
|
+
- examples/module_graph.jpg
|
109
|
+
- examples/module_graph.rb
|
110
|
+
- examples/self_graph.jpg
|
111
|
+
- examples/visualize.jpg
|
112
|
+
- examples/visualize.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: https://github.com/amalagaura/gratr
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options:
|
119
|
+
- --title
|
120
|
+
- GRATR - Ruby Graph Library
|
121
|
+
- --main
|
122
|
+
- README
|
123
|
+
- --line-numbers
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
hash: 3
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
hash: 3
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
requirements: []
|
145
|
+
|
146
|
+
rubyforge_project: gratr19
|
147
|
+
rubygems_version: 1.4.2
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: Graph Theory Ruby library
|
151
|
+
test_files: []
|
152
|
+
|