2023-07-11 23:50:51 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: graph_bfs.c
|
|
|
|
|
* Created Time: 2023-07-11
|
|
|
|
|
* Author: NI-SW (947743645@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "graph_adjacency_list.c"
|
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
// 假设节点最大数量为 100
|
|
|
|
|
#define MAX_SIZE 100
|
2023-07-11 23:50:51 +08:00
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 节点队列结构体 */
|
2023-10-18 02:16:26 +08:00
|
|
|
|
typedef struct {
|
2023-10-29 19:52:19 +08:00
|
|
|
|
Vertex *vertices[MAX_SIZE];
|
|
|
|
|
int front, rear, size;
|
2023-10-18 02:16:26 +08:00
|
|
|
|
} Queue;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 构造函数 */
|
|
|
|
|
Queue *newQueue() {
|
2023-10-18 02:16:26 +08:00
|
|
|
|
Queue *q = (Queue *)malloc(sizeof(Queue));
|
2023-10-29 19:52:19 +08:00
|
|
|
|
q->front = q->rear = q->size = 0;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
int isEmpty(Queue *q) {
|
|
|
|
|
return q->size == 0;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 入队操作 */
|
|
|
|
|
void enqueue(Queue *q, Vertex *vet) {
|
|
|
|
|
q->vertices[q->rear] = vet;
|
|
|
|
|
q->rear = (q->rear + 1) % MAX_SIZE;
|
|
|
|
|
q->size++;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 出队操作 */
|
|
|
|
|
Vertex *dequeue(Queue *q) {
|
|
|
|
|
Vertex *vet = q->vertices[q->front];
|
|
|
|
|
q->front = (q->front + 1) % MAX_SIZE;
|
|
|
|
|
q->size--;
|
|
|
|
|
return vet;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
/* 检查顶点是否已被访问 */
|
|
|
|
|
int isVisited(Vertex **visited, int size, Vertex *vet) {
|
|
|
|
|
// 遍历查找节点,使用 O(n) 时间
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
if (visited[i] == vet)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 广度优先遍历 */
|
2023-07-20 19:08:23 +08:00
|
|
|
|
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
2023-10-29 19:52:19 +08:00
|
|
|
|
void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {
|
2023-07-20 19:08:23 +08:00
|
|
|
|
// 队列用于实现 BFS
|
2023-10-29 19:52:19 +08:00
|
|
|
|
Queue *queue = newQueue();
|
|
|
|
|
enqueue(queue, startVet);
|
|
|
|
|
visited[(*visitedSize)++] = startVet;
|
2023-07-20 19:08:23 +08:00
|
|
|
|
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
2023-10-29 19:52:19 +08:00
|
|
|
|
while (!isEmpty(queue)) {
|
|
|
|
|
Vertex *vet = dequeue(queue); // 队首顶点出队
|
|
|
|
|
res[(*resSize)++] = vet; // 记录访问顶点
|
|
|
|
|
// 遍历该顶点的所有邻接顶点
|
|
|
|
|
AdjListNode *node = findNode(graph, vet);
|
|
|
|
|
while (node != NULL) {
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 跳过已被访问的顶点
|
2023-10-29 19:52:19 +08:00
|
|
|
|
if (!isVisited(visited, *visitedSize, node->vertex)) {
|
|
|
|
|
enqueue(queue, node->vertex); // 只入队未访问的顶点
|
|
|
|
|
visited[(*visitedSize)++] = node->vertex; // 标记该顶点已被访问
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
2023-10-29 19:52:19 +08:00
|
|
|
|
node = node->next;
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-20 19:08:23 +08:00
|
|
|
|
// 释放内存
|
2023-10-29 19:52:19 +08:00
|
|
|
|
free(queue);
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 19:08:23 +08:00
|
|
|
|
/* Driver Code */
|
2023-07-11 23:50:51 +08:00
|
|
|
|
int main() {
|
2023-10-29 19:52:19 +08:00
|
|
|
|
// 初始化无向图
|
|
|
|
|
int vals[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|
|
|
|
int size = sizeof(vals) / sizeof(vals[0]);
|
|
|
|
|
Vertex **v = valsToVets(vals, size);
|
|
|
|
|
Vertex *edges[][2] = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[1], v[4]}, {v[2], v[5]}, {v[3], v[4]},
|
|
|
|
|
{v[3], v[6]}, {v[4], v[5]}, {v[4], v[7]}, {v[5], v[8]}, {v[6], v[7]}, {v[7], v[8]}};
|
|
|
|
|
int egdeSize = sizeof(edges) / sizeof(edges[0]);
|
|
|
|
|
GraphAdjList *graph = newGraphAdjList();
|
|
|
|
|
// 添加所有顶点和边
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
addVertex(graph, v[i]);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < egdeSize; i++) {
|
|
|
|
|
addEdge(graph, edges[i][0], edges[i][1]);
|
2023-07-11 23:50:51 +08:00
|
|
|
|
}
|
2023-10-29 19:52:19 +08:00
|
|
|
|
printf("\n初始化后,图为\n");
|
2023-07-11 23:50:51 +08:00
|
|
|
|
printGraph(graph);
|
2023-07-20 19:08:23 +08:00
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
// 广度优先遍历
|
2023-10-29 19:52:19 +08:00
|
|
|
|
// 顶点遍历序列
|
|
|
|
|
Vertex *res[MAX_SIZE];
|
|
|
|
|
int resSize = 0;
|
|
|
|
|
// 用于记录已被访问过的顶点
|
|
|
|
|
Vertex *visited[MAX_SIZE];
|
|
|
|
|
int visitedSize = 0;
|
|
|
|
|
graphBFS(graph, v[0], res, &resSize, visited, &visitedSize);
|
|
|
|
|
printf("\n广度优先遍历(BFS)顶点序列为\n");
|
|
|
|
|
printArray(vetsToVals(res, resSize), resSize);
|
2023-07-11 23:50:51 +08:00
|
|
|
|
|
2023-10-29 19:52:19 +08:00
|
|
|
|
// 释放内存
|
|
|
|
|
delGraphAdjList(graph);
|
|
|
|
|
free(v);
|
2023-07-11 23:50:51 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|