This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07-bfs_search.c
202 lines (170 loc) · 3.83 KB
/
07-bfs_search.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NODES 50000 // Adjust as needed
/**
* Node structure for adjacency list
*/
typedef struct Node {
int vertex;
struct Node* next;
} Node;
/**
* Graph structure
*/
typedef struct Graph {
int numVertices;
Node** adjLists;
int* visited;
} Graph;
/**
* Queue structure for BFS
*/
typedef struct Queue {
int items[MAX_NODES];
int front;
int rear;
} Queue;
/**
* Function to create a node
*/
Node* createNode(int v) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
/**
* Function to create a graph
*/
Graph* createGraph(int vertices) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numVertices = vertices;
graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));
graph->visited = (int*)malloc(vertices * sizeof(int));
for (int i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
/**
* Function to add edge to an undirected graph
*/
void addEdge(Graph* graph, int src, int dest) {
// Add edge from src to dest
Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Since the graph is undirected, add edge from dest to src also
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
/**
* Function to create a queue
*/
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
}
/**
* Check if the queue is empty
*/
int isEmpty(Queue* q) { return q->rear == -1; }
/**
* Enqueue function
*/
void enqueue(Queue* q, int value) {
if (q->rear == MAX_NODES - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1) q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
/**
* Dequeue function
*/
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
// Reset the queue
q->front = q->rear = -1;
}
}
return item;
}
/**
* BFS algorithm
*/
void bfs(Graph* graph, int startVertex) {
Queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
int currentVertex = dequeue(q);
Node* temp = graph->adjLists[currentVertex];
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
free(q);
}
/**
* Main function to demonstrate BFS and measure execution time.
*/
int main() {
int N_values[] = {1000, 5000, 10000, 20000, 50000};
int num_tests = sizeof(N_values) / sizeof(N_values[0]);
for (int t = 0; t < num_tests; t++) {
int N = N_values[t];
if (N > MAX_NODES) {
printf("N exceeds MAX_NODES limit.\n");
continue;
}
Graph* graph = createGraph(N);
// Build a connected graph (e.g., a chain)
for (int i = 0; i < N - 1; i++) {
addEdge(graph, i, i + 1);
}
// Measure start time
clock_t start_time = clock();
// Perform BFS starting from vertex 0
bfs(graph, 0);
// Measure end time
clock_t end_time = clock();
// Calculate time taken
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;
// Print the result
printf("BFS with N = %d\n", N);
printf("Time taken: %f seconds\n\n", time_spent);
// Free memory
for (int i = 0; i < N; i++) {
Node* temp = graph->adjLists[i];
while (temp) {
Node* toFree = temp;
temp = temp->next;
free(toFree);
}
}
free(graph->adjLists);
free(graph->visited);
free(graph);
}
return 0;
}