Sub topic 8
Why Kosaraju's Algorithm Works
This chapter assumes you know DFS discovery and finish times, the white-path theorem, the condensation DAG, and the transpose graph from earlier chapters. The goal is to prove that each DFS tree in Kosaraju's second pass contains exactly one strongly connected component.
The Key Lemma: Finish Times Respect the Condensation
For a component C, define f(C) as the largest finish time of any vertex in C. All finish times come from the first DFS pass.
Lemma. Let C and C' be distinct SCCs, and suppose some edge goes from C to C'. Then f(C) > f(C').
The proof splits into two cases, depending on which component DFS enters first.
- DFS enters C first, at vertex x. At that moment every vertex of C and C' is still unvisited (white), and x reaches all of them through white vertices. By the white-path theorem, all of them become descendants of x, so they finish before x does. That makes f(C) = finish(x) the larger value.
- DFS enters C' first, at vertex y. The condensation is acyclic, so no path leads from C' back to C. DFS therefore finishes all of C' without touching C. Every vertex of C is discovered later, which means it also finishes later.
Both cases give f(C) > f(C').
From the Lemma to Correctness
The lemma has a direct consequence. The vertex with the highest finish time belongs to a source component, meaning one with no incoming edges from other components. If an edge entered it, the lemma would give the other component a higher f.
Transposing the graph reverses every edge in the condensation, so a source becomes a sink. A DFS in Gᵀ that starts in a sink component can only reach vertices of that component. It also reaches all of them, because reversing edges does not change which vertices are mutually reachable. The first tree of pass two is therefore exactly one SCC.
The proof then continues by induction. Remove that component and look at the unvisited vertex with the next-highest finish time. Its component is a source among the remaining components. Any edge in Gᵀ that leaves it points into a component that is already labeled, and dfs2 skips labeled vertices. Each later tree is therefore also exactly one SCC.
Checking It on the Example
Take the graph adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]. Starting the clock at 1, the first pass gives these times:
| Vertex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Discovery | 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 |
| Finish | 12 | 11 | 10 | 9 | 8 | 7 | 16 | 15 |
The component maxima are f({6,7}) = 16, f({0,1,2}) = 12 and f({3,4,5}) = 9. Both condensation edges point from a higher f to a lower one, as the lemma requires.
Pass two starts at vertex 6. In Gᵀ the only edges into {6,7} come from inside it, so the search stays there and labels it component 0. Next it takes vertex 0 and labels {0,1,2} as component 1. Its edge toward 3 in G becomes an edge from 3 to 2 in Gᵀ, so it does not lead out of the component. Finally it starts at 3. Gᵀ has edges from {3,4,5} to 2 and 6, but both vertices are already labeled, so the search stops at {3,4,5}, which becomes component 2. The labels come out in topological order of the condensation.
Exercise
Suppose the second pass runs on the original graph G instead of Gᵀ, still in decreasing finish order. What happens on the example graph, and which step of the proof fails?
Show answer
The search starts at 6, and in G it follows 6→5→3→4. That puts {3,4,5} and {6,7} in one tree. The vertex 6 lies in a source component, and in G a source can reach downstream components. The proof needs the search to start in a sink, and only the transpose turns the source into a sink.
Exercise
Could you use the vertex with the smallest discovery time in place of the largest finish time? Test this on the graph with edges 0→1 and 2→0, with DFS starting at vertex 0.
Show answer
No. Vertex 0 has the smallest discovery time, but the source component is {2}, because the edge 2→0 enters {0}. Discovery order depends on where DFS happens to start. The lemma holds only for maximum finish times.