#include <bits/stdc++.h>
using namespace std;
void bfstraversal(int graph[5][5])
{
bool visited[5];
queue<int>q;
int source=4;
for(int i=0;i<5;i++)
{
visited[i]=false;
}
q.push(source);
visited[source]=true;
while(!q.empty())
{
int v= q.front();
q.pop();
printf("%d ",v);
for(int i=0;i<5;i++)
{
if(!visited[i] && graph[v][i])
{
q.push(i);
visited[i]=true;
}
}
}
}
int main()
{
int graph[5][5]={{0,1,0,0,1},
{0,0,1,0,0},
{0,1,0,1,1},
{0,0,1,0,1},
{1,0,0,1,0}};
bfstraversal(graph);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGJmc3RyYXZlcnNhbChpbnQgZ3JhcGhbNV1bNV0pCnsKCWJvb2wgdmlzaXRlZFs1XTsKICAgIHF1ZXVlPGludD5xOwogICAgaW50IHNvdXJjZT00OwogICAgZm9yKGludCBpPTA7aTw1O2krKykKCXsKCQl2aXNpdGVkW2ldPWZhbHNlOwoJfQogICAgcS5wdXNoKHNvdXJjZSk7CiAgICB2aXNpdGVkW3NvdXJjZV09dHJ1ZTsKICAgIHdoaWxlKCFxLmVtcHR5KCkpCiAgICB7CiAgICAJaW50IHY9IHEuZnJvbnQoKTsKICAgIAlxLnBvcCgpOwogICAgCXByaW50ZigiJWQgIix2KTsKICAgIAlmb3IoaW50IGk9MDtpPDU7aSsrKQogICAgCXsKICAgIAkJaWYoIXZpc2l0ZWRbaV0gJiYgZ3JhcGhbdl1baV0pCiAgICAJCXsKICAgIAkJCXEucHVzaChpKTsKICAgIAkJCXZpc2l0ZWRbaV09dHJ1ZTsKICAgIAkJfQogICAgCX0KICAgIH0KfQoKaW50IG1haW4oKSAKewogICAgaW50IGdyYXBoWzVdWzVdPXt7MCwxLDAsMCwxfSwKCSAgICAgICAgICAgICAgICB7MCwwLDEsMCwwfSwKCSAgICAgICAgICAgICAgICB7MCwxLDAsMSwxfSwKCSAgICAgICAgICAgICAgICB7MCwwLDEsMCwxfSwKCSAgICAgICAgICAgICAgICB7MSwwLDAsMSwwfX07CmJmc3RyYXZlcnNhbChncmFwaCk7CglyZXR1cm4gMDsKfQ==