-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathundirected_graph.rb
More file actions
45 lines (42 loc) · 1.95 KB
/
undirected_graph.rb
File metadata and controls
45 lines (42 loc) · 1.95 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
require_relative './graph'
class UndirectedGraph < Graph
# Public: Populates the adjacency list for each vertex in a graph data structure
# NOTE: Vertices and Edges are to be initialized beforehand
# Examples
# graph = UndirectedGraph.new(vertex_list, array_list)
# graph.populate_adjacency_list
#
def populate_adjacency_list
@vertices.each do |vertex|
if vertex.adj_list.empty?
# Selection is done by checking if the vertex being iterated is at either
# location of the edge and then corresponding vertices are returned
vertex.adj_list = @edges.select { |edge| ((edge.v1).equal?(vertex) || (edge.v2).equal?(vertex)) }
.map { |edge| ((edge.v1).equal?(vertex)) ? edge.v2 : edge.v1 }
# vertex.key = vertex.adj_list.map { |x| get_edge_weight(x, vertex) }.min
end
end
end
# Public: Populates adjacency list using an array input assuming the elements
# as attribute of the Vertex class
#
# The method first builds vertices and edges, then calls populate_adjacency_list
# method which adds adjacency_list to each and every vertex
#
# Examples
# array = [ [0, 1], [0, 6], [0, 8], [1, 4], [1, 6], [1, 9], [2, 4], [2, 6],
# [3, 4], [3, 5], [3, 8], [4, 5], [4, 9], [7, 8], [7, 9] ]
# graph = UndirectedGraph.new([], []) # Since default constructor expects
# vertices and edges. Provide empty arrays
#
# graph.populate_adjacency_list_via_array(array)
#
def populate_adjacency_list_via_array(array)
@vertices = array.flatten.uniq.map { |v| Vertex.new('WHITE', 1, nil, [], v) }
# Both forward and backward edges are being added to the edges list
forward_edges = array.map { |edge| Edge.new(find(edge[0]), find(edge[1])) }
backward_edges = array.map { |edge| Edge.new(find(edge[1]), find(edge[0])) }
@edges = forward_edges + backward_edges
populate_adjacency_list
end
end