Lesson 5 · Implementing compute_path() — Knowledge ChecksAnswer key
Why are all distances initialized to 999999 except the start?
999999 is a stand-in for infinity. The start is 0 (zero steps from itself); everything else is 'unknown' until a real path is discovered.
What does this loop leave in the variable current?
It scans all nodes, ignores visited ones, and keeps the one with the smallest distance — exactly the 'pick the nearest unvisited node' step from the paper trace.
Why the check `if new_distance < distances[neighbor]` before updating?
Only a strictly shorter distance should replace what's stored. Without the check, a later, longer route could clobber a shorter one — breaking the shortest-path guarantee.
What does the `if current is None` check protect against?
If obstacles seal off the destination, no reachable unvisited node remains. Without the check the loop would spin forever (or crash); with it, compute_path returns [] gracefully.
compute_path returns [(0,0), (0,1), (0,2), (1,2), (2,2)]. How many steps is that?
The path includes the start, so it lists 5 nodes but 4 moves between them. Steps = len(path) − 1 = 4.