Theory:

Bangla:

গ্রাফ থিওরিতে হাতেখড়ি-৪(ব্রেডথ ফার্স্ট সার্চ)

Video:

code implementation:

code:

```//bfs implementation for undirected unweighted graph
#include<fstream>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;

int main()
{
int n,m,start;
int a,b;
cin>>n>>m>>start;
vector<int> samp;
vector<vector<int> > conn(n+1,samp); //conn taking custom value in vector to make it sizeable

for(int i=0; i<m; i++)
{
cin>>a>>b;
conn[a].push_back(b);
conn[b].push_back(a);
}
bool visited[n+1]; //visited node 1 to last
for(int i=0; i<n+1; i++)
{
visited[i]=false;  //making each and every node false
}
queue<int> Q;  //creating a queue
Q.push(start); //pushing the starting node in the queue
visited[start]=true; //make the starting node only true
int siz=0;
int lev=0;
while(!Q.empty())
{
cout<<"LEVEL:"<<lev<<endl;
siz=Q.size();
while(siz--)
{
int first=Q.front();
Q.pop();
cout<<first<<endl;
for(int j=0; j<conn[first].size(); j++)
{
if(!visited[conn[first][j]])
{
visited[conn[first][j]]=true; //making true which have visited
Q.push(conn[first][j]); //pushing the value in the queue
}
}

}
lev++; //level increasing in each iteration
}
return 0;
}
```

Input:

```9 10 1 //here 1 is starting node that can be any value
1 2
1 3
3 4
3 5
1 6
4 6
4 9
9 8
1 7
7 3```

Output:

Another code tried to write from the pseudocode is:

```#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
//void BFS(int);
int WHITE=1,GRAY=2,BLACK=3,inf=31000,NIL=-1;
int num_e,num_v;
queue<int> Q;
void initialization()
{
int k,j;
for(k=0; k<20; k++)
{
for(j=0; j<20; j++)
{
}
}
}
{
int i,a,b;
cin>>num_v;
cin>>num_e;
for(i=1; i<=num_e; i++)
{
cin>>a>>b;
}
}
void BFS(int s)
{
int i,u,v;
for(i=1; i<=num_v; i++)
{
color[i]=WHITE;
d[i]=inf;
p[i]=NIL;
}
color[s]=GRAY;
d[s]=0;
p[s]=NIL;
Q.push(s);
while(!Q.empty())
{
int u=Q.pop();
for(v=1; v<=num_v; v++)
{
{
if(color[v]==WHITE)
{
color[v]=GRAY;
d[v]=d[u]+1;
p[v]=u;
Q.push(v);
}
}
}
color[u]=BLACK;
}
}
void distance_output()
{
int i;
cout<<"Shortest path from source vertex";
for(i=1; i<=num_v; i++)
{
cout<<"to node "<<i<<"is "<<d[i];

}
}
void path_print(int s,int v)
{
if(v==s)
{
cout<<s<<endl;
}
else if(p[v]==NIL)
{
cout<<"No path from"<<s<<"to "<<v<<"exists";
}
else
{
path_print(s,p[v]);
cout<<v<<endl;
}
}
int main()
{

int source,destination;
initialization();