- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a directed graph is connected or not in C++
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
#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
- Check if a given directed graph is strongly connected in C++
- C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
- C++ Program to Check if a Directed Graph is a Tree or Not Using DFS
- C++ Program to Check Whether a Graph is Strongly Connected or Not
- Check if a given tree graph is linear or not in C++
- Check if a given graph is tree or not
- Check if the board is connected or not in Arduino IDE
- Check if a graph is strongly connected - Set 1 (Kosaraju using DFS) in C++
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- C++ Program to Check if a Given Graph must Contain Hamiltonian Cycle or Not
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- Check if a number is jumbled or not in C++
- Check if a Tree is Isomorphic or not in C++
- Check if a number is Quartan Prime or not in C++

Advertisements