# 贝尔曼-福特算法

**贝尔曼-福特算法**（Bellman–Ford algorithm）是用于求解加权有向图单源最短路径问题的算法，其边权可以为负值；它还能检测从源点可达的负权环——在这种情况下，最短路径无定义。对于含 \( V \) 个顶点、\( E \) 条边的图，其运行时间为 \( O(V \cdot E) \)，比迪杰斯特拉算法慢，但在迪杰斯特拉算法失效的输入上仍然正确。<sup>[1](https://xlinux.nist.gov/dads/HTML/bellmanford.html)</sup><sup> • </sup><sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>

| 关键事实 | 详情 |
|---|---|
| 解决的问题 | 有向图中边权为实数、可能为负时的单源最短路径<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup> |
| 运行时间 | \( O(V \cdot E) \)；空间 \( O(V) \)<sup>[1](https://xlinux.nist.gov/dads/HTML/bellmanford.html)</sup><sup> • </sup><sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup> |
| 结构 | 进行 \( V-1 \) 轮、每轮松弛所有边，再用额外一轮检测负权环<sup>[1](https://xlinux.nist.gov/dads/HTML/bellmanford.html)</sup> |
| 负权环 | 若从源点可达负权环，则返回假（在 NetworkX 等实现中抛出异常）<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup><sup> • </sup><sup>[4](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.bellman_ford_predecessor_and_distance.html)</sup> |
| 起源 | 贝尔曼 1958 年发表于兰德公司的论文《论路由问题》；福特 1956 年关于网络流理论的兰德公司报告<sup>[5](https://www.cs.yale.edu/homes/lans/readings/routing/bellman-routing-1958.pdf)</sup><sup> • </sup><sup>[6](https://dml.cz/bitstream/handle/10338.dmlcz/401175/DejinyMat_25-2004-1_7.pdf)</sup> |
| 应用 | 距离向量路由（RIP）、货币套利检测、差分约束、约翰逊算法中的重赋权<sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup> |
| 现代地位 | \( O(n \cdot m) \) 的界保持了 65 年以上，直到法恩曼 2024 年提出 \( \tilde{O}(mn^{8/9}) \) 算法<sup>[7](https://arxiv.org/abs/2607.19346)</sup> |

## 它所解决的问题

迪杰斯特拉算法计算从一个源点到其他所有顶点的最短路径，但其正确性建立在所有边权非负的假设之上。当一条边能够从路径总权重中减去一部分时，一条负权边可能改进迪杰斯特拉算法已经确定的顶点，而算法会在毫无警告的情况下返回错误答案。<sup>[8](https://dsamindmap.com/compare/dijkstra-vs-bellman-ford)</sup><sup> • </sup><sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup>

负权并非数学上的奇趣之物。带有返利、折扣或货币兑换获利的成本模型都会产生负权。贝尔曼-福特算法能正确处理它们，并且还提供第二项服务：它判定“最短路径”何时已不再是一个有意义的问题。如果从源点可达一个总权重为负的环，路径就可以反复经过该环而变得任意廉价，因此不存在有限的最短路径。<sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup><sup> • </sup><sup>[4](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.bellman_ford_predecessor_and_distance.html)</sup>

## 算法的工作原理

该算法使用松弛操作，与迪杰斯特拉算法所用的操作相同。每个顶点 \( v \) 携带一个对从源点 \( s \) 出发的最短路径权重的估计 \( d[v] \)。松弛一条边 \( (u, v) \) 即检验是否有 \( d[v] > d[u] + w(u, v) \)；若是，则更新该估计及前驱指针。估计值只会减小，并收敛到真正的最短路径权重。<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>

按 CLRS 风格给出的过程如下：<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>

1. 初始化：令 \( d[s] = 0 \)，其余每个顶点 \( d[v] = \infty \)。<sup>[1](https://xlinux.nist.gov/dads/HTML/bellmanford.html)</sup>
2. 重复 \( |V| - 1 \) 次：每一轮将图中的每条边松弛一次。<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>
3. 再将每条边检查一次。若某条边 \( (u, v) \) 仍满足 \( d[v] > d[u] + w(u, v) \)，则存在从源点可达的负权环，算法报告失败；否则返回距离与最短路径树。<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>

**为什么 \( V-1 \) 轮足够。** 当不存在可达负权环时，最短路径是简单路径，因而至多包含 \( |V| - 1 \) 条边。经过 \( i \) 轮后，对于最短路径至多使用 \( i \) 条边的每个顶点，估计值都已正确，因为每一轮都会沿任意最短路径，将距离估计已正确的范围至少再向前推进一条边。经过 \( |V| - 1 \) 轮后，每个可达顶点都已确定。最后一轮则检测负权环：若某条边仍可被松弛，说明距离尚未稳定，而这只有在负权环不断降低距离时才可能发生。<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup><sup> • </sup><sup>[9](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>

运行时间由此直接得出：初始化耗时 \( O(V) \)，\( |V| - 1 \) 轮各耗时 \( O(E) \)，最后的检查耗时 \( O(E) \)，总计 \( O(V \cdot E) \)。<sup>[2](https://bobson.ludost.net/books/algo/book6/chap25.htm)</sup>

**检测的范围。** 标准的单源版本只检测从所选源点可达的负权环。源点无法到达的连通分量中的负权环不会被检测到；若要覆盖所有点对，可以运行弗洛伊德-沃舍尔算法并检查其对角线。<sup>[4](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.bellman_ford_predecessor_and_distance.html)</sup><sup> • </sup><sup>[9](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>

## 历史

该算法带有两个名字，因为两位兰德公司的研究者在上世纪五十年代分别提出了密切相关的方法。小 L. R. 福特（L. R. Ford Jr.）的算法产生于网络流理论之中，首次发表于 1956 年的兰德公司论文 P–923。<sup>[6](https://dml.cz/bitstream/handle/10338.dmlcz/401175/DejinyMat_25-2004-1_7.pdf)</sup><sup> • </sup><sup>[10](https://bibbase.org/network/publication/ford-networkflowtheory-1956)</sup> 理查德·贝尔曼（Richard Bellman）的论文《论路由问题》（1958）处理的是在旅行时间随道路质量与交通状况而变的城市之间寻找最短时间路线的问题，他运用动态规划的函数方程技巧，结合策略空间中的近似，得到一种至多经过 \( N - 1 \) 次迭代即收敛的迭代方法。<sup>[5](https://www.cs.yale.edu/homes/lans/readings/routing/bellman-routing-1958.pdf)</sup> 爱德华·F. 摩尔（Edward F. Moore）的独立工作给出了常被称为贝尔曼-福特-摩尔算法的变体。<sup>[7](https://arxiv.org/abs/2607.19346)</sup>

2016 年《理论计算机科学》上的一项结果表明，若只允许最小值与求和运算，经典的贝尔曼-福特-摩尔算法对于最短 \( s \)-\( t \) 路径问题是最优的，因此它的长久统治地位并非仅仅是历史惯性。<sup>[11](https://www.sciencedirect.com/science/article/pii/S0304397516002073)</sup>

## 应用

**距离向量路由。** 运行距离向量协议的路由器针对其邻居执行贝尔曼-福特算法的分布式形式。RFC 1058（1988）中规定的路由信息协议（RIP）是经典例子：每台路由器通告其距离，协议的度量是 1 到 15 之间的整数，网络的最长路径限制为 15 跳。RIP 在解决路由丢失时的“计数到无穷”行为，正反映了网络核心处同样的迭代松弛过程。<sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup><sup> • </sup><sup>[12](https://www.rfc-editor.org/rfc/pdfrfc/rfc1058.txt.pdf)</sup>

**货币套利。** 若将汇率转换为对数，一连串能带来利润的交易就对应于对数空间图中的负总权重环。运行贝尔曼-福特算法并检查最后一轮，恰好能找出这类可利用的循环。<sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup>

**差分约束与重赋权。** 形如 \( x_j - x_i \le c \) 的差分约束系统可映射为带负权的最短路径问题。贝尔曼-福特算法也出现在用于全对最短路径的约翰逊算法内部：它从一个虚拟源点运行一次，计算每个顶点的势，从而产生一个边权非负的重赋权图，随后即可从每个顶点运行更快的迪杰斯特拉算法。<sup>[3](https://prism.nicofortuin.com/algorithms/bellman-ford/)</sup><sup> • </sup><sup>[9](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>

## 与迪杰斯特拉算法的比较

两种算法解决同一个单源问题，但权衡正好相反。<sup>[8](https://dsamindmap.com/compare/dijkstra-vs-bellman-ford)</sup>

| 维度 | 迪杰斯特拉 | 贝尔曼-福特 |
|---|---|---|
| 时间（使用堆） | \( O(E \log V) \) | \( O(V \cdot E) \) |
| 负权 | 不正确，且无提示 | 正确 |
| 负权环检测 | 不可能 | 额外一轮 |
| 方法 | 贪心，按序确定节点 | 反复松弛所有边 |
| 数据结构 | 优先队列 | 普通边表 |

对于稀疏图，即 \( E \) 接近 \( V \) 时，贝尔曼-福特算法大致以 \( O(V^2) \) 运行，而迪杰斯特拉算法为 \( O(V \log V) \)，因此这种通用性是以实实在在的代价换来的。若某一整轮没有改变任何距离，贝尔曼-福特算法可以提前停止，但它不具备迪杰斯特拉算法一旦确定目标顶点即可终止的能力。<sup>[8](https://dsamindmap.com/compare/dijkstra-vs-bellman-ford)</sup><sup> • </sup><sup>[13](https://waggertron.github.io/tech-learning/topics/cs/named-algorithms/bellman-ford/)</sup>

## 变体与实际性能

当某一轮没有带来任何改变时，朴素算法会做无用功。**最短路径快速算法**（SPFA），有时被描述为“带队列的贝尔曼-福特”，只重新处理距离最近得到改进的顶点，这在保持相同最坏情况界的同时加快了典型实例。<sup>[13](https://waggertron.github.io/tech-learning/topics/cs/named-algorithms/bellman-ford/)</sup> 实证研究印证了理论图景：迪杰斯特拉算法在稀疏图上表现最佳，弗洛伊德-沃舍尔算法与丹齐格的 \( O(V^3) \) 方法在稠密图上变得有竞争力，而贝尔曼-福特算法的 \( O(V \cdot E) \) 代价使其成为存在负权边时的专门选择。<sup>[14](https://www.mdpi.com/2673-4591/100/1/31)</sup>

## 突破该界

六十五年多来，贝尔曼-福特-摩尔算法 \( O(n \cdot m) \) 的最坏情况界（\( n \) 为顶点数、\( m \) 为弧数）在一般实值权重下一直未被改进，尽管许多启发式方法在实践中胜过它，却未超越理论极限。<sup>[7](https://arxiv.org/abs/2607.19346)</sup> 2024 年，J. T. 法恩曼（J. T. Fineman）发表了针对负实权单源最短路径的 \( \tilde{O}(mn^{8/9}) \) 时间算法，首次从渐近意义上突破了该界；后续工作进一步改进了运行时间，到 2026 年，运行时间为 \( m^{1+o(1)} \)、几乎与输入规模成线性关系的随机算法已经问世。<sup>[7](https://arxiv.org/abs/2607.19346)</sup> 这些结果改变了负权最短路径的理论格局，但贝尔曼-福特算法仍是教学中采用、通用库中实现的标准简单精确方法：例如 NetworkX 就将其作为处理带负权边图的方法，并在发现负权环时抛出异常。<sup>[4](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.bellman_ford_predecessor_and_distance.html)</sup>

## References

1. [Bellman-Ford algorithm, Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/bellmanford.html)
2. [Chapter 25: Single-Source Shortest Paths (Bellman-Ford), Introduction to Algorithms](https://bobson.ludost.net/books/algo/book6/chap25.htm)
3. [Bellman–Ford — visualized, explained, Prism](https://prism.nicofortuin.com/algorithms/bellman-ford/)
4. [bellman_ford_predecessor_and_distance, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.bellman_ford_predecessor_and_distance.html)
5. [Richard Bellman (1958). On a Routing Problem. Quarterly of Applied Mathematics](https://www.cs.yale.edu/homes/lans/readings/routing/bellman-routing-1958.pdf)
6. [The Bellman–Ford algorithm, historical note, Dejiny Matematiky](https://dml.cz/bitstream/handle/10338.dmlcz/401175/DejinyMat_25-2004-1_7.pdf)
7. [Bellman-Ford in Almost-Linear Time, arXiv](https://arxiv.org/abs/2607.19346)
8. [Dijkstra vs Bellman-Ford: Which to Use, DSA MindMap](https://dsamindmap.com/compare/dijkstra-vs-bellman-ford)
9. [Shortest paths decision tree, The DSA Handbook](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)
10. [L. R. Ford Jr. (1956). Network flow theory, RAND Corporation](https://bibbase.org/network/publication/ford-networkflowtheory-1956)
11. [On the optimality of Bellman–Ford–Moore shortest path algorithm, Theoretical Computer Science](https://www.sciencedirect.com/science/article/pii/S0304397516002073)
12. [C. Hedrick (1988). RFC 1058: Routing Information Protocol](https://www.rfc-editor.org/rfc/pdfrfc/rfc1058.txt.pdf)
13. [Bellman-Ford algorithm, Here Be Dragons](https://waggertron.github.io/tech-learning/topics/cs/named-algorithms/bellman-ford/)
14. [A Performance Comparison of Shortest Path Algorithms in Directed Graphs, MDPI Engineering Proceedings](https://www.mdpi.com/2673-4591/100/1/31)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Graph theory › Computational graph problems and algorithms › Shortest-path problems and algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bellman-ford-algorithm

English original: https://www.edgepedia.org/bellman-ford-algorithm
