Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Strongly connected components finder
Finds strongly connected components using two DFS passes
Select an algorithm and generate steps to begin visualization
Kosaraju's algorithm computes the strongly connected components of a directed graph using two passes of depth-first search, one on the original graph and one on its transpose (all edges reversed). It is conceptually the simplest linear-time SCC algorithm.
The first DFS records vertices in order of decreasing finish time. The graph is then transposed, and a second DFS processes vertices in that recorded order; each tree grown in the second pass is exactly one strongly connected component. The correctness follows from the fact that reversing edges preserves SCCs but breaks the connections between them. Two linear passes give O(V + E) total time.
Kosaraju's algorithm serves the same applications as Tarjan's: 2-SAT solvers, compiler analysis, social network community structure, and dependency condensation. Its two-pass structure is easier to explain and implement from scratch, which makes it a popular interview answer when asked to find SCCs.
Two depth-first searches and one transposed graph. Nothing clever happens inside either pass; all the work is done by the order the second pass runs in.
Kosaraju(graph):
// Pass 1: record finish order on the original graph
order = []
for each unvisited u: dfs1(u)
dfs1(u): mark u visited
for each edge (u,v): if unvisited: dfs1(v)
order.append(u) // on finish
// Pass 2: DFS the transpose in reverse finish order
gt = transpose(graph) // reverse every edge
for each u in reverse(order):
if u unvisited:
the tree grown from u in gt is one SCCWhy it works: reversing every edge leaves the strongly connected components untouched, since if you could get from x to y and back before, you still can. What reversal does change is the direction of the edges between components. Starting from the vertex that finished last guarantees you begin in a component that is a source of the condensation, so the second DFS cannot leak out of it into another component.
Run Kosaraju on the same directed graph used for the Tarjan example, so the two can be compared directly.
Example graph: Directed edges A to B, B to C, C to A, B to D, D to E, E to D, and C to F.
The components are {A, B, C}, then {D, E}, then {F}. Compare this with Tarjan on the same graph, which emits {F}, then {D, E}, then {A, B, C}. Both are correct and both find the same three components, but Kosaraju emits them in forward topological order of the condensation while Tarjan emits them in reverse. If the order matters to your downstream code, that difference is the reason to pick one over the other.
Time: O(V + E) · Space: O(V + E)
Two depth-first searches cost O(V + E) each, and building the transposed graph requires one pass over all edges, also O(V + E). Summing gives O(V + E) overall. The space is where Kosaraju genuinely loses to Tarjan: it must store the transposed adjacency structure, which is a second full copy of the edge list at O(V + E), whereas Tarjan needs only O(V) of bookkeeping on top of the original graph. On a graph with tens of millions of edges that difference is the deciding factor, which is why Tarjan tends to win in production even though the two are asymptotically identical in time.
All the linear SCC algorithms cost O(V + E). The trade-offs are memory, number of passes, and how easy the code is to get right.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Tarjan's algorithm | One pass, no transpose, O(V) extra space. Preferred when memory matters or the graph is huge. | O(V + E), one pass |
| Path-based SCC | One pass like Tarjan, but with two explicit stacks instead of low-link arithmetic. Some find it easier to reason about. | O(V + E) |
| Condensation to a DAG | The components are a means to an end. Kosaraju hands you them already in forward topological order. | O(V + E) |
| Union-Find | The graph is undirected, where connected components are a much simpler problem. | O(E·α(V)) |
Read the full article: Graph Algorithms and Their Complexity
Related algorithms: Tarjan's SCC Algorithm, Depth-First Search