forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadth-first-search.cpp
55 lines (43 loc) · 1.39 KB
/
breadth-first-search.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
// C++ program implementing BFS (Breadth First Search)
// BFS is a traversal technique where level wise traversal take place where nodes in higher level are visited once all the nodes in lower levels are visited.
// Queue data structure is used in this technique.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> g[N]; // adjacency list
int visited[N];
void bfs(int source){
queue<int> q;
q.push(source);
visited[source] = 1;
// From source vertex :
// 1. remove the firrst vertex of queue.
// 2. mark the vertex as visited.
// 3. Insert all unvisited nwighbours of vertex into queue.
while (!q.empty()){
int current_node = q.front();
cout << current_node << " ";
q.pop();
for(int child: g[current_node]){
if(!visited[child]){
q.push(child);
visited[child] = 1;
}
}
}
}
int main(){
int nodes, edges;
cout << "Enter number of nodes and edges : ";
cin >> nodes >> edges;
cout << "Enter "<< edges << " pairs of src, dest vertices of a edge : ";
for(int i=0; i< edges; i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
}
// assuming nodes start from 0 and assuming 0 as source vertex here
cout << "Nodes are : " ;
bfs(0);
return 0;
}