-
Notifications
You must be signed in to change notification settings - Fork 1
/
bfs.c
56 lines (47 loc) · 862 Bytes
/
bfs.c
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
/*
Written By Ashwin Raghav
Twitter @ashwinraghav
blog.ashwinraghav.com
github.com/ashwinraghav
*/
#include <stdio.h>
#define N 10
void bfs(int adj[][N],int visited[],int start)
{
int q[N],rear=-1,front=-1,i;
q[++rear]=start;
visited[start]=1;
while(rear != front)
{
start = q[++front];
if(start==9)
printf("10\t");
else
printf("%c \t",start+49); //change to 65 in case of alphabets
for(i=0;i<N;i++)
{
if(adj[start][i] && !visited[i])
{
q[++rear]=i;
visited[i]=1;
}
}
}
}
int main()
{
int visited[N]={0};
int adj[N][N]={
{0,1,1,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,1,0,0,0},
{1,0,1,0,0,1,1,0,0,1},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,1,1},
{0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,1,0}};
bfs(adj,visited,0);
return 0;
}