-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadth_first_search.c
98 lines (65 loc) · 1.23 KB
/
breadth_first_search.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
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 9
int queue[9], visited[9];
int front=0, rear=-1, count=0;
bool isEmpty();
bool isFull();
void enqueue(int data);
int dequeue();
void main()
{
int graph[9][9]={{0,1,1,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0},
{1,0,0,1,0,0,1,0,0},
{0,0,1,0,1,1,0,0,1},
{0,0,0,1,0,0,0,0,0},
{0,0,0,1,0,0,1,0,0},
{0,0,1,0,0,1,0,1,0},
{0,0,0,0,0,0,1,0,1},
{0,0,0,1,0,0,0,1,0}};
int vertex=0,i,j;
for(j=0;j<9;j++){visited[j]=0;}
printf("%d ",vertex);
visited[vertex]=1;
for(j=0;j<9;j++)
{
//explore
for(i=0;i<9;i++)
{
if(graph[vertex][i]!=0)
{
if(visited[i]!=1){
enqueue(i);
printf("%d ",i);
visited[i]=1;
}
}
}
vertex=dequeue();
}
}
bool isEmpty() {
return count == 0;
}
bool isFull() {
return count == MAX;
}
void enqueue(int data) {
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
queue[++rear] = data;
count++;
}
}
int dequeue() {
int data = queue[front++];
if(front == MAX) {
front = 0;
}
count--;
return data;
}