-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfsgrph.cpp
More file actions
90 lines (63 loc) · 1.55 KB
/
bfsgrph.cpp
File metadata and controls
90 lines (63 loc) · 1.55 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class graph{
public:
int V;
list<int>*adj;
graph(int V);
void add_edge(int u,int v);
void bfs(int s);
};
graph::graph(int V){
this->V= V;
adj= new list<int>[V];
}
void graph:: add_edge(int u,int v){
adj[u].push_back(v);
}
void graph:: bfs(int s){ ///bfs algorithm source gfg
bool *visited= new bool[V];
for(int i=0;i<V;i++){
visited[i]= false; // marked all node as not visited;
}
list<int>q; // declaring queue for storing the vertices
visited[s]= true;
q.push_back(s);
while(!q.empty()){
s= q.front();
cout<<s<<" ";
q.pop_front(); //pop the front element from the queue
for(auto it= adj[s].begin();it!=adj[s].end();it++){
// insert the neighbouring element into the queue and marked them visited
if(!visited[*it]){
visited[*it]= true;
q.push_back(*it); // 10 17
// 0 2 1 3 1 4 1 5 2 3 2 8 2 9 3 7 3 9 4 6 4 7 5 6 5 7 5 9 6 8 7 8 7 9
}
}
}
}
int main(){
graph g(4);
// g.add_edge(0,1);
// g.add_edge(1,4);
// g.add_edge(1,5);
// g.add_edge(2,4);
// g.add_edge(2,5);
// g.add_edge(1,4);
// g.add_edge(3,6);
// g.add_edge(6,5);
// cout<<"BFS traversal is given as \n";
// g.bfs(0);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(1, 2);
g.add_edge(2, 0);
g.add_edge(2, 3);
g.add_edge(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.bfs(2);
return 0;
}