hello-algo/codes/c/chapter_graph/graph_bfs.c

117 lines
3.3 KiB
C
Raw Normal View History

/**
* File: graph_bfs.c
* Created Time: 2023-07-11
* Author: NI-SW (947743645@qq.com)
*/
#include "graph_adjacency_list.c"
// 假设节点最大数量为 100
#define MAX_SIZE 100
/* 节点队列结构体 */
typedef struct {
Vertex *vertices[MAX_SIZE];
int front, rear, size;
} Queue;
/* 构造函数 */
Queue *newQueue() {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = q->rear = q->size = 0;
return q;
}
/* 判断队列是否为空 */
int isEmpty(Queue *q) {
return q->size == 0;
}
/* 入队操作 */
void enqueue(Queue *q, Vertex *vet) {
q->vertices[q->rear] = vet;
q->rear = (q->rear + 1) % MAX_SIZE;
q->size++;
}
/* 出队操作 */
Vertex *dequeue(Queue *q) {
Vertex *vet = q->vertices[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return vet;
}
/* 检查顶点是否已被访问 */
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;
}
/* 广度优先遍历 BFS */
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
// 队列用于实现 BFS
Queue *queue = newQueue();
enqueue(queue, startVet);
visited[(*visitedSize)++] = startVet;
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (!isEmpty(queue)) {
Vertex *vet = dequeue(queue); // 队首顶点出队
res[(*resSize)++] = vet; // 记录访问顶点
// 遍历该顶点的所有邻接顶点
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// 跳过已被访问过的顶点
if (!isVisited(visited, *visitedSize, node->vertex)) {
enqueue(queue, node->vertex); // 只入队未访问的顶点
visited[(*visitedSize)++] = node->vertex; // 标记该顶点已被访问
}
node = node->next;
}
}
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
// 释放内存
free(queue);
}
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
/* Driver Code */
int main() {
// 初始化无向图
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]);
}
printf("\n初始化后,图为\n");
printGraph(graph);
add c code for graph operation (#601) * Create chapter_graph * Delete chapter_graph * add C code for graph * add C code for graph * Create graph_adjacency_list.c add C code for graph * Update CMakeLists.txt * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update format and output * Update graph_adjacency_list.c * Update CMakeLists.txt for c code of graph * Update format of c code * Update format of c code * Update format of c code * Update verticesList Change the data structure of the storage list from a linked list to a linear table * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * Create graph_adjacency_list_test.c * Create graph_bfs * Update CMakeLists.txt * Update graph_adjacency_list.c * mv graph_bfs to graph_bfs.c * Update graph_bfs.c * Delete graph_bfs * Update graph_adjacency_list.c * update c code for graph operation. * Update CMakeLists.txt * Update graph_dfs.c * Update graph_dfs.c * Update CMakeLists.txt * Update graph_dfs.c * Update note of graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update output "初始化后,图为:" of graph_dfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_dfs.c * Update name of arrayVertex * Update name of arrayVertex * Update note of graph_dfs.c * Update note of graph_bfs.c * Update graph_dfs.c * Update graph_bfs.c * Update graph_adjacency_matrix.c * Update graph_adjacency_list_test.c * Update graph_adjacency_list.c * Update graph_dfs.c * Update graph_bfs.c * Update comment * Update comment * Update graph_adjacency_list.c * Update graph_adjacency_matrix.c * update comment * update comment * update comment for graph operation * update comment of graph operation * update comment * update comment --------- Co-authored-by: Yudong Jin <krahets@163.com> Co-authored-by: libr <libr@info2soft.com>
2023-07-20 19:08:23 +08:00
// 广度优先遍历 BFS
// 顶点遍历序列
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);
// 释放内存
delGraphAdjList(graph);
free(v);
return 0;
}