-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathbfs.py
33 lines (28 loc) · 942 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
31
32
33
# The illustrated graph is represented using an adjacency list.
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = [] # a list that is used to keep track of visited nodes.
queue = [] # a list that is used to keep track of nodes currently in the queue.
def bfs(visited, graph, node):
# checks and appends the starting node to the visited list and the queue
visited.append(node)
queue.append(node)
# This continues until the queue is empty.
while queue:
# Take out nodes fromt the queue
s = queue.pop(0)
print (s, end = " ")
# Append the neighbors of that node to the queue if they are unvisited
# Mark them as visited
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# Driver Code
bfs(visited, graph, 'A')