-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs1.cpp
59 lines (56 loc) · 963 Bytes
/
bfs1.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
#include <bits/stdc++.h>
using namespace std;
class graph
{
int V;
list <int> *adj;
//bool *visited;
public:
graph(int V);
void addEdge(int start,int end);
void bfs(int s);
};
graph::graph(int V){
this->V=V;
adj=new list<int>[V];
}
void graph::addEdge(int start,int end){
adj[start].push_back(end);
}
void graph::bfs(int s){
bool *visited=new bool[V];
for (int i = 0; i < V; ++i)
{
visited[i]=false;
}
list<int> queue;
visited[s]=true;
queue.push_back(s);
list <int> ::iterator i;
while(!queue.empty()){
s=queue.front();
cout<<s<<" ";
queue.pop_front();
for ( i =adj[s].begin() ; i !=adj[s].end() ; ++i)
{
if(!visited[*i]){
visited[*i]=true;
queue.push_back(*i);
}
}
}
}
int main(){
cout<<"Enter the no of nodes:::";
int n,edge,start,end;
cin>>n;
graph g(n);
cout<<"Enter the no of edges:::";
cin>>edge;
for (int i = 0; i < edge; ++i)
{
cin>>start>>end;
g.addEdge(start,end);
}
g.bfs(2);
}