forked from wafarifki/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.py
More file actions
54 lines (31 loc) · 760 Bytes
/
Copy pathBreadthFirstSearch.py
File metadata and controls
54 lines (31 loc) · 760 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
45
46
47
48
49
50
#!/usr/bin/env python
# coding: utf-8
# In[5]:
graph = {
'A' : ['B', 'E', 'F'],
'B' : ['A', 'F', 'G'],
'C' : ['D', 'G'],
'D' : ['C', 'H'],
'E' : ['A', 'F'],
'F' : ['A', 'B', 'E'],
'G' : ['B', 'C', 'H'],
'H' : ['G', 'D']
}
# In[2]:
visited = [] # List to keep track of visited nodes.
queue = [] #initialize a queue
# In[3]:
def bfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s,end=" ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# In[4]:
print("BFS Result : ")
bfs(visited,graph,'A')
# In[ ]: