Sub topic 10
Tarjan's Algorithm: One Pass with a Stack
This chapter assumes you know recursive depth-first search (DFS), DFS edge classification, and the low-link values from the previous chapter. Kosaraju's algorithm needs two DFS passes and a transpose graph. Tarjan's algorithm finds every strongly connected component (SCC) in a single DFS over the original graph.
The Working Model
Each vertex v carries three pieces of state:
index[v]is its discovery order: 0 for the first vertex visited, 1 for the next, and so on.low[v]is the smallest index reachable fromv's DFS subtree using at most one edge to a vertex still on the stack.on_stack[v]records whethervsits on the SCC stack, meaning it has been visited but not yet assigned to a component.
Every vertex is pushed onto the stack when DFS discovers it. When DFS finishes v and low[v] == index[v], v is the root of an SCC. Every vertex above v on the stack, plus v itself, forms that component. You pop them all off together.
Implementation
def tarjan(n, adj):
index = [None] * n
low = [0] * n
on_stack = [False] * n
stack = []
sccs = []
counter = 0
def strongconnect(v):
nonlocal counter
index[v] = low[v] = counter
counter += 1
stack.append(v)
on_stack[v] = True
for w in adj[v]:
if index[w] is None:
strongconnect(w)
low[v] = min(low[v], low[w])
elif on_stack[w]:
low[v] = min(low[v], index[w])
if low[v] == index[v]: # v is the root of an SCC
comp = []
while True:
w = stack.pop()
on_stack[w] = False
comp.append(w)
if w == v:
break
sccs.append(comp)
for v in range(n):
if index[v] is None:
strongconnect(v)
return sccs
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(tarjan(8, adj)) # [[5, 4, 3], [2, 1, 0], [7, 6]]Tracing the Example
| Event | Effect |
|---|---|
| Visit 0, 1, 2 | indices 0, 1, 2; stack `[0,1,2]` |
| Edge 2→0, 0 on stack | `low[2] = 0` |
| Visit 3, 4, 5 | indices 3, 4, 5 |
| Edge 5→3, 3 on stack | `low[5] = 3`, which propagates to `low[4] = 3` |
| Finish 3, `low == index` | pop `[5, 4, 3]` |
| Finish 0, `low == index` | pop `[2, 1, 0]` |
| Visit 6, edge 6→5 | 5 is off the stack, so the edge is ignored |
| Visit 7, edge 7→6 | `low[7] = 6`; finishing 6 pops `[7, 6]` |
Components come out in reverse topological order of the condensation DAG. The sink component {3,4,5} comes out first, and the source component {6,7} comes out last.
Common Errors
- Popping too little or too much. Pop until you remove
vitself, and no further. - Resetting
on_stackincorrectly. Clear the flag when you pop, never when DFS returns. - Recursion depth. Python's default recursion limit is about 1000, so a long path graph will crash this code. A later chapter converts it to an iterative version.
- Using
low[w]instead ofindex[w]for on-stack edges. For SCCs, this still gives correct components. It does break related algorithms such as articulation points, so stick withindex[w].
Exercise
Run Tarjan's algorithm on adj = [[1], [2], [1, 3], []] starting from vertex 0. List the SCCs in the order they are emitted.
Show answer
Vertices 0, 1, 2, 3 get indices 0–3. Vertex 3 has no edges, so low[3] = 3 and [3] is emitted first. The edge 2→1 sets low[2] = 1. When vertex 1 finishes, low[1] = 1, so it pops [2, 1]. Finally, low[0] = 0, so [0] is emitted. The order is [[3], [2, 1], [0]].
Exercise
Why can a vertex w that has an index but is not on the stack be safely ignored?
Show answer
Because w was already assigned to a completed SCC. Any vertex that is visited but off the stack belongs to a finished component that cannot reach back to v, so w and v cannot be in the same SCC.