Sub topic 13
Tarjan's Single-Pass Algorithm
This chapter assumes you know depth-first search, the definition of a strongly connected component (SCC), and Kosaraju's algorithm from the previous chapter. Kosaraju needs two traversals and a reversed copy of the graph. Robert Tarjan's algorithm finds every SCC in one depth-first search, with no reversed graph. It still runs in O(V + E) time.
The Working Model
Tarjan's algorithm gives each vertex two numbers during the DFS:
| Value | Meaning |
|---|---|
| `index[u]` | The order in which DFS first reached `u` (0, 1, 2, ...) |
| `low[u]` | The smallest `index` that `u` can reach through its DFS subtree plus at most one edge back to a vertex still on the stack |
The algorithm also keeps its own stack of vertices. A vertex goes onto this stack when the DFS first reaches it. It stays there after its DFS call returns, and comes off only when its whole SCC has been found.
When the DFS finishes vertex u, it checks one condition. If low[u] == index[u], then nothing in u's subtree can reach a vertex discovered earlier than u. That makes u the root of an SCC: the first vertex of the component that the DFS reached. Every vertex above u on the stack belongs to the same SCC, so the algorithm pops them all, down to and including u.
The Code
import sys
def tarjan_scc(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v)
index = [-1] * n
low = [0] * n
on_stack = [False] * n
stack = []
components = []
counter = 0
def visit(u):
nonlocal counter
index[u] = low[u] = counter
counter += 1
stack.append(u)
on_stack[u] = True
for v in adj[u]:
if index[v] == -1:
visit(v)
low[u] = min(low[u], low[v])
elif on_stack[v]:
low[u] = min(low[u], index[v])
if low[u] == index[u]:
component = []
while True:
w = stack.pop()
on_stack[w] = False
component.append(w)
if w == u:
break
components.append(sorted(component))
sys.setrecursionlimit(max(1000, 2 * n + 10))
for s in range(n):
if index[s] == -1:
visit(s)
return components
edges = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4), (4, 3)]
print(tarjan_scc(5, edges))
# [[3, 4], [0, 1, 2]]The algorithm finds [3, 4] before [0, 1, 2]. Tarjan always outputs SCCs in reverse topological order of the condensation graph, so sink components come first. That order is useful when you need to process components after everything they point to, as in 2-SAT solving or evaluating dependencies.
Tracing the Example
- The DFS visits 0, 1, and 2. The edge 2→0 points at a vertex that is still on the stack, so
low[2]becomes 0. - The DFS moves on to 3 and then 4. The edge 4→3 sets
low[4]to 3. - When vertex 3 finishes,
low[3] == index[3] == 3. The algorithm pops 4 and 3 as one SCC. - The value
low[2] = 0passes back up through 1 to 0. At vertex 0,low[0] == index[0], so the algorithm pops 2, 1, and 0.
Common Errors
- Skipping the
on_stackcheck. An edge into an SCC that has already been popped is a cross edge. Without the check, that edge would lowerlow[u]and merge two separate components. - Using
low[v]for on-stack neighbors. The original algorithm usesindex[v]in this case. Many implementations uselow[v]and still get correct SCCs, butindex[v]is the standard form and the one that is easy to prove correct. Using it also keeps the code consistent with Tarjan's related algorithms for bridges and articulation points. - Forgetting to clear
on_stackwhen you pop a vertex.
Kosaraju or Tarjan?
| Criterion | Kosaraju | Tarjan |
|---|---|---|
| DFS passes | 2 | 1 |
| Reversed graph needed | Yes | No |
| Extra memory | Reversed adjacency list | `index`, `low`, `on_stack` arrays |
| Conceptual simplicity | Higher | Lower |
Both algorithms run in O(V + E) time. Tarjan's algorithm saves the memory for the reversed graph and avoids a second pass over the edges.
Exercise
What does tarjan_scc(3, [(0, 1), (1, 0), (1, 2)]) print, and why does vertex 2 come out first?
Show answer
It prints [[2], [0, 1]]. Vertex 2 has no outgoing edges, so when its DFS call finishes, low[2] == index[2] == 2 and it is popped as its own SCC. The edge 1→0 then gives low[1] = 0, so vertices 1 and 0 are popped together when vertex 0 finishes. The sink component {2} comes out first because Tarjan outputs SCCs in reverse topological order.
Exercise
In the graph with edges 0→1 and 1→0 plus 2→0, suppose DFS starts at vertex 0, then later visits vertex 2. If you remove the on_stack check and always set low[2] = min(low[2], index[0]), what goes wrong?
Show answer
By the time vertex 2 is visited, {0, 1} has already been popped as an SCC. Without the check, low[2] becomes 0, which is less than index[2] = 2, so vertex 2 is never recognized as a root. It stays on the stack and is never output as its own SCC {2}.