-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphBFS_traversal.py
47 lines (43 loc) · 1.04 KB
/
graphBFS_traversal.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#BFS graph traversal,
a={1:[(1,2,0),(1,3,0)],
2:[(2,1,0),(2,7,0)],
3:[(3,1,0),(3,6,0),(3,5,0)],
4:[(4,7,0),(4,8,0)],
5:[(5,3,0),(5,7,0)],
6:[(6,3,0),(6,8,0)],
7:[(7,2,0),(7,5,0),(7,4,0)],
8:[(8,4,0),(8,6,0)]}
v={
1:False,
2:False,
3:False,
4:False,
5:False,
6:False,
7:False,
8:False}
def bfs(a,e):
q=[e] #first element add to the Queue
v={}
for i in a.keys():
v[i]=False #All other elemnet are not visited
v[e]=True #First element is visited
while len(q)!=0: #Queue is not mpty
curr=q.pop(0) #Pop the first element
print(curr) #Print the first element
for i in a[curr]:
if v[i[1]]==False: #If not visited then visit it
q.append(i[1])
v[i[1]]=True #Mark it visited
bfs(a,1) #call the BFS function
"""
OUTPUT:
1
2
3
7
6
5
4
8
"""