2023-02-16 03:39:01 +08:00
|
|
|
|
# 图的遍历
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-08-29 20:57:37 +08:00
|
|
|
|
树代表的是“一对多”的关系,而图则具有更高的自由度,可以表示任意的“多对多”关系。因此,我们可以把树看作是图的一种特例。显然,**树的遍历操作也是图的遍历操作的一种特例**。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-08-29 20:57:37 +08:00
|
|
|
|
图和树都都需要应用搜索算法来实现遍历操作。图的遍历方式可分为两种:「广度优先遍历 breadth-first traversal」和「深度优先遍历 depth-first traversal」。它们也常被称为「广度优先搜索 breadth-first search」和「深度优先搜索 depth-first search」,简称 BFS 和 DFS 。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 广度优先遍历
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
**广度优先遍历是一种由近及远的遍历方式,从某个节点出发,始终优先访问距离最近的顶点,并一层层向外扩张**。如下图所示,从左上角顶点出发,先遍历该顶点的所有邻接顶点,然后遍历下一个顶点的所有邻接顶点,以此类推,直至所有顶点访问完毕。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![图的广度优先遍历](graph_traversal.assets/graph_bfs.png)
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
### 算法实现
|
|
|
|
|
|
2023-08-20 23:28:30 +08:00
|
|
|
|
BFS 通常借助队列来实现。队列具有“先入先出”的性质,这与 BFS 的“由近及远”的思想异曲同工。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-07-26 08:59:36 +08:00
|
|
|
|
1. 将遍历起始顶点 `startVet` 加入队列,并开启循环。
|
|
|
|
|
2. 在循环的每轮迭代中,弹出队首顶点并记录访问,然后将该顶点的所有邻接顶点加入到队列尾部。
|
|
|
|
|
3. 循环步骤 `2.` ,直到所有顶点被访问完成后结束。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
为了防止重复遍历顶点,我们需要借助一个哈希表 `visited` 来记录哪些节点已被访问。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="graph_bfs.java"
|
|
|
|
|
[class]{graph_bfs}-[func]{graphBFS}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="graph_bfs.cpp"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="graph_bfs.py"
|
2023-02-23 20:23:49 +08:00
|
|
|
|
[class]{}-[func]{graph_bfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="graph_bfs.go"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="graph_bfs.js"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="graph_bfs.ts"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="graph_bfs.c"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="graph_bfs.cs"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{graph_bfs}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="graph_bfs.swift"
|
2023-02-22 19:41:31 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="graph_bfs.zig"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphBFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="graph_bfs.dart"
|
|
|
|
|
[class]{}-[func]{graphBFS}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="graph_bfs.rs"
|
|
|
|
|
[class]{}-[func]{graph_bfs}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
代码相对抽象,建议对照下图来加深理解。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![图的广度优先遍历步骤](graph_traversal.assets/graph_bfs_step1.png)
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<2>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step2](graph_traversal.assets/graph_bfs_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<3>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step3](graph_traversal.assets/graph_bfs_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<4>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step4](graph_traversal.assets/graph_bfs_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<5>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step5](graph_traversal.assets/graph_bfs_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<6>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step6](graph_traversal.assets/graph_bfs_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<7>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step7](graph_traversal.assets/graph_bfs_step7.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<8>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step8](graph_traversal.assets/graph_bfs_step8.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<9>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step9](graph_traversal.assets/graph_bfs_step9.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<10>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step10](graph_traversal.assets/graph_bfs_step10.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<11>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_bfs_step11](graph_traversal.assets/graph_bfs_step11.png)
|
|
|
|
|
|
|
|
|
|
!!! question "广度优先遍历的序列是否唯一?"
|
|
|
|
|
|
2023-08-27 23:40:39 +08:00
|
|
|
|
不唯一。广度优先遍历只要求按“由近及远”的顺序遍历,**而多个相同距离的顶点的遍历顺序是允许被任意打乱的**。以上图为例,顶点 $1$、$3$ 的访问顺序可以交换、顶点 $2$、$4$、$6$ 的访问顺序也可以任意交换。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 03:09:06 +08:00
|
|
|
|
**时间复杂度:** 所有顶点都会入队并出队一次,使用 $O(|V|)$ 时间;在遍历邻接顶点的过程中,由于是无向图,因此所有边都会被访问 $2$ 次,使用 $O(2|E|)$ 时间;总体使用 $O(|V| + |E|)$ 时间。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
**空间复杂度:** 列表 `res` ,哈希表 `visited` ,队列 `que` 中的顶点数量最多为 $|V|$ ,使用 $O(|V|)$ 空间。
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 深度优先遍历
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
**深度优先遍历是一种优先走到底、无路可走再回头的遍历方式**。如下图所示,从左上角顶点出发,访问当前顶点的某个邻接顶点,直到走到尽头时返回,再继续走到尽头并返回,以此类推,直至所有顶点遍历完成。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![图的深度优先遍历](graph_traversal.assets/graph_dfs.png)
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
### 算法实现
|
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中我们也需要借助一个哈希表 `visited` 来记录已被访问的顶点,以避免重复访问顶点。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="graph_dfs.java"
|
|
|
|
|
[class]{graph_dfs}-[func]{dfs}
|
|
|
|
|
|
|
|
|
|
[class]{graph_dfs}-[func]{graphDFS}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="graph_dfs.cpp"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="graph_dfs.py"
|
2023-02-23 20:23:49 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-23 20:23:49 +08:00
|
|
|
|
[class]{}-[func]{graph_dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="graph_dfs.go"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="graph_dfs.js"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="graph_dfs.ts"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="graph_dfs.c"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="graph_dfs.cs"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{graph_dfs}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{graph_dfs}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="graph_dfs.swift"
|
2023-02-22 19:41:31 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-22 19:41:31 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="graph_dfs.zig"
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{dfs}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-25 01:26:34 +08:00
|
|
|
|
[class]{}-[func]{graphDFS}
|
2023-02-15 03:34:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="graph_dfs.dart"
|
|
|
|
|
[class]{}-[func]{dfs}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{graphDFS}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="graph_dfs.rs"
|
|
|
|
|
[class]{}-[func]{dfs}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{graph_dfs}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
深度优先遍历的算法流程如下图所示。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-07-26 08:59:36 +08:00
|
|
|
|
- **直虚线代表向下递推**,表示开启了一个新的递归方法来访问新顶点。
|
|
|
|
|
- **曲虚线代表向上回溯**,表示此递归方法已经返回,回溯到了开启此递归方法的位置。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-09 03:09:06 +08:00
|
|
|
|
为了加深理解,建议将图示与代码结合起来,在脑中(或者用笔画下来)模拟整个 DFS 过程,包括每个递归方法何时开启、何时返回。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![图的深度优先遍历步骤](graph_traversal.assets/graph_dfs_step1.png)
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<2>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step2](graph_traversal.assets/graph_dfs_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<3>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step3](graph_traversal.assets/graph_dfs_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<4>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step4](graph_traversal.assets/graph_dfs_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<5>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step5](graph_traversal.assets/graph_dfs_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<6>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step6](graph_traversal.assets/graph_dfs_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<7>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step7](graph_traversal.assets/graph_dfs_step7.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<8>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step8](graph_traversal.assets/graph_dfs_step8.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<9>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step9](graph_traversal.assets/graph_dfs_step9.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<10>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step10](graph_traversal.assets/graph_dfs_step10.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<11>"
|
2023-02-15 03:34:06 +08:00
|
|
|
|
![graph_dfs_step11](graph_traversal.assets/graph_dfs_step11.png)
|
|
|
|
|
|
|
|
|
|
!!! question "深度优先遍历的序列是否唯一?"
|
|
|
|
|
|
2023-04-09 03:09:06 +08:00
|
|
|
|
与广度优先遍历类似,深度优先遍历序列的顺序也不是唯一的。给定某顶点,先往哪个方向探索都可以,即邻接顶点的顺序可以任意打乱,都是深度优先遍历。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
2023-04-09 03:09:06 +08:00
|
|
|
|
以树的遍历为例,“根 $\rightarrow$ 左 $\rightarrow$ 右”、“左 $\rightarrow$ 根 $\rightarrow$ 右”、“左 $\rightarrow$ 右 $\rightarrow$ 根”分别对应前序、中序、后序遍历,它们展示了三种不同的遍历优先级,然而这三者都属于深度优先遍历。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
### 复杂度分析
|
|
|
|
|
|
2023-04-09 03:09:06 +08:00
|
|
|
|
**时间复杂度:** 所有顶点都会被访问 $1$ 次,使用 $O(|V|)$ 时间;所有边都会被访问 $2$ 次,使用 $O(2|E|)$ 时间;总体使用 $O(|V| + |E|)$ 时间。
|
2023-02-15 03:34:06 +08:00
|
|
|
|
|
|
|
|
|
**空间复杂度:** 列表 `res` ,哈希表 `visited` 顶点数量最多为 $|V|$ ,递归深度最大为 $|V|$ ,因此使用 $O(|V|)$ 空间。
|