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: dThink before coding
Use a deque as the queue and mark nodes when they are added, not when they are taken out, so nothing is queued twice. Store each node's parent to rebuild the path backwards from the goal. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
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.
The explanation opens here once every check passes.