-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.c
62 lines (58 loc) · 1022 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
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
#define size 5
int visited[5]={0,0,0,0,0};
int a[5][5]={
{0,1,1,1,1},
{1,0,1,1,1},
{1,1,0,1,1},
{1,1,1,0,1},
{1,1,1,1,0}
};
typedef struct queue
{
int item[size];
int f,r;
} que;
que q;
void enqueue(que *q, int a)
{
q->item[q->r++]=a;
}
int dequeue(que *q)
{
return q->item[q->f++];
}
void bfs(int v)
{
int j,node;
printf("%d ",v);
visited[v]=1;
enqueue(&q,v);
while(q.f<=q.r)
{
for(j=0;j<5;j++)
{
node=dequeue(&q);
if(a[node][j]==1 && visited[j]==0)
{
enqueue(&q,j);
printf("%d ",j);
visited[j]=1;
}
}
}
}
int main()
{
int v;
q.f=0;
q.r=0;
printf("BFS traversal\nEnter the node to start from:\n");
scanf("%d",&v);
bfs(v);
// printf("printing the visited array to check whether all vertices are visited\n");
// for(int i=0;i<5;i++)
// printf("%d ",visited[i]);
return 0;
}