-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphBFS.cpp
More file actions
49 lines (40 loc) · 965 Bytes
/
GraphBFS.cpp
File metadata and controls
49 lines (40 loc) · 965 Bytes
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
// BFS
#include<bits/stdc++.h>
using namespace std;
void eddEdge(vector<int> gr[], int u, int v){
gr[u].emplace_back(v);
gr[v].emplace_back(u);
}
void bfs(vector<int> gr[], int strt){
vector<bool> visited(gr->size(), false);
queue<int> kt;
kt.push(strt);
visited[strt]=true;
while(!kt.empty()){
int v=kt.front();
cout<<v<<" ";
kt.pop();
// Enqueue all adjacent nodes of v and mark them visited
for(auto i=gr[v].begin(); i!=gr[v].end(); i++){
if(!visited[*i]){
kt.push(*i);
visited[*i]=true;
}
}
}
}
int main(){
int v=7; //vertices
vector<int> gr[v];
eddEdge(gr,0,1);
eddEdge(gr,0,3);
eddEdge(gr,1,2);
eddEdge(gr,2,3);
eddEdge(gr,2,6);
eddEdge(gr,3,4);
eddEdge(gr,4,5);
eddEdge(gr,5,6);
cout<<"BFS traversal starting from node 0"<<endl;
bfs(gr,0);
return 0;
}