This commit is contained in:
krahets
2023-11-09 05:13:48 +08:00
parent 9701430089
commit 0105644232
83 changed files with 516 additions and 509 deletions

View File

@@ -16,7 +16,7 @@ $$
如果将顶点看作节点,将边看作连接各个节点的引用(指针),我们就可以将图看作是一种从链表拓展而来的数据结构。如图 9-1 所示,**相较于线性关系(链表)和分治关系(树),网络关系(图)的自由度更高**,从而更为复杂。
![链表、树、图之间的关系](graph.assets/linkedlist_tree_graph.png)
![链表、树、图之间的关系](graph.assets/linkedlist_tree_graph.png){ class="animation-figure" }
<p align="center"> 图 9-1 &nbsp; 链表、树、图之间的关系 </p>
@@ -27,7 +27,7 @@ $$
- 在无向图中,边表示两顶点之间的“双向”连接关系,例如微信或 QQ 中的“好友关系”。
- 在有向图中,边具有方向性,即 $A \rightarrow B$ 和 $A \leftarrow B$ 两个方向的边是相互独立的,例如微博或抖音上的“关注”与“被关注”关系。
![有向图与无向图](graph.assets/directed_graph.png)
![有向图与无向图](graph.assets/directed_graph.png){ class="animation-figure" }
<p align="center"> 图 9-2 &nbsp; 有向图与无向图 </p>
@@ -36,13 +36,13 @@ $$
- 对于连通图,从某个顶点出发,可以到达其余任意顶点。
- 对于非连通图,从某个顶点出发,至少有一个顶点无法到达。
![连通图与非连通图](graph.assets/connected_graph.png)
![连通图与非连通图](graph.assets/connected_graph.png){ class="animation-figure" }
<p align="center"> 图 9-3 &nbsp; 连通图与非连通图 </p>
我们还可以为边添加“权重”变量,从而得到图 9-4 所示的「有权图 weighted graph」。例如在王者荣耀等手游中系统会根据共同游戏时间来计算玩家之间的“亲密度”这种亲密度网络就可以用有权图来表示。
![有权图与无权图](graph.assets/weighted_graph.png)
![有权图与无权图](graph.assets/weighted_graph.png){ class="animation-figure" }
<p align="center"> 图 9-4 &nbsp; 有权图与无权图 </p>
@@ -62,7 +62,7 @@ $$
如图 9-5 所示,设邻接矩阵为 $M$、顶点列表为 $V$ ,那么矩阵元素 $M[i, j] = 1$ 表示顶点 $V[i]$ 到顶点 $V[j]$ 之间存在边,反之 $M[i, j] = 0$ 表示两顶点之间无边。
![图的邻接矩阵表示](graph.assets/adjacency_matrix.png)
![图的邻接矩阵表示](graph.assets/adjacency_matrix.png){ class="animation-figure" }
<p align="center"> 图 9-5 &nbsp; 图的邻接矩阵表示 </p>
@@ -78,7 +78,7 @@ $$
「邻接表 adjacency list」使用 $n$ 个链表来表示图,链表节点表示顶点。第 $i$ 条链表对应顶点 $i$ ,其中存储了该顶点的所有邻接顶点(即与该顶点相连的顶点)。图 9-6 展示了一个使用邻接表存储的图的示例。
![图的邻接表表示](graph.assets/adjacency_list.png)
![图的邻接表表示](graph.assets/adjacency_list.png){ class="animation-figure" }
<p align="center"> 图 9-6 &nbsp; 图的邻接表表示 </p>

View File

@@ -16,19 +16,19 @@ comments: true
- **初始化**:传入 $n$ 个顶点,初始化长度为 $n$ 的顶点列表 `vertices` ,使用 $O(n)$ 时间;初始化 $n \times n$ 大小的邻接矩阵 `adjMat` ,使用 $O(n^2)$ 时间。
=== "初始化邻接矩阵"
![邻接矩阵的初始化、增删边、增删顶点](graph_operations.assets/adjacency_matrix_initialization.png)
![邻接矩阵的初始化、增删边、增删顶点](graph_operations.assets/adjacency_matrix_initialization.png){ class="animation-figure" }
=== "添加边"
![adjacency_matrix_add_edge](graph_operations.assets/adjacency_matrix_add_edge.png)
![adjacency_matrix_add_edge](graph_operations.assets/adjacency_matrix_add_edge.png){ class="animation-figure" }
=== "删除边"
![adjacency_matrix_remove_edge](graph_operations.assets/adjacency_matrix_remove_edge.png)
![adjacency_matrix_remove_edge](graph_operations.assets/adjacency_matrix_remove_edge.png){ class="animation-figure" }
=== "添加顶点"
![adjacency_matrix_add_vertex](graph_operations.assets/adjacency_matrix_add_vertex.png)
![adjacency_matrix_add_vertex](graph_operations.assets/adjacency_matrix_add_vertex.png){ class="animation-figure" }
=== "删除顶点"
![adjacency_matrix_remove_vertex](graph_operations.assets/adjacency_matrix_remove_vertex.png)
![adjacency_matrix_remove_vertex](graph_operations.assets/adjacency_matrix_remove_vertex.png){ class="animation-figure" }
<p align="center"> 图 9-7 &nbsp; 邻接矩阵的初始化、增删边、增删顶点 </p>
@@ -1056,19 +1056,19 @@ comments: true
- **初始化**:在邻接表中创建 $n$ 个顶点和 $2m$ 条边,使用 $O(n + m)$ 时间。
=== "初始化邻接表"
![邻接表的初始化、增删边、增删顶点](graph_operations.assets/adjacency_list_initialization.png)
![邻接表的初始化、增删边、增删顶点](graph_operations.assets/adjacency_list_initialization.png){ class="animation-figure" }
=== "添加边"
![adjacency_list_add_edge](graph_operations.assets/adjacency_list_add_edge.png)
![adjacency_list_add_edge](graph_operations.assets/adjacency_list_add_edge.png){ class="animation-figure" }
=== "删除边"
![adjacency_list_remove_edge](graph_operations.assets/adjacency_list_remove_edge.png)
![adjacency_list_remove_edge](graph_operations.assets/adjacency_list_remove_edge.png){ class="animation-figure" }
=== "添加顶点"
![adjacency_list_add_vertex](graph_operations.assets/adjacency_list_add_vertex.png)
![adjacency_list_add_vertex](graph_operations.assets/adjacency_list_add_vertex.png){ class="animation-figure" }
=== "删除顶点"
![adjacency_list_remove_vertex](graph_operations.assets/adjacency_list_remove_vertex.png)
![adjacency_list_remove_vertex](graph_operations.assets/adjacency_list_remove_vertex.png){ class="animation-figure" }
<p align="center"> 图 9-8 &nbsp; 邻接表的初始化、增删边、增删顶点 </p>

View File

@@ -12,7 +12,7 @@ comments: true
**广度优先遍历是一种由近及远的遍历方式,从某个节点出发,始终优先访问距离最近的顶点,并一层层向外扩张**。如图 9-9 所示,从左上角顶点出发,先遍历该顶点的所有邻接顶点,然后遍历下一个顶点的所有邻接顶点,以此类推,直至所有顶点访问完毕。
![图的广度优先遍历](graph_traversal.assets/graph_bfs.png)
![图的广度优先遍历](graph_traversal.assets/graph_bfs.png){ class="animation-figure" }
<p align="center"> 图 9-9 &nbsp; 图的广度优先遍历 </p>
@@ -421,37 +421,37 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
代码相对抽象,建议对照图 9-10 来加深理解。
=== "<1>"
![图的广度优先遍历步骤](graph_traversal.assets/graph_bfs_step1.png)
![图的广度优先遍历步骤](graph_traversal.assets/graph_bfs_step1.png){ class="animation-figure" }
=== "<2>"
![graph_bfs_step2](graph_traversal.assets/graph_bfs_step2.png)
![graph_bfs_step2](graph_traversal.assets/graph_bfs_step2.png){ class="animation-figure" }
=== "<3>"
![graph_bfs_step3](graph_traversal.assets/graph_bfs_step3.png)
![graph_bfs_step3](graph_traversal.assets/graph_bfs_step3.png){ class="animation-figure" }
=== "<4>"
![graph_bfs_step4](graph_traversal.assets/graph_bfs_step4.png)
![graph_bfs_step4](graph_traversal.assets/graph_bfs_step4.png){ class="animation-figure" }
=== "<5>"
![graph_bfs_step5](graph_traversal.assets/graph_bfs_step5.png)
![graph_bfs_step5](graph_traversal.assets/graph_bfs_step5.png){ class="animation-figure" }
=== "<6>"
![graph_bfs_step6](graph_traversal.assets/graph_bfs_step6.png)
![graph_bfs_step6](graph_traversal.assets/graph_bfs_step6.png){ class="animation-figure" }
=== "<7>"
![graph_bfs_step7](graph_traversal.assets/graph_bfs_step7.png)
![graph_bfs_step7](graph_traversal.assets/graph_bfs_step7.png){ class="animation-figure" }
=== "<8>"
![graph_bfs_step8](graph_traversal.assets/graph_bfs_step8.png)
![graph_bfs_step8](graph_traversal.assets/graph_bfs_step8.png){ class="animation-figure" }
=== "<9>"
![graph_bfs_step9](graph_traversal.assets/graph_bfs_step9.png)
![graph_bfs_step9](graph_traversal.assets/graph_bfs_step9.png){ class="animation-figure" }
=== "<10>"
![graph_bfs_step10](graph_traversal.assets/graph_bfs_step10.png)
![graph_bfs_step10](graph_traversal.assets/graph_bfs_step10.png){ class="animation-figure" }
=== "<11>"
![graph_bfs_step11](graph_traversal.assets/graph_bfs_step11.png)
![graph_bfs_step11](graph_traversal.assets/graph_bfs_step11.png){ class="animation-figure" }
<p align="center"> 图 9-10 &nbsp; 图的广度优先遍历步骤 </p>
@@ -469,7 +469,7 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
**深度优先遍历是一种优先走到底、无路可走再回头的遍历方式**。如图 9-11 所示,从左上角顶点出发,访问当前顶点的某个邻接顶点,直到走到尽头时返回,再继续走到尽头并返回,以此类推,直至所有顶点遍历完成。
![图的深度优先遍历](graph_traversal.assets/graph_dfs.png)
![图的深度优先遍历](graph_traversal.assets/graph_dfs.png){ class="animation-figure" }
<p align="center"> 图 9-11 &nbsp; 图的深度优先遍历 </p>
@@ -829,37 +829,37 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
为了加深理解,建议将图示与代码结合起来,在脑中(或者用笔画下来)模拟整个 DFS 过程,包括每个递归方法何时开启、何时返回。
=== "<1>"
![图的深度优先遍历步骤](graph_traversal.assets/graph_dfs_step1.png)
![图的深度优先遍历步骤](graph_traversal.assets/graph_dfs_step1.png){ class="animation-figure" }
=== "<2>"
![graph_dfs_step2](graph_traversal.assets/graph_dfs_step2.png)
![graph_dfs_step2](graph_traversal.assets/graph_dfs_step2.png){ class="animation-figure" }
=== "<3>"
![graph_dfs_step3](graph_traversal.assets/graph_dfs_step3.png)
![graph_dfs_step3](graph_traversal.assets/graph_dfs_step3.png){ class="animation-figure" }
=== "<4>"
![graph_dfs_step4](graph_traversal.assets/graph_dfs_step4.png)
![graph_dfs_step4](graph_traversal.assets/graph_dfs_step4.png){ class="animation-figure" }
=== "<5>"
![graph_dfs_step5](graph_traversal.assets/graph_dfs_step5.png)
![graph_dfs_step5](graph_traversal.assets/graph_dfs_step5.png){ class="animation-figure" }
=== "<6>"
![graph_dfs_step6](graph_traversal.assets/graph_dfs_step6.png)
![graph_dfs_step6](graph_traversal.assets/graph_dfs_step6.png){ class="animation-figure" }
=== "<7>"
![graph_dfs_step7](graph_traversal.assets/graph_dfs_step7.png)
![graph_dfs_step7](graph_traversal.assets/graph_dfs_step7.png){ class="animation-figure" }
=== "<8>"
![graph_dfs_step8](graph_traversal.assets/graph_dfs_step8.png)
![graph_dfs_step8](graph_traversal.assets/graph_dfs_step8.png){ class="animation-figure" }
=== "<9>"
![graph_dfs_step9](graph_traversal.assets/graph_dfs_step9.png)
![graph_dfs_step9](graph_traversal.assets/graph_dfs_step9.png){ class="animation-figure" }
=== "<10>"
![graph_dfs_step10](graph_traversal.assets/graph_dfs_step10.png)
![graph_dfs_step10](graph_traversal.assets/graph_dfs_step10.png){ class="animation-figure" }
=== "<11>"
![graph_dfs_step11](graph_traversal.assets/graph_dfs_step11.png)
![graph_dfs_step11](graph_traversal.assets/graph_dfs_step11.png){ class="animation-figure" }
<p align="center"> 图 9-12 &nbsp; 图的深度优先遍历步骤 </p>

View File

@@ -7,7 +7,7 @@ icon: material/graphql
<div class="center-table" markdown>
![](../assets/covers/chapter_graph.jpg){ width="600" }
![](../assets/covers/chapter_graph.jpg){ class="cover-image" }
</div>