-
Notifications
You must be signed in to change notification settings - Fork 1
/
Breadth_first_search_using_array.c
86 lines (86 loc) · 1.79 KB
/
Breadth_first_search_using_array.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int size;
int front, rear;
int *A;
};
void create(struct queue *q)
{
(*(q)).size = 100;
(*(q)).front = (*(q)).rear = (-1);
(*(q)).A = (int *)malloc((*(q)).size * sizeof(int));
}
void enqueue(struct queue *q, int num)
{
if ((*(q)).rear == (*(q)).size - 1)
{
printf("Queue is full!");
}
else
{
(*(q)).rear++;
(*(q)).A[(*(q)).rear] = num;
}
}
int dequeue(struct queue *q)
{
int x = (-1);
if ((*(q)).front == (*(q)).rear)
{
return x;
}
else
{
(*(q)).front++; // at front no value is stored.
x = (*(q)).A[(*(q)).front];
return x;
}
}
int isempty(struct queue *q)
{
if ((*(q)).front == (*(q)).rear)
{
return 1;
}
else
{
return 0;
}
}
void BFS(struct queue *q, int A[][8], int head, int n)
{
int visited[8] = {0};
printf("%d ", head);
visited[head] = 1;
enqueue(q, head);
int u, v;
while (isempty(q) != 1)
{
u = dequeue(q);
for (v = 1; v < n; v++)
{
if (A[u][v] == 1 && visited[v] == 0)
{
printf("%d ", v);
visited[v] = 1;
enqueue(q, v);
}
}
}
}
int main()
{
struct queue q;
create(&q);
int M[8][8] = {{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1, 0, 0},
{0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0}};
BFS(&q, M, 6, 8);
}