I was trying to understand and implement breadth first search.
Using a Queue in this instance since Stack would have been for Depth first search.
But when I print out the result, it ends up being Depth first instead of Breadth.
What have I missed? Thanks.
from collections import deque
adj_list = {
'A': ['B', 'D'],
'B': ['A', 'C'],
'C': ['B', 'C'],
'D': ['A', 'E', 'F'],
'E': ['D', 'F', 'G'],
'F': ['D', 'E', 'H'],
'G': ['E', 'H'],
'H': ['G', 'F']
}
q = deque()
visited = {}
level = {}
parent = {}
result = []
for node in adj_list.keys():
visited[node] = False
parent[node] = None
level[node] = float('-inf')
s = 'A'
visited[s] = True
level[s] = 0
q.append(s)
while q:
u = q.pop()
result.append(u)
for v in adj_list[u]:
if not visited[v]:
visited[v] = True
parent[v] = u
level[v] = level[u] + 1
q.append(v)
print(result)
This prints:
['A', 'D', 'F', 'H', 'G', 'E', 'B', 'C']
This result is depth first.
For breadth first, it should have been:
['A', 'B', 'D', 'C', 'E', 'F', 'G', 'H']