-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbfs.cpp
61 lines (59 loc) · 1.21 KB
/
bfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//bfs
//by me
//using vector of vectors for adjacency list
#include<bits/stdc++.h>
using namespace std;
struct vertex
{
char col;
int d;
int par;
};
int main()
{
int i,j,n,no,e,temp;
cout<<"enter no of vertices : ";
cin>>n;
queue<int>q;
vertex l[n+1];
vector<vector<int> >v;
v.resize(n+1);
for(i=1;i<=n;i++)
{
cout<<"enter no of edges from "<<i<<" : ";
cin>>no;
if(no>0)
{
cout<<"enter the edges : ";
for(j=0;j<no;j++)
{
cin>>e;
v[i].push_back(e);
}
}
}
for(i=1;i<=n;i++)
l[i].col='w';
l[1].col='g';
l[1].d=0;
l[1].par=0;
q.push(1);
while(q.size()>0) //!q.empty()
{
temp=q.front();
q.pop();
for(i=0;i<v[temp].size();i++)
if(l[v[temp][i]].col=='w')
{
l[v[temp][i]].col='g';
l[v[temp][i]].d=l[temp].d+1;
l[v[temp][i]].par=temp;
q.push(v[temp][i]);
}
l[temp].col='b';
}
cout<<endl;
for(i=1;i<=n;i++)
cout<<i<<" "<<l[i].col<<" "<<l[i].d<<" "<<l[i].par<<endl;
return 0;
}