-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBreadth First Search.cpp
49 lines (44 loc) · 1.03 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
/*
* File Name: Breadth First Search (BFS)
*
* Credit : Course : Graph Search, Shortest Paths, and Data Structures, Stanford University on Coursera.
* Instructor : Tim Roughgarden, Professor
*
* Author: Mrunal Nirajkumar Shah
* Date: 18th Of FEBURARY, 2023
*
* Description: The breadth-first search (BFS) algorithm is used to search a tree or graph data structure for a node that meets a set of criteria.
*/
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector<int> adj[], int s, int d) {
adj[s].push_back(d);
}
void BFS(vector<int> adj[], int s, int V) {
int arr[V] = {0};
arr[s] = 1;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int v = Q.front();
cout << v << " ";
Q.pop();
for (auto x : adj[v]) {
if (arr[x] == 0) {
arr[x] = 1;
Q.push(x);
}
}
}
}
int main() {
int V = 4;
vector<int> adj[V];
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 0);
addEdge(adj, 2, 3);
addEdge(adj, 3, 3);
BFS(adj, 2, V);
}