-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.c
106 lines (97 loc) · 1.47 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
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXLIMIT 100
struct queue
{
int front, rear;
int data[MAXLIMIT];
};
void create(struct queue *q)
{
q->front=-1;
q->rear=-1;
}
bool isEmpty(struct queue *q)
{
if (q->front==-1)
return true;
else
return false;
}
void enqueue(struct queue *q,int d)
{
if (isEmpty(q))
{
q->front++;
q->rear++;
q->data[q->rear]=d;
}
else
{
if (q->rear==MAXLIMIT-1)
return;
else
{
q->rear++;
q->data[q->rear]=d;
}
}
}
int dequeue(struct queue *q)
{
int temp = q->data[q->front];
if(q->front==-1)
return 0;
if (q->front == q->rear)
{
q->front=q->rear=-1;
}
else
{
q->front++;
}
return temp;
}
void creategraph(int adjmat[][50])
{
int i,x,y;
while(1)
{
scanf("%d %d",&x,&y);
if(x==-1 && y==-1)
break;
else
adjmat[x][y]=1;
}
}
void BFS(int adjmat[][50],int n,int svertex)
{
int v,i,visited[50]={0};
struct queue q;
create(&q);
enqueue(&q,svertex);
visited[svertex]=1;
while(!isEmpty(&q))
{
v=dequeue(&q);
printf("%d ",v);
for(i=1;i<=n;i++)
if(adjmat[v][i]==1 && visited[i]==0)
{
enqueue(&q,i);
visited[i]=1;
}
}
}
void main()
{
int adjmat[50][50]={0};
int n,svertex;
printf("\nEnter the number of vertices in the graph\n");
scanf("%d",&n);
creategraph(adjmat);
printf("Enter the starting vertex\n");
scanf("%d",&svertex);
BFS(adjmat,n,svertex);
}