-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbreadth_first_search.rb
More file actions
61 lines (55 loc) · 1.64 KB
/
breadth_first_search.rb
File metadata and controls
61 lines (55 loc) · 1.64 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
require_relative './graph.rb'
module Graphs
class BreadthFirstSearch
class << self
# Internal: Traverses through the vertices of a graph in a level wise,
# one level at a time manner
# Next level is considered only after all the elements in the
# current level are traversed through
#
# graph - Graph to be searched
# s - Starting node in the graph from which the search has to be initiated
# searchable - Node in the graph to be searched for
#
# Examples
# BFS(graph, graph.vertices.first, graph.vertices.last)
#
def BFS(graph, s, searchable = nil)
searchable_vertices = searchable ? graph.vertices.select { |x| x != s } : graph.vertices
searchable_vertices.each do |u|
u.color = 'WHITE'
u.d = Float::INFINITY
u.pi = nil
end
s.color = 'GRAY'
s.d = 0
s.pi = nil
q = Queue.new
q.enq(s)
while q.size != 0
u = q.deq
u.adj_list.each do |v|
if v.color == 'WHITE'
return "FOUND!!" if searchable && v.attribute == searchable.attribute
v.color = 'GRAY'
v.d = u.d + 1
v.pi = u
q.enq(v)
end
end
u.color = 'BLACK'
end
end
def print_BFS_path(g, s, v)
if v == s
print s.attribute
elsif v.pi == nil
print "NO PATH FROM #{s.attribute} TO #{v.attribute} EXISTS"
else
print_BFS_path(g, s, v.pi)
print v.attribute
end
end
end
end
end