Sub topic 11
Why Tarjan's Algorithm Works
This chapter assumes you know the recursive Tarjan code from the previous chapter. You should also be comfortable with DFS trees, discovery indices, and the four edge types: tree, back, forward, and cross. The goal is to explain why the test low[v] == index[v] finds exactly the component roots, and why each pop takes off exactly one SCC.
The root of a component
Let C be an SCC. Its root is the first vertex of C that DFS discovers. Call it r. When r is discovered, every other vertex of C is still unvisited and reachable from r. By the white-path theorem, all of C ends up in r's DFS subtree. So each SCC hangs below its root as a connected piece of the DFS tree.
Two facts about the stack
The proof rests on two facts:
- A vertex stays on the stack until the root of its SCC finishes. Pops happen only at roots, and a pop removes only vertices discovered after that root.
- Suppose w is on the stack while v is being explored, and
index[w] < index[v]. Then w can reach v. The root of w's SCC is still active, so it is an ancestor of v, and w reaches that root.
Claim: low[v] == index[v] exactly when v is a root
If v is a root, low cannot drop. Suppose some edge out of v's subtree lowered low[v] to index[w] < index[v] with w on the stack. By fact 2, w reaches v, and v reaches w through its subtree. That puts w in v's SCC even though w was discovered before v, which contradicts v being the root.
If v is not a root, low must drop. Let r be the root of v's SCC. Then r is a proper ancestor of v. Follow a path from v to r. At some edge (x, y) this path first leaves v's subtree. Vertex y lies on a path between members of C, so y is in C. By fact 1, y is still on the stack. When x scanned this edge, y was already visited. Otherwise y would have become x's tree child. Since y is visited but outside v's subtree, it was discovered before v. The update low[x] = min(low[x], index[y]) then carries up the tree to v, which gives low[v] < index[v].
Why each pop yields one SCC
When root v finishes, the stack above v holds only vertices discovered inside v's subtree. Any of those that belonged to a different SCC had their own root inside the subtree. That root finished earlier and popped them. What remains is exactly C.
This also explains the output order. An SCC is emitted only after every SCC reachable from it has been emitted, so Tarjan's algorithm produces components in reverse topological order of the condensation.
Trace on the example graph
The example graph is adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]].
| Vertex | index | final low | Event |
|---|---|---|---|
| 5 | 5 | 3 | back edge 5→3 |
| 3 | 3 | 3 | root: pops [5, 4, 3] |
| 2 | 2 | 0 | back edge 2→0 |
| 0 | 0 | 0 | root: pops [2, 1, 0] |
| 6 | 6 | 6 | edge 6→5 ignored, since 5 is off the stack |
| 7 | 7 | 6 | edge 7→6; 6 is root, pops [7, 6] |
Exercise
In the example graph, suppose you remove the on_stack[w] condition, so any visited w updates low[v] = min(low[v], index[w]). What happens at vertex 6?
Show answer
Edge 6→5 sets low[6] = min(6, 5) = 5. Then low[6] != index[6], so 6 is never treated as a root. Vertices 6 and 7 are left on the stack and never output as a component, even though {6, 7} is a valid SCC.
Exercise
Why can't the edge (x, y) in the "not a root" argument lead to a vertex y discovered after v finished?
Show answer
When x scanned the edge, either y was unvisited and became x's tree child, which puts it inside v's subtree, or y was already visited. Since y lies outside v's subtree, it must have been discovered before v.