In a directed graph, a strongly connected component (SCC) is a maximal group of vertices in which every vertex can reach every other. Kosaraju's and Tarjan's algorithms find all SCCs in linear time, and collapsing each SCC into a single node always leaves a DAG, which unlocks topological sorting and DP on any directed graph. The same low-link idea, applied to undirected graphs, finds bridges and articulation points: the single edges and vertices whose failure splits a network.
Think of it like
One-way streets and a single bridge
Picture a city with many one-way streets. A neighborhood where you can drive from any intersection to any other and back again is a strongly connected component. Shrink each neighborhood to a dot and the remaining map has no round trips at all: you can leave a neighborhood but never come back to it. Now picture a two-way road network split by a river with a single bridge. Close that bridge and the city falls apart. That bridge is what the low-link algorithm finds.
Strongly connected components
Write u ~ v when u can reach v and v can reach u. This relation is reflexive, symmetric and transitive, so it splits the vertices into equivalence classes: the SCCs. Every vertex belongs to exactly one SCC, possibly a component of size one.
- Strongly connected
- Every vertex reaches every other vertex following edge directions.
- SCC
- A maximal strongly connected set of vertices. Adding any other vertex would break mutual reachability.
- Transpose graph
Gᵀhas every edge reversed. It has exactly the same SCCs asG.- Condensation
- The graph with one node per SCC and an edge between two SCCs whenever some original edge connects them.
- Weakly connected
- Connected once edge directions are ignored. A weakly connected graph can still have many SCCs.
Running example: edges 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 6→5, 6→7, 7→6. The SCCs are {0, 1, 2}, {3, 4, 5} and {6, 7}. The visualization above colors each component as it is discovered.
The condensation DAG
Contract each SCC into a single node. The result is always a DAG: if two SCCs could reach each other, they would be one SCC. In the example the condensation is {0,1,2} → {3,4,5} ← {6,7}, with two source components (in-degree 0) and one sink component (out-degree 0).
Kosaraju's algorithm
- 1
DFS on G, record finish order
Run Depth-First Search over all vertices and push each vertex onto a list when it finishes.
- 2
Build the transpose
Reverse every edge to get
Gᵀ. - 3
DFS on Gᵀ in decreasing finish time
Take vertices from the end of the finish list. Each DFS in
Gᵀstarted from an unvisited vertex collects exactly one SCC.
Why it works: if there is an edge from SCC C to SCC C′ in G, the latest finish time in C is larger than the latest in C′. So the vertex that finishes last lies in a source SCC of the condensation. In Gᵀ all edges flip, that SCC becomes a sink, and a DFS from it cannot escape. Remove it and repeat: the next-latest unvisited vertex is in a source of what remains.
| Pass | What happens on the example |
|---|---|
| 1: DFS on G | From 0: visits 0, 1, 2, 3, 4, 5. Finish order 5, 4, 3, 2, 1, 0. Then from 6: visits 6, 7 (5 already done). Finish order 7, 6. |
| Finish list | [5, 4, 3, 2, 1, 0, 7, 6], processed from the end: 6, 7, 0, 1, … |
| 2: DFS on Gᵀ from 6 | Reaches 7 only (the reversed edge 6→5 now points into 6, not out of it): SCC {6, 7} |
| 2: DFS on Gᵀ from 0 | Reaches 2, then 1: SCC {0, 1, 2} |
| 2: DFS on Gᵀ from 3 | Reaches 5, then 4 (2 and 6 are already assigned): SCC {3, 4, 5} |
Tarjan's algorithm
Tarjan finds SCCs in a single DFS. Each vertex gets a discovery index disc[u] and a low-link value low[u]: the smallest discovery index reachable from u's DFS subtree using tree edges plus edges into vertices that are still on a special stack. Vertices are pushed on that stack when discovered and stay there until their SCC is complete.
When u finishes and low[u] == disc[u], nothing in u's subtree can reach an earlier vertex that is still open, so u is the root of an SCC. Pop the stack down to and including u: those vertices form the component.
Kosaraju
- Two DFS passes plus a transposed copy of the graph
- Easy to explain and to prove
- Outputs SCCs in topological order
- Uses roughly twice the memory
Tarjan
- One DFS, no transpose
- Slightly trickier bookkeeping (disc, low, on-stack)
- Outputs SCCs in reverse topological order (sinks first)
- Same low-link idea as bridges and articulation points
Bridges and articulation points
In an undirected graph, a bridge is an edge whose removal increases the number of connected components, and an articulation point (cut vertex) is a vertex whose removal does. One DFS with disc and low finds both. Here low[u] is the smallest disc reachable from u's subtree using tree edges and at most one back edge, never re-using the edge to the parent.
Example: undirected edges 0-1, 1-2, 2-0, 1-3, 3-4, 4-5, 5-3, 4-6 (two triangles joined by 1-3, plus a leaf 6). DFS from 0 with neighbors in input order gives:
| Vertex | disc | low | Notes |
|---|---|---|---|
| 0 | 0 | 0 | Root with one DFS child: not a cut vertex |
| 1 | 1 | 0 | Child 3 has low 3 ≥ 1: articulation point; low 3 > 1: 1-3 is a bridge |
| 2 | 2 | 0 | Back edge to 0 |
| 3 | 3 | 3 | Child 4 has low 3 ≥ 3: articulation point (but 3 > 3 is false, so 3-4 is not a bridge) |
| 4 | 4 | 3 | Child 6 has low 6 > 4: 4-6 is a bridge, and 4 is an articulation point |
| 5 | 5 | 3 | Back edge to 3 |
| 6 | 6 | 6 | A leaf |
low[v] > disc[u] and low[v] ≥ disc[u] are checked after each child returns. Try the cycle (no bridges), the bow tie (root rule), parallel edges and a disconnected graph, or type your own edge list.Applications
2-SAT
A 2-SAT formula is an AND of clauses with two literals each, such as (a ∨ ¬b) ∧ (b ∨ c). Build an implication graph with a node for every literal x and ¬x. Clause (a ∨ b) means "if not a then b" and "if not b then a", so add edges ¬a → b and ¬b → a.
- The formula is unsatisfiable exactly when some variable
xand its negation¬xlie in the same SCC (each would imply the other). - Otherwise, set
x = truewhen the SCC ofxcomes after the SCC of¬xin a topological order of the condensation (with Tarjan's numbering, which is reverse topological, that meanscomp[x] < comp[¬x]). - Total time is linear in the number of variables plus clauses. General SAT with 3 literals per clause is NP-complete (see Complexity Classes: P vs NP).
Critical connections and more
- Critical connections: bridges in a server network are single points of failure (LeetCode's Critical Connections in a Network).
- Network reliability: articulation points are routers, airports or power substations whose loss disconnects the network.
- Social and web graphs: SCCs reveal tightly knit communities and the famous "bow-tie" structure of the web.
- Compilers and program analysis: SCCs of the call graph identify mutually recursive functions; loops in control-flow graphs are found similarly.
- Deadlock and dependency analysis: a non-trivial SCC in a wait-for or dependency graph is a cycle that must be broken.
Complexity
| Algorithm | Time | Space | Notes |
|---|---|---|---|
| Kosaraju | O(V + E) | O(V + E) | Two DFS passes; stores the transpose |
| Tarjan | O(V + E) | O(V) | One DFS with disc, low and a stack |
| Condensation DAG | O(V + E) | O(V + E) | Built after labeling components |
| Bridges and articulation points | O(V + E) | O(V) | One DFS with low-links |
| 2-SAT (n variables, m clauses) | O(n + m) | O(n + m) | SCCs of a graph with 2n literal nodes |
Space excludes the input graph. Recursive implementations also use O(V) stack depth in the worst case.
Implementation
One DFS. Returns the SCCs in reverse topological order of the condensation. On the running example: `[[5, 4, 3], [2, 1, 0], [7, 6]]`.
import sys
def tarjan_scc(adj: list[list[int]]) -> list[list[int]]:
n = len(adj)
sys.setrecursionlimit(max(1000, 2 * n + 10))
disc = [-1] * n # discovery index; -1 = unvisited
low = [0] * n
on_stack = [False] * n
stack: list[int] = []
sccs: list[list[int]] = []
timer = 0
def dfs(u: int) -> None:
nonlocal timer
disc[u] = low[u] = timer
timer += 1
stack.append(u)
on_stack[u] = True
for v in adj[u]:
if disc[v] == -1: # tree edge
dfs(v)
low[u] = min(low[u], low[v])
elif on_stack[v]: # edge into the SCC still being built
low[u] = min(low[u], disc[v])
# otherwise v's SCC is already complete: ignore the edge
if low[u] == disc[u]: # u is the root of an SCC
component = []
while True:
v = stack.pop()
on_stack[v] = False
component.append(v)
if v == u:
break
sccs.append(component)
for u in range(n):
if disc[u] == -1:
dfs(u)
return sccs
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(tarjan_scc(adj)) # [[5, 4, 3], [2, 1, 0], [7, 6]]Key takeaways
- An SCC is a maximal set of mutually reachable vertices; SCCs partition the vertices of any directed graph.
- Contracting SCCs always yields a DAG (the condensation), enabling topological sort and DP on any directed graph.
- Kosaraju uses two DFS passes (graph, then transpose in decreasing finish time); Tarjan uses one DFS with
disc,lowand a stack. Both areO(V + E). - In undirected graphs,
low[v] > disc[u]marks a bridge andlow[v] ≥ disc[u](plus the root rule) marks an articulation point. - 2-SAT reduces to SCCs on the implication graph: unsatisfiable exactly when
xand¬xshare an SCC.
Common pitfalls
- In Tarjan, lowering
low[u]through edges to vertices that are no longer on the stack, which merges distinct SCCs. - In Kosaraju, running the second pass on the original graph instead of the transpose, or in increasing finish order.
- For bridges, skipping the parent vertex instead of the parent edge, which mislabels parallel edges as bridges.
- Forgetting the special root rule for articulation points (the root is a cut vertex only with two or more DFS children).
- Stack overflow from deep recursion on large graphs; plan for an iterative DFS or a bigger stack.
Interview tips
- Critical Connections in a Network is the most common interview problem here; practice writing the bridge DFS in under 15 lines.
- Explain low-link in plain words: "the earliest vertex my subtree can get back to without using the edge I came in on".
- If you choose Kosaraju, mention that it needs the transposed graph and why the finish-time order matters.
Real-world applications
- Finding single points of failure in computer, power and transportation networks.
- 2-SAT solving for scheduling and configuration constraints.
- Detecting mutually recursive functions and loops in compilers.
- Community and structure analysis of social networks and the web graph.
- Simplifying dependency graphs by collapsing cycles before topological processing.
Practice problems
Test yourself
What kind of graph do you get by contracting every SCC of a directed graph into a single node?
Related topics
Finished this lesson? Track it to see your progress on the roadmap.