Skip to main content
Module 5 · Dijkstra's Algorithm

Lesson 5 · Implementing compute_path() — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. Why are all distances initialized to 999999 except the start?

    1. A.It is the maximum grid size
    2. B.It represents "infinity / no path yet" — any real distance is smaller, so the first path found always improves it
    3. C.It is a random seed
    4. D.To make the numbers line up

    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.

  2. What does this loop leave in the variable current?

    1. A.The last node in the graph
    2. B.The unvisited node with the smallest known distance
    3. C.The destination
    4. D.A node chosen at random

    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.

  3. Why the check `if new_distance < distances[neighbor]` before updating?

    1. A.To skip blocked nodes
    2. B.So a longer path never overwrites a shorter one already found
    3. C.To count the neighbors
    4. D.It is optional and does nothing

    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.

  4. What does the `if current is None` check protect against?

    1. A.Running out of memory
    2. B.An unreachable destination — no unvisited node has a known distance, so the loop would never end
    3. C.A blocked start node
    4. D.Too many neighbors

    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.

  5. compute_path returns [(0,0), (0,1), (0,2), (1,2), (2,2)]. How many steps is that?

    1. A.5
    2. B.4
    3. C.3
    4. D.0

    The path includes the start, so it lists 5 nodes but 4 moves between them. Steps = len(path) − 1 = 4.