-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.cpp
43 lines (35 loc) · 929 Bytes
/
BFS.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
// Ayush Bhardwaj
// Date : 07-03-23
#include <iostream>
#include <queue>
#define MAX_NODES 100
using namespace std;
void bfs(int graph[][MAX_NODES], int start, int numNodes) {
bool visited[MAX_NODES] = {false};
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
for (int neighbor = 0; neighbor < numNodes; neighbor++) {
if (graph[node][neighbor] == 1 && !visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
int main() {
int graph[MAX_NODES][MAX_NODES] = {
{0, 1, 1, 0},
{1, 0, 1, 1},
{1, 1, 0, 1},
{0, 1, 1, 0}
};
int start = 0;
int numNodes = 4;
bfs(graph, start, numNodes);
return 0;
}