codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

51 / 65 topics
49Graph Coloring50Topological Sorting51Strongly Connected Components52Eulerian Cycles and Paths53Hamiltonian Cycles
Tutorials/Data Structures & Algorithms/Strongly Connected Components
🧮Data Structures & Algorithms

Strongly Connected Components

Updated 2026-04-20
3 min read

Strongly Connected Components

Introduction

In graph theory, a strongly connected component (SCC) of a directed graph is a maximal subgraph where every vertex is reachable from every other vertex within the same subgraph. This concept is fundamental in understanding and analyzing complex networks, such as social networks, web graphs, and biological systems.

This tutorial will provide a detailed explanation of what strongly connected components are, how to identify them using algorithms like Kosaraju's and Tarjan's, and their real-world applications.

Understanding Strongly Connected Components

Definition

A directed graph \( G = (V, E) \) is said to be strongly connected if there is a path from every vertex \( u \) to every other vertex \( v \). A subgraph \( H \) of \( G \) is a strongly connected component if it is strongly connected and no larger subgraph containing \( H \) is strongly connected.

Properties

  • Maximal Subgraphs: Each SCC is a maximal subgraph, meaning adding any more vertices would break the strong connectivity.
  • Acyclic in Condensation Graph: The condensation graph of a directed graph (where each SCC becomes a single node) is always acyclic.

Algorithms for Finding Strongly Connected Components

Kosaraju's Algorithm

Kosaraju's algorithm is based on Depth First Search (DFS). It uses two DFS passes to identify all SCCs in \( O(V + E) \) time complexity, where \( V \) is the number of vertices and \( E \) is the number of edges.

Steps

  1. First DFS: Perform a DFS on the original graph \( G \) to compute the finishing times of each vertex.
  2. Transpose Graph: Create the transpose of the graph \( G^T \), where all edges are reversed.
  3. Second DFS: Perform DFS on \( G^T \), but this time start from the vertices in decreasing order of their finishing times from the first DFS.

Code Example

function kosaraju(graph) {
    const visited = new Set();
    let stack = [];
    
    // First DFS to compute finishing times
    function dfs1(node) {
        if (visited.has(node)) return;
        visited.add(node);
        for (let neighbor of graph[node]) {
            dfs1(neighbor);
        }
        stack.push(node);
    }

    // Second DFS on transposed graph
    function dfs2(node, component) {
        if (visited.has(node)) return;
        visited.add(node);
        component.push(node);
        for (let neighbor of transposeGraph[node]) {
            dfs2(neighbor, component);
        }
    }

    // Compute finishing times
    for (let node in graph) {
        dfs1(node);
    }

    // Transpose the graph
    const transposeGraph = {};
    for (let node in graph) {
        transposeGraph[node] = [];
    }
    for (let node in graph) {
        for (let neighbor of graph[node]) {
            transposeGraph[neighbor].push(node);
        }
    }

    visited.clear();
    let sccs = [];

    // Perform DFS on transposed graph
    while (stack.length > 0) {
        const node = stack.pop();
        if (!visited.has(node)) {
            let component = [];
            dfs2(node, component);
            sccs.push(component);
        }
    }

    return sccs;
}

Tarjan's Algorithm

Tarjan's algorithm is another efficient method to find SCCs, also running in \( O(V + E) \). It uses a single DFS and maintains two properties for each vertex: the discovery time and the lowest link value.

Steps

  1. DFS with Discovery Time: Perform DFS while keeping track of discovery times.
  2. Low Link Values: For each node, maintain a low-link value which is the smallest discovery time reachable from that node.
  3. Identify SCCs: If a node's low-link value equals its discovery time, it is the root of an SCC.

Code Example

function tarjan(graph) {
    const index = {};
    const lowlink = {};
    const onStack = new Set();
    let stack = [];
    let sccIndex = 0;
    const sccs = [];

    function strongconnect(node) {
        index[node] = sccIndex;
        lowlink[node] = sccIndex;
        sccIndex++;
        stack.push(node);
        onStack.add(node);

        for (let neighbor of graph[node]) {
            if (!index.hasOwnProperty(neighbor)) {
                strongconnect(neighbor);
                lowlink[node] = Math.min(lowlink[node], lowlink[neighbor]);
            } else if (onStack.has(neighbor)) {
                lowlink[node] = Math.min(lowlink[node], index[neighbor]);
            }
        }

        if (lowlink[node] === index[node]) {
            let scc = [];
            while (true) {
                const w = stack.pop();
                onStack.delete(w);
                scc.push(w);
                if (w === node) break;
            }
            sccs.push(scc);
        }
    }

    for (let node in graph) {
        if (!index.hasOwnProperty(node)) {
            strongconnect(node);
        }
    }

    return sccs;
}

Real-World Applications

Social Networks

In social networks, SCCs can identify groups of users who are tightly connected and interact frequently. This information is crucial for targeted advertising and community detection.

Web Graphs

On the web, SCCs can represent clusters of pages that link to each other internally but have few or no links to external pages. These clusters are useful for understanding website structure and improving search engine indexing.

Biological Systems

In biological systems, such as protein-protein interaction networks, SCCs can help identify functional modules within proteins that interact with each other in a strongly connected manner.

Best Practices

  • Choose the Right Algorithm: Kosaraju's algorithm is simpler to implement and understand, while Tarjan's algorithm is more efficient in terms of time complexity.
  • Handle Large Graphs: For very large graphs, consider using iterative versions of DFS or parallel processing techniques to optimize performance.
  • Memory Management: Be mindful of memory usage, especially when dealing with large graphs. Efficient data structures like adjacency lists are recommended.

Conclusion

Strongly connected components are a powerful tool in graph theory, providing insights into the structure and connectivity of complex networks. By understanding how to identify SCCs using algorithms like Kosaraju's and Tarjan's, you can gain valuable insights into real-world systems and optimize their performance.


PreviousTopological SortingNext Eulerian Cycles and Paths

Recommended Gear

Topological SortingEulerian Cycles and Paths