Check Connectivity of Directed Graph Using DFS in C++



To check if a directed graph is connected or not, we need to check if there exists a path between every pair of vertices. A directed graph (or digraph) is a graph where each edge has a direction, edges are in ordered pairs, and edges traverse from the source vertex (the tail) to the destination vertex (the head).

In this article, we have a directed graph with five vertices and its respective adjacency matrix. Our task is to use Depth-first Search(DFS) to check the connectivity of the given graph.

Example of Connected Graph

In the figure given below, we have displayed two graphs where the first graph is a connected graph and the second one is a disconnected graph.

Connected and disconnected graph

In the connected graph above, we can visit each node as an edge exists between every pair of vertices. In the undirected graph, we can not visit the node 'd' as there isn't any edge connecting the node d.

Steps to Check Connectivity of Directed Graph Using DFS

You can use the following steps to check if the given graph is a 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 connected() 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 a 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 connected graph.

Checking if Graph is Connected Using DFS in C++

Here is the code implementation of the above steps for checking the connected graph using DFS.

Open Compiler
#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 connected() { 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 (connected()) cout << "Given Directed Graph is connected."; else cout << "Given Directed Graph is not connected."; }

The output of the above code is as follows:

Given Directed Graph is connected.

Complexity to Check Connected Graph Using DFS

  • Time Complexity: The time complexity for checking if the given directed graph is connected using DFS is O(v^2).
  • Space Complexity: The space complexity for checking if the given directed graph is connected using DFS is O(v^2).
Updated on: 2025-05-30T18:59:12+05:30

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements