Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Strongly connected components finder
Finds strongly connected components using DFS and stack
Select an algorithm and generate steps to begin visualization
Tarjan's algorithm finds all strongly connected components (SCCs) of a directed graph in a single depth-first search. A strongly connected component is a maximal set of vertices where every vertex can reach every other by directed paths.
During one DFS the algorithm assigns each node a discovery index and a low-link value, the smallest index reachable from its subtree using at most one back edge. Nodes are pushed on a stack as they are visited. When a node finishes with a low-link equal to its own index it is the root of an SCC, and the stack is popped down to that node to output the component. Everything happens in O(V + E) time with a single pass.
SCC decomposition condenses a directed graph into a directed acyclic graph, the first step in solving 2-SAT, analyzing call graphs in compilers, detecting deadlocks, and finding cycles of mutual dependency in package managers or spreadsheets. Tarjan low-link values are a classic hard interview subject.
One DFS, one stack, two numbers per vertex. The insight is that a strongly connected component has a unique root: the vertex in it discovered first.
strongconnect(u):
disc[u] = low[u] = ++time
stack.push(u); onStack[u] = true
for each edge (u, v):
if v is unvisited:
strongconnect(v)
low[u] = min(low[u], low[v])
else if onStack[v]:
low[u] = min(low[u], disc[v])
// else: v is in a finished SCC, ignore it
if low[u] == disc[u]: // u is an SCC root
pop the stack down to and including u
that popped set is one SCCThe onStack test is what separates Tarjan from a naive low-link scheme. An edge into a vertex that is visited but already assigned to a finished component tells you nothing about your own component and must be skipped. Including it would merge two genuinely separate SCCs. Note also the asymmetry: a tree edge folds in low[v], a back edge folds in disc[v], and mixing those up is the other classic bug.
Run Tarjan on a directed graph holding one three-cycle, one two-cycle, and one vertex that belongs to neither.
Example graph: Directed edges A to B, B to C, C to A, B to D, D to E, E to D, and C to F.
Final low-links are A 1, B 1, C 1, F 4, D 5, E 5, and the components come out in the order {F}, then {D, E}, then {A, B, C}. Two things are worth noting. Components are emitted in reverse topological order of the condensation, which is why Tarjan is the usual first step for 2-SAT. And F, reachable from the cycle but with no way back, is correctly its own component rather than being absorbed into {A, B, C}.
Time: O(V + E) · Space: O(V)
A single depth-first search visits each vertex once and examines each directed edge exactly once, giving O(V + E). Each vertex is pushed onto the stack once and popped once, so the stack operations total O(V) across the whole run. The extra state is the discovery time, the low-link and the on-stack flag per vertex, plus the recursion stack, all O(V). Tarjan does this in one pass, where Kosaraju needs two full traversals plus the construction of the transposed graph, which is why Tarjan is usually preferred in practice even though both are linear.
All three linear SCC algorithms have the same asymptotic cost, so the choice is about constants, memory and how easy the code is to get right.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Kosaraju | You want the algorithm that is easiest to explain and implement. Two DFS passes plus a transpose. | O(V + E), two passes |
| Path-based SCC | You want a one-pass algorithm like Tarjan but with two stacks instead of low-link arithmetic. | O(V + E) |
| Union-Find | The graph is undirected. Connected components are much easier than strongly connected ones. | O(E·α(V)) |
| Condensation plus topological sort | You want the DAG of components rather than the components alone. Tarjan already emits them in reverse topological order. | O(V + E) |
Read the full article: Graph Algorithms and Their Complexity
Related algorithms: Kosaraju's SCC Algorithm, Depth-First Search, Topological Sort