VLVisual Learner

Tarjan's Algorithm

Time: O(V + E)One DFS passStrongly connected components
–
Pseudocode
1tarjan(u):
2 index[u] ← low[u] ← counter; counter ← counter + 1; push u
3 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 component
7 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

// Tarjan's algorithm: strongly connected components in one DFS.
function tarjan(graph) {
  const n = graph.length;
  const index = Array(n).fill(-1), low = Array(n).fill(0);
  const onStack = Array(n).fill(false), stack = [];
  const sccs = [];
  let counter = 0;

  function visit(u) {
    index[u] = low[u] = counter++;
    stack.push(u);
    onStack[u] = true;
    for (let v = 0; v < n; v++) {
      if (!graph[u][v]) continue;
      if (index[v] === -1) {               // tree edge: explore v first
        visit(v);
        low[u] = Math.min(low[u], low[v]);
      } else if (onStack[v]) {             // edge back into the current path
        low[u] = Math.min(low[u], index[v]);
      }
    }
    if (low[u] === index[u]) {             // u is the root of an SCC
      const scc = [];
      let w;
      do {
        w = stack.pop();
        onStack[w] = false;
        scc.push(w);
      } while (w !== u);
      sccs.push(scc);
    }
  }
  for (let v = 0; v < n; v++) if (index[v] === -1) visit(v);
  return sccs;
}

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)