This is my Breadth First Search implementation in Python 3 that assumes cycles and finds and prints path from start to goal.
Some background - Recently I've been preparing for interviews and am really focussing on writing clear and efficient code, rather than just hacking something up like I used to do.
This is another step in that direction when I'm revisiting some basic algorithms and trying to write best implementations of them as I can from my knowledge and obviously some help and explanations from internet. Any criticism would be helpful. I've tried to make the code as readable and efficient as I could.
from collections import deque
def bfs(graph, start, goal):
if start == goal:
print([start])
return
queue = deque([start])
# dict which holds parents, later helpful to retreive path.
# Also useful to keep track of visited node
parent = {}
parent[start] = start
while queue:
currNode = queue.popleft()
for neighbor in graph[currNode]:
# goal found
if neighbor == goal:
parent[neighbor] = currNode
print_path(parent, neighbor, start)
return
# check if neighbor already seen
if neighbor not in parent:
parent[neighbor] = currNode
queue.append(neighbor)
print("No path found.")
def print_path(parent, goal, start):
path = [goal]
# trace the path back till we reach start
while goal != start:
goal = parent[goal]
path.insert(0, goal)
print(path)
if __name__ == '__main__':
graph = {'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A', 'F']),
'D': set(['B']),
'E': set(['B', 'F']),
'F': set(['C', 'E'])}
bfs(graph, 'D', 'F')