-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathgraph.rb
More file actions
109 lines (87 loc) · 2.13 KB
/
graph.rb
File metadata and controls
109 lines (87 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Vertex
attr_accessor :color, :d, :pi, :adj_list, :key, :attribute, :f
def initialize(color, d, pi, adj_list=[], attribute)
@color, @d, @pi, @adj_list, @attribute= color, d, pi, adj_list, attribute
end
end
# TODO: Create a new class GenericVertex to avoid multiple definitions of Vertex
class MSTVertex
attr_accessor :d, :key, :pi, :adj_list, :set_pointer, :next_pointer
def initialize(key, set_pointer, next_pointer)
@key, @set_pointer, @next_pointer = key, set_pointer, next_pointer
end
end
class MSTPrimVertex
include Comparable
attr_accessor :d, :key, :pi, :adj_list
def <=> (anotherVertex)
key <=> anotherVertex.key
end
def initialize(d, key, pi, adj_list)
@d, @key, @pi, @adj_list = d, key, pi, adj_list
end
def belongs_to?(vertex_list)
vertex_list.each do |x|
return true if self.equal?(x)
end
return false
end
end
class Edge
attr_accessor :v1, :v2, :w
def initialize(v1, v2)
@v1, @v2 = v1, v2
end
end
# TODO: Create a new class GenericEdge to avoid multiple definitions of Edge
class MSTEdge
attr_accessor :v1, :v2, :w
def initialize(v1, v2, w)
@v1, @v2, @w = v1, v2, w
end
end
class Graph
attr_accessor :vertices, :edges
def initialize(edges)
@edges = edges
@vertices = build_vertices(edges)
end
def initialize(vertices, edges)
@vertices, @edges = vertices, edges
end
def find(attribute)
@vertices.select { |v| v.attribute == attribute }.first
end
def get_adjacency_list(vertex)
vertex.adj_list
end
def add_vertex(vertex)
@vertices << vertex
end
def add_edge(edge)
edge.v1.adj_list << edge.v2
edge.v2.adj_list << edge.v1
end
def add_edge(v1, v2)
@edges << Edge.new(v1, v2)
v1.adj_list << v2
v2.adj_list << v1
end
def get_edge_weight(v1, v2)
@edges.each do |edge|
if ((edge.v1).equal?(v1) && (edge.v2).equal?(v2)) || ((edge.v1).equal?(v2) && (edge.v2).equal?(v1))
return edge.w
end
end
return 0
end
private
def build_vertices(edges)
vertices = []
edges.each do |x|
vertices << x.v1
vertices << x.v2
end
vertices.uniq
end
end