Bellman–Ford:Negative Edges、负环检测与 Θ(VE) 复杂度
Algorithms:Bellman–Ford
本文按 MIT 6.006 Fall 2011 Lecture 17《Shortest Paths III:Bellman–Ford》的顺序整理,并结合 CLRS 第四版第 22 章补齐 Bellman–Ford 的 invariant、correctness proof、negative-weight cycle detection 与 complexity analysis。它是允许 negative edges 的 single-source shortest-path algorithm。
TL;DR
- 目标:在一般 directed graph 中求 single-source shortest paths,允许 negative edge weights。
- 核心做法:重复扫描全部 edges,并对每条 edge 做 relaxation;进行 |V| − 1 轮后,再扫描一轮检测是否仍有 edge 可 relax。
- 正确性:若 graph 没有 source 可达的 negative-weight cycle,任意 shortest path 都可以取为 simple path,最多包含 |V| − 1 条 edges。
- 负环检测:|V| − 1 轮后若仍存在 d[v] > d[u] + w(u,v),则存在从 source 可达的 negative-weight cycle。
- 复杂度:Θ(VE) time,Θ(V) extra space;可以在某一轮没有任何更新时提前终止。
- 边界:Bellman–Ford 不能为 negative cycle 可达的目标返回有限 shortest-path weight;它应报告该 cycle 的存在。
目录
| 章节 | 说明 |
|---|---|
| Lecture 17:Shortest Paths III:Bellman–Ford | 从 generic shortest paths 进入 Bellman–Ford |
| Review:path 与 negative cycle | shortest-path notation |
| Generic shortest-path algorithm 的问题 | 不受控的 relaxation 顺序 |
| Bellman–Ford algorithm | pseudocode 与停止条件 |
| Correctness:为什么 n 轮足够 | shortest simple path proof |
| Negative-weight cycle detection | extra pass 的含义 |
| Worked example | 多轮 relaxation 的传播 |
| Complexity 与 early termination | 时间、空间和优化 |
| Longest simple path 的提醒 | 不要把取负权重当作通用解法 |
| 小结 | Bellman–Ford 的适用范围 |
Lecture 17:Shortest Paths III:Bellman–Ford
Dijkstra 依赖 non-negative edge weights,因此不能处理一般的 negative edges。本讲回到更通用的策略:不试图一次性 finalise 某个 vertex,而是让 shortest-path information 通过 repeated relaxation 在 graph 中传播。
Bellman–Ford 的特点是:
- 不要求 edge weights 非负;
- 能检测从 source 可达的 negative-weight cycles;
- 代价是需要扫描整个 edge set 多轮,复杂度为 Θ(VE)。
Review:path 与 negative cycle
path 写作:
p = <v₀, v₁, ..., vₖ>
其中每条相邻 edge 都属于 E。path weight 为:
w(p) = Σᵢ₌₀ᵏ⁻¹ w(vᵢ, vᵢ₊₁)
shortest-path weight 记作 δ(u, v)。如果 v 从 u 不可达,则 δ(u, v) = ∞。如果从 u 到 v 的某条 path 能接触 negative-weight cycle,那么可以不断绕行并降低 path weight,δ(u, v) 没有有限定义。
Bellman–Ford 的目标不是“在任何情况下返回一个数”,而是区分有限 shortest-path weights、不可达 vertices,以及受 source 可达 negative-weight cycle 影响的 vertices。
Generic shortest-path algorithm 的问题
通用框架从下面的初始化开始:
for v ∈ V:
d[v] ← ∞
Π[v] ← NIL
d[s] ← 0
然后反复选择 edge 并 relaxation:
select some edge (u, v)
if d[v] > d[u] + w(u, v):
d[v] ← d[u] + w(u, v)
Π[v] ← u
relaxation safety 保证 d[v] ≥ δ(s, v),但仅仅“不断找能 relax 的 edge”仍然不够:
- 如果存在 negative cycle,algorithm 会永远找到可以继续降低 estimate 的 edge,无法终止;
- 如果 edge selection order 很差,即使没有 negative cycle,也可能产生指数级的重复工作;
- 因此需要一个固定的、可证明的 relaxation schedule。
Bellman–Ford 选择最直接的 schedule:按轮次扫描全部 edges。
Bellman–Ford algorithm
BELLMAN-FORD(G, w, s):
INITIALIZE(G, s)
for i = 1 to |V| − 1:
for each edge (u, v) ∈ E:
RELAX(u, v, w)
for each edge (u, v) ∈ E:
if d[v] > d[u] + w(u, v):
report that a negative-weight cycle exists
return d and Π
第一部分执行 |V| − 1 次 full pass。每一轮对所有 edges 进行 relaxation,不依赖 edge 的特殊顺序。第二部分再扫描一次全部 edges:若没有 edge 可以继续改善任何 estimate,就返回正确的 shortest-path weights;若仍有 edge 可以 relax,则报告 negative-weight cycle。
为什么是 |V| − 1 轮
如果没有 negative-weight cycle,shortest path 可以选为 simple path:它不会重复 vertex。一个包含 |V| 个 vertices 的 simple path 最多有 |V| − 1 条 edges。因此,最多需要这么多轮,才能让 shortest-path information 从 source 逐 edge 传播到目标。
Correctness:为什么 n 轮足够
任取 vertex v。若 v 从 source s 可达,取一条从 s 到 v 的 shortest path,并在所有 shortest paths 中选择 edge 数量最少的一条:
p = <v₀, v₁, ..., vₖ>
v₀ = s, vₖ = v
没有 negative-weight cycle 时,这条 path 不会重复 vertex,否则删除其中的 cycle 会得到更短或同样短、但 edge 更少的 path。因此 k ≤ |V| − 1。
按照 path 上的顺序观察 Bellman–Ford:
- 初始时,d[v₀] = d[s] = 0 = δ(s, s);
- 第 1 轮扫描会 relax (v₀, v₁),从而得到 d[v₁] = δ(s, v₁);
- 第 2 轮扫描会 relax (v₁, v₂),从而得到 d[v₂] = δ(s, v₂);
- 一般地,在第 i 轮之后,d[vᵢ] = δ(s, vᵢ)。
即使 edge 在一轮中的出现顺序不利,下一轮也会继续传播上一轮已经得到的结果。因此每轮至少能沿 chosen shortest path 向前推进一条 edge。当执行完 k ≤ |V| − 1 轮时:
d[v] = d[vₖ] = δ(s, v)
relaxation safety 又保证 estimate 不会低于真实值,所以最终只能相等。
Theorem:如果 G = (V, E) 不包含 source 可达的 negative-weight cycle,那么 Bellman–Ford 结束后,对所有 vertices v 都有 d[v] = δ(s, v)。
Negative-weight cycle detection
执行完 |V| − 1 轮后,再检查每条 edge:
if d[v] > d[u] + w(u, v):
negative-weight cycle exists
如果某条 edge 仍能改善 estimate,说明某个 shortest estimate 需要一条超过 |V| − 1 条 edges 的 path 才能继续变小。这样的 path 必然重复某个 vertex,因此含有 cycle。若 cycle 的总 weight 是非负的,删除它不会增加 path weight;要使重复 cycle 的 path 严格优于 simple paths,该 cycle 必须具有 negative total weight。
这个检测只对 从 source 可达 的 negative cycle 有意义。如果 negative cycle 位于 source 的 disconnected component 中,它不会影响从 s 出发的 SSSP 结果。
Worked example
考虑 source 为 A 的 graph,其中存在 negative edges,但没有 source 可达的 negative cycle:
| 时刻 | 关键变化 |
|---|---|
| 初始化 | d[A] = 0,其余 vertices 为 ∞ |
| pass 1 | source 的 outgoing edges 把一批 vertices 从 ∞ 改为有限值 |
| pass 2 | 新获得的 estimates 沿后续 edges 传播;negative edge 可能使某个值进一步下降 |
| pass 3 及之后 | 若 shortest paths 已经稳定,后续 pass 不再产生变化 |
| extra pass | 若没有 edge 能 relax,则确认没有 source 可达 negative cycle |
与 Dijkstra 不同,Bellman–Ford 不会把某个 vertex 一旦取出就永久 finalise。一个早先得到的 finite estimate 仍可能在后续 pass 中被改善,这正是它能够处理 negative edges 的原因。
Complexity 与 early termination
每一轮扫描全部 E 条 edges,一共最多 V − 1 轮,随后还有一次检测 pass:
Θ(VE)
初始化 d 和 Π 需要 Θ(V) space;不计算 graph storage 时,algorithm 的 auxiliary space 为 Θ(V)。
如果某一整轮扫描中没有任何 edge 发生 relaxation,所有 estimates 已经稳定,可以立即停止:
for i = 1 to |V| − 1:
changed ← false
for each edge (u, v) ∈ E:
if RELAX(u, v, w):
changed ← true
if not changed:
break
最坏情况复杂度仍是 Θ(VE),但在 shortest-path information 很快传播完时,early termination 可以减少实际工作。
Longest simple path warning
不要把 Bellman–Ford 理解成“把所有 weights 取负,就能求 longest path”。
在 non-negative weighted graph 中,longest simple path 是 NP-hard 问题。若把 weights 取负后运行 Bellman–Ford,原问题可能产生 negative-weight cycle;algorithm 会报告 cycle 并停止,而不是返回 longest simple path。
同样地,在含有 negative cycles 的 graph 中,想求从 source 到目标的 longest simple path,也不能通过普通 Bellman–Ford 解决。甚至 shortest simple path 在一般情形下也可能是 NP-hard;Bellman–Ford 求的是允许一般 path、但要求 shortest value 有定义的 SSSP 问题。
与前两篇的关系
| Graph 条件 | 适用算法 | 主要策略 | 复杂度 |
|---|---|---|---|
| DAG,可包含 negative edges | topological-order relaxation | topological order,每条 edge 一次 | Θ(V + E) |
| 一般 directed graph,所有 edges non-negative | Dijkstra | greedy finalisation + priority queue | 取决于 priority queue |
| 可包含 negative edges | Bellman–Ford | V − 1 轮 full relaxation | Θ(VE) |
| 存在 source 可达的 negative-weight cycle | 无有限 shortest path | 报告 cycle | — |
小结
- Bellman–Ford 通过重复扫描全部 edges 处理 negative edges。
- 没有 source 可达 negative-weight cycle 时,simple shortest path 最多包含 |V| − 1 条 edges。
- 每一轮至少能沿 chosen shortest path 传播一条 edge 的正确 distance,因此 |V| − 1 轮足够。
- 额外扫描中若仍存在可 relax 的 edge,说明存在 source 可达 negative-weight cycle。
- algorithm 的 worst-case complexity 为 Θ(VE),但可以在某轮无更新时提前停止。
- Bellman–Ford 解决的是 shortest paths,不是一般的 longest simple path。
参考资料
导航
- 上一篇:Algorithms:Dijkstra
- 系列索引:Algorithms-CLRS-MIT 6.006 系列索引
- 下一篇:Lecture 18《Speeding Up Dijkstra》
评论 (0)