Tarjan’s algorithm identifies strongly connected components in directed graphs using depth-first search to analyze graph structure efficiently.
Tarjan's algorithm finds the strongly connected components of a directed graph with a single depth-first search. A strongly connected component is a maximal set of vertices in which every vertex reaches every other. It is used to detect dependency cycles, to simplify a directed graph into an acyclic graph, and to solve satisfiability problems [1][2].
During the search each vertex $v$ receives a discovery index $\mathrm{idx}(v)$ and a value $\mathrm{low}(v)$, the smallest index reachable from $v$ following tree edges and at most one back edge to a vertex still on the stack [1][2]. Discovered vertices are pushed onto a stack. When at the end of the visit of $v$ it holds that $\mathrm{low}(v)=\mathrm{idx}(v)$, $v$ is the root of a component: all vertices are popped from the stack up to $v$, which form the component.
Consider the graph with $8$ vertices and $10$ edges: $1\to2$, $2\to3$, $3\to1$, $3\to4$, $4\to5$, $5\to6$, $6\to4$, $7\to6$, $7\to8$, $8\to7$. Starting from vertex $1$ the indices are $\mathrm{idx}=0,1,2,3,4,5$ for vertices $1,\dots,6$, then $6$ and $7$ for $7$ and $8$. The values of $\mathrm{low}$ are $0,0,0$ for $1,2,3$, then $3,3,3$ for $4,5,6$ and $6,6$ for $7,8$. The components found, in order, are $\{4,5,6\}$, $\{1,2,3\}$ and $\{7,8\}$.
The components come out in reverse topological order of the condensed graph: $\{4,5,6\}$ has no outgoing edges to other components, and is extracted first; $\{1,2,3\}$ has the edge $3\to4$ towards it; $\{7,8\}$ has the edge $7\to6$ towards $\{4,5,6\}$ [1][2]. The condensed graph, with one component per vertex, is acyclic: it is the dependency graph among groups of mutually reachable vertices.
Each vertex and each edge is visited only once, so the time is $O(V+E)$: for the example graph $8+10=18$ elementary operations, up to constants [1][2]. Memory is $O(V)$ for indices, $\mathrm{low}$ values, and stack. Another procedure with the same complexity is Kosaraju’s, with two passes, one on the graph and one on the transpose graph.
Analysis of dependencies among software modules, compiler optimization, model checking (states mutually reachable), solving 2-SAT, and web page analysis [1][2] (general observation).
The recursive implementation may exhaust the call stack on deep graphs, so an iterative version with an explicit stack is used. The algorithm applies to directed graphs; for undirected graphs the corresponding concept is that of connected component, simpler, or biconnected component.
Generating summary…