Self-Assessment Quiz: Graph Representations, Traversal, and Search
Twenty questions to check your understanding. Answer before opening the key. Aim for 16+.
Throughout, "the example graph" is the chapter's running graph
(adj = {0: [1, 2], 1: [0, 3], 2: [0, 3], 3: [1, 2, 4], 4: [3], 5: []}).
Question 1
The adjacency matrix of an undirected simple graph on $n$ vertices is always:
A) lower-triangular B) symmetric with a zero diagonal C) symmetric with a diagonal of all 1s D) asymmetric in general
Question 2
On an adjacency list, the cost of testing whether a specific edge $\{i,j\}$ exists is:
A) $\Theta(1)$ B) $\Theta(\deg(i))$ C) $\Theta(n)$ D) $\Theta(n^2)$
Question 3
You have a dense graph and must answer ten million "is $\{i,j\}$ an edge?" queries. The better representation is:
A) adjacency list, because it is always the default B) adjacency matrix, because each query is $\Theta(1)$ and density removes its space penalty C) it makes no difference D) neither — you need a balanced tree
Question 4
The data structure that gives BFS its ring-by-ring behavior is:
A) a LIFO stack B) a FIFO queue C) a priority queue D) a hash set only
Question 5
The data structure that gives DFS its plunge-deep behavior is:
A) a FIFO queue B) a LIFO stack (often the call stack via recursion) C) a min-heap D) a deque used as a queue
Question 6
In BFS, a vertex must be marked visited at the moment it is:
A) dequeued and processed B) enqueued (discovered) C) added to the final order list D) it does not matter when
Question 7
bfs_order(adj, 0) on the example graph returns:
A) [0, 1, 3, 2, 4] B) [0, 1, 2, 3, 4] C) [0, 1, 2, 3, 4, 5] D) [0, 2, 1, 3, 4]
Question 8
dfs_order(adj, 0) on the example graph (neighbor lists increasing) returns:
A) [0, 1, 2, 3, 4] B) [0, 1, 3, 2, 4] C) [0, 2, 3, 1, 4] D) [0, 1, 3, 4, 2]
Question 9 (True/False, justify)
True or false: On the example graph, bfs_distances(adj, 0) returns a dist map whose keys include
5. Justify in one sentence.
Question 10
BFS computes correct shortest paths:
A) on any graph, weighted or not B) only when every edge counts the same (unweighted or all weights equal) C) only on trees D) only on directed graphs
Question 11
To detect a cycle in an undirected graph with DFS, you report a cycle when you find an edge to an already-visited vertex that is:
A) any visited vertex B) the parent you arrived from C) a visited vertex other than the parent (a back edge) D) an unvisited vertex
Question 12
Topological sort via DFS records each vertex:
A) at discovery (pre-order), no reversal B) at finish (post-order), then reverses the list C) at finish (post-order), no reversal D) in the order the outer loop visits keys
Question 13
A topological order of a directed graph exists if and only if the graph:
A) is connected B) has no directed cycle (is a DAG) C) is undirected D) has at most $n - 1$ edges
Question 14
On an adjacency list, both BFS and DFS run in time:
A) $\Theta(V^2)$ B) $\Theta(V \cdot E)$ C) $\Theta(V + E)$ D) $\Theta(E^2)$
Question 15
On an adjacency matrix, both BFS and DFS run in time:
A) $\Theta(V + E)$ B) $\Theta(V^2)$ C) $\Theta(E)$ D) $\Theta(V \log V)$
Question 16 (True/False, justify)
True or false: The neighbor-scanning work of a traversal on an adjacency list is $\Theta(V \cdot E)$ because each of the $V$ vertices scans a neighbor list. Justify in one sentence.
Question 17
The lemma that makes the total neighbor-scanning work sum to $\Theta(E)$ rather than $\Theta(V^2)$ is:
A) the pigeonhole principle B) the handshaking lemma, $\sum_v \deg(v) = 2E$ C) the master theorem D) Euler's formula
Question 18 (Short answer)
A graph has $n = 4$ vertices and exactly $n - 1 = 3$ tree edges after a BFS. In one sentence, why can the BFS tree never contain a cycle?
Question 19 (Short answer)
State the one-line test (in terms of the size of the reached set) for whether a traversal from a single vertex proves the whole undirected graph is connected.
Question 20 (True/False, justify)
True or false: Reusing the undirected cycle-detection routine on a directed graph (by ignoring edge directions) correctly detects directed cycles. Justify with the kind of graph that breaks it.
Answer Key
| Q | Ans | Note |
|---|---|---|
| 1 | B | Undirected ⇒ symmetric; simple (no self-loops) ⇒ zero diagonal. |
| 2 | B | You must scan $i$'s neighbor list, length $\deg(i)$. |
| 3 | B | $\Theta(1)$ lookups beat $\Theta(\deg)$ scans, and density nullifies the matrix's space cost. |
| 4 | B | A FIFO queue releases earlier-discovered (nearer) vertices first. |
| 5 | B | A LIFO stack — usually the recursion call stack — drives "go deep, then backtrack." |
| 6 | B | Marking at enqueue guarantees each vertex enters the queue exactly once, at its true distance. |
| 7 | B | Ring order: distance 0 = {0}, 1 = {1,2}, 2 = {3}, 3 = {4}; vertex 5 is unreachable. |
| 8 | B | DFS dives $0\to1\to3$, then $3\to2$, backtracks, then $3\to4$. |
| 9 | False | 5 is absent — it is unreachable from 0, so BFS never discovers it; that is how we detect disconnection. |
| 10 | B | The ring argument needs equal edge weights; weighted graphs need Dijkstra (Ch. 29). |
| 11 | C | A back edge to a visited non-parent vertex closes a cycle; the parent edge is just the one you arrived on. |
| 12 | B | Post-order (record at finish), then reverse — the finishing-time property requires the reversal. |
| 13 | B | A directed cycle would force a vertex strictly before itself; acyclicity (DAG) is exactly the condition. |
| 14 | C | $\Theta(V)$ per-vertex work + $\Theta(E)$ total scanning. |
| 15 | B | Listing each vertex's neighbors scans a full row ($n$ cells): $V \cdot \Theta(V) = \Theta(V^2)$. |
| 16 | False | It is $\Theta(E)$: by handshaking the scans examine each edge a constant number of times in total, not once per vertex. |
| 17 | B | Handshaking: $\sum_v \deg(v) = 2E$, so all scans together cost $\Theta(E)$. |
| 18 | — | Each non-source vertex gets exactly one parent (one incoming tree edge), so the tree edges form a tree — by definition acyclic. |
| 19 | — | len(reached) == n: the traversal reached every vertex iff the reached-set size equals the vertex count. |
| 20 | False | A directed "diamond" $a\to b, a\to c, b\to d, c\to d$ is a DAG (no directed cycle), yet ignoring directions sees the 4-cycle $a\!-\!b\!-\!d\!-\!c\!-\!a$. |
Topics to review by question
| Questions | Topic |
|---|---|
| 1–3 | Adjacency matrix vs. list; space–time tradeoff (§28.1) |
| 4–9 | BFS: queue discipline, marking at enqueue, ring order, shortest distances (§28.2) |
| 8 | DFS: stack/recursion, pre-order (§28.3) |
| 10 | BFS limited to unweighted graphs; Dijkstra forward-pointer (§28.2, Ch. 29) |
| 11, 20 | Cycle detection — undirected vs. directed (§28.4–28.5) |
| 12–13 | Topological sort via DFS post-order; DAG condition (§28.5) |
| 14–17 | Complexity of traversal; the handshaking-lemma aggregate argument (§28.6, Ch. 14, Ch. 27) |
| 18 | Traversal trees and spanning trees (§28.4) |
| 19 | Connectivity test (§28.4, Ch. 27) |