A strongly connected component is the component of a directed graph that has a path from every vertex to every other vertex in that component. It can only be used in a directed graph.

For example, The below graph has two strongly connected components {1,2,3,4} and {5,6,7} since there is path from each vertex to every other vertex in the same strongly connected component.

Strongly Connected Component

## Difference between Connected and Strongly Connected Components?

Connectivity in a undirected graph refers to whether two vertices are reachable from each other or not. Two vertices are said to be connected if there is path between them. Meanwhile Strongly Connected is applicable only to directed graphs. A subgraph of a directed graph is considered to be an Strongly Connected Components (SCC) if and only if for every pair of vertices A and B, there exists a path from A to B and a path from B to A. Let’s see why the standard dfs method to find connnected components in a graph cannot be used to determine strongly connected components.

Now, Consider the following graph

Now, let’s start a dfs call from vertex 1 to visit other vertices.

Why conventional DFS method cannot be used to find the strongly connected components?

All the vertices can be reached from vertex 1. But vertices 1 and 5,6,7 can not be in the same strongly connected component because there is no directed path from vertex 5,6 or 7 to vertex 1. The graph has two strongly connected components {1,2,3,4} and {5,6,7}. So the conventional dfs method cannot be used to find the strongly connected components.

### Connecting Two Strongly Connected Component by a Unidirectional Edge

Two different connected components becomes a single component if a edge is added between a vertex from one component to a vertex of other component. But this is not the case in strongly connected components. Two strongly connected components doesn’t become a single strongly connected component if there is only a unidirectional edge from one SCC to other SCC.

## Brute Force Approach for Finding Strongly Connected Components:

The simple method will be for each vertex i (which is not a part of any strongly component) find the vertices which will be the part of strongly connected component containing vertex i. Two vertex i and j will be in the same strongly connected component if they there is a directed path from vertex i to vertex j and vice-versa.

Let’s understand the approach with the help of following example:-

• Starting with vertex 1. There is path from vertex 1 to vertex 2 and vice-versa. Similarly there is a path from vertex 1 to vertex 3 and vice versa. So, vertex 2 and 3 will be in the same Strongly Connected Component as vertex 1. Although there is directed path form vertex 1 to vertex 4 and vertex 5. But there is no directed path from vertex 4,5 to vertex 1 so vertex 4 and 5 won’t be in the same Strongly Connected Component as vertex 1. Thus Vertex 1,2 and 3 forms a Strongly Connected Component.
• For Vertex 2 and 3, there Strongly Connected Component has already been determined.
• For Vertex 4, there is a path from vertex 4 to vertex 5 but there is no path from vertex 5 to vertex 4. So vertex 4 and vertex won’t be in the Same Strongly Connected Component. So both Vertex 4 and Vertex 5 will be part of Single Strongly Connected Component.
• Hence there will be 3 Strongly Connected Component {1,2,3}, {4} and {5}.

Below is the implementation of above approach:

## C++

 `#include ` `using` `namespace` `std;` `class` `GFG {` `public` `:` `bool` `dfs(` `int` `curr, ` `int` `des, vector >& adj,` `vector<` `int` `>& vis)` `{` `if` `(curr == des) {` `return` `true` `;` `}` `vis[curr] = 1;` `for` `(` `auto` `x : adj[curr]) {` `if` `(!vis[x]) {` `if` `(dfs(x, des, adj, vis)) {` `return` `true` `;` `}` `}` `}` `return` `false` `;` `}` `bool` `isPath(` `int` `src, ` `int` `des, vector >& adj)` `{` `vector<` `int` `> vis(adj.size() + 1, 0);` `return` `dfs(src, des, adj, vis);` `}` `vector > findSCC(` `int` `n,` `vector >& a)` `{` `vector > ans;` `vector<` `int` `> is_scc(n + 1, 0);` `vector > adj(n + 1);` `for` `(` `int` `i = 0; i < a.size(); i++) {` `adj[a[i][0]].push_back(a[i][1]);` `}` `for` `(` `int` `i = 1; i <= n; i++) {` `if` `(!is_scc[i]) {` `vector<` `int` `> scc;` `scc.push_back(i);` `for` `(` `int` `j = i + 1; j <= n; j++) {` `if` `(!is_scc[j] && isPath(i, j, adj)` `&& isPath(j, i, adj)) {` `is_scc[j] = 1;` `scc.push_back(j);` `}` `}` `ans.push_back(scc);` `}` `}` `return` `ans;` `}` `};` `int` `main()` `{` `GFG obj;` `int` `V = 5;` `vector > edges{` `{ 1, 3 }, { 1, 4 }, { 2, 1 }, { 3, 2 }, { 4, 5 }` `};` `vector > ans = obj.findSCC(V, edges);` `cout << ` `"Strongly Connected Components are:\n"` `;` `for` `(` `auto` `x : ans) {` `for` `(` `auto` `y : x) {` `cout << y << ` `" "` `;` `}` `cout << ` `"\n"` `;` `}` `}`
Output

Strongly Connected Components are:
1 2 3
4
5

Time complexity: O(N3) because for each pair of vertices we are checking whether a path exists between them.
Auxiliary Space: O(N)

Related article for finding strongly connected components in a directed graph in O(V+E) time:

Last Updated :
09 Aug, 2023

Like Article

Save Article

You are watching: Strongly Connected Components. Info created by GBee English Center selection and synthesis along with other related topics.