Sub topic 3
Why Tarjan's Algorithm Works
This chapter assumes you have already seen the code for Kosaraju's and Tarjan's algorithms, and that you know depth-first search (DFS), how DFS classifies edges, and Big-O notation.
Why Tarjan's Algorithm Works
The first vertex DFS discovers in an SCC is that component's root. Every other vertex of the SCC becomes a descendant of the root in the DFS tree. The value low[v] holds the smallest discovery index reachable from v's subtree using tree edges plus one edge to a vertex that is still on the stack.
- If
low[v] < index[v], then v's subtree can reach an older vertex that can reach back to v. That means v belongs to an older vertex's SCC, so v is not a root. - If
low[v] == index[v], nothing in v's subtree escapes above v, so v is a root.
When v finishes as a root, any descendant that belonged to a different SCC has already been popped, because that descendant's own root finished first. The vertices left above v on the stack are therefore exactly v's SCC. A cross edge to a vertex that is no longer on the stack points into a finished SCC, so the algorithm ignores it.
Linear Time: O(V + E)
Each vertex gets an index once, is pushed once, and is popped once. Each edge is examined once. That gives O(V + E) total. Kosaraju's algorithm is also O(V + E), but it scans the edges twice and builds the transpose graph.
Avoiding Recursion Limits with Iterative DFS
Python's default recursion limit is about 1000 frames, so a path graph with 10,000 vertices makes either recursive version crash. The iterative Tarjan replaces each call frame with a (vertex, next edge position) pair. When a vertex finishes, the code applies low[parent] = min(low[parent], low[v]) by hand. In the recursive version, that update happens after the recursive call returns.
Tarjan vs. Kosaraju
| Aspect | Kosaraju | Tarjan |
|---|---|---|
| DFS passes | 2 | 1 |
| Extra memory | Transpose graph, O(V + E) | Index, low, and on-stack arrays, O(V) |
| Output order | Topological order of condensation | Reverse topological order |
| Ease of proof | Simpler | Subtler |
Solving 2-SAT with SCCs
2-SAT asks whether you can assign true or false to every variable so that each clause of two literals is satisfied. Build an implication graph with 2n vertices, one for x and one for ¬x per variable. For each clause (a ∨ b), add the edges ¬a → b and ¬b → a.
- The formula is unsatisfiable exactly when some x and ¬x share an SCC.
- Otherwise, set x to true when x's component comes later in topological order than ¬x's. With Tarjan's numbering, that means x's component was found earlier.
More Applications
- Cycle detection: the graph has a cycle exactly when some SCC has more than one vertex or a vertex has a self-loop.
- Dependency analysis: SCCs of an import or build graph are the groups of circular dependencies.
- Reachability: answer reachability queries on the smaller condensation DAG instead of the full graph.
Exercise
In Tarjan's algorithm, why is it wrong to update low[v] using a neighbor w that has an index but is no longer on the stack?
Show answer
Vertex w has already been assigned to a completed SCC, which cannot reach back to v. Using index[w] could lower low[v] below index[v] and stop v from being recognized as a root, which would merge separate components.
Exercise
For the clause (x ∨ ¬y), which edges go into the implication graph?
Show answer
¬x → ¬y and y → x.