-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbfs.c
More file actions
321 lines (257 loc) · 7.39 KB
/
bfs.c
File metadata and controls
321 lines (257 loc) · 7.39 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* 广度优先搜索(BFS)实现 (C语言)
*
* 特点:
* - 逐层探索,由近到远
* - 使用队列(FIFO)
* - 找到的第一条路径就是最短路径
* - 时间复杂度:O(V + E)
* - 空间复杂度:O(V)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_VERTICES 100
#define MAX_QUEUE_SIZE 100
// 队列结构
typedef struct {
int items[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
// 图结构(邻接表)
typedef struct Node {
int vertex;
struct Node* next;
} Node;
typedef struct {
Node* adjLists[MAX_VERTICES];
int numVertices;
} Graph;
// 初始化队列
void initQueue(Queue* q) {
q->front = -1;
q->rear = -1;
}
// 检查队列是否为空
bool isEmpty(Queue* q) {
return q->rear == -1;
}
// 入队
void enqueue(Queue* q, int value) {
if (q->rear == MAX_QUEUE_SIZE - 1) {
printf("队列已满\n");
return;
}
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->items[q->rear] = value;
}
// 出队
int dequeue(Queue* q) {
int item;
if (isEmpty(q)) {
printf("队列为空\n");
return -1;
}
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
return item;
}
// 创建新节点
Node* createNode(int v) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// 初始化图
void initGraph(Graph* graph, int vertices) {
graph->numVertices = vertices;
for (int i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
}
}
// 添加边(无向图)
void addEdge(Graph* graph, int src, int dest) {
// 添加 src -> dest 的边
Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// 添加 dest -> src 的边(无向图)
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
// BFS遍历
void bfs(Graph* graph, int startVertex) {
bool visited[MAX_VERTICES] = {false};
Queue q;
initQueue(&q);
// 标记起始顶点为已访问并入队
visited[startVertex] = true;
enqueue(&q, startVertex);
printf("BFS遍历结果: ");
while (!isEmpty(&q)) {
// 出队一个顶点
int currentVertex = dequeue(&q);
printf("%d ", currentVertex);
// 访问所有邻接顶点
Node* temp = graph->adjLists[currentVertex];
while (temp != NULL) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
enqueue(&q, adjVertex);
}
temp = temp->next;
}
}
printf("\n");
}
// BFS查找最短路径
void bfsShortestPath(Graph* graph, int startVertex, int endVertex) {
bool visited[MAX_VERTICES] = {false};
int distance[MAX_VERTICES] = {0};
int parent[MAX_VERTICES];
Queue q;
initQueue(&q);
// 初始化
for (int i = 0; i < graph->numVertices; i++) {
parent[i] = -1;
}
// 标记起始顶点为已访问并入队
visited[startVertex] = true;
enqueue(&q, startVertex);
while (!isEmpty(&q)) {
int currentVertex = dequeue(&q);
// 如果找到目标顶点,停止搜索
if (currentVertex == endVertex) {
break;
}
// 访问所有邻接顶点
Node* temp = graph->adjLists[currentVertex];
while (temp != NULL) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
distance[adjVertex] = distance[currentVertex] + 1;
parent[adjVertex] = currentVertex;
enqueue(&q, adjVertex);
}
temp = temp->next;
}
}
// 输出最短路径
if (visited[endVertex]) {
printf("从 %d 到 %d 的最短路径长度: %d\n", startVertex, endVertex, distance[endVertex]);
// 重建路径
int path[MAX_VERTICES];
int pathLength = 0;
int current = endVertex;
while (current != -1) {
path[pathLength++] = current;
current = parent[current];
}
printf("路径: ");
for (int i = pathLength - 1; i >= 0; i--) {
printf("%d", path[i]);
if (i > 0) printf(" -> ");
}
printf("\n");
} else {
printf("从 %d 到 %d 没有路径\n", startVertex, endVertex);
}
}
// BFS计算连通分量
void bfsConnectedComponents(Graph* graph) {
bool visited[MAX_VERTICES] = {false};
int componentCount = 0;
printf("连通分量:\n");
for (int i = 0; i < graph->numVertices; i++) {
if (!visited[i]) {
componentCount++;
printf("分量 %d: ", componentCount);
Queue q;
initQueue(&q);
visited[i] = true;
enqueue(&q, i);
while (!isEmpty(&q)) {
int currentVertex = dequeue(&q);
printf("%d ", currentVertex);
Node* temp = graph->adjLists[currentVertex];
while (temp != NULL) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
enqueue(&q, adjVertex);
}
temp = temp->next;
}
}
printf("\n");
}
}
printf("总连通分量数: %d\n", componentCount);
}
// 释放图内存
void freeGraph(Graph* graph) {
for (int i = 0; i < graph->numVertices; i++) {
Node* temp = graph->adjLists[i];
while (temp != NULL) {
Node* toDelete = temp;
temp = temp->next;
free(toDelete);
}
}
}
int main() {
printf("=== 广度优先搜索(BFS)演示 ===\n\n");
// 创建图
Graph graph;
int vertices = 8;
initGraph(&graph, vertices);
// 添加边
addEdge(&graph, 0, 1);
addEdge(&graph, 0, 2);
addEdge(&graph, 1, 3);
addEdge(&graph, 1, 4);
addEdge(&graph, 2, 5);
addEdge(&graph, 2, 6);
addEdge(&graph, 3, 7);
addEdge(&graph, 4, 7);
addEdge(&graph, 5, 6);
printf("图结构:\n");
printf("0 -- 1 -- 3 -- 7\n");
printf("| | |\n");
printf("| | |\n");
printf("2 -- 4 --------|\n");
printf("| |\n");
printf("| |\n");
printf("5 -- 6\n\n");
// 1. 基本BFS遍历
printf("1. 基本BFS遍历(从顶点0开始)\n");
bfs(&graph, 0);
printf("\n");
// 2. BFS查找最短路径
printf("2. BFS查找最短路径\n");
bfsShortestPath(&graph, 0, 7);
printf("\n");
// 3. BFS计算连通分量
printf("3. BFS计算连通分量\n");
bfsConnectedComponents(&graph);
printf("\n");
// 4. 从不同起点开始BFS
printf("4. 从不同起点开始BFS\n");
printf("从顶点3开始: ");
bfs(&graph, 3);
// 释放内存
freeGraph(&graph);
return 0;
}