-
Notifications
You must be signed in to change notification settings - Fork 2
/
BFS.c
65 lines (63 loc) · 1009 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
63
64
65
/*
Program for implementing Breadth First Search (BFS) algorithm
Author: Vivek K J
S3 CSA
*/
#include<stdio.h>
int n, mat[20][20], visited[20], queue[20], REAR=-1, FRONT=-1;
void readAdjacencyMatrix(){
int i, j;
printf("Enter no. of vertices");
scanf("%d", &n);
printf("Enter the Adjacency Matrix: \n");
for(i=0; i<n; i++){
for(j=0; j<n; j++){
scanf("%d", &mat[i][j]);
}
}
}
// Function to Enqueue
void enqueue(int item){
if(REAR==-1 && FRONT==-1){
REAR=0;
FRONT=0;
}
else{
REAR=REAR+1;
}
queue[REAR]=item;
}
// Function to Dequeue
int dequeue(){
int item;
if(REAR==FRONT){
item = queue[FRONT];
FRONT=-1;
REAR=-1;
}else{
item = queue[FRONT];
FRONT+= 1;
}
return item;
}
void bfs(int v){
int i;
printf("%d \t", v);
visited[v] = 1;
enqueue(v);
while(FRONT != -1){
v = dequeue();
for(i=1; i<=n; i++){
if(mat[v][i] && !visited[i]){
visited[i]=1;
enqueue(i);
printf("%d \t", i);
}
}
}
}
void main(){
int i;
readAdjacencyMatrix();
bfs(0);
}