abstract_graph 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/lib/abstract_graph/composition/uniquenamecollection/dup.rb +1 -1
- data/lib/abstract_graph/composition/uniquenamecollection/rename.rb +28 -0
- data/lib/abstract_graph/composition/uniquenamecollection.rb +1 -0
- data/lib/abstract_graph/graph/add_edge.rb +2 -2
- data/lib/abstract_graph/graph/adjacency_list.rb +28 -0
- data/lib/abstract_graph/graph/change_edge_name.rb +15 -0
- data/lib/abstract_graph/graph/change_vertex_name.rb +15 -0
- data/lib/abstract_graph/graph/dup.rb +9 -2
- data/lib/abstract_graph/graph/get_edge_name.rb +24 -0
- data/lib/abstract_graph/graph/is_adjacent.rb +20 -0
- data/lib/abstract_graph/graph/merge_vertices.rb +84 -0
- data/lib/abstract_graph/graph/split_vertex.rb +40 -0
- data/lib/abstract_graph/graph/templates/complete_bipartite_graph.rb +40 -0
- data/lib/abstract_graph/graph/templates/complete_graph.rb +25 -0
- data/lib/abstract_graph/graph/templates/cycle_graph.rb +34 -0
- data/lib/abstract_graph/graph/templates/path_graph.rb +28 -0
- data/lib/abstract_graph/graph/templates/petersen_graph.rb +36 -0
- data/lib/abstract_graph/graph/templates.rb +15 -0
- data/lib/abstract_graph/graph.rb +8 -0
- data/lib/abstract_graph/version.rb +1 -1
- data/spec/abstract_graph/composition/uniquenamecollection/rename_spec.rb +42 -0
- data/spec/abstract_graph/graph/adjacency_list_spec.rb +44 -0
- data/spec/abstract_graph/graph/change_edge_name_spec.rb +42 -0
- data/spec/abstract_graph/graph/change_vertex_name_spec.rb +40 -0
- data/spec/abstract_graph/graph/dup_spec.rb +27 -22
- data/spec/abstract_graph/graph/get_edge_name_spec.rb +38 -0
- data/spec/abstract_graph/graph/has_edge_spec.rb +17 -19
- data/spec/abstract_graph/graph/has_vertex_spec.rb +13 -15
- data/spec/abstract_graph/graph/is_adjacent_spec.rb +36 -0
- data/spec/abstract_graph/graph/merge_vertices_spec.rb +199 -0
- data/spec/abstract_graph/graph/split_vertex_spec.rb +104 -0
- data/spec/abstract_graph/graph/templates/complete_bipartite_graph_spec.rb +63 -0
- data/spec/abstract_graph/graph/templates/complete_graph_spec.rb +36 -0
- data/spec/abstract_graph/graph/templates/cycle_graph_spec.rb +36 -0
- data/spec/abstract_graph/graph/templates/path_graph_spec.rb +52 -0
- data/spec/abstract_graph/graph/templates/petersen_graph_spec.rb +34 -0
- metadata +50 -13
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@vertex = "MyVertex"
|
8
|
+
@vertexchanged = "MyChangedName"
|
9
|
+
end
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
@graph = Graph.new
|
13
|
+
@graph.add_vertex @vertex
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#change_vertex_name(String,String)" do
|
17
|
+
|
18
|
+
it "returns an object of class Graph" do
|
19
|
+
@graph.change_vertex_name( @vertex, @vertexchanged ).should be_an_instance_of(Graph)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "changes the name of the vertex" do
|
23
|
+
@graph.change_vertex_name( @vertex, @vertexchanged )
|
24
|
+
@graph.has_vertex?( @vertex ).should be_false
|
25
|
+
@graph.has_vertex?( @vertexchanged ).should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns nil if the vertex does not exist" do
|
29
|
+
@graph.change_vertex_name( @vertexchanged, @vertex ).should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "throws an exception if the name has already been taken" do
|
33
|
+
@graph.add_vertex @vertexchanged
|
34
|
+
expect { @graph.change_vertex_name( @vertex, @vertexchanged ) }.to raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -1,37 +1,42 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module AbstractGraph
|
4
|
-
|
5
|
-
describe Graph do
|
4
|
+
describe Graph do
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
before :each do
|
7
|
+
@graph = Graph.new
|
8
|
+
@graph.add_vertex "MyVertex"
|
9
|
+
@graph.add_vertex "MyOtherVertex"
|
10
|
+
@graph.add_edge "MyEdge", "MyVertex", "MyOtherVertex"
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
+
describe "#dup" do
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
it "returns a graph object" do
|
16
|
+
@graph.dup.should be_an_instance_of(Graph)
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
it "has a different id than the original graph" do
|
20
|
+
@graph.object_id.should_not == @graph.dup.object_id
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
it "copies over the existing vertices" do
|
24
|
+
graphdup = @graph.dup
|
25
|
+
graphdup.has_vertex?( "MyVertex" ).should be_true
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
28
|
+
it "copies over the existing edges" do
|
29
|
+
graphdup = @graph.dup
|
30
|
+
graphdup.has_edge?( "MyEdge" ).should be_true
|
31
|
+
end
|
32
32
|
|
33
|
+
it "will not modify the original if it is modified" do
|
34
|
+
graphdup = @graph.dup
|
35
|
+
graphdup.vertices.clear
|
36
|
+
@graph.vertices.size.should == 2
|
33
37
|
end
|
34
38
|
|
35
39
|
end
|
40
|
+
|
36
41
|
end
|
37
42
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
# create the 3-path
|
8
|
+
@v1 = "v1"
|
9
|
+
@v2 = "v2"
|
10
|
+
@v3 = "v3"
|
11
|
+
@e1 = "e1"
|
12
|
+
@e2 = "e2"
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
@graph = Graph.new
|
17
|
+
@graph.add_vertex @v1
|
18
|
+
@graph.add_vertex @v2
|
19
|
+
@graph.add_vertex @v3
|
20
|
+
@graph.add_edge @e1, @v1, @v2
|
21
|
+
@graph.add_edge @e2, @v3, @v2
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#get_edge_name(String,String)" do
|
25
|
+
|
26
|
+
it "returns the name of the edge connecting the two string vertices" do
|
27
|
+
@graph.get_edge_name( @v1, @v2 ).should eql(@e1)
|
28
|
+
@graph.get_edge_name( @v2, @v3 ).should eql(@e2)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns nil if the two vertices are not adjacent" do
|
32
|
+
@graph.get_edge_name( @v1, @v3).should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -1,31 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module AbstractGraph
|
4
|
-
|
5
|
-
describe Graph do
|
4
|
+
describe Graph do
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
describe "#has_edge?(String)" do
|
6
|
+
before :each do
|
7
|
+
@vertex1 = "Vertex1"
|
8
|
+
@vertex2 = "Vertex2"
|
9
|
+
@graph = Graph.new
|
10
|
+
@graph.add_vertex @vertex1
|
11
|
+
@graph.add_vertex @vertex2
|
12
|
+
end
|
16
13
|
|
17
|
-
|
18
|
-
@graph.add_edge( "MyEdge", @vertex1, @vertex2 )
|
19
|
-
@graph.has_edge?( "MyEdge" ).should be_true
|
20
|
-
end
|
14
|
+
describe "#has_edge?(String)" do
|
21
15
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
it "returns whether the string name is a named edge in the graph" do
|
17
|
+
@graph.add_edge( "MyEdge", @vertex1, @vertex2 )
|
18
|
+
@graph.has_edge?( "MyEdge" ).should be_true
|
19
|
+
end
|
26
20
|
|
21
|
+
it "returns false when the string is not a named edge" do
|
22
|
+
@graph.has_edge?( "MyVertex" ).should be_false
|
23
|
+
@graph.has_edge?( "AlsoFalse" ).should_not be_nil
|
27
24
|
end
|
28
25
|
|
29
26
|
end
|
27
|
+
|
30
28
|
end
|
31
29
|
end
|
@@ -1,27 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module AbstractGraph
|
4
|
-
|
5
|
-
describe Graph do
|
4
|
+
describe Graph do
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
describe "#has_vertex?(String)" do
|
6
|
+
before :each do
|
7
|
+
@graph = Graph.new
|
8
|
+
end
|
12
9
|
|
13
|
-
|
14
|
-
@graph.add_vertex "MyVertex"
|
15
|
-
@graph.has_vertex?( "MyVertex" ).should be_true
|
16
|
-
end
|
10
|
+
describe "#has_vertex?(String)" do
|
17
11
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
it "returns whether the string name is a named vertex in the graph" do
|
13
|
+
@graph.add_vertex "MyVertex"
|
14
|
+
@graph.has_vertex?( "MyVertex" ).should be_true
|
15
|
+
end
|
22
16
|
|
17
|
+
it "returns false when the string is not a named vertex" do
|
18
|
+
@graph.has_vertex?( "MyVertex" ).should be_false
|
19
|
+
@graph.has_vertex?( "AlsoFalse" ).should_not be_nil
|
23
20
|
end
|
24
21
|
|
25
22
|
end
|
23
|
+
|
26
24
|
end
|
27
25
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@vertex1 = "Vertex1"
|
8
|
+
@vertex2 = "Vertex2"
|
9
|
+
@vertex3 = "Vertex3"
|
10
|
+
@edge1 = "Edge1"
|
11
|
+
@edge2 = "Edge2"
|
12
|
+
end
|
13
|
+
|
14
|
+
before :each do
|
15
|
+
@graph = Graph.new
|
16
|
+
@graph.add_vertex @vertex1
|
17
|
+
@graph.add_vertex @vertex2
|
18
|
+
@graph.add_vertex @vertex3
|
19
|
+
@graph.add_edge @edge1, @vertex1, @vertex2
|
20
|
+
@graph.add_edge @edge2, @vertex2, @vertex3
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#is_adjacent?(String,String)" do
|
24
|
+
|
25
|
+
it "returns true if two vertices are joined with an edge" do
|
26
|
+
@graph.is_adjacent?( @vertex1, @vertex2 ).should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns false if two vertices are not joined with an edge" do
|
30
|
+
@graph.is_adjacent?( @vertex1, @vertex3 ).should be_false
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
# we're building a 4-cycle!
|
8
|
+
@v1 = "v1"
|
9
|
+
@v2 = "v2"
|
10
|
+
@v3 = "v3"
|
11
|
+
@v4 = "v4"
|
12
|
+
@vmerged = "vMerged"
|
13
|
+
@e1 = "e1"
|
14
|
+
@e2 = "e2"
|
15
|
+
@e3 = "e3"
|
16
|
+
@e4 = "e4"
|
17
|
+
end
|
18
|
+
|
19
|
+
before :each do
|
20
|
+
@g = Graph.new
|
21
|
+
@g.add_vertex @v1
|
22
|
+
@g.add_vertex @v2
|
23
|
+
@g.add_vertex @v3
|
24
|
+
@g.add_vertex @v4
|
25
|
+
@g.add_edge @e1, @v1, @v2
|
26
|
+
@g.add_edge @e2, @v2, @v3
|
27
|
+
@g.add_edge @e3, @v3, @v4
|
28
|
+
@g.add_edge @e4, @v4, @v1
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#merge_vertices(String,String,String)" do
|
32
|
+
|
33
|
+
it "returns an object of type graph" do
|
34
|
+
@g.merge_vertices( @v1, @v2, @vmerged ).should be_an_instance_of(Graph)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "does not modify the original graph" do
|
38
|
+
@g.merge_vertices @v1, @v2, @vmerged
|
39
|
+
@g.has_vertex?( @v1 ).should be_true
|
40
|
+
@g.has_vertex?( @v2 ).should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "removes the first two vertices" do
|
44
|
+
@gnext = @g.merge_vertices @v1, @v2, @vmerged
|
45
|
+
@gnext.has_vertex?( @v1 ).should be_false
|
46
|
+
@gnext.has_vertex?( @v2 ).should be_false
|
47
|
+
end
|
48
|
+
|
49
|
+
it "creates a vertex of the third string" do
|
50
|
+
@gnext = @g.merge_vertices @v1, @v2, @vmerged
|
51
|
+
@gnext.has_vertex?( @vmerged ).should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "removes the edge connecting the two vertices" do
|
55
|
+
@gnext = @g.merge_vertices @v1, @v2, @vmerged
|
56
|
+
@gnext.has_edge?( @e1 ).should be_false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "connects the merged vertex with the vertices adjacent to the previous two" do
|
60
|
+
@gnext = @g.merge_vertices @v1, @v2, @vmerged
|
61
|
+
@gnext.is_adjacent?( @vmerged, @v3 ).should be_true
|
62
|
+
@gnext.is_adjacent?( @vmerged, @v4 ).should be_true
|
63
|
+
@gnext = @g.merge_vertices @v1, @v3, @vmerged
|
64
|
+
@gnext.is_adjacent?( @vmerged, @v2 ).should be_true
|
65
|
+
@gnext.is_adjacent?( @vmerged, @v4 ).should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "preserves the edges of the first vertex over the second" do
|
69
|
+
@gnext = @g.merge_vertices @v1, @v3, @vmerged
|
70
|
+
@gnext.has_edge?( @e1 ).should be_true
|
71
|
+
@gnext.has_edge?( @e2 ).should be_false
|
72
|
+
@gnext.has_edge?( @e3 ).should be_false
|
73
|
+
@gnext.has_edge?( @e4 ).should be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#merge_vertices!(String,String,String)" do
|
79
|
+
|
80
|
+
it "returns an object of type graph" do
|
81
|
+
@g.merge_vertices!( @v1, @v2, @vmerged ).should be_an_instance_of(Graph)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "modifies the original graph" do
|
85
|
+
@g.merge_vertices! @v1, @v2, @vmerged
|
86
|
+
@g.has_vertex?( @v1 ).should be_false
|
87
|
+
@g.has_vertex?( @v2 ).should be_false
|
88
|
+
end
|
89
|
+
|
90
|
+
it "removes the first two vertices" do
|
91
|
+
@g.merge_vertices! @v1, @v2, @vmerged
|
92
|
+
@g.has_vertex?( @v1 ).should be_false
|
93
|
+
@g.has_vertex?( @v2 ).should be_false
|
94
|
+
end
|
95
|
+
|
96
|
+
it "creates a vertex of the third string" do
|
97
|
+
@g.merge_vertices! @v1, @v2, @vmerged
|
98
|
+
@g.has_vertex?( @vmerged ).should be_true
|
99
|
+
end
|
100
|
+
|
101
|
+
it "removes the edge connecting the two vertices" do
|
102
|
+
@g.merge_vertices! @v1, @v2, @vmerged
|
103
|
+
@g.has_edge?( @e1 ).should be_false
|
104
|
+
end
|
105
|
+
|
106
|
+
it "connects the merged vertex with the vertices adjacent to the previous two" do
|
107
|
+
@g.merge_vertices! @v1, @v3, @vmerged
|
108
|
+
@g.is_adjacent?( @vmerged, @v2 ).should be_true
|
109
|
+
@g.is_adjacent?( @vmerged, @v4 ).should be_true
|
110
|
+
end
|
111
|
+
|
112
|
+
it "preserves the edges of the first vertex over the second" do
|
113
|
+
@g.merge_vertices! @v1, @v3, @vmerged
|
114
|
+
@g.has_edge?( @e1 ).should be_true
|
115
|
+
@g.has_edge?( @e2 ).should be_false
|
116
|
+
@g.has_edge?( @e3 ).should be_false
|
117
|
+
@g.has_edge?( @e4 ).should be_true
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#merge_vertices(Array,String)" do
|
123
|
+
|
124
|
+
it "deletes the vertices in the array" do
|
125
|
+
arr = [ @v1, @v2 ]
|
126
|
+
@gnext = @g.merge_vertices( arr, @vmerged )
|
127
|
+
arr.each do |v|
|
128
|
+
@gnext.has_vertex?( v ).should be_false
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
it "removes the edges connecting the vertices in the array" do
|
133
|
+
arrV = [ @v1, @v2, @v3 ]
|
134
|
+
arrE = [ @e1, @e2 ]
|
135
|
+
@gnext = @g.merge_vertices( arrV, @vmerged )
|
136
|
+
arrE.each do |e|
|
137
|
+
@gnext.has_edge?( e ).should be_false
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it "connects the merged vertex with the vertices in the array" do
|
142
|
+
arr = [ @v1, @v3 ]
|
143
|
+
@gnext = @g.merge_vertices( arr, @vmerged )
|
144
|
+
[ @v2, @v4 ].each do |v|
|
145
|
+
@gnext.is_adjacent?( @vmerged, v ).should be_true
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
it "preserves the edges of earlier vertices than later" do
|
150
|
+
arr = [ @v1, @v3 ]
|
151
|
+
@gnext = @g.merge_vertices( arr, @vmerged )
|
152
|
+
@gnext.has_edge?( @e1 ).should be_true
|
153
|
+
@gnext.has_edge?( @e2 ).should be_false
|
154
|
+
@gnext.has_edge?( @e3 ).should be_false
|
155
|
+
@gnext.has_edge?( @e4 ).should be_true
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "#merge_vertices!(Array,String)" do
|
161
|
+
|
162
|
+
it "deletes the vertices in the array" do
|
163
|
+
arr = [ @v1, @v2 ]
|
164
|
+
@g.merge_vertices!( arr, @vmerged )
|
165
|
+
arr.each do |v|
|
166
|
+
@g.has_vertex?( v ).should be_false
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
it "removes the edges connecting the vertices in the array" do
|
171
|
+
arrV = [ @v1, @v2, @v3 ]
|
172
|
+
arrE = [ @e1, @e2 ]
|
173
|
+
@g.merge_vertices!( arrV, @vmerged )
|
174
|
+
arrE.each do |e|
|
175
|
+
@g.has_edge?( e ).should be_false
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
it "connects the merged vertex with the vertices in the array" do
|
180
|
+
arr = [ @v1, @v3 ]
|
181
|
+
@g.merge_vertices!( arr, @vmerged )
|
182
|
+
[ @v2, @v4 ].each do |v|
|
183
|
+
@g.is_adjacent?( @vmerged, v ).should be_true
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
it "preserves the edges of earlier vertices than later" do
|
188
|
+
arr = [ @v1, @v3 ]
|
189
|
+
@g.merge_vertices!( arr, @vmerged )
|
190
|
+
@g.has_edge?( @e1 ).should be_true
|
191
|
+
@g.has_edge?( @e2 ).should be_false
|
192
|
+
@g.has_edge?( @e3 ).should be_false
|
193
|
+
@g.has_edge?( @e4 ).should be_true
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@v1 = "v1"
|
8
|
+
@v2 = "v2"
|
9
|
+
@v3 = "v3"
|
10
|
+
@v4 = "v4"
|
11
|
+
@vsplit1 = "v1-1"
|
12
|
+
@vsplit2 = "v1-2"
|
13
|
+
@e1 = "e1"
|
14
|
+
@e2 = "e2"
|
15
|
+
@e3 = "e3"
|
16
|
+
@e4 = "e4"
|
17
|
+
@esplit1 = "e1-1"
|
18
|
+
@esplit2 = "e1-2"
|
19
|
+
end
|
20
|
+
|
21
|
+
before :each do
|
22
|
+
@g5path = Graph.path_graph 5
|
23
|
+
@g = Graph.new
|
24
|
+
@g.add_vertex @v1
|
25
|
+
@g.add_vertex @v2
|
26
|
+
@g.add_vertex @v3
|
27
|
+
@g.add_vertex @v4
|
28
|
+
@g.add_edge @e1, @v1, @v2
|
29
|
+
@g.add_edge @e2, @v2, @v3
|
30
|
+
@g.add_edge @e3, @v3, @v4
|
31
|
+
@g.add_edge @e4, @v4, @v2
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#split_vertex(String)" do
|
35
|
+
|
36
|
+
it "returns an object of type graph" do
|
37
|
+
@g.split_vertex(@v1).should be_an_instance_of(Graph)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not modify the original graph" do
|
41
|
+
@g.split_vertex(@v1)
|
42
|
+
@g.has_vertex?( @v1 ).should be_true
|
43
|
+
@g.has_vertex?( @v2 ).should be_true
|
44
|
+
@g.has_edge?( @e1 ).should be_true
|
45
|
+
@g.has_vertex?( @vsplit1 ).should be_false
|
46
|
+
@g.has_vertex?( @vsplit2 ).should be_false
|
47
|
+
@g.has_edge?( @esplit1 ).should be_false
|
48
|
+
end
|
49
|
+
|
50
|
+
it "removes the original vertex and adjacent edges" do
|
51
|
+
@gnext = @g.split_vertex @v1
|
52
|
+
@gnext.has_vertex?( @v1 ).should be_false
|
53
|
+
@gnext.has_edge?( @e1 ).should be_false
|
54
|
+
|
55
|
+
@gnext = @g5path.split_vertex "v4"
|
56
|
+
@gnext.has_vertex?("v4").should be_false
|
57
|
+
@gnext.has_vertex?( "e6" ).should be_false
|
58
|
+
@gnext.has_vertex?( "e12" ).should be_false
|
59
|
+
end
|
60
|
+
|
61
|
+
it "adds split versions of the original vertex and adjacent edges" do
|
62
|
+
@gnext = @g.split_vertex @v1
|
63
|
+
@gnext.has_vertex?( @vsplit1 ).should be_true
|
64
|
+
@gnext.has_vertex?( @vsplit2 ).should be_true
|
65
|
+
@gnext.has_edge?( @esplit1 ).should be_true
|
66
|
+
@gnext.has_edge?( @esplit2 ).should be_true
|
67
|
+
|
68
|
+
@gnext = @g5path.split_vertex "v4"
|
69
|
+
@gnext.has_vertex?( "v4-1" ).should be_true
|
70
|
+
@gnext.has_vertex?( "v4-2" ).should be_true
|
71
|
+
@gnext.has_edge?( "e6-1" ).should be_true
|
72
|
+
@gnext.has_edge?( "e6-2" ).should be_true
|
73
|
+
@gnext.has_edge?( "e12-1" ).should be_true
|
74
|
+
@gnext.has_edge?( "e12-2" ).should be_true
|
75
|
+
end
|
76
|
+
|
77
|
+
it "connects the split vertices with previous adjacent vertices" do
|
78
|
+
@gnext = @g.split_vertex @v1
|
79
|
+
@gnext.is_adjacent?( @vsplit1, @v2 ).should be_true
|
80
|
+
@gnext.is_adjacent?( @vsplit2, @v2 ).should be_true
|
81
|
+
|
82
|
+
@gnext = @g5path.split_vertex "v4"
|
83
|
+
@gnext.is_adjacent?( "v4-1", "v2" ).should be_true
|
84
|
+
@gnext.is_adjacent?( "v4-2", "v2" ).should be_true
|
85
|
+
@gnext.is_adjacent?( "v4-1", "v8" ).should be_true
|
86
|
+
@gnext.is_adjacent?( "v4-2", "v8" ).should be_true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can be chained to create very elaborate graphs very fast" do
|
90
|
+
@gnext = @g5path.split_vertex("v2").split_vertex("v4")
|
91
|
+
@gnext.is_adjacent?( "v2-1", "v1" ).should be_true
|
92
|
+
@gnext.is_adjacent?( "v2-2", "v1" ).should be_true
|
93
|
+
@gnext.is_adjacent?( "v4-1", "v8" ).should be_true
|
94
|
+
@gnext.is_adjacent?( "v4-2", "v8" ).should be_true
|
95
|
+
@gnext.is_adjacent?( "v4-1", "v2-1" ).should be_true
|
96
|
+
@gnext.is_adjacent?( "v4-2", "v2-1" ).should be_true
|
97
|
+
@gnext.is_adjacent?( "v4-1", "v2-2" ).should be_true
|
98
|
+
@gnext.is_adjacent?( "v4-2", "v2-2" ).should be_true
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
describe ".complete_bipartite_graph(Integer,Integer)" do
|
7
|
+
|
8
|
+
before :all do
|
9
|
+
@n, @m = 5, 6
|
10
|
+
@graphknm = Graph.complete_bipartite_graph @n, @m
|
11
|
+
@graphk1010 = Graph.complete_bipartite_graph 10, 10
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns an object of class Graph" do
|
15
|
+
Graph.complete_bipartite_graph(1, 1).should be_an_instance_of(Graph)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates as many vertices as the sum of the Integers, named with powers of two" do
|
19
|
+
(@n + @m).times do |i|
|
20
|
+
@graphknm.has_vertex?("v#{2**i}").should be_true
|
21
|
+
end
|
22
|
+
@graphk1010.has_vertex?("v#{2**19}").should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "ensures the first Integer vertices have second Integer degree" do
|
26
|
+
(@n).times do |i|
|
27
|
+
@graphknm.adjacency_list("v#{2**i}").size.should eql(@m)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "ensures the second Integer vertices are vertices (First Integer..Second Integer - 1) and have first integer degree" do
|
32
|
+
(@m).times do |i|
|
33
|
+
@graphknm.adjacency_list("v#{2**(i+@n)}").size.should eql(@n)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "creates as many edges as the product of the Integers" do
|
38
|
+
adjacencies = 0
|
39
|
+
(@n + @m).times do |i|
|
40
|
+
adjacencies += @graphknm.adjacency_list("v#{2**i}").size
|
41
|
+
end
|
42
|
+
adjacencies.should eql(@n*@m*2)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "contains the proper edges" do
|
46
|
+
nVertices = @n.times.collect {|i| 2**i}
|
47
|
+
mVertices = @m.times.collect {|i| 2**(i+@n)}
|
48
|
+
edges = []
|
49
|
+
nVertices.each do |n|
|
50
|
+
mVertices.each do |m|
|
51
|
+
edges << n + m
|
52
|
+
end
|
53
|
+
end
|
54
|
+
edges.each do |e|
|
55
|
+
@graphknm.has_edge?("e#{e}").should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
describe ".complete_graph(Integer)" do
|
7
|
+
|
8
|
+
before :all do
|
9
|
+
@n = 5
|
10
|
+
@graphkn = Graph.complete_graph @n
|
11
|
+
@graphk10 = Graph.complete_graph 10
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns an object of class Graph" do
|
15
|
+
Graph.complete_graph(1).should be_an_instance_of(Graph)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates as many vertices as the integer, named with powers of two" do
|
19
|
+
@n.times do |i|
|
20
|
+
@graphkn.has_vertex?("v#{2**i}").should be_true
|
21
|
+
end
|
22
|
+
@graphk10.has_vertex?("v#{2**9}").should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "creates an edge between each vertex" do
|
26
|
+
n = @n.times.collect { |i| 2**i }
|
27
|
+
n.combination(2).each do |i|
|
28
|
+
@graphkn.has_edge?("e#{i[0]+i[1]}").should be_true
|
29
|
+
end
|
30
|
+
@graphk10.has_edge?("e768").should be_true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|