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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a6bbd7d12e215a907b79ebaef777d9282f4e283
|
4
|
+
data.tar.gz: 8207dfe01745e9a6c50a75bc59f79a2f1248045f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7c6d5082e389a24a6db96e4c103825803ecfd8ebc1f41117192093a349dfe7104329d14d7c7b1098f04a783f225cfbbffd25242514a0cad5e2b569e61564e3c
|
7
|
+
data.tar.gz: 406b2b87218d98b9832fe479791192c38098ca7453a8e9fcb3bb68e9a7050ffee51aa4a4a2fdc97a7a7ab84d268ec1ee80fe35faeca8a1d9745cc90633911e06
|
data/.gitignore
CHANGED
@@ -7,7 +7,7 @@ module AbstractGraph
|
|
7
7
|
# does a deep copy of the object, in otherwords
|
8
8
|
# copies every object in the collection
|
9
9
|
def dup
|
10
|
-
other =
|
10
|
+
other = self.class.new
|
11
11
|
# copy each object in our collection over
|
12
12
|
@collection.each_value do |o|
|
13
13
|
other.add o
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# required in "abstract_graph/composition/uniquenamecollection"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
class UniqueNameCollection
|
6
|
+
|
7
|
+
# Change the name of a vertex in our graph
|
8
|
+
# p: String oldname represents the current vertex's name
|
9
|
+
# String newname represents the new name of our vertex
|
10
|
+
# note: the object needs to implement #name=
|
11
|
+
def rename( oldname, newname )
|
12
|
+
return nil if @collection[oldname].nil?
|
13
|
+
|
14
|
+
@otherUnique.each do |unc|
|
15
|
+
throw Exception if unc[newname]
|
16
|
+
end
|
17
|
+
|
18
|
+
# rename the object itself
|
19
|
+
@collection[oldname].name = newname
|
20
|
+
# remap the name
|
21
|
+
@collection[newname] = @collection[oldname]
|
22
|
+
# clear the old name
|
23
|
+
@collection.delete oldname
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -19,3 +19,4 @@ require "abstract_graph/composition/uniquenamecollection/add"
|
|
19
19
|
require "abstract_graph/composition/uniquenamecollection/dup"
|
20
20
|
require "abstract_graph/composition/uniquenamecollection/method_missing"
|
21
21
|
require "abstract_graph/composition/uniquenamecollection/link"
|
22
|
+
require "abstract_graph/composition/uniquenamecollection/rename"
|
@@ -12,14 +12,14 @@ module AbstractGraph
|
|
12
12
|
v1 = @vertices[v1] or return nil
|
13
13
|
v2 = @vertices[v2] or return nil
|
14
14
|
|
15
|
-
raise Exception if v1 == v2
|
15
|
+
raise Exception.new( "AddEdge: Same vertices passed, #{v1.name}" ) if v1 == v2
|
16
16
|
|
17
17
|
# create the edge
|
18
18
|
edge = Edge.new s, v1, v2
|
19
19
|
|
20
20
|
# check if it's an multiple edge
|
21
21
|
@edges.each_value do |e|
|
22
|
-
raise Exception if e.is_coincident? edge
|
22
|
+
raise Exception.new( "AddEdge: Multiple edge being added between #{v1.name}, #{v2.name}" ) if e.is_coincident? edge
|
23
23
|
end
|
24
24
|
|
25
25
|
@edges.add edge
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# return the adjacent vertices in of a vertex
|
7
|
+
# p: String s represents the name of the query vertex
|
8
|
+
def adjacency_list( s )
|
9
|
+
# this collects all the edges at first
|
10
|
+
result = @edges.collect do |id,e|
|
11
|
+
e.vertices.collect do |v|
|
12
|
+
v.name
|
13
|
+
end - [s]
|
14
|
+
end.delete_if do |adj|
|
15
|
+
# and deletes the ones with only one remaining tracked
|
16
|
+
adj.size == 2
|
17
|
+
end.flatten
|
18
|
+
|
19
|
+
# return nil if result is empty
|
20
|
+
if result.size == 0
|
21
|
+
return nil
|
22
|
+
else
|
23
|
+
return result
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# changes an edge name to another valid name
|
7
|
+
# p: String s represents the current edge we want to rename
|
8
|
+
# String snew is what we want to rename the edge to
|
9
|
+
def change_edge_name( s, snew )
|
10
|
+
return nil if @edges.rename(s, snew).nil?
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# changes a vertex name to another valid name
|
7
|
+
# p: String s represents the current vertex we want to rename
|
8
|
+
# String snew is what we want to rename the vertex to
|
9
|
+
def change_vertex_name( s, snew )
|
10
|
+
return nil if @vertices.rename(s, snew).nil?
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -5,8 +5,15 @@ module AbstractGraph
|
|
5
5
|
|
6
6
|
# does a deep copy of the graph
|
7
7
|
def dup
|
8
|
-
other =
|
9
|
-
|
8
|
+
other = self.class.new
|
9
|
+
# cannot call UniqueNameCollection#dup because we'll lose
|
10
|
+
# link data
|
11
|
+
@vertices.each_value do |v|
|
12
|
+
other.vertices.add v
|
13
|
+
end
|
14
|
+
@edges.each_value do |e|
|
15
|
+
other.edges.add e
|
16
|
+
end
|
10
17
|
other
|
11
18
|
end
|
12
19
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# returns the edge connecting v1 and v2, if there is no
|
7
|
+
# edge, then it returns nil
|
8
|
+
# p: String v1, v2 represents the names of the vertices
|
9
|
+
def get_edge_name( v1, v2 )
|
10
|
+
vertices = [v1, v2].sort!
|
11
|
+
|
12
|
+
@edges.each_value do |e|
|
13
|
+
eVertices = e.vertices.map do |v|
|
14
|
+
v.name
|
15
|
+
end.sort
|
16
|
+
|
17
|
+
return e.name if eVertices == vertices
|
18
|
+
end
|
19
|
+
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# returns whether or not v1 and v2 are joined by an edge
|
7
|
+
# p: String v1, v2 represents the names of the vertices
|
8
|
+
def is_adjacent?( v1, v2 )
|
9
|
+
vertices = [v1, v2].sort!
|
10
|
+
|
11
|
+
( @edges.each_value.find do |e|
|
12
|
+
vertices == e.vertices.map do |v|
|
13
|
+
v.name
|
14
|
+
end.sort
|
15
|
+
end && true ) || false
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# returns a replicated graph where the vertices with names
|
7
|
+
# v1 and v2 merge together to a vertex named vmerged and
|
8
|
+
# the edges that were adjacent to v1 and v2 are now
|
9
|
+
# adjacent to vmerged
|
10
|
+
# p: (Array, String)
|
11
|
+
# array is the string names of vertices to merge together
|
12
|
+
# string is the name of the final vertex
|
13
|
+
# (String, String, String)
|
14
|
+
# first two strings are the vertices to merge together
|
15
|
+
# last string is the name of the merged vertex
|
16
|
+
# e: the edges prioritize v1 to v2 if there are any conflicts
|
17
|
+
def merge_vertices( *args )
|
18
|
+
other = self.dup
|
19
|
+
other.merge_vertices!( *args )
|
20
|
+
end
|
21
|
+
|
22
|
+
# same as before except operates on the current graph
|
23
|
+
def merge_vertices!( *args )
|
24
|
+
|
25
|
+
# create a list of vertices we want to merge
|
26
|
+
mergeV = []
|
27
|
+
if args[0].class == Array
|
28
|
+
mergeV = args[0]
|
29
|
+
else
|
30
|
+
mergeV << args[0] << args[1]
|
31
|
+
end
|
32
|
+
|
33
|
+
# first construct an array of edges in between vertices
|
34
|
+
edgesInBetween = []
|
35
|
+
if ( args.size == 3 )
|
36
|
+
# do not need to go through the array of vertices
|
37
|
+
edgesInBetween << get_edge_name( args[0], args[1] )
|
38
|
+
else
|
39
|
+
edgesInBetween = @edges.values.collect do |e|
|
40
|
+
e.name if ( e.vertices.map do |v|
|
41
|
+
v.name
|
42
|
+
end & mergeV ).count == 2
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# delete the edges in between vertices
|
47
|
+
edgesInBetween.each do |e|
|
48
|
+
delete_edge!( e ) if e
|
49
|
+
end
|
50
|
+
|
51
|
+
# get the list of connections we want vmerged to be merged with
|
52
|
+
finalConnections = {}
|
53
|
+
mergeV.reverse.each do |vCheck|
|
54
|
+
@edges.each do |name, e|
|
55
|
+
[0,1].each do |edgeVId|
|
56
|
+
|
57
|
+
# check if the edge contains our vertex
|
58
|
+
if e.vertices[edgeVId].name == vCheck
|
59
|
+
otherVertex = e.vertices[(edgeVId+1)%2].name
|
60
|
+
# track the vertex with the edge name
|
61
|
+
finalConnections[otherVertex] = name
|
62
|
+
delete_edge! name
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# delete the vertices we want to merge
|
70
|
+
mergeV.each do |v|
|
71
|
+
delete_vertex! v
|
72
|
+
end
|
73
|
+
|
74
|
+
# add vmerged and connect it to adjacent vertices
|
75
|
+
add_vertex args.last
|
76
|
+
finalConnections.each do | vertexName, name |
|
77
|
+
add_edge name, vertexName, args.last
|
78
|
+
end
|
79
|
+
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# returns a replicated graph where all the vertices are the same but one
|
7
|
+
# gets split and all it's adjacent vertices are now connected to two replaced vertices
|
8
|
+
# p: String s the name of the vertex that will be split into vertexName-1 and vertexName-2
|
9
|
+
# all adjacent edges will also be split and adjacent to the correct vertex
|
10
|
+
def split_vertex( s )
|
11
|
+
other = self.dup
|
12
|
+
other.split_vertex!( s )
|
13
|
+
other
|
14
|
+
end
|
15
|
+
|
16
|
+
# same as before except operates on the current graph
|
17
|
+
def split_vertex!( s )
|
18
|
+
adjacentVertices = @edges.dup.keep_if do |id, e|
|
19
|
+
e.vertices.collect(&:name).include? s
|
20
|
+
end.collect do |id, e|
|
21
|
+
[id, ( e.vertices.collect(&:name) - [s] )[0] ]
|
22
|
+
end
|
23
|
+
|
24
|
+
delete_vertex! s
|
25
|
+
adjacentVertices.each do |e|
|
26
|
+
delete_edge! e[0]
|
27
|
+
end
|
28
|
+
|
29
|
+
add_vertex "#{s}-1"
|
30
|
+
add_vertex "#{s}-2"
|
31
|
+
adjacentVertices.each do |e|
|
32
|
+
add_edge "#{e[0]}-1", "#{s}-1", e[1]
|
33
|
+
add_edge "#{e[0]}-2", "#{s}-2", e[1]
|
34
|
+
end
|
35
|
+
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# required in "abstract_graph/graph/template"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# create a connected graph with two sets of vertices where
|
8
|
+
# the two sets only have vertices adjacent to the other set
|
9
|
+
# p: the number of vertices in the two sets. the first set has
|
10
|
+
# n vertices from 2**(0..n-1) and the second set has m
|
11
|
+
# vertices from 2**(n**m+n-1)
|
12
|
+
def complete_bipartite_graph n, m
|
13
|
+
result = new
|
14
|
+
|
15
|
+
# add the vertices to the graph
|
16
|
+
vertexN = (0..n-1).map do |i|
|
17
|
+
vName = 2**i
|
18
|
+
result.add_vertex "v#{vName}"
|
19
|
+
vName
|
20
|
+
end
|
21
|
+
vertexM = (0..m-1).map do |i|
|
22
|
+
vName = 2**(n+i)
|
23
|
+
result.add_vertex "v#{vName}"
|
24
|
+
vName
|
25
|
+
end
|
26
|
+
|
27
|
+
# add the edges to the graph, and make sure they're
|
28
|
+
# connected to the other set
|
29
|
+
vertexN.each do |vn|
|
30
|
+
vertexM.each do |vm|
|
31
|
+
result.add_edge "e#{vn + vm}", "v#{vn}", "v#{vm}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# required in "abstract_graph/graph/template"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# create a variabled sized complete graph
|
8
|
+
# that is, a graph where every vertex is adjacent
|
9
|
+
# to every other vertex
|
10
|
+
# p: the number of vertices in the desired complete graph
|
11
|
+
def complete_graph n
|
12
|
+
result = new
|
13
|
+
vertexNames = n.times.collect { |i| 2**i }
|
14
|
+
vertexNames.each do |i|
|
15
|
+
result.add_vertex "v#{i}"
|
16
|
+
end
|
17
|
+
vertexNames.combination(2).each do |i|
|
18
|
+
result.add_edge( "e#{i[0]+i[1]}", "v#{i[0]}", "v#{i[1]}" )
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# required in "abstract_graph/graph/template"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# create a variabled sized cycle graph,
|
8
|
+
# ie, a connected graph such that every vertex has
|
9
|
+
# exactly two distinct paths to every other vertex
|
10
|
+
# p: the number of vertices in the cycle graph
|
11
|
+
# coincidently, also the number of edges
|
12
|
+
def cycle_graph n
|
13
|
+
result = new
|
14
|
+
vertexNames = (n-1).times.collect { |i| 2**(i + 1) }
|
15
|
+
lastVertex = 2**(n-1)
|
16
|
+
|
17
|
+
# insert first vertex
|
18
|
+
result.add_vertex "v1"
|
19
|
+
vertexNames.each do |i|
|
20
|
+
# insert the rest of the vertices
|
21
|
+
result.add_vertex "v#{i}"
|
22
|
+
# insert the edge with the previous vertex
|
23
|
+
result.add_edge "e#{i + i/2}", "v#{i}", "v#{i/2}"
|
24
|
+
end
|
25
|
+
|
26
|
+
#insert the closing loop
|
27
|
+
result.add_edge "e#{1 + lastVertex}", "v1", "v#{lastVertex}"
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# required in "abstract_graph/graph/template"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# create a variable sized path graph,
|
8
|
+
# ie, a connected graph with 2 end vertices and n-2 connector
|
9
|
+
# vertices. Each connector vertex has two adjacent vertices
|
10
|
+
# p: the number of vertices in the path, n-1 edges
|
11
|
+
def path_graph n
|
12
|
+
result = new
|
13
|
+
|
14
|
+
# add the first vertex
|
15
|
+
result.add_vertex "v1"
|
16
|
+
(1..n-1).each do |i|
|
17
|
+
# add the rest of them
|
18
|
+
result.add_vertex "v#{2**i}"
|
19
|
+
result.add_edge "e#{2**i + 2**(i-1)}", "v#{2**i}", "v#{2**(i-1)}"
|
20
|
+
end
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# required in "abstract_graph/graph/template"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# create a petersen graph.
|
8
|
+
# ie, a 3 regular graph with 10 vertices and 15 edges
|
9
|
+
def petersen_graph
|
10
|
+
result = new
|
11
|
+
|
12
|
+
vertexNames = []
|
13
|
+
# add all the vertices
|
14
|
+
10.times do |i|
|
15
|
+
vName = 2**i
|
16
|
+
vertexNames << vName
|
17
|
+
result.add_vertex "v#{vName}"
|
18
|
+
end
|
19
|
+
|
20
|
+
specifiedEdges = [
|
21
|
+
[0, 1], [0, 5], [0, 4],
|
22
|
+
[1, 2], [2, 3], [3, 4],
|
23
|
+
[1, 6], [2, 7], [3, 8],
|
24
|
+
[4, 9], [5, 8], [5, 7],
|
25
|
+
[6, 9], [7, 9], [6, 8]]
|
26
|
+
|
27
|
+
specifiedEdges.each do |edgePair|
|
28
|
+
result.add_edge "e#{vertexNames[edgePair[0]] + vertexNames[edgePair[1]]}", "v#{vertexNames[edgePair[0]]}", "v#{vertexNames[edgePair[1]]}"
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
# Define a bunch of pre-constructed graphs
|
4
|
+
|
5
|
+
module Abstractgraph
|
6
|
+
class Graph
|
7
|
+
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require "abstract_graph/graph/templates/complete_graph"
|
12
|
+
require "abstract_graph/graph/templates/cycle_graph"
|
13
|
+
require "abstract_graph/graph/templates/path_graph"
|
14
|
+
require "abstract_graph/graph/templates/complete_bipartite_graph"
|
15
|
+
require "abstract_graph/graph/templates/petersen_graph"
|
data/lib/abstract_graph/graph.rb
CHANGED
@@ -23,3 +23,11 @@ require "abstract_graph/graph/delete_vertex"
|
|
23
23
|
require "abstract_graph/graph/add_edge"
|
24
24
|
require "abstract_graph/graph/has_edge"
|
25
25
|
require "abstract_graph/graph/delete_edge"
|
26
|
+
require "abstract_graph/graph/change_vertex_name"
|
27
|
+
require "abstract_graph/graph/change_edge_name"
|
28
|
+
require "abstract_graph/graph/is_adjacent"
|
29
|
+
require "abstract_graph/graph/adjacency_list"
|
30
|
+
require "abstract_graph/graph/merge_vertices"
|
31
|
+
require "abstract_graph/graph/get_edge_name"
|
32
|
+
require "abstract_graph/graph/templates"
|
33
|
+
require "abstract_graph/graph/split_vertex"
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@name = "name"
|
9
|
+
@newname = "newname"
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
@collection = UniqueNameCollection.new
|
14
|
+
@dummy = Dummy.new
|
15
|
+
@dummy.name = @name
|
16
|
+
@collection.add @dummy
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#rename(String,String)" do
|
20
|
+
|
21
|
+
it "removes the original name from the hash" do
|
22
|
+
@collection.rename @name, @newname
|
23
|
+
@collection[@name].should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "renames the dummy object" do
|
27
|
+
@collection.rename @name, @newname
|
28
|
+
@dummy.name.should == @newname
|
29
|
+
end
|
30
|
+
|
31
|
+
it "throws an exception if the new name exists" do
|
32
|
+
@dummy = Dummy.new
|
33
|
+
@dummy.name = @newname
|
34
|
+
@collection.add @dummy
|
35
|
+
expect { @collection.rename @name, @newname }.to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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
|
+
@vertex4 = "Vertex4"
|
11
|
+
@edge1 = "Edge1"
|
12
|
+
@edge2 = "Edge2"
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
@graph = Graph.new
|
17
|
+
@graph.add_vertex @vertex1
|
18
|
+
@graph.add_vertex @vertex2
|
19
|
+
@graph.add_vertex @vertex3
|
20
|
+
@graph.add_vertex @vertex4
|
21
|
+
@graph.add_edge @edge1, @vertex1, @vertex2
|
22
|
+
@graph.add_edge @edge2, @vertex2, @vertex3
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#adjacency_list(String)" do
|
26
|
+
|
27
|
+
it "returns an array if there are adjacent vertices" do
|
28
|
+
@graph.adjacency_list( @vertex1 ).should be_an_instance_of(Array)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns nil if there are no adjacent vertices" do
|
32
|
+
@graph.adjacency_list( @vertex4).should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns the vertices that are connected with edges" do
|
36
|
+
@graph.adjacency_list( @vertex2 ).should include(@vertex1)
|
37
|
+
@graph.adjacency_list( @vertex2 ).should include(@vertex3)
|
38
|
+
@graph.adjacency_list( @vertex1 ).should include(@vertex2)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
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
|
+
@edge = "MyEdge"
|
11
|
+
@edgechanged = "MyChangedName"
|
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 @edge, @vertex1, @vertex2
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#change_edge_name(String,String)" do
|
23
|
+
|
24
|
+
it "returns an object of class Graph" do
|
25
|
+
@graph.change_edge_name( @edge, @edgechanged ).should be_an_instance_of(Graph)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "changes the name of the vertex" do
|
29
|
+
@graph.change_edge_name @edge, @edgechanged
|
30
|
+
@graph.has_edge?( @edge ).should be_false
|
31
|
+
@graph.has_edge?( @edgechanged ).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "throws an exception if the name has already been taken" do
|
35
|
+
@graph.add_edge @edgechanged, @vertex2, @vertex3
|
36
|
+
expect { @graph.change_edge_name( @edge, @edgechanged ) }.to raise_error
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|