DSA

Kosaraju's Algorithm in Java: Two-Pass SCC Detection

Learn how to implement Kosaraju's algorithm in Java. Understand the power of graph transposition and DFS to find Strongly Connected Components (SCCs) in directed graphs.

Sachin Sarawgi·April 20, 2026·3 min read
#dsa#java#graph#scc#kosaraju#dfs#interview preparation#algorithms

While Tarjan's algorithm is known for its single-pass efficiency, Kosaraju's Algorithm is often preferred for its conceptual simplicity. It leverages a fundamental property of directed graphs: a graph and its transpose share the same Strongly Connected Components (SCCs).

The Core Concept: Two DFS Passes

Kosaraju's algorithm finds SCCs in three main steps:

  1. First DFS Pass: Perform a DFS on the original graph and push nodes onto a stack based on their finishing times (similar to topological sort).
  2. Graph Transposition: Create a new graph by reversing the direction of every edge in the original graph.
  3. Second DFS Pass: Pop nodes from the stack and perform a DFS on the transposed graph. Each DFS call from a popped node identifies one complete SCC.

Kosaraju's Algorithm Implementation in Java

import java.util.*;

public class KosarajuSCC {
    private int n;
    private List<List<Integer>> adj;
    private List<List<Integer>> transposedAdj;
    private boolean[] visited;
    private Stack<Integer> stack;

    public List<List<Integer>> findSCCs(int n, List<List<Integer>> adj) {
        this.n = n;
        this.adj = adj;
        this.visited = new boolean[n];
        this.stack = new Stack<>();
        List<List<Integer>> sccs = new ArrayList<>();

        // Step 1: First DFS Pass to fill the stack
        for (int i = 0; i < n; i++) {
            if (!visited[i]) fillStack(i);
        }

        // Step 2: Transpose the graph
        transpose();

        // Step 3: Second DFS Pass on the transposed graph
        Arrays.fill(visited, false);
        while (!stack.isEmpty()) {
            int node = stack.pop();
            if (!visited[node]) {
                List<Integer> scc = new ArrayList<>();
                dfsTransposed(node, scc);
                sccs.add(scc);
            }
        }
        return sccs;
    }

    private void fillStack(int u) {
        visited[u] = true;
        for (int v : adj.get(u)) {
            if (!visited[v]) fillStack(v);
        }
        stack.push(u);
    }

    private void transpose() {
        transposedAdj = new ArrayList<>();
        for (int i = 0; i < n; i++) transposedAdj.add(new ArrayList<>());
        for (int u = 0; u < n; u++) {
            for (int v : adj.get(u)) {
                transposedAdj.get(v).add(u);
            }
        }
    }

    private void dfsTransposed(int u, List<Integer> scc) {
        visited[u] = true;
        scc.add(u);
        for (int v : transposedAdj.get(u)) {
            if (!visited[v]) dfsTransposed(v, scc);
        }
    }
}

Why use Kosaraju's?

Feature Kosaraju's Tarjan's
Conceptual Ease High (uses standard DFS) Moderate (uses low-link values)
Passes Two One
Complexity $O(V + E)$ $O(V + E)$
Space $O(V + E)$ (for transposed graph) $O(V)$

Real-World Applications

  1. Social Network Analysis: Finding groups of people who all follow each other.
  2. Compiler Optimization: Identifying loops in a control-flow graph.
  3. Data Mining: Discovering clusters of related information in a directed dataset.

Summary

Kosaraju's algorithm is a beautiful demonstration of how reversing a graph can reveal its hidden structure. By combining two simple DFS passes with a stack, it provides a robust and easy-to-implement solution for SCC detection. Understanding both Kosaraju's and Tarjan's algorithms shows that you can evaluate multiple approaches to the same problem based on complexity and maintainability.

📚

Recommended Resources

Java Masterclass — UdemyBest Seller

Comprehensive Java course covering Java 17+, OOP, concurrency, and modern APIs.

View Course
Effective Java, 3rd EditionMust Read

Joshua Bloch's classic guide to writing clear, correct, and efficient Java code.

View on Amazon
Java Concurrency in Practice

The authoritative book on writing thread-safe, concurrent Java programs.

View on Amazon

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Found this useful? Share it: