How Tarjan finds strongly connected components

Short answer: Tarjan’s strongly connected components algorithm is one depth-first search. Each vertex carries a discovery index and a low-link, the smallest index reachable from its subtree by tree edges and at most one back edge. Vertices sit on a stack until their component is emitted. When a vertex’s low-link equals its own index, it is the root of a component, and the stack down to that vertex is the component. The time is Θ(V+E).


What the problem actually asks

Two vertices are strongly connected when each can reach the other by directed paths. The strongly connected components are the maximal such sets. Condensing each component to a vertex leaves a DAG. The algorithmic problem is to find the sets in one pass, without building the transpose graph.

A depth-first search already knows which vertices are on the path from the root of the current tree. A back edge can reach one of them. The low-link summarizes the oldest discovery index the subtree can see. A vertex that cannot see anything older than itself is the first vertex of its component in this search.


What the algorithm guarantees

The algorithm assigns each vertex to exactly one component and runs in Θ(V+E) time and Θ(V) extra memory, including the recursion stack. The components are emitted in reverse topological order of the condensation DAG: an edge between components points from a later-emitted component toward an earlier one, or the implementation’s stack order produces the reverse, and a caller who needs a topological order of the condensation can reverse the emission list. Within a component the order of names is the stack order, not a special order.

The low-link update uses a successor that is on the stack, not every gray or black vertex. A cross edge to a vertex already assigned to a finished component must not pull the low-link down. That restriction is the usual bug.


The working steps

  1. Initialize index = 0, an empty stack, and every vertex unvisited.
  2. From each unvisited vertex, recurse. On entry, set both index and low-link to the clock, increment the clock, and push the vertex.
  3. For each successor, recurse if it is unvisited, then set low-link to the minimum of itself and the successor’s low-link. If the successor is already on the stack, set low-link to the minimum of itself and the successor’s index.
  4. When the successor list is done and low-link equals index, pop the stack through this vertex. Those popped vertices are one strongly connected component.
  5. A vertex popped in an earlier component is not on the stack. An edge to it does not update low-link.

The single pass needs the stack test. Kosaraju’s algorithm computes the same partition with two depth-first searches and an explicit transpose, which is easier to see and uses the same linear time. Neither algorithm orders vertices inside a component. The condensation is the DAG that a later topological sort can schedule.


Approaches that look simpler and fail

Approach Why it fails when low-link follows a finished component
Minimize low-link over every visited successor, including popped ones A cross edge into an earlier component pulls the low-link below the true root. Distinct components merge.
Pop only the root and call it the component The component is the contiguous stack segment from the root to the top. Members sit above the root.
Omit the outer loop over unvisited vertices A vertex the first tree cannot reach never joins a component.
Use low-link equality with the parent index instead of the vertex’s own index The root test is low-link = discovery index of this vertex. The parent’s index is a different number.

Two passes and a transpose are Kosaraju. The DAG of components, once known, is what topological sort orders. Bridges and articulation points of an undirected graph use a related but different low value and are not this algorithm.


A working example

This Python fragment computes Tarjan’s low-link. A root of a strongly connected component is a node whose low-link equals its discovery index. The graph 0→1→2→0 and 2→3 has components {0, 1, 2} and {3}. It prints 2 1.

adj = [[1], [2], [0, 3], []]
disc = [-1] * 4
low = [-1] * 4
stack, on = [], [False] * 4
time = 0
comps = []

def dfs(u):
    global time
    disc[u] = low[u] = time
    time += 1
    stack.append(u)
    on[u] = True
    for v in adj[u]:
        if disc[v] < 0:
            dfs(v)
            low[u] = min(low[u], low[v])
        elif on[v]:
            low[u] = min(low[u], disc[v])
    if low[u] == disc[u]:
        comp = []
        while True:
            v = stack.pop()
            on[v] = False
            comp.append(v)
            if v == u:
                break
        comps.append(comp)

for u in range(4):
    if disc[u] < 0:
        dfs(u)
sizes = sorted(len(c) for c in comps)
assert sizes == [1, 3]
print(*sizes)

Where it is used

Tarjan’s strongly connected components condense a call graph or a dependency graph, and they are the components a 2-SAT solver needs. Compilers use this condensation. Kosaraju’s algorithm computes it with a transpose pass instead of one depth-first search.


When you can drop this algorithm

Drop Tarjan’s one-pass structure if a transpose and two simpler searches are easier to audit. Kosaraju returns the same components. Keep Tarjan when the graph is huge, the transpose is inconvenient, or one recursion must emit the components.


Why this stays published

We publish this so a search for Tarjan’s strongly connected components returns the low-link root test and the rule that finished components stay off the stack. Tarjan’s algorithm finds strongly connected components in one depth-first search by popping the stack when a vertex’s low-link equals its discovery index.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *