gratr 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Grater.xcf +0 -0
- data/README +328 -0
- data/Rakefile +220 -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 +33 -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 +73 -0
- data/lib/gratr/comparability.rb +92 -0
- data/lib/gratr/digraph.rb +113 -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 +315 -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 +326 -0
- data/lib/gratr/search.rb +409 -0
- data/lib/gratr/strong_components.rb +127 -0
- data/lib/gratr/undirected_graph.rb +153 -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/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 +257 -0
- data/tests/TestStrongComponents.rb +85 -0
- data/tests/TestTriagulated.rb +137 -0
- data/tests/TestUndirectedGraph.rb +219 -0
- metadata +92 -0
@@ -0,0 +1,138 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
# are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
8
|
+
# this list of conditions and the following disclaimer.
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer in the documentation
|
11
|
+
# and/or other materials provided with the distribution.
|
12
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
13
|
+
# may be used to endorse or promote products derived from this software
|
14
|
+
# without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
20
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
#++
|
27
|
+
|
28
|
+
|
29
|
+
require 'test/unit'
|
30
|
+
require 'gratr/import'
|
31
|
+
|
32
|
+
class TestDigraphDistance < Test::Unit::TestCase # :nodoc:
|
33
|
+
|
34
|
+
def setup
|
35
|
+
@d = Digraph[ :a,:b, :a,:e,
|
36
|
+
:b,:c, :b,:e,
|
37
|
+
:c,:d,
|
38
|
+
:d,:c,
|
39
|
+
:e,:b, :e,:f,
|
40
|
+
:f,:c, :f,:d, :f,:e ]
|
41
|
+
|
42
|
+
@w = { Arc[:a,:b] => 9,
|
43
|
+
Arc[:a,:e] => 3,
|
44
|
+
Arc[:b,:c] => 2,
|
45
|
+
Arc[:b,:e] => 6,
|
46
|
+
Arc[:c,:d] => 1,
|
47
|
+
Arc[:d,:c] => 2,
|
48
|
+
Arc[:e,:b] => 2,
|
49
|
+
Arc[:e,:f] => 1,
|
50
|
+
Arc[:f,:c] => 2,
|
51
|
+
Arc[:f,:d] => 7,
|
52
|
+
Arc[:f,:e] => 2 }
|
53
|
+
@a = { :a => 0,
|
54
|
+
:b => 5,
|
55
|
+
:c => 6,
|
56
|
+
:d => 7,
|
57
|
+
:e => 3,
|
58
|
+
:f => 4 }
|
59
|
+
@simple_weight = Proc.new {|e| 1}
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_shortest_path
|
63
|
+
x = Digraph[ :s,:u, :s,:w,
|
64
|
+
:j,:v,
|
65
|
+
:u,:j,
|
66
|
+
:v,:y,
|
67
|
+
:w,:u, :w,:v, :w,:y, :w,:x,
|
68
|
+
:x,:z ]
|
69
|
+
assert x.acyclic?
|
70
|
+
cost, path = x.shortest_path(:s,@simple_weight)
|
71
|
+
assert_equal({:x=>2, :v=>2, :y=>2, :w=>1, :s=>0, :z=>3, :u=>1, :j=> 2}, cost)
|
72
|
+
assert_equal({:x=>:w, :v=>:w, :y=>:w, :w=>:s, :z=>:x, :u=>:s, :j=>:u}, path)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_dijkstra_with_proc
|
76
|
+
p = Proc.new {|e| @w[e]}
|
77
|
+
distance, path = @d.dijkstras_algorithm(:a,p)
|
78
|
+
assert_equal @a, distance
|
79
|
+
assert_equal({ :d => :c, :c => :f, :f => :e, :b => :e, :e => :a}, path)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_dijkstra_with_label
|
83
|
+
@w.keys.each {|e| @d[e] = @w[e]}
|
84
|
+
assert_equal @a, @d.dijkstras_algorithm(:a)[0]
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_dijkstra_with_bracket_label
|
88
|
+
@w.keys.each do |e|
|
89
|
+
@d[e] = { :xyz => (@w[e])}
|
90
|
+
end
|
91
|
+
assert_equal @a, @d.dijkstras_algorithm(:a, :xyz)[0]
|
92
|
+
@w.keys.each do |e|
|
93
|
+
@d[e] = [@w[e]]
|
94
|
+
end
|
95
|
+
assert_equal @a, @d.dijkstras_algorithm(:a, 0)[0]
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_floyd_warshall
|
99
|
+
simple = Digraph[ 0,1, 0,2, 1,2, 1,3, 2,3, 3,0 ]
|
100
|
+
|
101
|
+
cost, path, delta = simple.floyd_warshall(@simple_weight)
|
102
|
+
# Costs
|
103
|
+
assert_equal({0=>3, 1=>1, 2=>1, 3=>2}, cost[0])
|
104
|
+
assert_equal({0=>2, 1=>3, 2=>1, 3=>1}, cost[1])
|
105
|
+
assert_equal({0=>2, 1=>3, 2=>3, 3=>1}, cost[2])
|
106
|
+
assert_equal({0=>1, 1=>2, 2=>2, 3=>3}, cost[3])
|
107
|
+
|
108
|
+
# Paths
|
109
|
+
assert_equal({0=>1, 1=>1, 2=>2, 3=>1}, path[0])
|
110
|
+
assert_equal({0=>3, 1=>3, 2=>2, 3=>3}, path[1])
|
111
|
+
assert_equal({0=>3, 1=>3, 2=>3, 3=>3}, path[2])
|
112
|
+
assert_equal({0=>0, 1=>0, 2=>0, 3=>0}, path[3])
|
113
|
+
|
114
|
+
# Deltas
|
115
|
+
assert_equal 1, delta[0]
|
116
|
+
assert_equal 1, delta[1]
|
117
|
+
assert_equal -1, delta[2]
|
118
|
+
assert_equal -1, delta[3]
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_bellman_ford_moore
|
122
|
+
fig24 = Digraph[ [:s,:e] => 8,
|
123
|
+
[:s,:d] => 4,
|
124
|
+
[:e,:c] => 2,
|
125
|
+
[:e,:d] => -5,
|
126
|
+
[:c,:b] => -2,
|
127
|
+
[:d,:c] => -2,
|
128
|
+
[:d,:a] => 4,
|
129
|
+
[:a,:c] => 10,
|
130
|
+
[:a,:b] => 9,
|
131
|
+
[:b,:c] => 5,
|
132
|
+
[:b,:a] => -3]
|
133
|
+
cost, path = fig24.bellman_ford_moore(:s)
|
134
|
+
assert_equal({:e=>8, :d=>3, :c=>1, :b=>-1, :a=>-4, :s=>0}, cost)
|
135
|
+
assert_equal({:e=>:s, :d=>:e, :c=>:d, :b=>:c, :a=>:b}, path)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
data/tests/TestEdge.rb
ADDED
@@ -0,0 +1,171 @@
|
|
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 TestArc < Test::Unit::TestCase # :nodoc:
|
34
|
+
|
35
|
+
def setup
|
36
|
+
@e = Arc.new(1,2,'boo')
|
37
|
+
@u = Edge.new(1,2,'hoo')
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_edge_new
|
41
|
+
assert_raises(ArgumentError) {Arc.new}
|
42
|
+
assert_raises(ArgumentError) {Arc.new(1)}
|
43
|
+
assert Arc.new(1,2)
|
44
|
+
assert Arc.new(1,2,'label')
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_edge_getters
|
48
|
+
|
49
|
+
assert_equal(1, @e.source)
|
50
|
+
assert_equal(2, @e.target)
|
51
|
+
assert_equal('boo', @e.label)
|
52
|
+
|
53
|
+
assert_equal(1, @e[0])
|
54
|
+
assert_equal(2, @e[1])
|
55
|
+
assert_equal('boo', @e[2])
|
56
|
+
|
57
|
+
assert_equal(1, @e[-3])
|
58
|
+
assert_equal(2, @e[-2])
|
59
|
+
assert_equal('boo', @e[-1])
|
60
|
+
|
61
|
+
assert_raise(IndexError) {@e[-4]}
|
62
|
+
assert_raise(IndexError) {@e[3]}
|
63
|
+
|
64
|
+
assert_equal(1, @e['source'])
|
65
|
+
assert_equal(2, @e['target'])
|
66
|
+
assert_equal('boo', @e['label'])
|
67
|
+
|
68
|
+
assert_equal(1, @e[:source])
|
69
|
+
assert_equal(2, @e[:target])
|
70
|
+
assert_equal('boo', @e[:label])
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_edge_setters
|
74
|
+
@e.source = 23
|
75
|
+
@e.target = 42
|
76
|
+
@e.label = 'Yabba'
|
77
|
+
assert_equal(23, @e.source)
|
78
|
+
assert_equal(42, @e.target)
|
79
|
+
assert_equal('Yabba',@e.label)
|
80
|
+
|
81
|
+
@e['source'] = 2
|
82
|
+
@e['target'] = 1
|
83
|
+
@e['label'] = 'Dabba'
|
84
|
+
assert_equal(2, @e.source)
|
85
|
+
assert_equal(1, @e.target)
|
86
|
+
assert_equal('Dabba',@e.label)
|
87
|
+
|
88
|
+
@e[:source] = 9
|
89
|
+
@e[:target] = 8
|
90
|
+
@e['label'] = 'Doooo!'
|
91
|
+
assert_equal(9, @e.source)
|
92
|
+
assert_equal(8, @e.target)
|
93
|
+
assert_equal('Doooo!',@e.label)
|
94
|
+
|
95
|
+
@e[0] = 'Fred'
|
96
|
+
@e[1] = 'Flintstone'
|
97
|
+
@e[2] = 'and'
|
98
|
+
assert_equal('Fred', @e.source)
|
99
|
+
assert_equal('Flintstone',@e.target)
|
100
|
+
assert_equal('and', @e.label)
|
101
|
+
|
102
|
+
@e[-3] = 'Barney'
|
103
|
+
@e[-2] = 'Rubble'
|
104
|
+
@e[-1] = nil
|
105
|
+
assert_equal('Barney', @e.source)
|
106
|
+
assert_equal('Rubble', @e.target)
|
107
|
+
assert_equal(nil, @e.label)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_edge_simple_methods
|
111
|
+
assert_equal([1,2,'boo'], @e.to_a)
|
112
|
+
assert_equal("(1-2 'boo')", @e.to_s)
|
113
|
+
@e.label = nil
|
114
|
+
assert_equal("(1-2)", @e.to_s)
|
115
|
+
assert(@e.eql?(Arc.new(1,2)))
|
116
|
+
assert(!@e.eql?(Arc.new(1,3)))
|
117
|
+
assert(!Arc.new(2,1).eql?(@e))
|
118
|
+
|
119
|
+
assert(@e == (Arc.new(1,2)))
|
120
|
+
assert(@e.reverse == (Arc.new(2,1)))
|
121
|
+
assert(Arc.new(1,2) != (Arc.new(1,3)))
|
122
|
+
assert(Arc.new(2,1) != @e)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_edge_sort
|
126
|
+
x = [ Arc.new(2,3), Arc.new(1,3), Arc.new(1,2), Arc.new(2,1) ].sort
|
127
|
+
assert_equal [Arc.new(1,2), Arc.new(1,3), Arc.new(2,1), Arc.new(2,3)], x
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_undirected_edge_new
|
131
|
+
assert_raises(ArgumentError) {Edge.new}
|
132
|
+
assert_raises(ArgumentError) {Edge.new(1)}
|
133
|
+
assert Edge.new(1,2)
|
134
|
+
assert Edge.new(1,2,'label')
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_undirected_edge_getters
|
138
|
+
assert_equal(1,@u.source)
|
139
|
+
assert_equal(2,@u.target)
|
140
|
+
assert_equal([1,2,'hoo'],@u.to_a)
|
141
|
+
assert_equal("(1=2 'hoo')",@u.to_s)
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_undirected_edge_methods
|
145
|
+
@u.label = nil
|
146
|
+
assert_equal("(1=2)",@u.to_s)
|
147
|
+
assert_equal("(1=2)",Edge.new(2,1).to_s)
|
148
|
+
|
149
|
+
assert @u.eql?(Edge.new(2,1))
|
150
|
+
assert @u == Edge.new(2,1,'boo')
|
151
|
+
assert @u != Edge.new(2,3)
|
152
|
+
|
153
|
+
assert_equal(@u.hash,Edge.new(2,1).hash)
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_undirected_edge_sort
|
157
|
+
x=[Edge.new(12, 1), Edge.new(2,11)].sort
|
158
|
+
assert_equal [Edge.new(2,11), Edge.new(1,12)], x
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_hash
|
162
|
+
assert_equal Arc[1,2,:b], Arc[1,2,:c]
|
163
|
+
assert_equal Arc[1,2,:b].hash, Arc[1,2,:c].hash
|
164
|
+
assert Arc[1,2] != Arc[2,1]
|
165
|
+
assert Arc[1,2] != Edge[1,2]
|
166
|
+
assert_equal Edge[1,2], Edge[2,1]
|
167
|
+
assert_equal Edge[1,2,:a], Edge[2,1,:b]
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
# are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
8
|
+
# this list of conditions and the following disclaimer.
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer in the documentation
|
11
|
+
# and/or other materials provided with the distribution.
|
12
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
13
|
+
# may be used to endorse or promote products derived from this software
|
14
|
+
# without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
20
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
#++
|
27
|
+
|
28
|
+
|
29
|
+
require 'test/unit'
|
30
|
+
require 'gratr/import'
|
31
|
+
|
32
|
+
class TestInspection < Test::Unit::TestCase # :nodoc:
|
33
|
+
|
34
|
+
def setup
|
35
|
+
@dg = DirectedMultiGraph[
|
36
|
+
[0,0,1] => 1,
|
37
|
+
[1,2,2] => 2,
|
38
|
+
[1,3,3] => 4,
|
39
|
+
[1,4,4] => nil,
|
40
|
+
[4,1,5] => 8,
|
41
|
+
[1,2,6] => 16,
|
42
|
+
[3,3,7] => 32,
|
43
|
+
[3,3,8] => 64 ]
|
44
|
+
@dg[3] = 128
|
45
|
+
@dg[0] = 256
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_inspection
|
49
|
+
inspect = @dg.inspect
|
50
|
+
assert_equal 384, @dg.vertices.inject(0) {|a,v| a += (@dg[v] || 0)}
|
51
|
+
assert_equal 127, @dg.edges.inject(0) {|a,e| a += (@dg[e] || 0)}
|
52
|
+
reflect = eval inspect
|
53
|
+
assert reflect == @dg
|
54
|
+
assert_equal 127, reflect.edges.inject(0) {|a,e| a += (reflect[e] || 0)}
|
55
|
+
assert_equal 384, reflect.vertices.inject(0) {|a,v| a += (reflect[v] || 0)}
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
# are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
8
|
+
# this list of conditions and the following disclaimer.
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer in the documentation
|
11
|
+
# and/or other materials provided with the distribution.
|
12
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
13
|
+
# may be used to endorse or promote products derived from this software
|
14
|
+
# without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
20
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
#++
|
27
|
+
|
28
|
+
|
29
|
+
require 'test/unit'
|
30
|
+
require 'gratr/import'
|
31
|
+
|
32
|
+
class TestMultiArc < Test::Unit::TestCase # :nodoc:
|
33
|
+
|
34
|
+
def test_directed_pseudo_graph
|
35
|
+
dpg=DirectedPseudoGraph[ :a,:b,
|
36
|
+
:a,:b,
|
37
|
+
:a,:b ]
|
38
|
+
assert_equal 3, dpg.edges.size
|
39
|
+
x=0
|
40
|
+
dpg.edges.each {|e| dpg[e] = (x+=1)}
|
41
|
+
assert_equal 6, dpg.edges.inject(0) {|a,v| a+=dpg[v]}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_directed_multi_graph
|
45
|
+
dmg=DirectedMultiGraph[ :a,:a,
|
46
|
+
:a,:a,
|
47
|
+
:a,:b,
|
48
|
+
:a,:b,
|
49
|
+
:b,:b,
|
50
|
+
:b,:b ]
|
51
|
+
assert_equal 6, dmg.edges.size
|
52
|
+
x = 0
|
53
|
+
dmg.edges.each {|e| dmg[e] = (x+=1)}
|
54
|
+
assert_equal 21, dmg.edges.inject(0) {|a,v| a+=dmg[v]}
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006 Shawn Patrick Garbett
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
# are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
8
|
+
# this list of conditions and the following disclaimer.
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer in the documentation
|
11
|
+
# and/or other materials provided with the distribution.
|
12
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
13
|
+
# may be used to endorse or promote products derived from this software
|
14
|
+
# without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
20
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
#++
|
27
|
+
|
28
|
+
|
29
|
+
require 'test/unit'
|
30
|
+
require 'gratr/import'
|
31
|
+
|
32
|
+
class TestNeighborhood < Test::Unit::TestCase # :nodoc:
|
33
|
+
|
34
|
+
def setup
|
35
|
+
@d = Digraph[:a,:b, :a,:f,
|
36
|
+
:b,:g,
|
37
|
+
:c,:b, :c,:g,
|
38
|
+
:d,:c, :d,:g,
|
39
|
+
:e,:d,
|
40
|
+
:f,:e, :f,:g,
|
41
|
+
:g,:a, :g,:e]
|
42
|
+
@w = [:a,:b]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_open_out_neighborhood
|
46
|
+
assert_equal [:g], @d.set_neighborhood([:a], :in)
|
47
|
+
assert_equal [], [:f,:g] - @d.set_neighborhood(@w, :out)
|
48
|
+
assert_equal [], @w - @d.open_pth_neighborhood(@w, 0, :out)
|
49
|
+
assert_equal [], [:f, :g] - @d.open_pth_neighborhood(@w, 1, :out)
|
50
|
+
assert_equal [], [:e] - @d.open_pth_neighborhood(@w, 2, :out)
|
51
|
+
assert_equal [], [:d] - @d.open_pth_neighborhood(@w, 3, :out)
|
52
|
+
assert_equal [], [:c] - @d.open_pth_neighborhood(@w, 4, :out)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_closed_out_neighborhood
|
56
|
+
assert_equal [], @w - @d.closed_pth_neighborhood(@w, 0, :out)
|
57
|
+
assert_equal [], [:a,:b,:f,:g] - @d.closed_pth_neighborhood(@w, 1, :out)
|
58
|
+
assert_equal [], [:a,:b,:e,:f,:g] - @d.closed_pth_neighborhood(@w, 2, :out)
|
59
|
+
assert_equal [], [:a,:b,:d,:e,:f,:g] - @d.closed_pth_neighborhood(@w, 3, :out)
|
60
|
+
assert_equal [], [:a,:b,:c,:d,:e,:f,:g] - @d.closed_pth_neighborhood(@w, 4, :out)
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
end
|