-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.py
56 lines (46 loc) · 1.37 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
The steps of the algorithm work as follow:
- Start by putting any one of the graph’s vertices at the back of the queue.
- Now take the front item of the queue and add it to the visited list.
- Create a list of that vertex's adjacent nodes. Add those which are not within the visited list to the rear of the queue.
- Keep continuing steps two and three till the queue is empty.
NOTE: for this implementation to work,
you have to mark leaf node in a self loop while defining the graph
else it will give KeyError
'''
visited = [] # List for visited nodes.
queue = [] # Initialize a queue
def bfs(visited, graph, node): # function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node
m = queue.pop(0)
print(m, end=" ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# eg 1
# graph = {
# '0': ['1', '2'],
# '1': ['2'],
# '2': ['0', '3'],
# '3': ['3'],
# }
# print("Following is the Breadth-First Search")
# bfs(visited, graph, '2') # function calling
# op 1
# 2 0 3 1
# eg 2
graph = {
'5': ['3', '7'],
'3': ['2', '4'],
'7': ['8'],
'2': [],
'4': ['8'],
'8': []
}
print("Following is the Breadth-First Search")
bfs(visited, graph, '5')
# op 2
# 5 3 7 2 4 8