Skip to main content
Module 5 · Dijkstra's Algorithm

Lesson 2 · Dictionaries — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. In our graph dictionary, what are the keys and what are the values?

    1. A.Keys are numbers; values are strings
    2. B.Keys are (row, col) node tuples; values are lists of neighbor tuples
    3. C.Keys are lists; values are tuples
    4. D.Both are single integers

    Each node is a tuple key, and its value is the list of nodes it connects to — the Python version of the paper graph.

  2. Given graph = {(0,0): [(0,1)]}, what does (0,0) in graph return?

    1. A.True
    2. B.False
    3. C.[(0,1)]
    4. D.A KeyError

    in checks whether a key exists. (0,0) is a key, so it returns True. (Accessing a missing key with [] would raise KeyError — which is why we use in.)

  3. In build_grid_graph, why is each neighbor guarded by an if (like `if row > 0`)?

    1. A.To make the code shorter
    2. B.To keep neighbors inside the grid — a corner or edge node has no neighbor off the grid
    3. C.To skip blocked nodes
    4. D.It has no effect

    The bounds checks prevent adding neighbors that fall off the edge of the grid, so corners get 2 neighbors and interior nodes get 4.

  4. To block node (1,1), what two things must happen to the graph dictionary?

    1. A.Just delete the key (1,1)
    2. B.Delete the key (1,1) AND remove (1,1) from every other node's neighbor list
    3. C.Set graph[(1,1)] to an empty list
    4. D.Nothing — blocked nodes are ignored automatically

    Deleting only the key leaves dangling references — neighbors still list (1,1). You must also clean it out of every neighbor list.