forked from dishanp/Algorithms-For-Software-Developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph BFS.cpp
57 lines (56 loc) · 1.07 KB
/
Graph 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
#include<bits/stdc++.h>
using namespace std;
void init(vector<vector<int>>&g,int n){
for(int i=0;i<n;i++){
vector<int> v;
for(int j=0;j<n;j++)
{
if((i+j)%2==0){
v.push_back(0);
}
else{
v.push_back(1);
}
}
g.push_back(v);
}
}
void BFS(vector<vector<int>>g){
queue<int>q;
int start=0;
int i,j;
i=start;
int n=5;
int visited[5]={0};
cout<<i<<" ";
visited[i]=1;
q.push(i);
while(!q.empty()){
i=q.front();
q.pop();
for(int j=1;j<n;j++){
if(g[i][j]==1&&visited[j]==0){
cout<<j<<" ";
visited[j]=1;
q.push(j);
}
}
}
}
void display(vector<vector<int>>g){
for(int i=0;i<g.size();i++){
for(int j=0;j<g[i].size();j++)
{
cout<<g[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int main(){
vector<vector<int>>g;
init(g,5);
display(g);
BFS(g);
return 0;
}