-
Notifications
You must be signed in to change notification settings - Fork 7
/
BFS.cpp
44 lines (37 loc) · 998 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
44
#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<<"\n";
}
int main(){
int A[8][8] = {{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: -> ";
BFS(1,A,8);
cout<<"Vertex 2: -> ";
BFS(2,A,8);
return 0;
}