-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbfs.c
123 lines (111 loc) · 2.17 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void enQueue(int vertex);
int deQueue();
int isEmptyQueue();
void BFS(int v);
void bfsTraversal();
int queue[MAX], front = -1, rear = -1;
void createGraph()
{
int count, max_edge, origin, destination;
printf("\nEnter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count = 1; count <= max_edge; count++)
{
printf("Enter edge %d : ",count);
scanf("%d %d",&origin,&destination);
if(origin == -1 && destination == -1)
{
break;
}
else if(origin >= n || destination >= n || origin < 0 || destination < 0)
{
printf("\nInvalid Edge !");
}
else
{
adj[origin][destination] = 1;
}
}
}
void enQueue(int vertex)
{
if(rear == MAX-1)
{
printf("\nOverflow");
return ;
}
else
{
if(front == -1)
{
front = 0;
}
rear = rear + 1;
queue[rear] = vertex;
}
}
int deQueue()
{
if(front == -1 || front > rear)
{
printf("\nUnderflow");
exit(1);
}
int delete_item = queue[front];
front = front + 1;
return delete_item;
}
int isEmptyQueue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
void bfsTraversal()
{
int v;
for(v = 0; v < n; v++)
{
state[v] = initial;
}
printf("\nEnter starting vertex for BFS : ");
scanf("%d",&v);
BFS(v);
}
void BFS(int v)
{
int i;
enQueue(v);
state[v] = waiting;
while(!isEmptyQueue())
{
v = deQueue();
printf("%d ",v);
state[v] = visited;
for (i = 0; i < n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
enQueue(i);
state[i] = waiting;
}
}
}
}
int main()
{
createGraph();
bfsTraversal();
return 0;
}