Breadth First Search
graph = {0:[1,4,5], 1:[3,4], 2:[1], 3:[2,4], 4:[], 5:[]}
def bfs(node):
queue = [node]
marked = [node]
parent = {node: None}
while queue:
curr = queue.pop(0)
print('Node %i' % curr)
for i in graph[curr]:
if i not in marked:
queue.append(i)
marked.append(i)
parent[i] = curr
return parent
if __name__ == '__main__':
parents = bfs(0)
node = 3
par = 3
while par is not None:
par = parents[node]
print(node)
node = par