

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Check the Connectivity of Undirected Graph Using BFS
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 undirected graph, we will select one node and traverse from it.
In this case the traversal algorithm is recursive BFS traversal.
Input − Adjacency matrix of a graph
0 | 1 | 1 | 0 | 0 |
1 | 0 | 1 | 1 | 0 |
1 | 1 | 0 | 1 | 1 |
0 | 1 | 1 | 0 | 1 |
0 | 0 | 1 | 1 | 0 |
Output − The Graph is connected.
Algorithm
traverse(s, visited)
Input − The start node s and the visited node to mark which node is visited.
Output − Traverse all connected vertices.
Begin mark s as visited insert s into a queue Q until the Q is not empty, do u = node that is taken out from the queue for each node v of the graph, do if the u and v are connected, then if u is not visited, then mark u as visited insert u into the queue Q. done 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 (C++)
#include<iostream> #include<queue> #define NODE 5 using namespace std; int graph[NODE][NODE] = { {0, 1, 1, 0, 0}, {1, 0, 1, 1, 0}, {1, 1, 0, 1, 1}, {0, 1, 1, 0, 1}, {0, 0, 1, 1, 0}}; void traverse(int s, bool visited[]) { visited[s] = true; //mark v as visited queue<int> que; que.push(s);//insert s into queue while(!que.empty()) { int u = que.front(); //delete from queue and print que.pop(); for(int i = 0; i < NODE; i++) { if(graph[i][u]) { //when the node is non-visited if(!visited[i]) { visited[i] = true; que.push(i); } } } } } 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 Questions & Answers
- C++ Program to Check the Connectivity of Directed Graph Using BFS
- C++ Program to Check the Connectivity of Undirected Graph Using DFS
- Python Program to Find if Undirected Graph contains Cycle using BFS
- C++ Program to Check the Connectivity of Directed Graph Using DFS
- Python Program to Find All Connected Components using BFS in an Undirected Graph
- Connectivity of Graph
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Find the Edge Connectivity of a Graph
- C++ Program to Find the Vertex Connectivity of a Graph
- C++ Program to Find the Connected Components of an UnDirected Graph
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Path
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- Connectivity in a directed graph
- Python Program to Find All Connected Components using DFS in an Undirected Graph
Advertisements