PythiloLesson 22 of 32 · progress kept in this browser

Find the fewest steps with breadth-first search

Breadth-first search explores a graph in rings: every node one step away, then every node two steps away. The first time it reaches a node, it has used the fewest possible edges.

graph = {"a": ["b", "c"], "b": ["d"],
         "c": ["d"], "d": []}
# ring 0: a   ring 1: b, c   ring 2: d
Your task

Write shortest_path(graph, start, goal) for an adjacency dict. Return the list of nodes on a path with the fewest edges, or None when goal cannot be reached. Visit neighbours in listed order.