-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
38 lines (32 loc) · 1.16 KB
/
bfs.py
File metadata and controls
38 lines (32 loc) · 1.16 KB
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
# Program to implement breadth first search for a given graph
# Instance of a graph stored as a dictionary
# Keys : Nodes
# Values : Neighbours
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
visited = []
queue = []
def bfs(graph, start_node, visited):
print("The nodes with which we are beginning the bfs is", start_node)
visited.append(start_node)
print("The initial visited array is ", visited)
queue.append(start_node)
print("The queue of nodes to be searched is ", queue )
while queue:
search = queue.pop(0)
print("The node being searched is ", search)
for neighbour in graph[search]:
print("Neighbour of ",search,"being checked is ", neighbour)
print("Test whether ", neighbour, "is already seen in ", visited)
if neighbour not in visited:
visited.append(neighbour)
print("If not seen, add to the seen ", visited)
queue.append(neighbour)
print("Also add it to the list of queue to be searched", queue)
bfs(graph, 'A', visited)