-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs_graph.c
68 lines (64 loc) · 819 Bytes
/
bfs_graph.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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdio.h"
void bfs_traversal(int [][6],int );
void main()
{
int a[6][6]={ //graph
1,1,1,0,1,0,
0,1,0,1,1,1,
0,0,1,1,1,0,
0,0,0,1,1,0,
0,0,0,0,0,1
};
int n;
printf("enter source: ");
scanf("%d",&n);
bfs_traversal(a,n);
}
void bfs_traversal(int a[][6],int n)
{
int inp[6];
int out[6];
int queue[6];
int i,r=0,f=-1,j,k=0,l=0,m,flag=0;
queue[r]=n;
f=r;
r++;
inp[l]=n;
l++;
while(f!=r)
{
out[k]=queue[f];
k++;
f++;
if(f==r)
f=r=0;
n=queue[f];
for(j=0;j<6;j++)
{
if(a[n][j]==1)
{
for(m=0;m<l;m++)
{
if(j==inp[m])
{
flag=1;
break;
}
}
if(flag==0)
{
inp[l]=j;
l++;
queue[r]=j;
r++;
}
flag=0;
}
}
}
for(i=0;i<k;i++)
{
printf("%c",out[i]+65);
}
printf("\n");
}