-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathBFS.cpp
52 lines (49 loc) · 1.18 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
49
50
51
52
#include <iostream>
#include <queue>
using namespace std;
void BFS(int vtx,int A[][8],int n){
queue<int> Q;
int visited[8] {0};
cout << vtx << ", " ;
visited[vtx] = 1;
Q.emplace(vtx);
while (!Q.empty()){
int u = Q.front();
Q.pop();
for (int v=1; v<=n; v++){
if (A[u][v] == 1 && visited[v] == 0){
cout << v << ", " ;
visited[v] = 1;
Q.emplace(v);
}
}
}
cout << endl;
}
int main (){
int a;
int A[8][8] = {{0, 0, 0, 0, 0,0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1},
{0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 0, 0, 0},
{0,1, 0, 1, 0, 1, 0, 0},
{0, 1,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->";
BFS(1,A,8);
cout<<"Vertex:2->";
BFS(2,A,8);
cout<<"Vertex:3->";
BFS(3,A,8);
cout<<"Vertex:4->";
BFS(4,A,8);
cout<<"Vertex:5->";
BFS(5,A,8);
cout<<"Vertex:6->";
BFS(6, A, 8);
cout<<"Vertex:7->";
BFS(7,A,8);
cout<<"hello";
return 0;
}