Sub topic 8
Why Kosaraju's Algorithm Works
This chapter assumes you already know four things from earlier chapters: how to identify strongly connected components (SCCs), how to record DFS finish times, how to build the transpose graph, and how to build the condensation DAG. It also assumes you have seen the code for Kosaraju's algorithm. Here we prove that the second pass labels each component exactly, and we explain why changing its vertex order breaks the algorithm.
The Claim We Need to Prove
Kosaraju's algorithm runs DFS on the graph G and records vertices by finish time. It then runs DFS on the transpose G^T, taking start vertices in decreasing finish time. The claim is that each DFS tree in the second pass contains exactly one SCC: all of its vertices and nothing more.
Two facts make this work:
- G and G^T have the same SCCs. If u reaches v and v reaches u in G, reversing every edge reverses both paths, so the two vertices are still mutually reachable. The condensation of G^T is therefore the condensation of G with every edge reversed.
- Finish times from the first pass order the components consistently with the edges of the condensation. The next section states this as a lemma.
Finish Times of Whole Components
The proof compares components, not single vertices. For a set of vertices C, define f(C) as the largest finish time of any vertex in C. For the same set, define d(C) as the smallest discovery time.
The Key Lemma
Let C and C' be distinct SCCs of G, and suppose G has an edge (u, v) with u in C and v in C'. Then f(C) > f(C').
The proof splits on which component the first DFS enters first.
Case 1: d(C) < d(C'). Let x be the first vertex discovered in C. At that moment every vertex in C and C' is still unvisited. Every vertex of C is reachable from x, and through the edge (u, v), so is every vertex of C'. By the white-path theorem, all of those vertices become descendants of x in the DFS tree. A descendant finishes before its ancestor, so x has the largest finish time of all of them, and f(C) = f(x) > f(C').
Case 2: d(C') < d(C). Let y be the first vertex discovered in C'. The DFS from y visits all of C' and finishes y. Could it reach C along the way? No. If C' could reach C, then C' and C would reach each other, and they would be one component. So when y finishes, every vertex of C is still unvisited, and each of them finishes later. That gives f(C) > f(y) = f(C').
In both cases the component at the tail of the edge has the larger maximum finish time.
A Corollary
The lemma has a corollary for the transpose. Suppose G^T has an edge from C into a different component C'. That edge was reversed, so G has an edge from C' to C, and the lemma gives f(C') > f(C). Put another way, in G^T every edge between components runs from a smaller f value to a larger one.
The Induction on the Second Pass
The second pass processes vertices in decreasing finish time. The first start vertex x has the largest finish time in the whole graph, so it lies in the component C with the largest f. By the corollary, an edge leaving C in G^T would have to point to a component with an even larger f, and no such component exists. So C is a sink in the condensation of G^T. The DFS from x visits all of C, because C is strongly connected in G^T too, and it cannot leave C.
For the inductive step, suppose the first k trees were each exactly one SCC. The next start vertex x' is the unvisited vertex with the largest finish time. It lies in the component C that has the largest f among unvisited components. Any G^T edge leaving C points to a component with a larger f, which must already be fully visited. A DFS never enters visited vertices, so the tree from x' is exactly C*.
By induction, every tree in the second pass is exactly one SCC. Components are also labeled in decreasing order of f. That is a topological order of the condensation of G, which is why Kosaraju's numbering comes out topologically sorted.
Checking the Lemma in Code
The script below computes finish times, finds the SCCs by brute-force mutual reachability (deliberately independent of Kosaraju), and checks the lemma on every edge between components.
def finish_times(n, adj):
visited = [False] * n
finish = [0] * n
clock = 0
def dfs(u):
nonlocal clock
visited[u] = True
for v in adj[u]:
if not visited[v]:
dfs(v)
clock += 1
finish[u] = clock
for u in range(n):
if not visited[u]:
dfs(u)
return finish
def reach(adj, s):
seen = {s}
stack = [s]
while stack:
u = stack.pop()
for v in adj[u]:
if v not in seen:
seen.add(v)
stack.append(v)
return seen
def scc_labels_bruteforce(n, adj):
r = [reach(adj, s) for s in range(n)]
# Label each vertex by the smallest vertex it is mutually reachable with.
return [min(v for v in range(n) if v in r[u] and u in r[v]) for u in range(n)]
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
n = len(adj)
finish = finish_times(n, adj)
label = scc_labels_bruteforce(n, adj)
fmax = {}
for u in range(n):
fmax[label[u]] = max(fmax.get(label[u], 0), finish[u])
print(finish) # [6, 5, 4, 3, 2, 1, 8, 7]
for u in range(n):
for v in adj[u]:
a, b = label[u], label[v]
if a != b:
assert fmax[a] > fmax[b]
print(f"{u}->{v}: f(C{a})={fmax[a]} > f(C{b})={fmax[b]}")
# 2->3: f(C0)=6 > f(C3)=3
# 6->5: f(C6)=8 > f(C3)=3Now trace the second pass on G^T. The decreasing finish order is 6, 7, 0, 1, 2, 3, 4, 5.
| Start vertex | Reachable in G^T among unvisited | Component label |
|---|---|---|
| 6 | 6, 7 (G^T has only 7→6 and 6→7 here) | 0 |
| 0 | 0, 2, 1 (G^T edge 3→2 points into this set, not out of it) | 1 |
| 3 | 3, 5, 4 (G^T edge 5→6 leads to a visited vertex) | 2 |
This matches the earlier chapter's output, [1, 1, 1, 2, 2, 2, 0, 0]. The DFS from 3 does try the G^T edges toward 2 and toward 6. Those vertices are already labeled, so the search stops there. This is the inductive step at work.
Common Mistakes
Using the Smallest Finish Time on G
It is tempting to skip the transpose: "the vertex that finishes first must sit in a sink component, so run DFS on G in increasing finish order." That is false. Take edges 0→1, 1→0, 0→2.
adj = [[1, 2], [0], []]
print(finish_times(3, adj)) # [3, 1, 2]Vertex 1 finishes first, yet it belongs to {0, 1}, which is a source component. A DFS on G from vertex 1 would reach 2 and merge two components. The lemma only controls the maximum finish time of a component. That is why the algorithm takes the largest finish time and reverses the edges, instead of taking the smallest finish time on G.
Other Mistakes
| Mistake | Why it fails |
|---|---|
| Ordering the second pass by discovery time | The lemma does not hold for discovery times of single vertices, so a start vertex can land in a non-sink component of G^T. |
| Recording a vertex before its neighbors are explored | That records preorder, not finish order, and the proof needs finish order. |
| Clearing the visited marks between second-pass trees | The inductive step depends on earlier components blocking the search. |
| Running both passes on G | Decreasing finish order on G starts in a source component, and the DFS spills into everything downstream. |
Exercises
Exercise
In the Case 2 argument of the lemma, why can't the DFS started at y (the first vertex discovered in C') reach any vertex of C?
Show answer
There is an edge from C to C'. If C' could also reach C, every vertex of C and C' would be mutually reachable, so the two sets would form a single SCC. That contradicts the assumption that C and C' are distinct, so the DFS from y finishes without touching C.
Exercise
For the graph with edges 0→1, 1→2, 2→0, 2→3, the first DFS from 0 gives finish times f(3)=1, f(2)=2, f(1)=3, f(0)=4. List the trees that the second pass on G^T produces, and say which component is a sink in the condensation of G^T.
Show answer
The order is 0, 1, 2, 3. On G^T, starting at 0 follows 0→2→1 and visits {0, 1, 2}. The reversed edge 3→2 points into this set, so the search never reaches 3. The next unvisited vertex is 3, which forms the tree {3}. The component {0, 1, 2} has no edges leaving it in G^T, so it is the sink, and it is found first.