Tarjan's Algorithm
–
Pseudocode
1tarjan(u):2 index[u] ← low[u] ← counter; counter ← counter + 1; push u3 for each edge u → v:4 if v has no index: tarjan(v); low[u] ← min(low[u], low[v])5 else if v is on the stack: low[u] ← min(low[u], index[v])6 if low[u] == index[u]: // u is the root of a component7 pop the stack down to u: those vertices form one SCC
Visited
0
Components
0
3/1index / low-linkstackon the stack (blue)colourone strongly connected component
What is Tarjan's Algorithm?
Tarjan's algorithm finds the strongly connected components of a directed graph in a single depth-first search, without reversing the graph like Kosaraju's does.
Every vertex gets an index (the order it was discovered) and a low-link value: the smallest index it can reach back to through the part of the graph still being explored. Vertices wait on a stack. When a vertex finishes with low == index, it is the first vertex of its component, and it and everything above it on the stack are popped off as one SCC.
If nothing below u can climb above it (low[u] == index[u]), u's component is complete.
Using the Visualizer
- • The label 3/1 next to a vertex means index 3, low-link 1.
- • Blue vertices are on the stack (shown on the right); each colour is a finished SCC.
- • Watch low values drop when an edge leads back to a vertex still on the stack.
How It Works
1. Discover
Give u the next index, set low[u] = index[u], push it on the stack.
2. Tree edge
Unvisited v: explore it, then low[u] = min(low[u], low[v]).
3. Back edge
v still on the stack: low[u] = min(low[u], index[v]).
4. Root found
If low[u] == index[u], pop the stack down to u: that is one SCC.
Implementation
Properties & Complexity
Time Complexity
- • One DFS: O(V + E)
- • Each vertex is pushed and popped exactly once
Space & Properties
- • Space: O(V) for index, low and the stack
- • Output order: reverse topological order of the SCCs
Where It Is Used
- • Compilers: finding mutually recursive functions
- • Deadlock detection in wait-for graphs
- • 2-SAT and condensing graphs into DAGs
- • Bridges and articulation points use the same index/low idea
Limitations
- • Harder to understand than Kosaraju at first
- • Deep recursion on long paths (use an explicit stack in production)