# Dijkstra 算法

**Dijkstra 算法**是一种贪心算法，用于在边权非负的加权图中，计算从单个源节点到所有其他节点的最短路径。[艾兹格·W·迪杰斯特拉](https://www.edgepedia.org/zh/edsger-w-dijkstra)（Edsger W. Dijkstra）于 1956 年构想出该算法，1959 年在《数值数学》（*Numerische Mathematik*）杂志上发表了一篇三页长的文章《关于图论中两个问题的注记》（*A Note on Two Problems in Connexion with Graphs*）<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup><sup> • </sup><sup>[2](https://research.tue.nl/en/publications/a-note-on-two-problems-in-connexion-with-graphs/)</sup>。在无负权图的最短路径问题中，它至今仍是默认方法，也是路由协议、地图服务和网络优化软件的基础<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 解决的问题 | 边权 \( w(u, v) \) 非负的图 \( G = (V, E) \) 中的单源最短路径；也可用于单源–单目标查询并提前终止<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup> |
| 起源 | 由艾兹格·W·迪杰斯特拉于 1956 年构想，用于演示 ARMAC 计算机；1959 年发表于《数值数学》第 1 卷，第 269–271 页<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup><sup> • </sup><sup>[2](https://research.tue.nl/en/publications/a-note-on-two-problems-in-connexion-with-graphs/)</sup> |
| 运行时间 | 简单数组为 \( O(\lvert V \rvert^2) \)，二叉堆为 \( O((\lvert V \rvert + \lvert E \rvert) \log \lvert V \rvert) \)，斐波那契堆为 \( O(\lvert E \rvert + \lvert V \rvert \log \lvert V \rvert) \)<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup> |
| 权重限制 | 仅适用于非负权重；在负权重下算法会无报错地运行完毕，但返回的距离可能错得任意离谱<sup>[4](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup> |
| 负权重替代方案 | 贝尔曼–福特算法（Bellman–Ford）在不存在负环的情况下，以 \( O(nm) \) 时间解决一般单源问题<sup>[5](https://doi.org/10.1145/3816151)</sup> |
| 实际部署 | OSPF 链路状态路由使用 Dijkstra 算法计算最短路径树；收缩层级使用改良的双向 Dijkstra，在大陆规模的道路网络上只访问数百个节点<sup>[6](https://www.rfc-editor.org/rfc/rfc1583.pdf)</sup><sup> • </sup><sup>[7](https://pubsonline.informs.org/doi/pdf/10.1287/trsc.1110.0401)</sup> |

## 工作原理

该算法为每个顶点维护一个暂定距离 \( dist[v] \)，即从源点 \( s \) 出发的真实最短路径距离的上界。初始化时设 \( dist[s] = 0 \)，其余所有顶点设为无穷大<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup>。

每一轮迭代选出暂定距离最小的未访问顶点 \( u \)，将其距离标记为最终值，并松弛它的出边：对每个邻居 \( v \)，计算 \( alt = dist[u] + w(u, v) \)，当 \( alt < dist[v] \) 时更新 \( dist[v] = alt \)，同时记录 \( u \) 为 \( v \) 的前驱，以便之后重建路径本身<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup>。

这种“一次确定”步骤的正确性依赖于非负权重。在所有尚未确定最终距离的顶点中，暂定距离最小的那个必定已经处于其真实最短距离上，因为任何更短的路径都必须经过已确定集合之外的某个顶点，而在非负权重下，经过这样的顶点的距离不可能更小<sup>[8](https://users.ece.utexas.edu/~garg/publicCompanion/chapter7-classical.html)</sup>。算法终止时，距离向量中保存的就是从源点到每个可达顶点的最短路径长度<sup>[9](https://gtl.csa.iisc.ac.in/dsa/node162.html)</sup>。

## 起源

迪杰斯特拉于 1952 年至 1962 年在阿姆斯特丹的数学中心（Mathematisch Centrum）工作，后来称这个最短路径算法是“我声名的一大基石之一”。他形容这是二十分钟的发明，是他和未婚妻丽娅（Ria）在咖啡馆露台上喝咖啡时想出来的<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup>。1956 年他首次用它来展示 ARMAC 计算机，特意选了一个外行听众也能理解的问题：在简化地图上，荷兰两座城市之间的最短路线——从[鹿特丹](https://www.edgepedia.org/zh/rotterdam)到[格罗宁根](https://www.edgepedia.org/zh/groningen)<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup>。论文于 1959 年发表<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup>。丹麦计算机科学家、算法与数据结构研究者米克尔·托鲁普（Mikkel Thorup）在 1999 年总结了它的地位：自 1959 年以来，关于一般有向图和无向图单源最短路径的所有理论进展，都建立在 Dijkstra 算法之上<sup>[1](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)</sup>。

迪杰斯特拉自己的方法是一个 \( O(n^2) \) 算法，实现起来比丹齐格（Dantzig）的方法更容易，因为寻找下一个使 \( d(v) \) 最小的顶点 \( v \) 只需扫描所有顶点，无需维护任何列表<sup>[10](https://homepages.cwi.nl/~lex/files/histco.pdf)</sup>。

## 复杂度与优先队列

运行时间几乎完全取决于算法如何找到暂定距离最小的未访问顶点。扫描简单数组为 \( O(\lvert V \rvert^2) \)；二叉堆为 \( O((\lvert V \rvert + \lvert E \rvert) \log \lvert V \rvert) \)；[斐波那契堆](https://www.edgepedia.org/zh/fibonacci-heap)支持均摊常数时间的减小键值操作，为 \( O(\lvert E \rvert + \lvert V \rvert \log \lvert V \rvert) \)<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup>。斐波那契堆正是为提供 Dijkstra 算法的高效实现而设计的<sup>[11](https://collaborate.princeton.edu/en/publications/thin-heaps-thick-heaps/)</sup>。

**理论与实践在这里分道扬镳。** 斐波那契堆的常数开销很高，正确实现也很复杂，因此尽管其渐近界更好，在典型问题规模下却可能比二叉堆更慢；例如，NetworkX 就用 Python 内置的二叉堆实现 Dijkstra 算法<sup>[3](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)</sup>。2023 年，卡迪夫大学的里迪安·刘易斯（Rhydian Lewis）做了一项实证研究，比较了使用斐波那契堆、二叉堆和自平衡二叉树的 C++ Dijkstra 实现，发现最快的方法并不总是渐近复杂度最低的那个<sup>[12](https://arxiv.org/abs/2303.10034)</sup>。实践者也报告了同样的模式：尽管原始描述并不建议使用斐波那契堆，但二叉堆或 d 叉堆实现在实践中要明显更快<sup>[13](https://arxiv.org/abs/1505.05033)</sup><sup> • </sup><sup>[19](https://www.cl.cam.ac.uk/teaching/1213/AlgorithII/1987-FredmanTar-fibonacci.pdf)</sup>。

## 负权重与相关算法

Dijkstra 算法在负边权下失效，因为“一次确定”不变式被打破了：之后访问某个节点可能得到更低的路径总和，贝尔曼最优性原理不再适用<sup>[14](https://labs.acme.byu.edu/Volume2/Dijkstra/Dijkstra.html)</sup>。这种失效是无提示的。没有运行时检查、没有异常、没有越界错误；算法会正常终止，返回一个错误——且误差可能任意大的——距离数组<sup>[4](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>。

两种补救尝试也都会失败。允许在找到更短路径时重新访问节点，能给出正确答案，但会破坏 \( O((V + E) \log V) \) 的界，在对抗性输入下退化为指数时间。给每条边权加上一个常数使所有权重变为非负，同样不能保持最短路径，因为偏移在较长路径上累积得比较短路径更多。要正确地对图重新赋权，需使用约翰逊算法（Johnson's algorithm）：它先添加一个虚拟源节点，运行贝尔曼–福特算法为每个顶点计算一个势函数，然后以重赋权后的边从每个顶点执行 Dijkstra<sup>[4](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>。

当可能出现负权重时，贝尔曼–福特算法是标准的替代方案。它跳过选择最小距离顶点这一步，只是反复松弛每条边，这使其更慢（\( O(nm) \)），但更灵活：只要不存在负环，它就能处理负权重；在第 \( V \) 次遍历时检测出从源点可达的负环；并支持去中心化更新<sup>[5](https://doi.org/10.1145/3816151)</sup><sup> • </sup><sup>[15](https://web.stanford.edu/class/archive/cs/cs161/cs161.1236/Lectures/Lecture12/Lecture12-compressed.pdf)</sup>。对于无权图，广度优先搜索以 \( O(V + E) \) 时间运行；对于权重仅限于 0 和 1 的情况，使用双端队列的 0-1 BFS 能达到同样的线性界<sup>[4](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>。

A* 搜索是 Dijkstra 加上一个启发式函数：下一个顶点在优先队列中按估计总代价而非仅按距离选取。其最坏情况界与 Dijkstra 完全相同，只有当启发函数既可采纳——即对每个 \( v \) 都有 \( h(v) \le dist(v, goal) \)——又具有信息量时，改进才来自常数因子。当 \( h \equiv 0 \) 时，A* 就完全等同于 Dijkstra，每次扩展都一一对应<sup>[4](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)</sup>。

## 应用

凡是沿网络累积非负代价的场景，都会用到 Dijkstra 算法。OSPF 路由协议（RFC 1583）将其作为路由表计算的第一阶段：每台路由器以自身为根，从链路状态数据库构建最短路径树，沿候选路径累加通告的链路代价并取最小值<sup>[6](https://www.rfc-editor.org/rfc/rfc1583.pdf)</sup>。地理信息系统将其用于网络路由和分配；1993 年的一项研究将其应用于 Arc/Info GIS 中，用于排水管网验证、流域等时线确定和稳态水流计算，并指出了其局限：必须在确定流量大小之前就已知对流动的阻力<sup>[16](https://ascelibrary.org/doi/10.1061/%28ASCE%290733-9496%281993%29119%3A2%28229%29)</sup>。手持设备上的行人导航系统已将 Dijkstra 与 RFID、二维条码和 WiFi 定位结合，在数字城市模型上即时规划路线<sup>[17](https://www.sciencedirect.com/science/article/abs/pii/S0926580509001885)</sup>。

**道路网络推动了对朴素 Dijkstra 的超越。** 在大陆规模的网络上，收缩层级在预处理阶段利用道路的层级结构添加捷径边；随后改良的双向 Dijkstra 在不到一毫秒内找到精确最短路径，只访问数百个节点，快到可以在移动设备上运行，并能处理交通拥堵等实时变化<sup>[7](https://pubsonline.informs.org/doi/pdf/10.1287/trsc.1110.0401)</sup>。时间相关收缩层级将同一框架扩展到考虑拥堵的旅行时间，但其通常的查询过程仍依赖双向 Dijkstra 搜索<sup>[18](https://ojs.aaai.org/index.php/ICAPS/article/view/19818)</sup>。

## References

1. [Edsger W. Dijkstra: Brilliant, colourful, and opinionated, Centrum Wiskunde & Informatica](https://www.cwi.nl/en/about/history/e-w-dijkstra-brilliant-colourful-and-opinionated/)
2. [A note on two problems in connexion with graphs, E.W. Dijkstra, Numerische Mathematik 1 (1959), TU Eindhoven research portal](https://research.tue.nl/en/publications/a-note-on-two-problems-in-connexion-with-graphs/)
3. [Dijkstra's Algorithm, NetworkX 3.7 documentation](https://networkx.org/documentation/stable/reference/algorithms/shortest_paths/dijkstra.html)
4. [Shortest-paths decision tree, The DSA Handbook](https://dsa.handbook.academy/patterns/shortest-paths-decision-tree/)
5. [Lower Bounds for Adaptive Relaxation-Based Algorithms for Single-Source Shortest Paths, ACM Transactions on Algorithms](https://doi.org/10.1145/3816151)
6. [OSPF Version 2, RFC 1583](https://www.rfc-editor.org/rfc/rfc1583.pdf)
7. [Exact Routing in Large Road Networks Using Contraction Hierarchies, Transportation Science 46(3), 2012](https://pubsonline.informs.org/doi/pdf/10.1287/trsc.1110.0401)
8. [Chapter 7, The Shortest Path Problem (Classical), University of Texas at Austin](https://users.ece.utexas.edu/~garg/publicCompanion/chapter7-classical.html)
9. [Single Source Shortest Paths Problem: Dijkstra's Algorithm, Indian Institute of Science DSA notes](https://gtl.csa.iisc.ac.in/dsa/node162.html)
10. [Dijkstra 1959: historical commentary, CWI](https://homepages.cwi.nl/~lex/files/histco.pdf)
11. [Thin heaps, thick heaps, Princeton University](https://collaborate.princeton.edu/en/publications/thin-heaps-thick-heaps/)
12. [Rhydian Lewis (2023). A Comparison of Dijkstra's Algorithm Using Fibonacci Heaps, Binary Heaps, and Self-Balancing Binary Trees. arXiv:2303.10034](https://arxiv.org/abs/2303.10034)
13. [Optimizing Dijkstra for real-world performance, arXiv:1505.05033](https://arxiv.org/abs/1505.05033)
14. [Dijkstra's Algorithm, ACME Labs, Brigham Young University](https://labs.acme.byu.edu/Volume2/Dijkstra/Dijkstra.html)
15. [Bellman-Ford vs. Dijkstra, Stanford CS161 Lecture 12](https://web.stanford.edu/class/archive/cs/cs161/cs161.1236/Lectures/Lecture12/Lecture12-compressed.pdf)
16. [Djokic, D. and Maidment, D.R. (1993). Application of GIS Network Routines for Water Flow and Transport. Journal of Water Resources Planning and Management 119(2)](https://ascelibrary.org/doi/10.1061/%28ASCE%290733-9496%281993%29119%3A2%28229%29)
17. [Delivery of pedestrian real-time location and routing information to mobile architectural guide, Automation in Construction](https://www.sciencedirect.com/science/article/abs/pii/S0926580509001885)
18. [Improving Time-Dependent Contraction Hierarchies, ICAPS](https://ojs.aaai.org/index.php/ICAPS/article/view/19818)
19. [cl.cam.ac.uk](https://www.cl.cam.ac.uk/teaching/1213/AlgorithII/1987-FredmanTar-fibonacci.pdf)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Graph and network algorithms › Shortest paths

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/dijkstras-algorithm

English original: https://www.edgepedia.org/dijkstras-algorithm
