Skip to main content
Module 5 · Dijkstra's Algorithm

Lesson 4 · The Dijkstra Class — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. When is self.graph built?

    1. A.The first time you call compute_path
    2. B.Automatically in __init__, when the object is created
    3. C.You must call build_graph yourself after creating the object
    4. D.Every time you access it

    The constructor calls self.build_graph() once, so the graph is stored in self.graph and ready to use as soon as the object exists.

  2. What does `if (row, col) in self.blocked: continue` do?

    1. A.Adds the node with an empty neighbor list
    2. B.Skips a blocked node so it never becomes a key in the graph
    3. C.Stops the whole loop
    4. D.Marks the node as visited

    continue jumps to the next loop iteration, so a blocked node is never added as a key. Combined with the neighbor checks, blocked nodes are fully absent.

  3. Why leave compute_path as a placeholder for now?

    1. A.Because the algorithm is impossible
    2. B.To test the class structure (constructor and graph) first, before adding the harder algorithm next lesson
    3. C.To save memory
    4. D.It will never be implemented

    Building in stages keeps the load manageable — verify the graph is correct now, then focus entirely on the algorithm in Lesson 5.

  4. On a 4×4 grid, how many nodes does Dijkstra((0,0), [(1,0),(2,1)]).graph contain?

    1. A.16
    2. B.15
    3. C.14
    4. D.2

    16 total minus 2 blocked = 14. Both blocked nodes are skipped as keys, and removed from all neighbor lists too.

  5. Why does it matter that Dijkstra's compute_path returns the same type as Manhattan's?

    1. A.It makes the code run faster
    2. B.The Navigator can use either class interchangeably, since it just calls compute_path and gets a list of tuples back
    3. C.It saves disk space
    4. D.It doesn't matter

    A shared interface — same method name, same return type — is what makes Dijkstra a drop-in replacement for Manhattan in the Navigator.