Given a directed graph, check if it is strongly connected or not. A directed graph is said to be strongly connected if every vertex is reachable from every other vertex.

For example, the following graph is strongly connected as a path exists between all pairs of vertices:

 
Strongly Connected Graph

Practice this problem

A simple solution is to perform Depth–first search (DFS) or Breadth–first search (BFS) starting from every vertex in the graph. If each DFS/BFS call visits every other vertex in the graph, then the graph is strongly connected.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The graph is strongly connected

Java


Download  Run Code

Output:

The graph is strongly connected

Python


Download  Run Code

Output:

The graph is strongly connected

The time complexity of the above solution is O(n × (n + m)), where n is the total number of vertices and m is the total number of edges in the graph.

Can we do better?

We can say that G is strongly connected if:

  1. DFS(G, v) visits all vertices in the graph G, then there exists a path from v to every other vertex in G, and
  2. There exists a path from every other vertex in G to v.

Proof:

For G to be strongly connected, a path from x —> y and y —> x should exist for any pair of vertices (x, y) in the graph.

If points 1 and 2 are true, we can reach x —> y by going from vertex x to vertex v (from pt. 2), and then from vertex v to vertex y (from pt. 1).

Similarly, we can reach y —> x by going from vertex y to vertex v (from pt. 2), and then from vertex v to vertex x (from pt. 1).

Complete Algorithm:

  1. Start DFS(G, v) from a random vertex v of the graph G. If DFS(G, v) fails to reach every other vertex in the graph G, then there is some vertex u, such that there is no directed path from v to u. Thus, G is not strongly connected. If it does reach every vertex, then there is a directed path from v to every other vertex in the graph G.
  2. Reverse the direction of all edges in the directed graph G.
  3. Again, run a DFS starting from vertex v. If the DFS fails to reach every vertex, then there is some vertex u, such that in the original graph, there is no directed path from u to v. On the other hand, if it does reach every vertex, then there is a directed path from every vertex u to v in the original graph.

If G passes both DFS, it is strongly connected. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The graph is strongly connected

Java


Download  Run Code

Output:

The graph is strongly connected

Python


Download  Run Code

Output:

The graph is strongly connected

The time complexity of the above solution is O(V + E), where V and E are the total number of vertices and edges in the graph, respectively. Please note that O(E) may vary between O(1) and O(V2), depending on how dense the graph is.

 
Reference: Dr. Naveen Garg, IIT–D (Lecture – 30 Applications of DFS in Directed Graphs)