forked from csfx-py/hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.c
48 lines (43 loc) · 900 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
#include<stdio.h>
#include<time.h>
void bfs(int a[10][10],int n, int source)
{
int s[10],q[10],f=0,r=-1,t,v;
for(v=0;v<n;v++)
{
s[v]=0;
}
q[++r]= source;
s[source]=1;
while(f<=r)
{
t=q[f++];
for(v=0; v<n; v++)
if(a[t][v]==1 && s[v]==0)
{
q[++r]=v;
printf("The BFS traversal is:\n %d %d", t, v);
printf(“%d is reachable from source %d \n”, v, source);
s[v]=1;
}
}
}
main()
{
int a[10][10],n,i,j,s;
double clk;
clock_t starttime,endtime;
printf("\n Enter the number of vertices\n");
scanf("%d",&n);
printf("\nEnter the matrix represention:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source vertix");
scanf("%d",&s);
starttime=clock();
bfs(a,n,s);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\nThe run time is %f\n",clk);
}