I'd extremely grateful if someone could explain me what is made wrong. The output is not the one that should be.
def bfs(g, s)
color, d, p, q, res = [], [], [], [], []
(0..g[0]).each do |el|
color[el], d[el], p[el] = "white", "inf", nil if el != s
end
color[s], d[s], p[s] = "gray", 0, nil
q << s
until q.empty?
u = q.shift
adj = Array.new
g[1].each do |x|
adj << x[1] if x[0] == u
end
adj.each do |v|
if color[v] == "white"
color[v] = "gray"
d[v] = d[u]+1
p[v] = u
q << v
end
end
color[u] = "black"
end
res = [p, d]
return res
end
The input variables are the following:
e = [ [1,2], [2,1], [2,3], [3,2], [3,4], [4,3], [4,5], [4,6], [5,4], [5,6], [5,7], [6,4], [6,5], [6,7], [6,8], [7,5], [7,6], [7,8], [8,6], [8,7] ]
g = [n, e]
n, s = 3, 8
Trouble-shooting, debugging, or understanding code snippets
. – ANeves Mar 23 '12 at 13:19