mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
build
This commit is contained in:
@@ -16,7 +16,7 @@ $$
|
||||
|
||||
如果将顶点看作节点,将边看作连接各个节点的引用(指针),我们就可以将图看作是一种从链表拓展而来的数据结构。如图 9-1 所示,**相较于线性关系(链表)和分治关系(树),网络关系(图)的自由度更高**,从而更为复杂。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-1 链表、树、图之间的关系 </p>
|
||||
|
||||
@@ -27,7 +27,7 @@ $$
|
||||
- 在无向图中,边表示两顶点之间的“双向”连接关系,例如微信或 QQ 中的“好友关系”。
|
||||
- 在有向图中,边具有方向性,即 $A \rightarrow B$ 和 $A \leftarrow B$ 两个方向的边是相互独立的,例如微博或抖音上的“关注”与“被关注”关系。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-2 有向图与无向图 </p>
|
||||
|
||||
@@ -36,13 +36,13 @@ $$
|
||||
- 对于连通图,从某个顶点出发,可以到达其余任意顶点。
|
||||
- 对于非连通图,从某个顶点出发,至少有一个顶点无法到达。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-3 连通图与非连通图 </p>
|
||||
|
||||
我们还可以为边添加“权重”变量,从而得到图 9-4 所示的「有权图 weighted graph」。例如在王者荣耀等手游中,系统会根据共同游戏时间来计算玩家之间的“亲密度”,这种亲密度网络就可以用有权图来表示。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-4 有权图与无权图 </p>
|
||||
|
||||
@@ -62,7 +62,7 @@ $$
|
||||
|
||||
如图 9-5 所示,设邻接矩阵为 $M$、顶点列表为 $V$ ,那么矩阵元素 $M[i, j] = 1$ 表示顶点 $V[i]$ 到顶点 $V[j]$ 之间存在边,反之 $M[i, j] = 0$ 表示两顶点之间无边。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-5 图的邻接矩阵表示 </p>
|
||||
|
||||
@@ -78,7 +78,7 @@ $$
|
||||
|
||||
「邻接表 adjacency list」使用 $n$ 个链表来表示图,链表节点表示顶点。第 $i$ 条链表对应顶点 $i$ ,其中存储了该顶点的所有邻接顶点(即与该顶点相连的顶点)。图 9-6 展示了一个使用邻接表存储的图的示例。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-6 图的邻接表表示 </p>
|
||||
|
||||
|
||||
@@ -16,19 +16,19 @@ comments: true
|
||||
- **初始化**:传入 $n$ 个顶点,初始化长度为 $n$ 的顶点列表 `vertices` ,使用 $O(n)$ 时间;初始化 $n \times n$ 大小的邻接矩阵 `adjMat` ,使用 $O(n^2)$ 时间。
|
||||
|
||||
=== "初始化邻接矩阵"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "添加边"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "删除边"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "添加顶点"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "删除顶点"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-7 邻接矩阵的初始化、增删边、增删顶点 </p>
|
||||
|
||||
@@ -1056,19 +1056,19 @@ comments: true
|
||||
- **初始化**:在邻接表中创建 $n$ 个顶点和 $2m$ 条边,使用 $O(n + m)$ 时间。
|
||||
|
||||
=== "初始化邻接表"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "添加边"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "删除边"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "添加顶点"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "删除顶点"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-8 邻接表的初始化、增删边、增删顶点 </p>
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ comments: true
|
||||
|
||||
**广度优先遍历是一种由近及远的遍历方式,从某个节点出发,始终优先访问距离最近的顶点,并一层层向外扩张**。如图 9-9 所示,从左上角顶点出发,先遍历该顶点的所有邻接顶点,然后遍历下一个顶点的所有邻接顶点,以此类推,直至所有顶点访问完毕。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-9 图的广度优先遍历 </p>
|
||||
|
||||
@@ -421,37 +421,37 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
代码相对抽象,建议对照图 9-10 来加深理解。
|
||||
|
||||
=== "<1>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<2>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<3>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<4>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<5>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<6>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<7>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<8>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<9>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<10>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<11>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-10 图的广度优先遍历步骤 </p>
|
||||
|
||||
@@ -469,7 +469,7 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
|
||||
**深度优先遍历是一种优先走到底、无路可走再回头的遍历方式**。如图 9-11 所示,从左上角顶点出发,访问当前顶点的某个邻接顶点,直到走到尽头时返回,再继续走到尽头并返回,以此类推,直至所有顶点遍历完成。
|
||||
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-11 图的深度优先遍历 </p>
|
||||
|
||||
@@ -829,37 +829,37 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
为了加深理解,建议将图示与代码结合起来,在脑中(或者用笔画下来)模拟整个 DFS 过程,包括每个递归方法何时开启、何时返回。
|
||||
|
||||
=== "<1>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<2>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<3>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<4>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<5>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<6>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<7>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<8>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<9>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<10>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<11>"
|
||||

|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> 图 9-12 图的深度优先遍历步骤 </p>
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ icon: material/graphql
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||
{ class="cover-image" }
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user