C++ Program to Check Whether a Graph is Strongly Connected or Not



To check if a graph is strongly connected or not, we need to check if for any pair of vertices u and v in the directed graph, there exists a directed path from u to v and a directed path from v to u.

In this article, we have a directed graph with five vertices and its respective adjacency matrix. Our task is to check if the given graph is strongly connected or not.

Example of Strongly Connected Graph

The graph displayed in the figure below is an example of a strongly connected graph.

Strongly Connected Graph

In the above graph, we can visit each node from any chosen node and traverse back. If we start from node 'a' then, a -> b -> c -> e -> c -> d -> a. Similarly, we can choose any random node to traverse the graph and also traverse back to starting node.

Steps to Check If Graph is Strongly Connected

You can use the following steps to check if the given graph is a strongly connected graph or not:

  • At first, we defined the number of nodes and provided the adjacency matrix of the graph.
  • Then, we implemented the Depth-First Search (DFS) algorithm using the dfs() function to visit the nodes of the graph.
  • In the isStronglyConnected() function, first, we check if we can traverse from node 0 to all nodes or not.
  • Then we find the transpose of the graph. We find the transpose of the matrix to check if we can traverse back from the visited node to node 0.
  • After creating a transposed graph, we fill the visited array with false value using the fill() function.
  • Then, we apply the dfs() function on the transposed graph to check if we can reach all the nodes from node 0.
  • If we can reach all the nodes from node 0 in the graph and the transposed graph, then the graph is strongly connected graph.

Checking if Graph is Strongly Connected in C++

Here is the code implementation of the above steps for checking the strongly connected graph:

#include <iostream>
#define NODE 5
using namespace std;

int graph[NODE][NODE] = {
   {0, 1, 0, 0, 0},
   {0, 0, 1, 0, 0},
   {0, 0, 0, 1, 1},
   {1, 0, 0, 0, 0},
   {0, 0, 1, 0, 0}};

void dfs(int u, bool visited[], int g[NODE][NODE]) {
    visited[u] = true;
    for (int v = 0; v < NODE; v++) {
        if (g[u][v] && !visited[v])
            dfs(v, visited, g);
    }
}

bool isStronglyConnected() {
    bool visited[NODE] = {false};

    // Checking if we can reach each node from node 0
    // in original graph
    dfs(0, visited, graph);
    for (int i = 0; i < NODE; i++) {
        if (!visited[i])
            return false;
    }

    // Create transpose graph
    int transpose[NODE][NODE] = {0};
    for (int i = 0; i < NODE; i++) {
        for (int j = 0; j < NODE; j++) {
            transpose[i][j] = graph[j][i];
        }
    }

    // Check reachability in transpose graph
    fill(visited, visited + NODE, false);
    dfs(0, visited, transpose);
    for (int i = 0; i < NODE; i++) {
        if (!visited[i])
            return false;
    }

    return true;
}

int main() {
    if (isStronglyConnected())
        cout << "Given Directed Graph is strongly connected.";
    else
        cout << "Given Directed Graph is not strongly connected.";
}

The output of the above code is as follows:

Given Directed Graph is strongly connected.

Complexity to Check Strongly Connected Graph

  • Time Complexity: The time complexity for checking if the given directed graph is strongly connected is O(v^2).
  • Space Complexity: The space complexity for checking if the given directed graph is strongly connected is O(v^2).
Updated on: 2025-05-29T19:19:15+05:30

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements