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);
}
}

}

/******************************************************************************

EASY GRAPH REPRESENTATION AND BFS TRAVERSAL (by deepak jaiswal)

*******************************************************************************/

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

void addEdge(vector<int>adj[],int u,int v){
adj[u].push_back(v); //this will add edge from 0 to 1
adj[v].push_back(u); //add edge from 1 to 0
}
void print(vector<int>adj[],int v){
for(int i=0;i<v;i++){
for(int x:adj[i]){ //this will iterate every array element of…

DEEPAK JAISWAL

coder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store