-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.c
95 lines (87 loc) · 1.65 KB
/
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
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
#include<stdio.h>
int adj[10][10];
int queue[30];
int front =0;
int rear=0;
int color[10];
int parent[10];
int breadth[10];
int v,e;
void enqueue(int x) // Function to enqueue elements
{
queue[rear]=x;
rear++;
}
int dequeue() // Function to dequeue the elements
{
int x;
x=queue[front];
front++;
return x;
}
void initGraph() // Function to initialize the graph
{
int i,j;
int s,d;
printf("Enter the number of vertices:");
scanf("%d", &v);
printf("\nEnter the number of edges:");
scanf("%d", &e);
for (i=0; i<v; i++)
{
for (j=0; j<v; j++)
{
adj[i][j]=0;
}
}
for(i=0; i<e; i++)
{
printf("\nEnter the source:");
scanf("%d", &s);
printf("\nEnter the destination:");
scanf("%d", &d);
adj[s][d] = adj[d][s] = 1;
}
for (i=0; i<v; i++)
{
color[i]=0;
parent[i]=-1;
breadth[i]=0;
}
}
void BFS(int start) // Function to start Breadth first search
{
int x,j;
enqueue(start);
color[start]=1;
while(front!=rear)
{
x=queue[front];
for(j=0; j<v; j++)
{
if(adj[x][j] == 1)
{
if (color[j] == 0)
{
enqueue(j);
color[j] = 1;
parent[j] = x;
breadth[j]=breadth[x] + 1;
}
}
}
color[x]=2;
dequeue();
}
}
void main() // Driver function
{
int startingNode=1;
int i;
initGraph();
BFS(startingNode);
for(i=0; i<v; i++)
{
printf("%d ",breadth[i]);
}
}