# Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)

Given a directed graph, find out whether the graph is strongly connected or not. A directed graph is strongly connected if there is a path between any two pairs of vertices. There are different methods to check the connectivity of directed graph but one of the optimized method is Kosaraju’s DFS based simple algorithm.

Kosaraju’s BFS based simple algorithm also work on the same principle as DFS based algorithm does.

Following is Kosaraju’s BFS based simple algorithm that does two BFS traversals of graph: 1) Initialize all vertices as not visited. 2) Do a BFS traversal of graph starting from any arbitrary vertex v. If BFS traversal doesn’t visit all vertices, then return false. 3) Reverse all edges (or find transpose or reverse of graph) 4) Mark all vertices as not visited in reversed graph. 5) Again do a BFS traversal of reversed graph starting from same vertex v (Same as step 2). If BFS traversal doesn’t visit all vertices, then return false. Otherwise, return true.

The idea is again simple if every node can be reached from a vertex v, and every node can reach same vertex v, then the graph is strongly connected. In step 2, we check if all vertices are reachable from v. In step 5, we check if all vertices can reach v (In reversed graph, if all vertices are reachable from v, then all vertices can reach v in original graph).

**Explanation with some examples: **

**Example 1 :**

Given a directed to check if it is strongly connected or not.

step 1: Starting with vertex 2 BFS obtained is 2 3 4 0 1

step 2: After reversing the given graph we got listed graph.

step 3: Again after starting with vertex 2 the BFS is 2 1 4 0 3

step 4: No vertex in both case (step 1 & step 3) remains unvisited.

step 5: So, given graph is strongly connected.

**Example 2 :**

Given a directed to check if it is strongly connected or not.

step 1: Starting with vertex 2 BFS obtained is 2 3 4

step 2: After reversing the given graph we got listed graph.

step 3: Again after starting with vertex 2 the BFS is 2 1 0

step 4: vertex 0, 1 in original graph and 3, 4 in reverse graph remains unvisited.

step 5: So, given graph is not strongly connected.

Following is the implementation of above algorithm.

`// C++ program to check if a given directed graph ` `// is strongly connected or not with BFS use ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `class` `Graph ` `{ ` ` ` `int` `V; ` `// No. of vertices ` ` ` `list<` `int` `> *adj; ` `// An array of adjacency lists ` ` ` ` ` `// A recursive function to print DFS starting from v ` ` ` `void` `BFSUtil(` `int` `v, ` `bool` `visited[]); ` `public` `: ` ` ` ` ` `// Constructor and Destructor ` ` ` `Graph(` `int` `V) { ` `this` `->V = V; adj = ` `new` `list<` `int` `>[V];} ` ` ` `~Graph() { ` `delete` `[] adj; } ` ` ` ` ` `// Method to add an edge ` ` ` `void` `addEdge(` `int` `v, ` `int` `w); ` ` ` ` ` `// The main function that returns true if the ` ` ` `// graph is strongly connected, otherwise false ` ` ` `bool` `isSC(); ` ` ` ` ` `// Function that returns reverse (or transpose) ` ` ` `// of this graph ` ` ` `Graph getTranspose(); ` `}; ` ` ` `// A recursive function to print DFS starting from v ` `void` `Graph::BFSUtil(` `int` `v, ` `bool` `visited[]) ` `{ ` ` ` `// Create a queue for BFS ` ` ` `list<` `int` `> queue; ` ` ` ` ` `// Mark the current node as visited and enqueue it ` ` ` `visited[v] = ` `true` `; ` ` ` `queue.push_back(v); ` ` ` ` ` `// 'i' will be used to get all adjacent vertices ` ` ` `// of a vertex ` ` ` `list<` `int` `>::iterator i; ` ` ` ` ` `while` `(!queue.empty()) ` ` ` `{ ` ` ` `// Dequeue a vertex from queue ` ` ` `v = queue.front(); ` ` ` `queue.pop_front(); ` ` ` ` ` `// Get all adjacent vertices of the dequeued vertex s ` ` ` `// If a adjacent has not been visited, then mark it ` ` ` `// visited and enqueue it ` ` ` `for` `(i = adj[v].begin(); i != adj[v].end(); ++i) ` ` ` `{ ` ` ` `if` `(!visited[*i]) ` ` ` `{ ` ` ` `visited[*i] = ` `true` `; ` ` ` `queue.push_back(*i); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function that returns reverse (or transpose) of this graph ` `Graph Graph::getTranspose() ` `{ ` ` ` `Graph g(V); ` ` ` `for` `(` `int` `v = 0; v < V; v++) ` ` ` `{ ` ` ` `// Recur for all the vertices adjacent to this vertex ` ` ` `list<` `int` `>::iterator i; ` ` ` `for` `(i = adj[v].begin(); i != adj[v].end(); ++i) ` ` ` `g.adj[*i].push_back(v); ` ` ` `} ` ` ` `return` `g; ` `} ` ` ` `void` `Graph::addEdge(` `int` `v, ` `int` `w) ` `{ ` ` ` `adj[v].push_back(w); ` `// Add w to v’s list. ` `} ` ` ` `// The main function that returns true if graph ` `// is strongly connected ` `bool` `Graph::isSC() ` `{ ` ` ` `// St1p 1: Mark all the vertices as not ` ` ` `// visited (For first BFS) ` ` ` `bool` `visited[V]; ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `visited[i] = ` `false` `; ` ` ` ` ` `// Step 2: Do BFS traversal starting ` ` ` `// from first vertex. ` ` ` `BFSUtil(0, visited); ` ` ` ` ` `// If BFS traversal doesn’t visit all ` ` ` `// vertices, then return false. ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `if` `(visited[i] == ` `false` `) ` ` ` `return` `false` `; ` ` ` ` ` `// Step 3: Create a reversed graph ` ` ` `Graph gr = getTranspose(); ` ` ` ` ` `// Step 4: Mark all the vertices as not ` ` ` `// visited (For second BFS) ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `visited[i] = ` `false` `; ` ` ` ` ` `// Step 5: Do BFS for reversed graph ` ` ` `// starting from first vertex. ` ` ` `// Staring Vertex must be same starting ` ` ` `// point of first DFS ` ` ` `gr.BFSUtil(0, visited); ` ` ` ` ` `// If all vertices are not visited in ` ` ` `// second DFS, then return false ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `if` `(visited[i] == ` `false` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `// Create graphs given in the above diagrams ` ` ` `Graph g1(5); ` ` ` `g1.addEdge(0, 1); ` ` ` `g1.addEdge(1, 2); ` ` ` `g1.addEdge(2, 3); ` ` ` `g1.addEdge(3, 0); ` ` ` `g1.addEdge(2, 4); ` ` ` `g1.addEdge(4, 2); ` ` ` `g1.isSC()? cout << ` `"Yes\n"` `: cout << ` `"No\n"` `; ` ` ` ` ` `Graph g2(4); ` ` ` `g2.addEdge(0, 1); ` ` ` `g2.addEdge(1, 2); ` ` ` `g2.addEdge(2, 3); ` ` ` `g2.isSC()? cout << ` `"Yes\n"` `: cout << ` `"No\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes No

**Time Complexity:** Time complexity of above implementation is same as Breadth First Search which is O(V+E) if the graph is represented using adjacency matrix representation.

**Can we improve further?**

The above approach requires two traversals of graph. We can find whether a graph is strongly connected or not in one traversal using Tarjan’s Algorithm to find Strongly Connected Components.

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Strongly Connected Components
- Tarjan's Algorithm to find Strongly Connected Components
- Connected Components in an undirected graph
- All vertex pairs connected with exactly k edges in a graph
- Cycles of length n in an undirected and connected graph
- Detect Cycle in a Directed Graph using BFS
- Euler Circuit in a Directed Graph
- Clone a Directed Acyclic Graph
- Hierholzer's Algorithm for directed graph
- Detect Cycle in a Directed Graph
- Clone an undirected graph with multiple connected components
- Sum of the minimum elements in all connected components of an undirected graph
- Detect Cycle in a directed graph using colors
- Shortest Path in Directed Acyclic Graph