forked from Ankita-Nandkumar-Patil/HactoberFest2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
78 lines (62 loc) · 1020 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//BFS
#include<iostream>
#include "queue.cpp"
using namespace std;
void bfs(int v,int adj_mat[20][20]){
bool visited[v];
for(int i=0;i<v;i++){
visited[i]=false;
}
Queue q;
q.enQueue(0);
visited[0]=true;
int vis;
while(!q.isEmpty()){
vis=q.peek();
cout<<(char)(vis+'A')<<" ";
int it=q.deQueue();
for(int i=0;i<v;i++){
if(adj_mat[vis][i]==1 && (!visited[i])){
q.enQueue(i);
visited[i]=true;
}
}
}
cout<<endl;
}
int main(){
freopen("input11.txt","r",stdin);
int v;
cin>>v;
int adj_mat[20][20];
for(int i=0;i<v;i++){
for(int j=0;j<v;j++){
cin>>adj_mat[i][j];
}
}
system("clear");
cout<<"The Adjacency Matrix is"<<endl;
for(int i=0;i<v;i++){
for(int j=0;j<v;j++){
cout<<adj_mat[i][j]<<" ";
}
cout<<endl;
}
bool visited[20];
for(int i=0;i<v;i++){
visited[i]=false;
}
cout<<"The BFS Traversal"<<endl;
bfs(v,adj_mat);
cout<<endl;
return 0;
}
/*
6
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 0 1 0
0 1 0 0 1 1
0 1 1 1 0 1
0 0 0 1 1 0
*/