Graphs and traversal
引入
7:45
数据结构与算法
这门课讲的是如何根据开销选择容器。第一个单元介绍衡量标准:开销如何随输入规模增长。接下来的四个单元介绍容器:列表、栈和队列、哈希表、树和堆,以及图。最后两个单元介绍算法:排序和搜索。这里所有关于开销的结论,都是在讲义中列出的数据集上统计得出的。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| adjacency list/əˈdʒeɪsənsi lɪst/ | 邻接表 | lín jiē biǎo |
| adjacency matrix/əˈdʒeɪsənsi ˈmeɪtrɪks/ | 邻接矩阵 | lín jiē jǔ zhèn |
| sparse/spɑːs/ | 稀疏 | xī shū |
| Breadth-first search/bredθ fɜːst sɜːtʃ/ | 广度优先搜索 | guǎng dù yōu xiān sōu suǒ |
| Depth-first search/depθ fɜːst sɜːtʃ/ | 深度优先搜索 | shēn dù yōu xiān sōu suǒ |
| Dijkstra's algorithm/ˈdeɪkstrəz ˈælɡərɪθəm/ | 迪杰斯特拉算法 | dí jié sī tè lā suàn fǎ |
| topological sort/ˌtɒpəˈlɒdʒɪkl sɔːt/ | 拓扑排序 | tuò pū pái xù |
| Kahn's algorithm/kɑːnz ˈælɡərɪθəm/ | 卡恩算法 | kǎ ēn suàn fǎ |
| negative cycle | 负环 | fù huán |
| weighted shortest paths | 加权最短路径 | jiā quán zuì duǎn lù jìng |
列表或矩阵
- 图$G = (V, E)$:课程图包含$V = \{A \ldots H\}$($8$个节点)和$10$条无向边。邻接表存储每个节点的邻居——$20$个条目对应$10$条无向边;邻接矩阵是$8 \times 8$维的$64$个单元格的网格,当存在边时值为$1$。


Ten undirected edges need — adjacency-list entries
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Each undirected edge is stored twice, once per endpoint.
为什么是 O(V+E)
- 两种遍历方式均只标记每个节点一次($V$工作量)并扫描每条存储边一次($E$工作量):在课程图上总计$O(V + E)$。矩阵需要$O(V^2)$时间,因为每一行都要遍历所有列,无论是否存在边。
- 列表在稀疏图中占优($E \ll V^2$);当图密集或问题涉及单单元格查找时,矩阵更优。务必标记已访问节点——否则真实图中的环会导致无限循环。
BFS on an adjacency matrix costs —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Every row reads all V columns, edge or not, so the list is better for sparse graphs.
两次探测
- 从$A$开始的BFS(邻居按字母序):层级为$A \,|\, B, C \,|\, D, E \,|\, F, G \,|\, H$;遍历顺序为$A, B, C, D, E, F, G, H$,且某节点的层级即为其到$A$的最短无权距离——队列确保前沿按到达顺序排列。
- 从$A$开始的DFS:$A \to B \to D$;$D$的下一个未访问邻居$C$;$C \to E \to F \to H$;$H$的邻居$G$闭合路径:$A, B, D, C, E, F, H, G$——深度优先但伴随回溯。BFS回答最短跳数;DFS揭示路径与环。
BFS from A on the course graph reads —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The queue visits layer by layer: A, then B C, then D E, then F G, then H.
这是一张新卡片,翻译完成前会显示英文。
Weighted edges: Dijkstra
- BFS counts hops, not distance. Take the weights $S\text{–}A = 4$, $S\text{–}B = 1$, $B\text{–}A = 2$, $A\text{–}T = 1$, $B\text{–}T = 5$. Dijkstra's algorithm 迪杰斯特拉算法 settles the closest unsettled node each time. The shortest path is $S, B, A, T$, length 4:
| Settle | $d(S)$ | $d(B)$ | $d(A)$ | $d(T)$ |
|---|---|---|---|---|
| $S$ | 0 | 1 | 4 | ∞ |
| $B$ | 0 | 1 | 3 | 6 |
| $A$ | 0 | 1 | 3 | 4 |
| $T$ | 0 | 1 | 3 | 4 |
- Each row settles the smallest unsettled distance (bold) and then lowers its neighbours' distances where the new route is shorter.
- With a min-heap of distances it costs $O((V + E) \log V)$. The greedy choice is safe only with non-negative weights: a negative edge can lower a node that is already settled. Use Bellman–Ford in that case.
In the step's graph, the edge $A\text{–}T$ becomes $3$. What is the shortest distance from $S$ 到 $T$?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
A is still 3 via B, so T = min(1 + 5, 3 + 3) = 6: a tie between S, B, T and S, B, A, T.
Dijkstra's algorithm stays correct when one edge has a negative weight.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A settled node could later be reached more cheaply through the negative edge; Bellman-Ford handles that case.
这是一张新卡片,翻译完成前会显示英文。
Order a DAG: topological sort
- A directed acyclic graph (DAG) of prerequisites can be listed so that every edge points forward. This is a topological sort 拓扑排序. Kahn's algorithm 卡恩算法 keeps outputting a node with in-degree 0 and deleting its edges.
- For $A \to B$, $A \to C$, $B \to D$, $C \to D$: output $A$, then $B$ and $C$, then $D$. If nodes remain but none has in-degree 0, the graph has a cycle. The cost is $O(V + E)$.
Kahn's algorithm stops with nodes left and none of in-degree 0. This means:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Every node left waits on another: a cycle.
这是一张新卡片,翻译完成前会显示英文。
Worked example: where Dijkstra goes wrong
- Directed edges $S \to A = 2$, $S \to B = 4$, $B \to A = -3$. Dijkstra settles $A$ at 2 and then $B$ at 4. The edge $B \to A$ offers 1, but $A$ is already final.
- Bellman–Ford relaxes every edge $V - 1 = 2$ times and finds $d(A) = \mathbf{1}$. If one more pass still improves something, there is a negative cycle 负环. Cost: $O(VE)$.
$S \to A = 2$, $S \to B = 4$, $B \to A = -3$: true shortest distance to $A$?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Via $B$: $4 - 3 = 1$.
Dijkstra's algorithm is correct as long as no cycle has negative total weight.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
One negative edge is enough to break it, as here; Bellman–Ford handles negative edges.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- Forgetting the visited mark. Any cycle then makes BFS or DFS run for ever.
- BFS for weighted shortest paths 加权最短路径. BFS counts hops. In the weighted example the best route $S, B, A, T$ costs $4$ but uses three hops.
- Counting list entries as edges. Each undirected edge is in both endpoints' lists: $10$ edges, $20$ entries.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Adjacency list 邻接表: $O(V + E)$ space, best for sparse graphs; matrix: $V^2$ cells, $O(1)$ edge lookup.
- BFS gives shortest hop counts; DFS shows paths and cycles; both cost $O(V + E)$.
- Dijkstra needs non-negative weights and costs $O((V + E) \log V)$ with a heap.
出了什么问题?只有网站团队能看到这些信息。