Easy BFS traversal for disconnected graphs (by deepak jaiswal)

this traversal mainly used in web crawling to visit group of pages even they are not been connected

here is the code->

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

void addEdge(vector<int>adj[],int u,int v){
adj[u].push_back(v);
adj[v].push_back(u);
}

void bfsdisc(vector<int>adj[],int s,bool visited[]){
queue<int>q;
visited[s]=true;
q.push(s);
while(q.size()!=0){
int curr=q.front();
q.pop();
cout<<curr<<” “;
for(int x:adj[curr]){
if(visited[x]==false){
visited[x]=true;
q.push(x);
}
}
}
}
int main()
{
int v=6;

vector<int>adj[v]; //vector of array
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,1,2);
addEdge(adj,1,3);
addEdge(adj,4,5);
bool visited[v+1];
for(int i=0;i<v;i++){
visited[i]=false;
}
for(int i=0;i<v;i++){
if(visited[i]==false){
bfsdisc(adj,i,visited);
}
}

}

coder