-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathque14.c
111 lines (94 loc) · 2.77 KB
/
que14.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Queue {
int *arr;
int front, rear;
int size;
};
struct Graph {
int V;
int **adjMatrix;
};
struct Queue* createQueue(int size);
void enqueue(struct Queue* queue, int value);
int dequeue(struct Queue* queue);
bool isQueueEmpty(struct Queue* queue);
void BFS(struct Graph* graph, int startVertex, bool *visited);
struct Graph* createGraph(int V);
int main() {
int V, E, u, v;
printf("Enter the number of vertices: ");
scanf("%d", &V);
struct Graph* graph = createGraph(V);
printf("Enter the number of edges: ");
scanf("%d", &E);
for (int i = 0; i < E; i++) {
printf("Enter edge (u v): ");
scanf("%d %d", &u, &v);
graph->adjMatrix[u][v] = 1;
graph->adjMatrix[v][u] = 1; // For undirected graph
}
printf("BFS Traversal starting from vertex 0:\n");
bool *visited = (bool*)calloc(V, sizeof(bool));
BFS(graph, 0, visited);
printf("\n");
return 0;
}
struct Queue* createQueue(int size) {
struct Queue* queue = (struct Queue*)calloc(1, sizeof(struct Queue));
queue->arr = (int*)calloc(size, sizeof(int));
queue->front = queue->rear = -1;
queue->size = size;
return queue;
}
void enqueue(struct Queue* queue, int value) {
if (queue->rear == queue->size - 1) {
printf("Queue is full\n");
return;
}
if (queue->front == -1) {
queue->front = 0;
}
queue->arr[++queue->rear] = value;
}
int dequeue(struct Queue* queue) {
if (queue->front == -1) {
printf("Queue is empty\n");
return -1;
}
int item = queue->arr[queue->front];
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front++;
}
return item;
}
bool isQueueEmpty(struct Queue* queue) {
return queue->front == -1;
}
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)calloc(1, sizeof(struct Graph));
graph->V = V;
graph->adjMatrix = (int**)calloc(V, sizeof(int*));
for (int i = 0; i < V; i++) {
graph->adjMatrix[i] = (int*)calloc(V, sizeof(int));
}
return graph;
}
void BFS(struct Graph* graph, int startVertex, bool *visited) {
struct Queue* queue = createQueue(graph->V);
visited[startVertex] = true;
enqueue(queue, startVertex);
while (!isQueueEmpty(queue)) {
int currentVertex = dequeue(queue);
printf("%d ", currentVertex);
for (int i = 0; i < graph->V; i++) {
if (graph->adjMatrix[currentVertex][i] == 1 && !visited[i]) {
visited[i] = true;
enqueue(queue, i);
}
}
}
}