-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.py
30 lines (29 loc) · 919 Bytes
/
bfs.py
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
def bfs(G,s,d):
visited = []
queue=[s]
parent = [0] * len(G)
while queue:
node = queue.pop(0)
if node == d:
break
if node not in visited:
visited.append(node)
neighbours = G[node]
for n in neighbours:
print("node: " + str(node))
print("neighbor: "+str(n))
if n not in visited:
parent[n] = node
queue.append(n)
for i in range(0,len(parent)):
print("parent "+str(i)+" : "+str(parent[i]))
return parent
def printShortestPath(parent,s,d):
print("This is a bi-directional graph so we get bi-directional paths")
print("I am printing backward Path to source, but it is both ways: ")
val = d
while True:
print(str(val)+"->"+str(parent[val]),end=",")
val = parent[val]
if val == s:
break