forked from heyimShivam/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfsTraversal.cpp
69 lines (53 loc) · 1.14 KB
/
bfsTraversal.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
// BFS Traversal in a graph from a given source vertex
#include<bits/stdc++.h>
using namespace std;
class Graph {
int V;
vector<list<int>> adj;
public:
Graph(int V);
void addEdge(int v,int w);
void BFS(int src);
};
Graph::Graph(int V) {
this->V = V;
adj.resize(V);
}
void Graph:: addEdge(int v, int w) {
adj[v].push_back(w);
}
void Graph:: BFS(int src) {
vector<bool> visited;
visited.resize(V,false);
list<int> queue;
visited[src] = true;
queue.push_back(src);
while(!queue.empty()) {
src = queue.front();
cout<<src<<" ";
queue.pop_front();
for(auto it : adj[src]) {
if(!visited[it]) {
visited[it] = true;
queue.push_back(it);
}
}
}
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout<< " Following is the Breadth First Traversal (starting from vertex 1) \n";
g.BFS(1);
return 0;
}
/*
output :
Following is the Breadth First Traversal (starting from vertex 1)
1 2 0 3
*/