How to find Strongly Connected Components in a Graph?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Strongly connected components, or SCCs, are maximal groups of vertices in a directed graph where every node can reach every other node in the same group. SCC decomposition is useful in dependency analysis, deadlock detection, compiler optimization, and service graph diagnostics. This guide explains two standard linear-time algorithms, Kosaraju and Tarjan, with runnable Python examples.
Build Intuition for SCCs
Think of SCCs as mutual reachability islands. If you collapse each SCC into one node, the resulting graph is a directed acyclic graph. That condensed graph gives a high-level dependency order.
Example intuition:
- If service A calls B, B calls C, and C calls A, those three form one SCC.
- Any scheduling logic that ignores this cycle can produce unstable rollout order.
A graph is usually represented as adjacency lists.
Kosaraju Algorithm
Kosaraju performs two DFS passes.
Steps:
- DFS on original graph and push vertices by finish time.
- Reverse all edges.
- DFS on reversed graph in reverse finish order.
- Each DFS tree from step three is one SCC.
Kosaraju is easy to reason about and often preferred for teaching and debugging.
Tarjan Algorithm
Tarjan finds SCCs in one DFS pass using discovery indices, low-link values, and a stack.
Tarjan avoids explicit graph reversal and is efficient in memory-conscious workflows.
Choosing Between Kosaraju and Tarjan
Both are linear in vertices plus edges.
Practical tradeoff:
- Kosaraju is simpler to explain and inspect step-by-step.
- Tarjan is single-pass and often preferred in large graph processing systems.
For many applications, code clarity matters more than minor constant-factor differences.
Production Implementation Tips
- Include nodes that appear only as destination vertices.
- Do not mutate the graph during traversal.
- For very deep graphs, consider iterative DFS to avoid recursion depth limits.
- After SCC extraction, build condensed graph for topological dependency planning.
These details prevent subtle correctness issues in real pipelines.
Common Pitfalls
- Forgetting target-only vertices that are absent from adjacency keys.
- Incorrect low-link updates in Tarjan when stack-membership checks are missing.
- Processing Kosaraju second pass in wrong order.
- Ignoring isolated vertices and self-loops in tests.
- Mutating adjacency data while DFS runs.
Summary
- SCCs partition directed graphs into maximal mutual-reachability groups.
- Kosaraju uses two DFS passes with a reversed graph.
- Tarjan uses one DFS pass with index and low-link tracking.
- Both algorithms run in linear time and scale well.
- Reliable implementations must handle isolated and target-only nodes explicitly.
Related reading
- How to Find the Branching Factor of a Tree
- How to find the center of a subset of vertices in a graph?
- How to find the closest point on a right rectangular prism 3d rectangle
- How to find the element of an array that is repeated at least N/2 times?
- How to find the first key in a dictionary? python
- how to find the height of a node in binary tree recursively
- How to find the intersection of two NFA
- How to find the intersection point between a line and a rectangle?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.