-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.cpp
48 lines (39 loc) · 1.1 KB
/
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
44
45
46
47
48
// Online C++ compiler to run C++ online.
// Write C++ code in this online editor and run it.
#include <iostream>
#include <queue>
using namespace std;
void BFS (vector<vector<int>> A, int start, int end) {
int i = start;
vector<int> V(7, 0);
queue<int> q;
cout << i << " ";
V[i] = 1;
q.push(i);
while(!q.empty()){
int i = q.front();
q.pop();
for(int j = 1; j <= end; ++j) {
if(V[j] == 0 && A[i][j] == 1) {
cout << j << " ";
V[j] = 1;
q.push(j);
}
}
}
}
int main() {
vector<vector<int>> A = {{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1, 0, 0},
{0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0}};
cout << "Vertex: 1 -> " << flush;
BFS(A, 1, 7);
cout << "Vertex: 4 -> " << flush;
BFS(A, 4, 7);
return 0;
}