-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.py
More file actions
44 lines (40 loc) · 783 Bytes
/
BFS.py
File metadata and controls
44 lines (40 loc) · 783 Bytes
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
class Node:
def __init__(self,name):
self.n=name
self.d=0
self.p=None
r=Node("r")
v=Node("v")
s=Node("s")
w=Node("w")
t=Node("t")
x=Node("x")
u=Node("u")
y=Node("y")
gg={r:[v,s],v:[],s:[r,w],w:[s,t,x],t:[w,x,u],x:[w,t,u,y],u:[t,x,y,],y:[u,x]}
se=input("Enter the node to be searched\n")
Queue=list()
Visited=list()
d=0
Queue.append(s)
found=False
while len(Queue)!=0:
cur=Queue.pop()
if cur not in Visited:
Visited.append(cur)
for x in gg[cur]:
if x not in Visited:
x.p=cur
x.d=cur.d+1
if x.n==se:
print("Node found in {} steps".format(x.d))
found=True
break
Queue.append(x)
if found==True:
break
if found==False:
print("Node not found")
print("path of BFS:")
for y in Visited:
print(y.n)