-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbfs2.cpp
74 lines (67 loc) · 1.45 KB
/
bfs2.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
62
63
64
65
66
67
68
69
70
71
72
73
//for no cylce
#include<bits/stdc++.h>
#define pb push_back
#define pp pop_back
#define ps push
#define po pop
using namespace std;
int main()
{
queue <int> gq;
int v;
cout<<"Enter the vertex : ";
cin>>v;
vector <int> vi[10];
for(int i=1;i<=v;i++)
{
int k;
cout<<"Enter -1 if no adjacent element is there\n";
cout<<"enter the element adjacent to "<<i<<" :: ";
do
{
cin>>k;
if(k!=-1)
{
vi[i].pb(k);
}
}while(k!=-1);
}
for(int i=1;i<=v;i++)
{
cout<< i<<" -> ";
for(auto j=vi[i].begin();j!=vi[i].end();j++)
{
cout<<*j<<" ";
}
cout<<endl;
}
//cout<<1<<" ";
vector <int> vect;
vect.pb(1);
int p=1;
int y=1;
for(int i=1;i<=v;i++)
while(y<=v)
{
y++;
for(auto j=vi[p].begin();j!=vi[p].end();j++)
{
gq.ps(*j);
}
// cout<<gq.front()<<" ";
//auto find(vect.begin(),vect.end(),*j)!=vect.end();
if(find(vect.begin(),vect.end(),gq.front())==vect.end())
{ vect.pb(gq.front());
}
p=gq.front();
gq.po();
}
cout<<endl;
cout<<"Bfs traversal started from vertex 1 of the graph\n";
cout<<"Bfs of the given graph is :: ";
for(auto j=vect.begin();j!=vect.end();j++)
{
cout<<*j<<" ";
}
cout<<endl;
}