
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check the Connectivity of Directed Graph Using DFS
To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.
For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have only outward edge but no inward edge, so that node will be unvisited from any other starting node.
In this case the traversal algorithm is recursive DFS traversal.
Input: Adjacency matrix of a graph
0 | 1 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
0 | 0 | 0 | 1 | 1 |
1 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 | 0 |
Output: The Graph is connected.
Algorithm
traverse(u, visited)
Input: The start node u and the visited node to mark which node is visited.
Output: Traverse all connected vertices.
Begin mark u as visited for all vertex v, if it is adjacent with u, do if v is not visited, then traverse(v, visited) done End
isConnected(graph)
Input: The graph.
Output: True if the graph is connected.
Begin define visited array for all vertices u in the graph, do make all nodes unvisited traverse(u, visited) if any unvisited node is still remaining, then return false done return true End
Example Code
#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, 1, 0, 0, 0}}; void traverse(int u, bool visited[]) { visited[u] = true; //mark v as visited for(int v = 0; v<NODE; v++) { if(graph[u][v]) { if(!visited[v]) traverse(v, visited); } } } bool isConnected() { bool *vis = new bool[NODE]; //for all vertex u as start point, check whether all nodes are visible or not for(int u; u < NODE; u++) { for(int i = 0; i<NODE; i++) vis[i] = false; //initialize as no node is visited traverse(u, vis); for(int i = 0; i<NODE; i++) { if(!vis[i]) //if there is a node, not visited by traversal, graph is not connected return false; } } return true; } int main() { if(isConnected()) cout << "The Graph is connected."; else cout << "The Graph is not connected."; }
Output
The Graph is connected.
- Related Articles
- C++ Program to Check the Connectivity of Undirected Graph Using DFS
- C++ Program to Check the Connectivity of Directed Graph Using BFS
- C++ Program to Check if a Directed Graph is a Tree or Not Using DFS
- C++ Program to Check the Connectivity of Undirected Graph Using BFS
- Connectivity in a directed graph
- C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
- C++ Program to Check whether Graph is a Bipartite using DFS
- Golang program to check a graph is bipartite using DFS
- Check if a given graph is Bipartite using DFS in C++ program
- Java Program to Check Whether Undirected Graph is Connected Using DFS
- Check if a given graph is Bipartite using DFS using C++
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- C++ Program to Find the Edge Connectivity of a Graph
- C++ Program to Find the Vertex Connectivity of a Graph
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle

Advertisements