Algorithms · Visualized
Tarjan's SCC
Graph Algorithms
Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.
A strongly connected component is a maximal set of vertices where every vertex can reach every other one. Time: O(V + E) average, O(V + E) — one DFS, each edge inspected once worst; space: O(V) for disc, low, the on-stack flags and the component stack.
Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).
Tarjan's Strongly Connected Components
Graph AlgorithmsA strongly connected component is a maximal set of vertices where every vertex can reach every other one. Tarjan finds all of them in a single depth-first pass by recording two numbers per vertex: disc, the order in which it was first visited, and low, the smallest disc reachable from its subtree using at most one back edge. Vertices are pushed onto a stack as they are discovered; when a vertex finishes with low equal to disc it is the root of a component, and everything above it on the stack is popped off as one SCC. No second traversal and no reverse graph are needed, which is why it beats the two-pass Kosaraju approach in practice.
How Tarjan's SCC Works
- Assign disc = low = an increasing timestamp when a vertex is first entered, and push it on the stack.
- Recurse into every unvisited neighbour; afterwards set low[u] = min(low[u], low[v]).
- For a neighbour already on the stack, set low[u] = min(low[u], disc[v]) — that is a back edge.
- When all neighbours are done and low[u] equals disc[u], pop the stack down to u: those vertices form one SCC.
- Restart from any vertex still undiscovered until every vertex has been visited.
Tarjan's SCC Worked Example
Input: {start: 0}
The default directed graph is A→B, B→C, C→A, B→D, D→E, E→F, F→D, starting at A. The DFS gives A disc 0, B disc 1, C disc 2; C→A is a back edge to a vertex still on the stack, so low[C] = 0 and the value propagates back through B to A. A finishes with low = disc = 0 and pops {C, B, A} as the first component. B then continues to D (disc 3), E (4), F (5); F→D is a back edge, so low[F] = 3, and D finishes with low = disc = 3, popping {F, E, D}. The final answer is exactly two components: {A, B, C} and {D, E, F}.
def tarjan(adj):
disc = low = {}
stack, onstack, sccs = [], set(), []
time = [0]
def dfs(u):
disc[u] = low[u] = time[0]; time[0] += 1
stack.append(u); onstack.add(u)
for v in adj[u]:
if v not in disc:
dfs(v)
low[u] = min(low[u], low[v])
elif v in onstack:
low[u] = min(low[u], disc[v])
if low[u] == disc[u]: # SCC root: pop
comp = []
while True:
x = stack.pop(); onstack.discard(x)
comp.append(x)
if x == u: break
sccs.append(comp)
for u in adj:
if u not in disc: dfs(u)
return sccs # O(V + E)Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Tarjan's SCC Time & Space Complexity
When to Use Tarjan's SCC
- Condensing a dependency graph so cycles can be treated as single nodes before topological sorting.
- Finding mutual-reachability clusters in social, citation and web graphs.
- Two-satisfiability: a 2-SAT instance is satisfiable exactly when no variable shares an SCC with its negation.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.