How Kosaraju uses a transpose graph

Short answer: Kosaraju’s algorithm finds strongly connected components with two depth-first searches. The first search records vertices in reverse order of finish time on the original graph. The second search runs on the transpose, in which every edge is reversed, and starts vertices in that recorded order. Each tree of the second search is one component. Both passes together are Θ(V+E).


What the problem actually asks

A strongly connected component is a set that a forward search and a backward search would both fill. Doing that naively from every vertex is quadratic. Finish times from one full depth-first search give an order in which to try the backward searches: the vertex that finished last can reach everything in its component and nothing in a component that should be processed earlier.

Reversing the edges turns the backward search into an ordinary depth-first search on the transpose. The second forest then falls out component by component.


What the algorithm guarantees

Each second-pass tree is exactly one strongly connected component, and every vertex appears in one tree. Time is Θ(V+E) for the two searches plus the construction of the transpose, which is another Θ(V+E). Extra memory is the finish list, the transpose, and the visited marks. The order in which second-pass roots are started is a reverse topological order of the condensation, so the components themselves come out in an order useful for a DAG of components.

The first search must run to completion on every vertex, with the outer loop included, or the finish list omits a source. The second search must skip vertices already assigned. Starting the second search in the wrong order, such as increasing vertex id, merges nothing correctly and splits nothing correctly. The order is the algorithm.


The working steps

  1. Run depth-first search on the original graph. Push a vertex on a list when it finishes. The outer loop starts a search at every unvisited vertex.
  2. Build the transpose by reversing every edge.
  3. Clear the visited marks. Pop the finish list from the last-finished vertex toward the first.
  4. Each popped vertex that is still unvisited starts a depth-first search on the transpose. The vertices that search reaches form one strongly connected component.
  5. Assign those vertices before the next pop, so a later start does not reenter them.

The transpose is essential. A second search on the original graph, even in reverse finish order, follows edges outward and can walk into a later component. Tarjan’s algorithm avoids the transpose by tracking a stack and a low-link during the first search. Both are linear and both are correct. Kosaraju’s extra graph is the cost of the simpler argument.


Approaches that look simpler and fail

Approach Why it fails when the second search is not on the transpose
Second search on the original edges Outgoing edges leave the component. The tree can swallow vertices that cannot return.
Start the second search in finish order instead of reverse finish order A vertex that should be last is tried first and its transpose search is not confined to its component.
Skip building the transpose and reverse each adjacency list while the first search still runs The first search is still walking those lists. Reverse a copy, or reverse after the first search has finished.
Forget vertices that the first search never started Their finish times are absent, so the second search never starts them unless a separate loop adds unvisited vertices. The outer loop of the first search is required.

A one-pass algorithm with no transpose is Tarjan’s low-link stack. After either algorithm, the component DAG can be topologically sorted by the second-pass order Kosaraju already produces.


A working example

This Python fragment finishes a depth-first search, then walks the transpose in reverse finish order. On the same graph as the Tarjan example the component sizes are again 1 3.

adj = [[1], [2], [0, 3], []]
rev = [[] for _ in range(4)]
for u in range(4):
    for v in adj[u]:
        rev[v].append(u)
seen, order = [False] * 4, []

def dfs(u, graph, bucket):
    seen[u] = True
    for v in graph[u]:
        if not seen[v]:
            dfs(v, graph, bucket)
    bucket.append(u)

for u in range(4):
    if not seen[u]:
        dfs(u, adj, order)
seen = [False] * 4
comps = []
for u in reversed(order):
    if not seen[u]:
        comp = []
        dfs(u, rev, comp)
        comps.append(comp)
sizes = sorted(len(c) for c in comps)
assert sizes == [1, 3]
print(*sizes)

Where it is used

Kosaraju’s two passes find the strongly connected components of a dependency or a call graph, including the components a 2-SAT solver needs. The transpose graph is the extra structure. Tarjan’s one-pass algorithm computes the same components.


When you can drop this algorithm

Drop the transpose construction when memory is tight and Tarjan’s single search is acceptable. Keep Kosaraju when two ordinary depth-first searches are easier to prove and the extra edge list fits.


Why this stays published

We publish this so a search for Kosaraju returns the reverse finish order on the original graph and the second search on the transpose. Kosaraju’s algorithm finds strongly connected components by depth-first search in reverse finish order on the transpose graph, in linear time.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *