# 最短路径算法

最短路径算法在加权图中寻找最小代价路径，是网络路由协议、路线规划、交通控制、社交网络中的路径查找、计算机游戏和运输系统的基础。<sup>[1](https://ar5iv.labs.arxiv.org/html/1705.02044)</sup> 该算法族按问题类别组织：单源（SSSP）求从一个节点到所有其他节点的最短路径，全对（APSP）求每对节点之间的最短路径，单对则求两个选定节点之间的一条路径。输出通常是一张距离表（每个节点一个数值）加上前驱指针，路径本身通过反向追溯恢复；当从源点可达每个顶点、且从源点不可达任何负环时，一次单源运行产生一棵最短路径树；从源点不可达的顶点按惯例赋予无穷大的距离，因此它们的距离仍然有良好定义。<sup>[30](https://www.arl.wustl.edu/~jon.turner/gads/graphAlgorithms/shortPaths/shortPaths.html)</sup> 在最坏情况下，已知的单对问题算法没有渐近快于最佳单源算法的，因此实践中人们求解更大的问题。<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15451-s04/www/Lectures/shortestPaths.pdf)</sup>

| 关键事实 | 数值 |
|---|---|
| 问题类别 | SSSP、APSP、单对；输出为距离加前驱路径<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15451-s04/www/Lectures/shortestPaths.pdf)</sup> |
| Dijkstra（非负权） | 数组实现 O(V²)，二叉堆实现 O((V+E) lg V)，斐波那契堆实现 O(V lg V + E)<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15451-s04/www/Lectures/shortestPaths.pdf)</sup> |
| Bellman–Ford（允许负权） | \( O(V \cdot E) \) 时间；检测可达负环<sup>[3](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)</sup> |
| Floyd–Warshall（APSP） | \( O(n^{3}) \) 时间，\( O(n^{3}) \) 空间可缩减至 \( O(n^{2}) \)<sup>[1](https://ar5iv.labs.arxiv.org/html/1705.02044)</sup> |
| 含负边的 Johnson APSP | 通过重赋权加 n 次 Dijkstra 运行，O(V² lg V + VE)<sup>[2](https://www.cs.cmu.edu/afs/cs/academic/class/15451-s04/www/Lectures/shortestPaths.pdf)</sup> |
| 西欧路网（1800 万节点）上的收缩层次 | 双向 Dijkstra 扫描约 500 万节点缩减至 280 个；查询时间从 2 秒以上降至约 1 毫秒<sup>[4](https://drops.dagstuhl.de/storage/00lipics/lipics-vol173-esa2020/LIPIcs.ESA.2020.20/LIPIcs.ESA.2020.20.pdf)</sup> |
| 2022 年以来的负权 SSSP | 随机化近线性 \( O(m\log^{8}n\log W) \)<sup>[5](https://ieee-focs.org/FOCS-2022-Papers/pdfs/FOCS2022-4Bu7jGV9xIcveUWYj3oWoi/551900a600/551900a600.pdf)</sup>；确定性 \( \tilde{O}(m)\cdot\log(nW) \)（软 O 记号隐藏多对数因子）<sup>[6](https://dl.acm.org/doi/10.1145/3798129.3800740)</sup> |

## 工作原理

该算法族的所有成员都建立在边松弛之上：为每个节点维护一个距离估计 \( d[v] \)，当边 (u, v) 满足 \( d[u]+w(u,v)<d[v] \) 时，将 \( d[v] \) 降至 \( d[u]+w(u,v) \)，并记录 u 为 v 的前驱。<sup>[7](https://www.cs.umd.edu/class/spring2025/cmsc451-0101/Lects/lect04-graph-shortest-path.pdf)</sup> Dijkstra 算法增加一条贪心规则：维护一个顶点集合 S，其中顶点的最终最短路径权重已经确定，反复选择 V − S 中估计值最小的顶点，将其加入 S，并松弛其出边，前提是 \( w(u,v)\ge 0 \)。<sup>[8](https://courses.csail.mit.edu/6.006/fall11/lectures/lecture16.pdf)</sup> 正确性定理指出，在权重函数 w 非负的加权有向图上，以 s 为源点，算法终止时对所有顶点 u 都有 \( u.d=\delta(s,u) \)。<sup>[9](https://webdiis.unizar.es/~jcampos/ab/restringido/dijkstra.pdf)</sup>

非负权重至关重要。考虑一个图，边 s→a 代价为 2，s→b 代价为 7，a→t 代价为 3，b→t 代价为 −3，算法确定 a 的距离为 d = 2，然后返回代价为 5 的路径 s→a→t，永远不会看到代价为 4 的更便宜路径 s→b→t，因为它从不重新考虑已确定的节点。<sup>[10](https://courses.cs.washington.edu/courses/cse417/25au/readings/negative_edges.html)</sup> 给每条边权重加上一个常数并不能解决这个问题，因为边数更多的路径比边数更少的路径获得更多额外代价，而且目前不知道 Dijkstra 算法有任何能处理负代价的简单修改。<sup>[10](https://courses.cs.washington.edu/courses/cse417/25au/readings/negative_edges.html)</sup> 对于负权，Bellman–Ford 转而应用递推式 \( f_{i}=\mathrm{Min}_{j}[t_{ij}+f_{j}] \)，其中 \( f_{N}=0 \)，迭代直到没有估计值发生变化。<sup>[11](https://www.cs.yale.edu/homes/lans/readings/routing/bellman-routing-1958.pdf)</sup>

## 具体做法

使用优先队列运行 Dijkstra 的实践者遵循以下步骤<sup>[8](https://courses.csail.mit.edu/6.006/fall11/lectures/lecture16.pdf)</sup><sup> • </sup><sup>[12](https://stanford-cs161.github.io/winter2024/assets/files/lecture11-notes.pdf)</sup>：

1. 初始化 \( d[s]=0 \)，其余所有顶点 \( d[v]=\infty \)；将所有顶点插入以 d 为键的最小优先队列。
2. 反复删除最小顶点 u（总共 n 次 DeleteMin 调用；顶点从不重新插入），并松弛每条出边，每次成功松弛对应一次 DecreaseKey 调用，总共至多 m 次。<sup>[12](https://stanford-cs161.github.io/winter2024/assets/files/lecture11-notes.pdf)</sup>
3. 随着估计值改进存储前驱指针；路径通过在 O(n) 空间中反向追溯恢复。<sup>[7](https://www.cs.umd.edu/class/spring2025/cmsc451-0101/Lects/lect04-graph-shortest-path.pdf)</sup>

总时间为 \( T=O(m\cdot T_{\mathrm{decreaseKey}}(n)+n\cdot(T_{\mathrm{deleteMin}}(n)+T_{\mathrm{insert}}(n))) \)：Dijkstra 原始数组实现为 \( O(n^{2}) \)，二叉堆实现为 \( O((m+n)\log n) \)，斐波那契堆实现为 \( O(m+n\log n) \)。<sup>[13](https://people.mpi-inf.mpg.de/~mehlhorn/Toolbox/ShortestPaths.pdf)</sup> 基准测试使理论复杂化：在稀疏平面图的 C++ 测试中，二叉堆变体的运行速度约为斐波那契堆和自平衡树变体的两倍，在远不到一秒内计算出多达一百万个顶点的最短路径树。<sup>[14](https://arxiv.org/html/2303.10034v2)</sup>

## 起源

Dijkstra 算法由 E. W. Dijkstra 在1959 年发表于《数值数学》（Numerische Mathematik）的《关于与图有关的两个问题的注记》（A note on two problems in connexion with graphs）中报告，该文解决两个问题：在 n 个节点之间构造一棵最小总长度树，以及在两个给定节点 P 和 Q 之间寻找总长度最小的路径。<sup>[15](https://doi.org/10.1007/bf01386390)</sup><sup> • </sup><sup>[16](https://www.cs.utexas.edu/~EWD/ewd08xx/EWD841a.PDF)</sup>

Bellman–Ford 的动态规划前身由 Richard Bellman 在1958 年发表于《应用数学季刊》（Quarterly of Applied Mathematics）的《论一个路由问题》（On a routing problem）中报告。<sup>[17](https://doi.org/10.1090/qam/102435)</sup> 一种从起点向外展开且从不回溯的标号设定改进由 George B. Dantzig 在1960 年发表于《管理科学》（Management Science）的《论通过网络的最短路线》（On the Shortest Route Through a Network）中报告。<sup>[18](https://doi.org/10.1287/mnsc.6.2.187)</sup> A* 由 Peter Hart、Nils Nilsson 和 Bertram Raphael 在1968 年发表于《IEEE 系统科学与控制论汇刊》（IEEE Transactions on Systems Science and Cybernetics）的《启发式确定最小代价路径的形式基础》（A Formal Basis for the Heuristic Determination of Minimum Cost Paths）中报告。<sup>[19](https://doi.org/10.1109/tssc.1968.300136)</sup> Johnson 的优先队列和稀疏网络算法由 Donald B. Johnson 在1977 年发表于《ACM 杂志》（Journal of the ACM）的《稀疏网络中最短路径的高效算法》（Efficient Algorithms for Shortest Paths in Sparse Networks）中报告。<sup>[20](https://doi.org/10.1145/321992.321993)</sup> 一种近线性时间的负权 SSSP 算法由 Aaron Bernstein、Danupon Nanongkai 和 Christian Wulff-Nilsen 于 2022 年在 arXiv 上报告。<sup>[5](https://ieee-focs.org/FOCS-2022-Papers/pdfs/FOCS2022-4Bu7jGV9xIcveUWYj3oWoi/551900a600/551900a600.pdf)</sup> 一种几乎线性时间的最小代价流算法，可推导出时间复杂度为 \( m^{1+o(1)} \) 的负权 SSSP 算法，由 Li Chen 及其同事于 2022 年在 arXiv 上报告。<sup>[21](https://doi.org/10.48550/arxiv.2203.00671)</sup>

## 变体

**A\*** 通过按 \( v.\mathrm{dist}+h(v) \) 排序优先队列来推广 Dijkstra，其中 h 是估计剩余代价的启发式函数，将搜索聚焦于目标方向。<sup>[22](https://www.cs.cornell.edu/courses/cs2112/2021fa/lectures/ssp/)</sup> Hart、Nilsson 和 Raphael 证明了 A* 是可采纳的：如果 \( h(n) \) 是真实代价的下界，A* 保证找到最优路径；对于道路网络，\( h(n) \) 可以是到目标的直线距离。<sup>[23](https://cs.auckland.ac.nz/courses/compsci709s2c/resources/Mike.d/astarNilsson.pdf)</sup>

**ALT 算法**（A* 搜索、地标、三角不等式），由 Andrew V. Goldberg 和 Chris Harrelson 于 2005 年报告，预处理少量常数个地标，存储所有顶点与每个地标之间的距离，通过三角不等式获得常数时间的下界。<sup>[24](https://doi.org/10.5555/1070432.1070455)</sup>

**收缩层次**按重要性顺序收缩节点，用捷径替换经过被收缩节点的最短路径；查询运行修改后的双向 Dijkstra，在上行图中向前、在下行图中向后，在路径上序最高的节点相遇，按需停滞剪枝那些计算距离次优的搜索。<sup>[25](https://ae.iti.kit.edu/download/contract.pdf)</sup> 在拥有 1800 万节点的西欧道路网络上，原始图中的双向 Dijkstra 平均扫描近 500 万个节点，而在收缩层次覆盖图中仅扫描 280 个节点，查询时间从两秒以上降至约一毫秒。<sup>[4](https://drops.dagstuhl.de/storage/00lipics/lipics-vol173-esa2020/LIPIcs.ESA.2020.20/LIPIcs.ESA.2020.20.pdf)</sup> 在真实世界实例中，收缩层次比普通 Dijkstra 提供约三个数量级的加速，同时辅助数据使网络规模大约翻倍。<sup>[26](https://ad-publications.informatik.uni-freiburg.de/ISAAC_randCH_FS_2015.pdf)</sup>

## 应用

最短路径算法用于网络路由协议、路线规划、交通控制、社交网络中的路径查找、计算机游戏和运输系统。<sup>[1](https://ar5iv.labs.arxiv.org/html/1705.02044)</sup> 二叉堆 Dijkstra 变体是开源库 NetworkX 和 GraphHopper 选择的实现<sup>[14](https://arxiv.org/html/2303.10034v2)</sup>，而在运输网络上，A* 通常比普通 Dijkstra 运行速度快得多。<sup>[14](https://arxiv.org/html/2303.10034v2)</sup>

## 局限与替代方案

**负环。** 从 s 到 t 的最短路径存在，当且仅当从 s 到 t 至少有一条路径，且从 s 到 t 没有路径触及负环；任何触及负环的路径都可以通过再次绕环而缩短，因此存在负环时最短路径可能根本不存在。<sup>[3](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)</sup><sup> • </sup><sup>[10](https://courses.cs.washington.edu/courses/cse417/25au/readings/negative_edges.html)</sup> 如果经过 \( V-1 \) 次迭代后仍有边可以松弛，Bellman–Ford 检测到负环<sup>[3](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)</sup>，总时间为 \( O(V \cdot E) \)。<sup>[3](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)</sup> 存在负环时寻找最短简单路径是 NP 难的，可由哈密顿路径归约证明。<sup>[27](https://www.cs.cmu.edu/~15850/notes/lec3.pdf)</sup>

**负边上的 Dijkstra。** 即使没有负环，Dijkstra 算法在含负边的图上也不正确，因为已确定的顶点从不重新考虑；在其通常实现中，每条边被检查有限次，运行时间仍然是多项式的，而且实践中即使在这类图上，Dijkstra 也常常比 Bellman–Ford 快。<sup>[3](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)</sup>

**静态图与替代方案。** Dijkstra 仅适用于静态图。<sup>[1](https://ar5iv.labs.arxiv.org/html/1705.02044)</sup> 对于有向无环图，基于拓扑排序的 SSSP 运行时间为 \( O(E) \)。<sup>[28](https://www.cs.princeton.edu/courses/archive/spring24/cos226/lectures/44ShortestPaths.pdf)</sup> 基于队列的 Bellman–Ford 维护一个距离发生变化的顶点队列，保持 Θ(EV) 最坏情况，但实践中快得多。<sup>[28](https://www.cs.princeton.edu/courses/archive/spring24/cos226/lectures/44ShortestPaths.pdf)</sup>

**2022 年以来的负权突破。** 几十年来，\( O(m \cdot n) \) 的 Bellman–Ford 一直是已知最快的通用边权 SSSP 算法。<sup>[27](https://www.cs.cmu.edu/~15850/notes/lec3.pdf)</sup> 2022 年，Bernstein、Nanongkai 和 Wulff-Nilsen 给出了一种随机化 Las Vegas 算法，对于 m 条边的图耗时 \( O(m\log^{8}(n)\log W) \)，返回最短路径树或负权环。<sup>[5](https://ieee-focs.org/FOCS-2022-Papers/pdfs/FOCS2022-4Bu7jGV9xIcveUWYj3oWoi/551900a600/551900a600.pdf)</sup> Bringmann、Cassis 和 Fischer 的后续工作将运行时间降至 \( O(m\log^{2}(n)\log(nW)\log\log n) \)。<sup>[29](https://cacm.acm.org/research-highlights/negative-weight-single-source-shortest-paths-in-near-linear-time/)</sup> 确定性来自一种基于路径覆盖的算法，这是一种确定性近线性时间的负权 SSSP 算法，对于 {−W, …, W} 中的整数权重，运行时间为 O(m)·log(nW)。<sup>[6](https://dl.acm.org/doi/10.1145/3798129.3800740)</sup>

## References

1. [A Survey of Shortest-Path Algorithms](https://ar5iv.labs.arxiv.org/html/1705.02044)
2. [Shortest Paths (CLRS Chapter 24-25 lecture notes, CMU)](https://www.cs.cmu.edu/afs/cs/academic/class/15451-s04/www/Lectures/shortestPaths.pdf)
3. [Erickson, Algorithms: Single-Source Shortest Paths (Chapter 8)](https://jeffe.cs.illinois.edu/teaching/algorithms/book/08-sssp.pdf)
4. [Lower Bounds and Approximation Algorithms for Search Space Sizes in Contraction Hierarchies](https://drops.dagstuhl.de/storage/00lipics/lipics-vol173-esa2020/LIPIcs.ESA.2020.20/LIPIcs.ESA.2020.20.pdf)
5. [Negative-Weight Single-Source Shortest Paths in Near-Linear Time (FOCS 2022)](https://ieee-focs.org/FOCS-2022-Papers/pdfs/FOCS2022-4Bu7jGV9xIcveUWYj3oWoi/551900a600/551900a600.pdf)
6. [Deterministic Negative-Weight Shortest Paths in Nearly Linear Time via Path Covers (STOC 2026)](https://dl.acm.org/doi/10.1145/3798129.3800740)
7. [CMSC 451 Lecture 4: Graph Shortest Paths, Dijkstra and Bellman-Ford](https://www.cs.umd.edu/class/spring2025/cmsc451-0101/Lects/lect04-graph-shortest-path.pdf)
8. [MIT 6.006 Lecture 16: Shortest Paths II, Dijkstra](https://courses.csail.mit.edu/6.006/fall11/lectures/lecture16.pdf)
9. [Introduction to Algorithms, Third Edition (CLRS), Chapter 24: Dijkstra's algorithm](https://webdiis.unizar.es/~jcampos/ab/restringido/dijkstra.pdf)
10. [CSE417 Reading: Negative Weight Edges (University of Washington)](https://courses.cs.washington.edu/courses/cse417/25au/readings/negative_edges.html)
11. [On a Routing Problem, Richard Bellman (Quarterly of Applied Mathematics, 1958)](https://www.cs.yale.edu/homes/lans/readings/routing/bellman-routing-1958.pdf)
12. [Stanford CS 161 Winter 2024 Lecture 11: Dijkstra with a priority queue](https://stanford-cs161.github.io/winter2024/assets/files/lecture11-notes.pdf)
13. [Shortest Paths (Algorithm Toolbox chapter, Mehlhorn & Sanders)](https://people.mpi-inf.mpg.de/~mehlhorn/Toolbox/ShortestPaths.pdf)
14. [A Comparison of Dijkstra's Algorithm Using Fibonacci Heaps, Binary Heaps, and Self-Balancing Binary Trees](https://arxiv.org/html/2303.10034v2)
15. [E. W. Dijkstra (1959). A note on two problems in connexion with graphs. Numerische Mathematik.](https://doi.org/10.1007/bf01386390)
16. [EWD 841a, Dijkstra's 1982 Citation Classic reflection](https://www.cs.utexas.edu/~EWD/ewd08xx/EWD841a.PDF)
17. [Richard Bellman (1958). On a routing problem. Quarterly of Applied Mathematics.](https://doi.org/10.1090/qam/102435)
18. [George B. Dantzig (1960). On the Shortest Route Through a Network. Management Science.](https://doi.org/10.1287/mnsc.6.2.187)
19. [Peter Hart, Nils Nilsson, Bertram Raphael (1968). A Formal Basis for the Heuristic Determination of Minimum Cost Paths. IEEE Transactions on Systems Science and Cybernetics.](https://doi.org/10.1109/tssc.1968.300136)
20. [Donald B. Johnson (1977). Efficient Algorithms for Shortest Paths in Sparse Networks. Journal of the ACM.](https://doi.org/10.1145/321992.321993)
21. [Chen, Li and colleagues (2022). Maximum Flow and Minimum-Cost Flow in Almost-Linear Time. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2203.00671)
22. [Cornell CS 2112: Dijkstra's single-source shortest path algorithm](https://www.cs.cornell.edu/courses/cs2112/2021fa/lectures/ssp/)
23. [A Formal Basis for the Heuristic Determination of Minimum Cost Paths (Hart, Nilsson, Raphael)](https://cs.auckland.ac.nz/courses/compsci709s2c/resources/Mike.d/astarNilsson.pdf)
24. [Andrew V. Goldberg, Chris Harrelson (2005). Computing the shortest path: A search meets graph theory. .](https://doi.org/10.5555/1070432.1070455)
25. [Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks (Geisberger, Sanders, Schultes, Vetter)](https://ae.iti.kit.edu/download/contract.pdf)
26. [Provable Efficiency of Contraction Hierarchies with Randomized Preprocessing](https://ad-publications.informatik.uni-freiburg.de/ISAAC_randCH_FS_2015.pdf)
27. [CMU 15-850 Advanced Algorithms, Lecture 3: Shortest Paths in Graphs](https://www.cs.cmu.edu/~15850/notes/lec3.pdf)
28. [Princeton COS 226, Lecture 44: Shortest Paths](https://www.cs.princeton.edu/courses/archive/spring24/cos226/lectures/44ShortestPaths.pdf)
29. [Negative-Weight Single-Source Shortest Paths in Near-Linear Time (CACM Research Highlight)](https://cacm.acm.org/research-highlights/negative-weight-single-source-shortest-paths-in-near-linear-time/)
30. [Shortest Paths in Directed Graphs](https://www.arl.wustl.edu/~jon.turner/gads/graphAlgorithms/shortPaths/shortPaths.html)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 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/shortest-path-algorithm

English original: https://www.edgepedia.org/shortest-path-algorithm
