1. Problem Understanding
We are given a directed graph containing V vertices, numbered from 0 to V - 1.
The graph is represented using an adjacency list adj[][].
Our task is to find the total number of Strongly Connected Components (SCCs).
What is a Strongly Connected Component?
A Strongly Connected Component is a group of vertices in which every vertex can reach every other vertex through directed paths.
For example:
Consider this directed graph:
Here, we have three groups:
{0, 1, 2} – All three vertices can reach each other.
{3, 4} – Both vertices can reach each other.
{5} – This vertex cannot reach any other vertex.
Therefore, the total number of SCCs is 3.
2. Understanding the Input
adj = [
[1],
[2],
[0, 3],
[4],
[3, 5],
[]
];Let's understand the connections.
Vertex | Connected vertices |
|---|---|
0 | 1 |
1 | 2 |
2 | 0, 3 |
3 | 4 |
4 | 3, 5 |
5 | None |
Graph connections
0 → 1 → 2
↑ |
|_______|
2 → 3 → 4
↑ |
| |
|_|
4 → 5Notice:
Vertices 0, 1, and 2 form a cycle.
Vertices 3 and 4 form another cycle.
Vertex 5 is alone.
This gives us three SCCs.
3. Which Algorithm Should We Use?
We can solve this problem using Kosaraju's Algorithm.
Kosaraju's Algorithm is used to find all strongly connected components in a directed graph.
It uses:
DFS traversal to find the finishing order.
Transpose graph to reverse all edges.
Another DFS traversal to identify and count SCCs.
The important point is that we need two DFS traversals.
4. Step-by-Step Algorithm Explanation
Step 1: First DFS – Find the finishing order
First, create a visited array.
let visited = new Array(V).fill(false);
let stack = [];Explanation:
visited tracks which vertices have already been visited.
stack stores vertices based on their DFS finishing order.
Now perform DFS.
The important logic is:
if (!visited[v]) {
visited[v] = true;
st.push([v, 0]);
}When all adjacent vertices have been processed, push the current vertex into the stack.
stack.push(u);Why do we push after visiting all adjacent vertices?
Because we need the finishing order, not the visiting order.
A vertex should enter the stack only after its DFS exploration is complete.
For our example, one possible finishing order is:
5 → 4 → 3 → 2 → 1 → 0When we pop the stack, we get:
0 → 1 → 2 → 3 → 4 → 5Step 2: Create the Transpose Graph
A transpose graph means reversing the direction of every edge.
For example:
Original graph
0 → 1Original direction
Transpose graph
0 ← 1Reversed direction
Code:
let reverse = Array.from({ length: V }, () => []);
for (let u = 0; u < V; u++) {
for (let v of adj[u]) {
reverse[v].push(u);
}
}Let's understand this carefully.
Suppose:
adj[0] = [1];This means:
0 → 1After reversing:
reverse[1].push(0);Now:
1 → 0We repeat this process for every edge.
Step 3: Reset the Visited Array
visited.fill(false);Why?
The first DFS already visited all vertices.
Now we need another DFS traversal on the reversed graph.
Therefore, we reset all values to false.
Step 4: Second DFS – Identify SCCs
Now we process vertices from the stack.
let count = 0;
while (stack.length > 0) {
let node = stack.pop();
if (!visited[node]) {
dfsReverse(node);
count++;
}
}This is the most important part of the algorithm.
Let's understand the condition:
if (!visited[node])If the vertex is already visited, it belongs to an SCC that we have already counted.
If the vertex is not visited, start a new DFS.
That DFS identifies one complete SCC.

Join the conversation! Your thoughts help the community grow.