目录
正在加载目录…
正在阅读专栏 Algorithms:CLRS + MIT 6.006 第 16 篇 · 共 24 篇 · 67%

Dijkstra:DAG Shortest Paths、Greedy Choice 与 Priority Queue

发布于 2026-09-18 19:40 · 最后编辑于 2026-09-18 19:40 · 版本 v2 · 字数 5,647 · 预计阅读 12 分钟 👁 17 次阅读

Algorithms:Dijkstra

本文按 MIT 6.006 Fall 2011 Lecture 16《Shortest Paths II:Dijkstra》的顺序整理,并结合 CLRS 第四版第 22 章补齐 invariant、correctness proof、priority queue 与 complexity analysis。本文先处理 DAG shortest paths,再处理 non-negative weighted graphs 中的 Dijkstra;negative edges 留给 Bellman–Ford。

TL;DR

  • DAG shortest paths:先做 topological sort,再按拓扑顺序对每个 vertex 的 outgoing edges 各 relax 一次,时间为 Θ(V + E),即使 edge weights 为负也可以工作。
  • Dijkstra:维护已经确定 final shortest-path weights 的集合 S;每次从 V − S 中取 d 最小的 vertex,加入 S,然后 relax 它的 outgoing edges。
  • 前提:Dijkstra 要求所有 edge weights 都满足 w(u, v) ≥ 0;否则 greedy choice 可能过早把一个 vertex 标记为 final。
  • 正确性核心:relaxation 是 safe 的;在 non-negative weights 下,每次选出的 minimum-estimate vertex u 都满足 d[u] = δ(s, u)。
  • 复杂度:array priority queue 为 Θ(V² + E);binary min-heap 为 Θ((V + E) lg V);Fibonacci heap 的 amortized bound 为 Θ(V lg V + E)。

目录

章节说明
Lecture 16:Shortest Paths II:Dijkstra从上一讲的 relaxation 进入两类 shortest-path algorithms
Review:relaxation 与 safetydistance estimates 和 invariant
DAG shortest pathstopological order 加一次 edge scan
Dijkstra 的基本策略set S 与 greedy choice
Dijkstra pseudocodepriority queue 版本
Dijkstra 的 worked example按 S 的增长跟踪 estimates
Correctness:为什么 greedy choice 安全key observation 与 proof sketch
Dijkstra 的 complexityarray、binary heap 和 Fibonacci heap
小结适用边界与后续 Bellman–Ford

Lecture 16:Shortest Paths II:Dijkstra

上一讲建立了 single-source shortest paths 的基本语言:d[v] 表示当前 distance estimate,Π[v] 保存 predecessor,而 relaxation 尝试通过 edge (u, v) 改善 d[v]。

本讲研究两种可以利用 graph 结构加速的情形:

  1. 如果 graph 是 DAG,可以利用 topological order;
  2. 如果 graph 没有 negative edges,可以使用 Dijkstra 的 greedy strategy。

两者都依赖 relaxation,但 edge 的处理顺序不同。顺序正是 shortest-path algorithm 能否高效并且可证明正确的关键。

Review:relaxation 与 safety

初始化仍然是:

text
INITIALIZE(G, s):
    for v ∈ V:
        d[v] ← ∞
        Π[v] ← NIL
    d[s] ← 0

对 edge (u, v) 做 relaxation:

text
RELAX(u, v, w):
    if d[v] > d[u] + w(u, v):
        d[v] ← d[u] + w(u, v)
        Π[v] ← u

Relaxation 是 safe 的

核心 invariant 是:

text
d[v] ≥ δ(s, v)    for every v ∈ V

证明采用 induction。假设在当前步骤之前 invariant 成立。对 RELAX(u, v, w),由 induction hypothesis:

text
d[u] ≥ δ(s, u)

根据 triangle inequality:

text
δ(s, v) ≤ δ(s, u) + δ(u, v)

而 edge (u, v) 本身是从 u 到 v 的一个候选 path,所以:

text
δ(u, v) ≤ w(u, v)

合并得到:

text
δ(s, v) ≤ d[u] + w(u, v)

因此,把 d[v] 更新为 d[u] + w(u, v) 不会越过真正的 shortest-path weight。relaxation 可以改善 estimate,但不会把它错误地降到 δ(s, v) 以下。

DAG shortest paths

对于 DAG(Directed Acyclic Graph),不存在 cycle,因此自然不存在 negative cycle。更重要的是,topological order 给出了一种不会“回头”的处理顺序:若存在从 u 到 v 的 path,u 会排在 v 前面。

Algorithm

  1. 对 DAG 做 topological sort;
  2. 按 topological order 依次处理每个 vertex u;
  3. 对 u 的每条 outgoing edge (u, v) 执行 relaxation。
text
DAG-SHORTEST-PATHS(G, w, s):
    topologically sort G
    INITIALIZE(G, s)

    for each vertex u in topological order:
        for each edge (u, v) leaving u:
            RELAX(u, v, w)

为什么一次就够

当按 topological order 处理 u 时,所有可能位于某条 s → u path 上的 predecessor 都已经处理过。因此,d[u] 在处理其 outgoing edges 时已经包含了所有来自 earlier vertices 的必要改进。由于 DAG 中没有 edge 能从 later vertex 回到 earlier vertex,之后不需要重新回头 relax u 的 outgoing edges。

Complexity

topological sort 为 Θ(V + E)。随后每个 vertex 处理一次,每条 edge 只被 relax 一次,也是 Θ(V + E)。总复杂度为:

text
Θ(V + E)

这比一般 graph 上的 Dijkstra 更简单,而且 DAG shortest paths 允许 negative edge;限制是 graph 必须是 acyclic 的。

一个小例子

假设 topological order 为:

text
r, s, t, x, y, z

source 是 s,初始 estimates 为:

text
d[r] = ∞, d[s] = 0, d[t] = ∞,
d[x] = ∞, d[y] = ∞, d[z] = ∞

处理 r 不会产生变化,因为 r 在 source 之前且不可由 s 到达。处理 s 后,例如得到:

text
d[t] = 2
d[x] = 6

继续处理 t 和 x,后续 vertices 的 estimates 会沿 topological order 传播;到处理 y、z 时,所有可能的 earlier contributions 已经就位。这正是 DAG shortest paths 也可以看作一种按拓扑顺序执行的 dynamic programming。

Dijkstra strategy

现在考虑没有 negative edges 的 graph:

text
w(u, v) ≥ 0    for every edge (u, v)

维护集合 S:

text
S = 已经确定 final shortest-path weights 的 vertices

初始时:

text
S = ∅
d[s] = 0
d[v] = ∞  (v ≠ s)

每一轮从 V − S 中选择 d 最小的 vertex u:

text
u = argmin { d[v] : v ∈ V − S }

然后:

  1. 把 u 加入 S;
  2. relax u 的所有 outgoing edges;
  3. 在 priority queue 中更新受影响的 estimates。

这一选择是 greedy 的:每次把当前看起来离 source 最近的 vertex 固定下来。

为什么需要 non-negative edges

若存在 negative edge,某个尚未加入 S 的 vertex 可能在之后通过另一条 path 被显著降低。特别是,一条先经过较远 vertex、再经过 negative edge 的 path,可能比当前 minimum estimate 更短。

non-negative 条件排除了这种“绕远后反超”的可能性:从 source 出发继续延伸 path 只会增加或保持 path weight,不会因为未来 edge 而突然降低已经确定的 prefix。于是 minimum estimate vertex 可以安全地被 finalise。

Dijkstra pseudocode

讲义中的实现使用 priority queue Q:

text
DIJKSTRA(G, w, s):
    INITIALIZE(G, s)
    S ← ∅
    Q ← V[G]       // 将所有 vertices 放入 priority queue

    while Q ≠ ∅:
        u ← EXTRACT-MIN(Q)
        S ← S ∪ {u}

        for each vertex v ∈ Adj[u]:
            RELAX(u, v, w)
            // relaxation 对应 priority queue 中的 DECREASE-KEY

这里的 Q 按 d[v] 排序。不可达 vertices 的 key 仍然是 ∞;它们会在所有 reachable vertices 被取出后依次被取出,但不会导致有效的 relaxation。

S、Q 和 predecessor tree

对象作用
S已证明 d[u] = δ(s, u) 的 vertices
Q尚未 finalise 的 vertices,按当前 d 组织
d[v]当前 best-known distance estimate
Π[v]产生当前 estimate 的 predecessor

最终,Π 中的 pointers 组成 shortest-path tree 的一部分;从目标 vertex 沿 Π 反向追踪,可以恢复具体 path。

Dijkstra 的 worked example

考虑 source 为 A 的 weighted graph。按照当前 estimate,Dijkstra 的过程可以用下表跟踪:

阶段Sd[A]d[B]d[C]d[D]d[E]发生的事情
初始化∅0∞∞∞∞所有 vertices 在 Q 中
取出 A{A}0∞∞∞∞relax A 的 edges
relax A 后{A}0103∞∞C 的 estimate 最小
取出 C{A,C}073115relax C,更新 B、D、E
取出 E{A,C,E}073115E 已经 finalise
取出 B{A,C,E,B}07395relax B,改善 D
取出 D{A,C,E,B,D}07395所有 vertices finalise

注意:B 的 estimate 最初是 10,但处理 C 后通过另一条 path 改善为 7;D 也会在处理 B 后从 11 改善为 9。priority queue 保证下一次取出的始终是当前 d 最小的未 finalise vertex。

Correctness:为什么 greedy choice 安全

Dijkstra correctness 的关键命题是:

每次把 vertex u 加入 S 时,都有 d[u] = δ(s, u)。

Proof idea

假设 u 是从 V − S 中取出的 minimum-estimate vertex,但暂时假设 d[u] > δ(s, u)。考虑一条从 s 到 u 的真正 shortest path,并沿这条 path 从 source 方向前进:

text
s → ... → x → y → ... → u

其中 x ∈ S 是 path 上最后一个已经加入 S 的 vertex,y ∉ S 是紧跟在 x 后面的第一个未加入 vertex。

当 x 被加入 S 时,algorithm relax 了 edge (x, y)。由于 shortest path 的 subpath 也是 shortest path,且 x 的 distance 已经正确,relaxation 会使:

text
d[y] = δ(s, y)

接下来,因所有 edge weights 都 non-negative,从 y 沿 shortest path 继续走到 u 不会降低 distance,所以:

text
δ(s, y) ≤ δ(s, u)

又由于 u 是 V − S 中 d 最小的 vertex:

text
d[u] ≤ d[y] = δ(s, y) ≤ δ(s, u)

另一方面,relaxation safety 已保证:

text
d[u] ≥ δ(s, u)

因此只能有:

text
d[u] = δ(s, u)

矛盾假设被排除,greedy choice 安全。

为什么 negative edge 会破坏 proof

proof 中使用了“沿 path 继续前进不会降低 weight”这一事实。它正是 non-negative edge assumption 的作用。如果允许 negative edge,可能出现:

text
δ(s, y) > δ(s, u)

因为从 y 到 u 的后缀包含足够负的 edge。于是当前最小的 d[y] 不代表最终最小的 path,不能安全地把它 finalise。Bellman–Ford 将采用不同的、允许反复传播改进的策略。

Dijkstra 的 complexity

Dijkstra 进行:

text
Θ(V) 次 INSERT
Θ(V) 次 EXTRACT-MIN
Θ(E) 次 DECREASE-KEY

总复杂度由 priority queue 的实现决定。

Array implementation

在未排序 array 中,EXTRACT-MIN 需要扫描整个 queue:

text
EXTRACT-MIN:Θ(V)
DECREASE-KEY:Θ(1)

因此:

text
Θ(V · V + E · 1) = Θ(V² + E) = Θ(V²)

因为简单 graph 满足 E = O(V²),通常写作 Θ(V²)。

Binary min-heap

binary min-heap 支持:

text
EXTRACT-MIN:Θ(lg V)
DECREASE-KEY:Θ(lg V)

总复杂度为:

text
Θ(V lg V + E lg V)

也可写作:

text
Θ((V + E) lg V)

对于 connected graph,通常 E ≥ V − 1,所以常见的简写是 Θ(E lg V)。

Fibonacci heap

Fibonacci heap 在 6.006 本讲中不展开实现,但其 amortized bounds 是:

text
EXTRACT-MIN:Θ(lg V)
DECREASE-KEY:Θ(1) amortized

于是 Dijkstra 的总复杂度为:

text
Θ(V lg V + E)

它在 dense graph 与 sparse graph 中的适用性取决于实现成本、常数和工程环境;算法分析中必须明确使用的 priority queue model。

与三类 shortest-path 问题的关系

Graph 条件适用算法典型复杂度
DAG,可包含 negative edgestopological-order relaxationΘ(V + E)
一般 directed graph,所有 edges non-negativeDijkstra取决于 priority queue
可包含 negative edgesBellman–FordΘ(VE)
存在 source 可达的 negative-weight cycle不能为相关 vertices 返回有限 shortest paths应报告 cycle

小结

  • DAG shortest paths 通过 topological order 把 graph 变成一次 forward pass,每条 edge 只需 relax 一次。
  • Dijkstra 维护 finalised set S,每次选择 V − S 中最小的 distance estimate。
  • relaxation 的 safety invariant 保证 estimates 不会小于真实 shortest-path weights。
  • Dijkstra 的 greedy correctness 依赖 non-negative edge weights;它保证“当前最近的未 finalise vertex”已经不可能被未来 path 改善。
  • array、binary min-heap 和 Fibonacci heap 会带来不同的 EXTRACT-MIN 与 DECREASE-KEY 成本。
  • negative edges 不应直接交给 Dijkstra;下一篇 Bellman–Ford 将处理这种更一般的情形。

参考资料

导航

← 返回列表
← 上一篇 Single-Source Shortest Paths:权重图、Relaxation 与 Optimal Substructure 下一篇 → Bellman–Ford:Negative Edges、负环检测与 Θ(VE) 复杂度

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论