I'd like to know how do I change the known BFS algorithm in order to find all of the connected components of a given graph. The original algorithm stops whenever we've colored an entire component in black, but how do I change it in order for it to run through all of the components? And how do I distinguish between one component to the other?
2026-03-26 23:09:05.1774566545
On
Finding connected components in a graph using BFS
19.3k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail At
2
There are 2 best solutions below
0
On
Recently I am started with competitive programming so written the code for finding the number of connected components in the un-directed graph. Using BFS.
I have implemented using the adjacency list representation of the graph. The Time complexity of the program is (V + E) same as the complexity of the BFS.
You can maintain the visited array to go through all the connected components of the graph.
Here is my code in C++.
/*
Finding the number of non-connected components in the graph
*/
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
void addEdge(vector<int> vec[],int source,int destination){
vec[source].pb(destination);
vec[destination].pb(source);
}
void BFSUtil(vector<bool> &visited ,vector<int> vec[],int i){
list<int> queue;
visited[i] = true;
queue.pb(i);
vector<int> :: iterator it;
if(vec[i].size()==0){
cout<<"vec["<<i<<"].size(): "<<vec[i].size()<<endl;
return;
}
while(!queue.empty()){
i = queue.front();
cout<<i<<" ";
queue.pop_front();
for(it = vec[i].begin(); it!= vec[i].end(); it++){
if(visited[*it] == false){
queue.pb(*it);
visited[*it] = true;
}
}
}
}
void BFS(vector<int> vec[],int V){
vector<bool> visited(V,false);
int total_disconnected_components = 0;
for(int i=0; i<V; i++){
if(visited[i] == false){
BFSUtil(visited,vec,i);
total_disconnected_components++;
}
}
cout<<endl;
cout<<total_disconnected_components<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
int v;
cin>>v;
vector<int> graph[v];
int e;
cin>>e;
for(int i=0; i<e; i++){
int source,destination;
cin>>source>>destination;
addEdge(graph,source,destination);
}
BFS(graph,v);
}
return 0;
}
Please let me know if any further information needed.
Thank you
Novice in competitive programming
Use an integer to keep track of the "colors" that identify each component, as @Joffan mentioned. Start BFS at a vertex $v$. When it finishes, all vertices that are reachable from $v$ are colored (i.e., labeled with a number). Loop through all vertices which are still unlabeled and call BFS on those unlabeled vertices to find other components.
Below is some pseudo-code which initializes all vertices with an unexplored label (an integer 0). It keeps a counter, $componentID$, which vertices are labeled with as they are explored. When a connected component is finished being explored (meaning that the standard BFS has finished), the counter increments. BFS is only called on vertices which belong to a component that has not been explored yet.
The total running time is $O(|V| + |E|)$ since each edge and vertex is labeled exactly twice - once to initialize and again when it's visited.