-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathBreadthFirstSearch.cpp
74 lines (58 loc) · 1.54 KB
/
BreadthFirstSearch.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
74
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Graph {
private:
int V; // No. of vertices
vector<vector<int>> adj; // Adjacency Lists
public:
Graph(int v) : V(v) {
adj.resize(V);
}
void addEdge(int v, int w) {
adj[v].push_back(w);
adj[w].push_back(v);
}
// BFS traversal from a given source node 's'
void BFS(int s) {
// Create a queue for BFS traversal
queue<int> q;
vector<bool> visited(V, false);
// Mark the current node as visited and add into queue
visited[s] = true;
q.push(s);
// Remove a vertex from queue and print it
cout << "BFS traversal starting from vertex " << s << ": ";
while (!q.empty()) {
int curr = q.front();
q.pop();
cout << curr << " ";
// Get all adjacent vertices of the dequeued vertex s.
// If an adjacent has not been visited, then mark it visited and enqueue it
for (int nbr : adj[curr]) {
if (!visited[nbr]) {
q.push(nbr);
visited[nbr] = true;
}
}
}
}
};
int main() {
int N, M;
cout << "Enter the number of vertices and edges: ";
cin >> N >> M;
Graph g(N);
for (int i = 1; i <= M; i++) {
int u, v;
cout << "Enter edge No. " << i << ": ";
cin >> u >> v;
g.addEdge(u, v);
}
int s;
cout << "Enter source vertex: ";
cin >> s;
g.BFS(s);
return 0;
}