Chapter 6: Shortest Paths
Multiple Choice · Basic Concepts
Question 1: Relaxation
Suppose d[u] = 6, d[v] = 10 and w(u, v) = 3, and RELAX(u, v, w) is called. What are d[v] and π[v] afterwards?
D: Since d[u] + w = 6 + 3 = 9 < 10, the estimate improves and the predecessor becomes u. B forgets the π update; then the path cannot be reconstructed. C mistakes the edge weight for the distance. d[v] can only decrease and never goes below the true δ(s, v).
Multiple Choice · Basic Concepts
Question 2: Adding a Constant to the Weights
If 2 is added to the weight of every edge in the graph, what happens to the shortest path from s to t?
B: Every path grows by (number of edges) × 2. s → a → t (2 edges) goes from 2 to 6, s → t (1 edge) from 3 to 5. Paths with more edges are penalized more, so the shortest path can change. C is true for minimum spanning trees (every spanning tree has V − 1 edges, so the order is preserved), not for shortest paths.
Multiple Choice · Basic Concepts
Question 3: A Negative Cycle
If a negative cycle is reachable from the source, which of the following is true?
A: Going around the cycle once more shortens the path every time; for every vertex reached through the cycle there is no minimum either. Bellman-Ford detects it from an edge that still relaxes in round V, Floyd-Warshall from D[i][i] < 0. Dijkstra cannot handle negative edges correctly anyway.
Step-by-Step Trace · Dijkstra
Question 4: Tracing Dijkstra (Array)
dijkstra(0) finalizes the vertices in which order? What are the final d and prev arrays?
Undirected weighted graph, source 0. d[0] = 0, all others ∞; no vertex has been finalized.
The unfinalized vertex with the smallest d is 0 (d = 0); it is chosen. Relaxed neighbors: 1: ∞ → 4, 2: ∞ → 1.
The unfinalized vertex with the smallest d is 2 (d = 1); it is chosen. Relaxed neighbors: 1: 4 → 3, 3: ∞ → 9, 4: ∞ → 13.
The unfinalized vertex with the smallest d is 1 (d = 3); it is chosen. Relaxed neighbors: 3: 9 → 8.
The unfinalized vertex with the smallest d is 3 (d = 8); it is chosen. Relaxed neighbors: 4: 13 → 11, 5: ∞ → 14.
The unfinalized vertex with the smallest d is 4 (d = 11); it is chosen. Relaxed neighbors: 5: 14 → 13.
The unfinalized vertex with the smallest d is 5 (d = 13); it is chosen. No neighbor improves.
Done. The order of selection is 0, 2, 1, 3, 4, 5. The green edges form the shortest-path tree; for example, the path to 5 is 0 → 2 → 1 → 3 → 4 → 5 (13).
void dijkstra(int s) { Arrays.fill(d, INF); d[s] = 0; for (int i = 0; i < V; i++) { int u = -1; // smallest d for (int v = 0; v < V; v++) if (!visited[v] && (u == -1 || d[v] < d[u])) u = v; visited[u] = true; // finalized for (Edge e : adj[u]) if (!visited[e.to] && d[u] + e.w < d[e.to]) { d[e.to] = d[u] + e.w; prev[e.to] = u; } } }
Output Question · Dijkstra
Question 5: Dijkstra Table
Source A. In which order does Dijkstra finalize the vertices? What are the final d and π values? Which is the shortest path from A to F?
Show answer
Finalization order: A, C, B, D, E, F
| vertex | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| d | 0 | 5 | 2 | 7 | 8 | 10 |
| π | — | C | A | B | D | E |
B first gets 6 from A, and drops to 2 + 3 = 5 once C is finalized. D: 5 + 2 = 7 (better than 9 via C → D); E: 7 + 1 = 8; F: 8 + 2 = 10 via E (better than 13 via D → F).
Path: A → C → B → D → E → F = 10.
Output Question · Dijkstra
Question 6: Reconstructing the Path
For source 4, Dijkstra produced the array prev = {3, 4, 0, 4, −1, 2} (indices 0–5). What does printPath(prev, dist, 4, 1) print (all vertices are reachable)? What would it print for target 5?
void printPath(int[] prev, int[] dist, int src, int target) { if (dist[target] == INF) { // unreachable System.out.print("no path"); return; } Deque<Integer> stack = new ArrayDeque<>(); for (int v = target; v != src; v = prev[v]) stack.push(v); stack.push(src); while (!stack.isEmpty()) System.out.print(stack.pop() + " "); }
Show answer
Target 1: the loop pushes 1; since v = prev[1] = 4 is the source, the loop ends and the source is pushed separately. Output: 4 1.
Target 5: 5 → prev 2 → prev 0 → prev 3 → prev 4 (the source). The stack reverses this: 4 3 0 2 5.
The stack turns the vertices collected from the target back to the source into the order from the source to the target. Without the initial dist[target] == INF check, for an unreachable target the chain would reach −1 without ever reaching the source, and prev[−1] would throw ArrayIndexOutOfBoundsException.
Multiple Choice · Dijkstra
Question 7: Dijkstra with a Negative Edge
Dijkstra (the version that never touches a finalized vertex again) is run from S. What are the d[T] it finds and the true shortest distance?
C: After S, the smallest d is A's (2), then T (3) is finalized. When B (d = 5) is finally processed, the edge B → T gives 5 − 4 = 1, but T is already finalized. The true path is S → A → B → T = 2 + 3 − 4 = 1. When Dijkstra finalizes a vertex, it assumes the shortest path to it has been found; a negative edge breaks this assumption.
Classic Question · Dijkstra
Question 8: Counting Shortest Paths
How would you modify Dijkstra so that it also computes the number of distinct shortest paths to every vertex (count[v])? In the graph with edges s → a (1), s → b (1), a → t (2), b → t (2), s → t (3), what is count[t]?
Show answer
count[src] = 1
on relaxing (u → v, w):
if d[u] + w < d[v]:
d[v] = d[u] + w; count[v] = count[u]
else if d[u] + w == d[v]:
count[v] += count[u]When a shorter path is found, the old count is invalid (it starts over); when an equally long path is found, the counts are added. When u is finalized, count[u] is final, because all shortest paths into u come from vertices with smaller d (weights > 0).
Example: d[t] = 3 and three paths: s → t, s → a → t, s → b → t; count[t] = 3.
Step-by-Step Trace · Bellman-Ford
Question 9: Tracing Bellman-Ford
In which round does Bellman-Ford relax which edges for source 0? What is the final d array?
Source 0, V = 4. The edge list is examined in this order: (2,3,2), (1,2,−1), (0,1,4), (0,2,5).
Round 1, edge (0,1): 0 + (4) = 4 < ∞ → d[1] = 4.
Round 1, edge (0,2): 0 + (5) = 5 < ∞ → d[2] = 5.
Round 1 is over; edges that changed nothing: (2,3), (1,2). Something changed, so we go on to the next round.
Round 2, edge (2,3): 5 + (2) = 7 < ∞ → d[3] = 7.
Round 2, edge (1,2): 4 + (−1) = 3 < 5 → d[2] = 3.
Round 2 is over; edges that changed nothing: (0,1), (0,2). Something changed, so we go on to the next round.
Round 3, edge (2,3): 3 + (2) = 5 < 7 → d[3] = 5.
Round 3 is over; edges that changed nothing: (1,2), (0,1), (0,2). i = V − 1 = 3 was the last round.
Check round: no edge relaxes, no exception. Result d = [0, 4, 3, 5]. Since the edge (2,3) is at the start of the list, in every round it used the value from the previous round: all V − 1 rounds were needed.
for (int i = 1; i < V; i++) { boolean changed = false; for (Edge e : edges) if (d[e.u] != INF && d[e.u] + e.w < d[e.v]) { d[e.v] = d[e.u] + e.w; changed = true; } if (!changed) break; // early exit } for (Edge e : edges) // check if (d[e.u] != INF && d[e.u] + e.w < d[e.v]) throw new IllegalStateException("Negative cycle");
Multiple Choice · Bellman-Ford
Question 10: Cycle Weight
A graph contains a cycle made of the edges A → B (3), B → C (−5) and C → A (w), and A is reachable from the source. For which integer values of w does the check round of Bellman-Ford report a negative cycle?
A: The weight of the cycle is 3 − 5 + w = w − 2; for it to be negative, w − 2 < 0, i.e. w ≤ 1 (integer). For w = 2 the cycle weight is 0: going around the cycle does not shorten the path, so there is no negative cycle. D takes only the edge B → C into account.
Output Question · Bellman-Ford
Question 11: Negative Cycle Check
Source 0, edge order (0,1), (1,2), (2,3), (3,1). Which d array does Bellman-Ford find after V − 1 = 3 rounds? Which edge is the first to relax in the check round?
Show answer
| round | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 1 | 0 | 1 | 3 | 2 |
| 2 | 0 | 0 | 2 | 1 |
| 3 | 0 | −1 | 1 | 0 |
The weight of the cycle 1 → 2 → 3 → 1 is 1 − 1 − 1 = −1: the values on the cycle decrease by 1 every round. In the check, the first edge (0,1): d[0] + 2 = 2 > d[1] = −1, does not relax; (1,2): −1 + 1 = 0 < d[2] = 1 → relaxes → IllegalStateException("Negative cycle").
The d values found are meaningless: the shortest paths to 1, 2 and 3 are −∞.
Step-by-Step Trace · Floyd-Warshall
Question 12: Tracing Floyd-Warshall
In each round k, which cells change? What is the final matrix D? (Highlighted cell: shortened in this round.)
Edges: 0→1 (3), 0→3 (7), 1→0 (8), 1→2 (2), 2→0 (5), 2→3 (1), 3→0 (2). Initially D holds the edge weights, the diagonal is 0, and ∞ where there is no edge.
k = 0 (intermediate vertex 0; the row and column outlined in blue do not change): D[1][3]: ∞ → 15 (8 + 7), D[2][1]: ∞ → 8 (5 + 3), D[3][1]: ∞ → 5 (2 + 3).
k = 1 (intermediate vertex 1; the row and column outlined in blue do not change): D[0][2]: ∞ → 5 (3 + 2), D[3][2]: ∞ → 7 (5 + 2).
k = 2 (intermediate vertex 2; the row and column outlined in blue do not change): D[0][3]: 7 → 6 (5 + 1), D[1][0]: 8 → 7 (2 + 5), D[1][3]: 15 → 3 (2 + 1).
k = 3 (intermediate vertex 3; the row and column outlined in blue do not change): D[1][0]: 7 → 5 (3 + 2), D[2][0]: 5 → 3 (1 + 2), D[2][1]: 8 → 6 (1 + 5).
No negative value on the diagonal → no negative cycle. The final D holds the shortest distances between all pairs; for example D[1][0] = 5 (1 → 2 → 3 → 0).
for (int k = 0; k < V; k++) // via for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) if (d[i][k] != INF && d[k][j] != INF && d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j]; for (int i = 0; i < V; i++) if (d[i][i] < 0) throw new IllegalStateException("Negative cycle");
Output Question · Floyd-Warshall
Question 13: Floyd-Warshall with a Negative Edge
Floyd-Warshall is run on this graph. What is the final matrix D? Is there a negative cycle? Which is the shortest path from 0 to 3?
Show answer
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | 0 | 4 | 1 | 3 |
| 1 | ∞ | 0 | −3 | −1 |
| 2 | ∞ | 6 | 0 | 2 |
| 3 | ∞ | 4 | 1 | 0 |
The diagonal is all 0 → no negative cycle (1 → 2 → 3 → 1 = −3 + 2 + 4 = 3 > 0). Column 0 is ∞: there is no edge into 0.
0 ⇝ 3: Π[0][3] = 2, Π[0][2] = 1, Π[0][1] = 0 → 0 → 1 → 2 → 3 = 4 − 3 + 2 = 3. Thanks to the negative edge, 0 → 1 → 2 (1) is used instead of 0 → 2 (6).
Multiple Choice · Floyd-Warshall
Question 14: Loop Order
What happens if the loops in the Floyd-Warshall code are rearranged into the order for i … for j … for k … (k innermost)?
B: The cells rely on D[i][k] and D[k][j] values that have not been shortened yet. The recurrence builds D⁽ᵏ⁾ from D⁽ᵏ⁻¹⁾: when round k starts, all pairs must already be computed with the intermediate vertices {0, …, k − 1}. With k innermost, the cell (i, j) is computed once, before the other cells are ready; it misses paths with many intermediate vertices. The number of loops is unchanged, and the time is still Θ(V³).
Step-by-Step Trace · A* Algorithm
Question 15: Tracing A*
Which vertices does A* expand, and in which order? What are the path found and its cost?
From s to t; next to each vertex are h (the estimate to the goal) and g (the cost found so far). h is admissible and consistent; if f is equal, the alphabetically first is chosen.
Expanding s, the vertex with the smallest f = 6. Opened/updated: a: g = 2, f = 6; b: g = 3, f = 6.
Expanding a, the vertex with the smallest f = 6. Opened/updated: c: g = 6, f = 8; t: g = 9, f = 9.
Expanding b, the vertex with the smallest f = 6. Opened/updated: c: g = 4, f = 6.
Expanding c, the vertex with the smallest f = 6. Opened/updated: t: g = 7, f = 7.
Smallest f: t (7). The goal was removed from the open list: stop. The path (via prev) is s → b → c → t, cost 7. The expensive paths opened through a (c: 6, t: 9) were corrected when b and c were expanded.
g[s] = 0; open.add(s); while (!open.isEmpty()) { int u = lowestF(open); // f = g + h open.remove(u); closed.add(u); if (u == target) break; for (Edge e : adj[u]) { int newG = g[u] + e.w; if (!closed.contains(e.v) && newG < g[e.v]) { g[e.v] = newG; prev[e.v] = u; open.add(e.v); } } }
Multiple Choice · A* Algorithm
Question 16: The Next Expansion
The open list of A* contains these vertices (g, h): p (4, 7), q (6, 3), r (2, 10), u (8, 2). Which one is expanded?
C: f(p) = 11, f(q) = 9, f(r) = 12, f(u) = 10. If A* looks only at g, it behaves like Dijkstra (A); if only at h, like greedy best-first search (B); the selection criterion is the total f.
Multiple Choice · A* Algorithm
Question 17: An Admissible Heuristic
A path is searched for on a grid; every step costs 1. Which statement is true?
D: With 4 directions, each step decreases |Δx| + |Δy| by at most 1, so h ≤ h*. With 8 directions, a diagonal step decreases it by 2 but costs 1: h exceeds the true remaining cost. A is wrong: Manhattan is greater than or equal to Euclidean. h = 0 is admissible (A* = Dijkstra). An overestimating h can miss the shortest path.
Output Question · A* Algorithm
Question 18: Greedy Search and A*
From s to t; the h value is next to each vertex. (a) Which path does greedy best-first search, which picks only the vertex with the smallest h from the open list, find, and what is its cost? (b) Which path does A* (f = g + h) find?
Show answer
(a) Greedy: after s, a (h = 1) looks more attractive than b (h = 4); t is opened from a (h = 0) and chosen immediately. The path is s → a → t, cost 10.
(b) A*: f(a) = 1 + 1 = 2, f(b) = 2 + 4 = 6. When a is expanded, t is opened with g = 10, f = 10. Then b (6) and c (f = 4 + 2 = 6) are expanded; from c, the g of t drops to 6. t (f = 6) is removed: the path is s → b → c → t, cost 6.
Since h is admissible, A* finds the shortest path; greedy search is fast but ignores g, so it may choose an expensive path.
Multiple Choice · Comparison
Question 19: Choosing an Algorithm
Which of the following pairings is wrong?
B is wrong: with a negative edge, Dijkstra errs because a shorter path to an already finalized vertex may be discovered later; Bellman-Ford (O(V · E)) must be used. For a graph with V = 1000, E = 5000 and negative edges, this is about 5 · 10⁶ relaxation attempts.