完成图

This commit is contained in:
lifei
2020-12-16 17:48:55 +08:00
parent 4408063965
commit 283013d06e
125 changed files with 487 additions and 4 deletions

View File

@@ -248,9 +248,6 @@ $$
- [表达式求值](ch3/stack-applications/README.md#2-表达式求值)
- [递归](ch3/stack-applications/README.md#3-递归)
- [队列的应用](ch3/README.md#5-队列的应用)
- 树的层次遍历
- 图的广度优先遍历
- 操作系统资源分配
- [矩阵的压缩存储](ch3/matrix/README.md#矩阵的压缩存储)
- [对称矩阵](ch3/matrix/README.md#3-对称矩阵)
- [三角矩阵](ch3/matrix/README.md#4-三角矩阵)
@@ -292,3 +289,29 @@ $$
- [树和森林的遍历](ch5/tree-traversal/README.md#树与森林)
- [树和森林及二叉树的转换](ch5/tree-traversal/README.md#树与森林)
- [树的应用:查并集](ch5/tree-applications/README.md#1-并查集)
## 7. [图](ch6/README.md#图)
- [逻辑结构](ch6/README.md#图)
- [图的定义](ch6/README.md#图)
- [相关概念](ch6/concept/README.md#图的基本概念)
- [无向图 & 有向图](ch6/concept/README.md#1-无向图-&-有向图)
- [完全图](ch6/concept/README.md#3-完全图)
- [子图](ch6/concept/README.md#4-子图)
- [连通分量 & 强连通分量](ch6/concept/README.md#7-连通分量-&-强连通分量)
- [带权图(网)](ch6/concept/README.md#11-网)
- [存储结构](ch6/storage/README.md#图的存储结构)
- [邻接矩阵](ch6/storage/README.md#1-邻接矩阵法)
- [邻接表](ch6/storage/README.md#2-邻接表法)
- [十字链表](ch6/storage/README.md#3-十字链表)
- [邻接多重表](ch6/storage/README.md#4-邻接多重表)
- [基本操作](ch6/operation/README.md#图的基本操作)
- [图的遍历](ch6/traversal/README.md#图的遍历)
- [广度优先搜索](ch6/traversal/README.md#1-广度优先搜索)
- [深度优先搜索](ch6/traversal/README.md#2-深度优先搜索)
- [遍历与连通性](ch6/traversal/README.md#3-遍历与连通性问题)
- [图的应用](ch6/applications/README.md#图的应用)
- [最小生成树](ch6/applications/README.md#1-最小生成树)
- [最短路径](ch6/applications/README.md#2-最短路径)
- [拓扑排序](ch6/applications/README.md#3-拓扑排序)
- [关键路径](ch6/applications/README.md#4-关键路径)

View File

@@ -4,7 +4,7 @@
一种简单的集合表示。
通常用的双亲表示法作为并查集的存储结构。
通常用的双亲表示法作为并查集的存储结构。
通常用数组元素的下标代表元素名,用根结点的下标代表子集合名,根结点的双亲结点为负数。

78
ch6/README.md Normal file
View File

@@ -0,0 +1,78 @@
# 图
## 1. [基本概念](concept/README.md#图的基本概念)
图 $G$ 是由顶点集 $V$ 和边集 $E$ 组成,记为:
$$
G=(V,E)
$$
其中,
- $V(G)$ 表示图 $G$ 中顶点的有限非空集;
- $E(G)$ 表示图 $G$ 中顶点之间的关系(边)的集合。
$\left \vert V \right \vert$ 表示图 $G$ 中顶点的个数,也称图 $G$ 的阶。
$\left \vert E \right \vert$ 表示图 $G$ 中边的条数。
![](graph.png)
$$
V=\{A,B,C,D,E\},\left \vert V \right \vert=5
$$
$$
E=\{(A,B),(A,C),(A,E),(B,C),(C,D),(C,E)\},\left \vert E \right \vert=6
$$
线性表、树都可以为空,但图不能为空。
- [无向图 & 有向图](concept/README.md#1-无向图-&-有向图)
- [简单图 & 多重图](concept/README.md#2-简单图-&-多重图)
- [完全图](concept/README.md#3-完全图)
- [子图](concept/README.md#4-子图)
- [连通 & 强连通](concept/README.md#5-连通-&-强连通)
- [连通图 & 强连通图](concept/README.md#6-连通图-&-强连通图)
- [连通分量 & 强连通分量](concept/README.md#7-连通分量-&-强连通分量)
- [极小连通子图](concept/README.md#8-极小连通子图)
- [生成树、生成森林](concept/README.md#9-生成树、生成森林)
- [顶点的度](concept/README.md#10-顶点的度)
- [](concept/README.md#11-网)
- [稠密图 & 稀疏图](concept/README.md#12-稠密图-&-稀疏图)
- [有向树](concept/README.md#13-有向树)
- [路径](concept/README.md#14-路径)
- [路径长度](concept/README.md#15-路径长度)
- [回路](concept/README.md#16-回路)
## 2. [存储结构](storage/README.md#图的存储结构)
- [邻接矩阵](storage/README.md#1-邻接矩阵法)
- [邻接表](storage/README.md#2-邻接表法)
- [十字链表](storage/README.md#3-十字链表)
- [邻接多重表](storage/README.md#4-邻接多重表)
## 3. [基本操作](operation/README.md#图的基本操作)
- [边是否存在](operation/README.md#1-边是否存在)
- [顶点的邻接边](operation/README.md#2-顶点的邻接边)
- [插入顶点](operation/README.md#3-插入顶点)
- [删除顶点](operation/README.md#4-删除顶点)
- [增加边](operation/README.md#5-增加边)
- [删除边](operation/README.md#6-删除边)
- [第一条边和下一条边](operation/README.md#7-第一条边和下一条边)
- [获取权值和设置权值](operation/README.md#8-获取权值和设置权值)
## 4. [遍历](traversal/README.md#图的遍历)
- [广度优先搜索](traversal/README.md#1-广度优先搜索)
- [深度优先搜索](traversal/README.md#2-深度优先搜索)
- [遍历与连通性问题](traversal/README.md#3-遍历与连通性问题)
## 5. [应用](applications/README.md#图的应用)
- [最小生成树](applications/README.md#1-最小生成树)
- [最短路径](applications/README.md#2-最短路径)
- [拓扑排序](applications/README.md#3-拓扑排序)
- [关键路径](applications/README.md#4-关键路径)

138
ch6/applications/README.md Normal file
View File

@@ -0,0 +1,138 @@
# 图的应用
## 1. 最小生成树
### 1.1. 概念
![最小生成树1](mst1.png)
![最小生成树2](mst2.png)
![最小生成树3](mst3.png)
### 1.2. 性质
![最小生成树4](mst4.png)
- 所有边的情况皆不相同。
![最小生成树5](mst5.png)
- $n$ 个顶点只有 $n-1$ 条边。
![最小生成树6](mst6.png)
![最小生成树7](mst7.png)
### 1.3. 算法
![最小生成树8](mst8.png)
- 不会产生回路
- 权值最小
都使用贪心算法策略。
> 考研中多考查算法步骤;代码编写考查较少。
#### 1.3.1. Prim
![最小生成树9](mst9.png)
![最小生成树10](mst10.png)
![最小生成树11](mst11.png)
![最小生成树12](mst12.png)
#### 1.3.2. Kruskal
![最小生成树13](mst13.png)
![最小生成树14](mst14.png)
![最小生成树15](mst15.png)
![最小生成树16](mst16.png)
## 2. 最短路径
![最短路径1](sp1.png)
![最短路径2](sp2.png)
> 考研中多考查算法步骤;代码编写考查较少。
### 2.1. Dijkstra
![最短路径3](sp3.png)
![最短路径4](sp4.png)
![最短路径5](sp5.png)
![最短路径6](sp6.png)
![最短路径7](sp7.png)
![最短路径8](sp8.png)
![最短路径9](sp9.png)
### 2.2. Floyd
![最短路径10](sp10.png)
![最短路径11](sp11.png)
![最短路径12](sp12.png)
## 3. 拓扑排序
### 3.1. 概念
![拓扑排序1](ts1.png)
![拓扑排序2](ts2.png)
### 3.2. 算法思想
![拓扑排序3](ts3.png)
![拓扑排序4](ts4.png)
![拓扑排序5](ts5.png)
![拓扑排序6](ts6.png)
![拓扑排序7](ts7.png)
![拓扑排序8](ts8.png)
## 4. 关键路径
![关键路径1](cp1.png)
- 事件最早发生时间:等待所有前置事件都完成。
![关键路径2](cp2.png)
- 事件最迟发生时间:不延期后置事件的发生。
![关键路径3](cp3.png)
- 活动最早开始时间。
![关键路径4](cp4.png)
- 活动最迟开始时间。
![关键路径5](cp5.png)
- 差额。
![关键路径6](cp6.png)
![关键路径7](cp7.png)
![关键路径8](cp8.png)

BIN
ch6/applications/cp1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 KiB

BIN
ch6/applications/cp2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

BIN
ch6/applications/cp3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 KiB

BIN
ch6/applications/cp4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 KiB

BIN
ch6/applications/cp5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 KiB

BIN
ch6/applications/cp6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

BIN
ch6/applications/cp7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 KiB

BIN
ch6/applications/cp8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 KiB

BIN
ch6/applications/mst1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
ch6/applications/mst10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 KiB

BIN
ch6/applications/mst11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 KiB

BIN
ch6/applications/mst12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 KiB

BIN
ch6/applications/mst13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 416 KiB

BIN
ch6/applications/mst14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 KiB

BIN
ch6/applications/mst15.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 KiB

BIN
ch6/applications/mst16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 KiB

BIN
ch6/applications/mst2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 KiB

BIN
ch6/applications/mst3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

BIN
ch6/applications/mst4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 KiB

BIN
ch6/applications/mst5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

BIN
ch6/applications/mst6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

BIN
ch6/applications/mst7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

BIN
ch6/applications/mst8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

BIN
ch6/applications/mst9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 KiB

BIN
ch6/applications/sp1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

BIN
ch6/applications/sp10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 KiB

BIN
ch6/applications/sp11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 428 KiB

BIN
ch6/applications/sp12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 KiB

BIN
ch6/applications/sp2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 435 KiB

BIN
ch6/applications/sp3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 682 KiB

BIN
ch6/applications/sp4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

BIN
ch6/applications/sp5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 KiB

BIN
ch6/applications/sp6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 464 KiB

BIN
ch6/applications/sp7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 KiB

BIN
ch6/applications/sp8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 468 KiB

BIN
ch6/applications/sp9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 586 KiB

BIN
ch6/applications/ts1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

BIN
ch6/applications/ts2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 422 KiB

BIN
ch6/applications/ts3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 KiB

BIN
ch6/applications/ts4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

BIN
ch6/applications/ts5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

BIN
ch6/applications/ts6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 KiB

BIN
ch6/applications/ts7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 464 KiB

BIN
ch6/applications/ts8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 KiB

83
ch6/concept/README.md Normal file
View File

@@ -0,0 +1,83 @@
# 图的基本概念
## 1. 无向图 & 有向图
![图2](graph2.png)
## 2. 简单图 & 多重图
![图3](graph3.png)
## 3. 完全图
- 无向完全图
- 有向完全图
![图4](graph4.png)
## 4. 子图
- 与原图相同也成为子图。
- 只有顶点也是子图
![图5](graph5.png)
![图5-2](graph5-2.png)
## 5. 连通 & 强连通
![图6](graph6.png)
## 6. 连通图 & 强连通图
![图7](graph7.png)
![图8](graph8.png)
## 7. 连通分量 & 强连通分量
![图9](graph9.png)
![图10](graph10.png)
![图11](graph11.png)
## 8. 极小连通子图
![图12](graph12.png)
## 9. 生成树、生成森林
![图13](graph13.png)
![图14](graph14.png)
## 10. 顶点的度
![图15](graph15.png)
![图15-2](graph15-2.png)
## 11. 网
![图16](graph16.png)
## 12. 稠密图 & 稀疏图
![图17](graph17.png)
## 13. 有向树
![图18](graph18.png)
## 14. 路径
![图19](graph19.png)
## 15. 路径长度
![图20](graph20.png)
## 16. 回路
![图21](graph21.png)

BIN
ch6/concept/graph10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
ch6/concept/graph11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 524 KiB

BIN
ch6/concept/graph12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

BIN
ch6/concept/graph13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 KiB

BIN
ch6/concept/graph14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

BIN
ch6/concept/graph15-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 575 KiB

BIN
ch6/concept/graph15.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 KiB

BIN
ch6/concept/graph16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

BIN
ch6/concept/graph17.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

BIN
ch6/concept/graph18.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 KiB

BIN
ch6/concept/graph19.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 394 KiB

BIN
ch6/concept/graph2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

BIN
ch6/concept/graph20.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 KiB

BIN
ch6/concept/graph21.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 343 KiB

BIN
ch6/concept/graph3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 526 KiB

BIN
ch6/concept/graph4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

BIN
ch6/concept/graph5-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 KiB

BIN
ch6/concept/graph5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 390 KiB

BIN
ch6/concept/graph6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

BIN
ch6/concept/graph7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 KiB

BIN
ch6/concept/graph8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 417 KiB

BIN
ch6/concept/graph9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

BIN
ch6/graph.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

41
ch6/operation/README.md Normal file
View File

@@ -0,0 +1,41 @@
# 图的基本操作
## 1. 边是否存在
![基本操作1](operation1.png)
![基本操作2](operation2.png)
## 2. 顶点的邻接边
![基本操作3](operation3.png)
![基本操作4](operation4.png)
## 3. 插入顶点
![基本操作5](operation5.png)
## 4. 删除顶点
![基本操作6](operation6.png)
## 5. 增加边
![基本操作7](operation7.png)
![基本操作8](operation8.png)
## 6. 删除边
![基本操作9](operation9.png)
![基本操作10](operation10.png)
## 7. 第一条边和下一条边
![基本操作11](operation11.png)
## 8. 获取权值和设置权值
![基本操作12](operation12.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 676 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 542 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 532 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 533 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 533 KiB

79
ch6/storage/README.md Normal file
View File

@@ -0,0 +1,79 @@
# 图的存储结构
## 1. 邻接矩阵法
- $V=\{A,B,C,D,E\}$ 用一维数组存储。
- $E=\{(A,B),(A,C),(A,E),(B,C),(C,D),(C,E)\}$ 用二维数组存储。
二维数组行号表示起始端点,列号表示终止端点,值表示该边是否存在或该边的权重。二维数组又称为邻接矩阵。
结点数为 $n$ 的图 $G=(V,E)$ 的邻接矩阵是 $n \times n$ 的。
将 $G$ 的顶点编号为 $V_1,V_2,...,V_n$ (数组下标),若 $<V_i,V_j> \in E$,则 $A[i][j]=1$,否则 $A[i][j]=0$。
$$
A[i][j]=
\begin{cases}
1,(V_i,V_j)或<V_i,V_j>是E(G)的边\\
0,(V_i,V_j)或<V_i,V_j>不是E(G)的边\\
\end{cases}
$$
![邻接矩阵1](adjacency-matrix1.png)
![邻接矩阵2](adjacency-matrix2.png)
![邻接矩阵3](adjacency-matrix3.png)
![邻接矩阵4](adjacency-matrix4.png)
![邻接矩阵5](adjacency-matrix5.png)
![邻接矩阵5-2](adjacency-matrix5-2.png)
![邻接矩阵5-3](adjacency-matrix5-3.png)
- 邻接矩阵法的空间复杂度为:$O(n^2)$,适用于稠密图。
- 无向图的邻接矩阵为对称矩阵。
- 无向图中第 $i$ 行(或第 $i$ 列)非 $0$ 元素(非正无穷)的个数为第 $i$ 个顶点的度。
- 有向图中第 $i$ 行(第 $i$ 列)非 $0$ 元素(非正无穷)的个数为第 $i$ 个顶点的出度(入度)。
![邻接矩阵6](adjacency-matrix6.png)
![邻接矩阵7](adjacency-matrix7.png)
## 2. 邻接表法
邻接矩阵法存储稀疏图会有很多空间浪费。
![邻接表1](adjacency-list1.png)
![邻接表2](adjacency-list2.png)
![邻接表3](adjacency-list3.png)
![邻接表4](adjacency-list4.png)
![邻接表5](adjacency-list5.png)
![邻接表6](adjacency-list6.png)
## 3. 十字链表
在邻接表中寻找所有的出边(以该顶点为弧尾的弧)非常容易,但是寻找某个顶点的入边非常困难,需要遍历整个边表。
![十字链表1](cross-list1.png)
![十字链表2](cross-list2.png)
![十字链表3](cross-list3.png)
## 4. 邻接多重表
![邻接多重表1](adjacency-multiple-list1.png)
![邻接多重表2](adjacency-multiple-list2.png)
![邻接多重表3](adjacency-multiple-list3.png)
![邻接多重表4](adjacency-multiple-list4.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 493 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 527 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 573 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 459 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 461 KiB

Some files were not shown because too many files have changed in this diff Show More