8. 图的表示
习题
#30012
⚡5⏳5
实现拥有邻接表的整数图,并提供广度优先遍历:
c
#include <stdbool.h>
#include <stddef.h>
typedef struct IntGraph IntGraph;
IntGraph *int_graph_create(size_t vertex_count, bool directed);
void int_graph_destroy(IntGraph *graph);
bool int_graph_add_edge(
IntGraph *graph,
size_t from,
size_t to
);
bool int_graph_bfs(
const IntGraph *graph,
size_t source,
size_t *out_order,
size_t capacity,
size_t *out_count
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
要求:
- 顶点编号为
0到vertex_count - 1;无向图加边时必须同时维护两个方向。 - 图拥有全部邻接结点;加边过程中分配失败时,图必须恢复到调用前的状态。
- 允许重复边,但不能因为重复边重复访问同一个顶点;邻接表按加边顺序保存。
bfs按邻接表顺序输出每个可达顶点一次;容量不足、参数无效时不得修改输出数组和*out_count。destroy(NULL)安全;时间复杂度为 ,并说明边和邻接结点的所有权边界。