e01.org

Dijkstra's algorithm

Dijkstra's algorithm ( , ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later. Dijkstra's algorithm finds the shortest path from a given source node to every other node. It can be used to find the shortest path to a specific destination node, by terminating the algorithm after determining the shortest path to that node. For example, if the nodes of the graph represent cities, and the costs of edges represent the distances between pairs of cities connected by a direct road, then Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. A common application of shortest path algorithms is network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and OSPF (Open Shortest Path First). It is also employed as a subroutine in algorithms such...

Dijkstra's algorithm ( , ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later. Dijkstra's algorithm finds the shortest path from a given source node to every other node. It can be used to find the shortest path to a specific destination node, by terminating the algorithm after determining the shortest path to that node. For example, if the nodes of the graph represent cities, and the costs of edges represent the distances between pairs of cities connected by a direct road, then Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. A common application of shortest path algorithms is network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and OSPF (Open Shortest Path First). It is also employed as a subroutine in algorithms such as Johnson's algorithm. The algorithm uses a min-priority queue data structure for selecting the shortest paths known so far. Before more advanced priority queue structures were discovered, Dijkstra's original algorithm ran in \Theta(|V|^2) time, where |V| is the number of nodes. proposed a Fibonacci heap priority queue to optimize the running time complexity to \Theta(|E|+|V|\log|V|) , where |E| is the number of edges. This is asymptotically the fastest known single-source shortest-path algorithm for arbitrary directed graphs with unbounded non-negative weights. However, specialized cases (such as bounded/integer weights, directed acyclic graphs etc.) can be improved further. If preprocessing is allowed, algorithms such as contraction hierarchies can be up to seven orders of magnitude faster. Dijkstra's algorithm is commonly used on graphs where the edge weights are positive integers or real numbers. It can be generalized to any graph where the edge weights are partially ordered, provided the subsequent labels (a subsequent label is produced when traversing an edge) are monotonically non-decreasing. In many fields, particularly artificial intelligence, Dijkstra's algorithm or a variant offers a uniform cost search and is formulated as an instance of the more general idea of best-first search. For the current node, consider all of its unvisited neighbors and update their distances through the current node; compare the newly calculated distance to the one currently assigned to the neighbor and assign the smaller one to it. For example, if the current node A is marked with a distance of 6, and the edge connecting it with its neighbor B has length 2, then the distance to B through A is 6 + 2 = 8. If B was previously marked with a distance greater than 8, then update it to 8 (the path to B through A is shorter). Otherwise, keep its current distance (the path to B through A is not the shortest). After considering all of the current node's unvisited neighbors, the current node is removed from the unvisited set. Thus a visited node is never rechecked, which is correct because the distance recorded on the current node is minimal (as ensured in step 3), and thus final. Repeat from step 3. Once the loop exits (steps 3–5), every visited node contains its shortest distance from the starting node. ==Pseudocode== In the following pseudocode, is an array that contains the current distances from the to other vertices, i.e. is the current distance from the source to the vertex . The array contains pointers to previous-hop nodes on the shortest path from source to the given vertex (equivalently, it is the next-hop on the path from the given vertex to the source). The code , searches for the vertex in the vertex set that has the least value. returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes and . The variable on line 14 is the length of the path from the node to the neighbor node if it were to go through . If this path is shorter than the current shortest path recorded for , then the distance of is updated to . [[File:DijkstraDemo.gif|thumb|A demo of Dijkstra's algorithm based on Euclidean distance. Red lines are the shortest path covering, i.e., connecting u and prev[u]. Blue lines indicate where relaxing happens, i.e., connecting v with a node u in Q, which gives a shorter path from the source to v. 1 function Dijkstra(Graph, source): 2 3 for each vertex v in Graph.Vertices: 4 dist[v] ← INFINITY 5 prev[v] ← UNDEFINED 6 add v to Q 7 dist[source] ← 0 8 9 while Q is not empty: 10 u ← vertex in Q with minimum dist[u] 11 Q.remove(u) 12 13 for each edge (u, v) in Graph: 14 alt ← dist[u] + Graph.Distance(u,v) 15 if alt if alt block, the becomes an operation. Yet another alternative is to add nodes unconditionally to the priority queue and to instead check after extraction ( u ← Q.extract_min() ) that it isn't revisiting, or that no shorter connection was found yet in the if alt block. This can be done by additionally extracting the associated priority p from the queue and only processing further if p == dist[u] inside the while Q is not empty loop. These alternatives can use entirely array-based priority queues without decrease-key functionality, which have been found to achieve even faster computing times in practice. However, the difference in performance was found to be narrower for denser graphs. The simplest version of Dijkstra's algorithm stores the vertex set as a linked list or array, and edges as an adjacency list or matrix. In this case, extract-minimum is simply a linear search through all vertices in , so the running time is \Theta(|E| + |V|^2) = \Theta(|V|^2) . For sparse graphs, that is, graphs with far fewer than |V|^2 edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a self-balancing binary search tree, binary heap, pairing heap, Fibonacci heap or a priority heap as a priority queue to implement extracting minimum efficiently. To perform decrease-key steps in a binary heap efficiently, it is necessary to use an auxiliary data structure that maps each vertex to its position in the heap, and to update this structure as the priority queue changes. With a self-balancing binary search tree or binary heap, the algorithm requires \Theta((|E| + |V|) \log |V|) time in the worst case; for connected graphs this time bound can be simplified to \Theta( | E | \log | V | ) . The Fibonacci heap improves this to \Theta(|E| + |V| \log|V|) . When using binary heaps, the average case time complexity is lower than the worst-case: assuming edge costs are drawn independently from a common probability distribution, the expected number of decrease-key operations is bounded by \Theta(|V| \log (|E|/|V|)) , giving a total running time of ===Practical optimizations and infinite graphs=== In common presentations of Dijkstra's algorithm, initially all nodes are entered into the priority queue. This is, however, not necessary: the algorithm can start with a priority queue that contains only one item, and insert new items as they are discovered (instead of doing a decrease-key, check whether the key is in the queue; if it is, decrease its key, otherwise insert it). This variant has the same worst-case bounds as the common variant, but maintains a smaller priority queue in practice, speeding up queue operations. Moreover, not inserting all nodes in a graph makes it possible to extend the algorithm to find the shortest path from a single source to the closest of a set of target nodes on infinite graphs or those too large to represent in memory. The resulting algorithm is called uniform-cost search (UCS) in the artificial intelligence literature and can be expressed in pseudocode as procedure uniform_cost_search(start) is node ← start frontier ← priority queue containing node only expanded ← empty set do if frontier is empty then return failure node ← frontier.pop() if node is a goal state then return solution(node) expanded.add(node) for each of node's neighbors n do if n is not in expanded and not in frontier then frontier.add(n) else if n is in frontier with higher cost replace existing node with n Its complexity can be expressed in an alternative way for very large graphs: when is the length of the shortest path from the start node to any node satisfying the "goal" predicate, each edge has cost at least , and the number of neighbors per node is bounded by , then the algorithm's worst-case time and space complexity are both in . Further optimizations for the single-target case include bidirectional variants, goal-directed variants such as the A* algorithm (see ), graph pruning to determine which nodes are likely to form the middle segment of shortest paths (reach-based routing), and hierarchical decompositions of the input graph that reduce routing to connecting and to their respective "transit nodes" followed by shortest-path computation between these transit nodes using a "highway". Combinations of such techniques may be needed for optimal practical performance on specific problems. ===Practical performance considerations=== Although Dijkstra's algorithm is optimal for graphs with non-negative edge weights, its practical runtime depends on both data structures and graph properties. Using a binary heap results in a running time of O((V+E)logV). Alternatives such as Fibonacci heaps provide better theoretical bounds but often perform worse in real applications because of large constant factors. Graph structure also plays a major role. Sparse networks, such as road maps, allow Dijkstra's algorithm to operate efficiently since most vertices have low degree. Dense graphs increase relaxations and slow down the process. Because of this, modern routing systems often use Dijkstra's algorithm together with preprocessing methods such as A* search, landmark heuristics, or contraction hierarchies, which significantly reduce the search space. Memory locality is another important factor. Cache-optimized priority queues and adjacency layouts can reduce latency for large graphs that exceed CPU cache limitations. Because of these considerations, most real-world systems use specialized Dijkstra variants tuned for specific graph families and hardware constraints. ===Specialized variants=== When arc weights are small integers (bounded by a parameter C ), specialized queues can be used for increased speed. The first algorithm of this type was Dial's algorithm for graphs with positive integer edge weights, which uses a bucket queue to obtain a running time The use of a van Emde Boas tree as the priority queue brings the complexity to Another interesting variant based on a combination of a new radix heap and the well-known Fibonacci heap runs in time Finally, the best algorithms in this special case run in O(|E|\log\log|V|) time and O(|E| + |V|\min\{(\log|V|)^{1/3+\varepsilon}, (\log C)^{1/4+\varepsilon}\}) time. ==Notes==

References

  1. ^ Controversial, see Moshe Sniedovich (2006). "Dijkstra's algorithm revisited: the dynamic programming connexion" . Control and Cybernetics . 35 : 599– 620. and below part . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  2. ^ "ARMAC" . Unsung Heroes in Dutch Computing History . 2007. Archived from the original on 13 November 2013. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  3. ^ Tarjan, Robert Endre (1983), Data Structures and Network Algorithms , CBMS_NSF Regional Conference Series in Applied Mathematics, vol. 44, Society for Industrial and Applied Mathematics, p. 75, The third classical minimum spanning tree algorithm was discovered by Jarník and rediscovered by Prim and Dikstra; it is commonly known as Prim's algorithm. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  4. ^ Prim, R.C. (1957). "Shortest connection networks and some generalizations" (PDF) . Bell System Technical Journal . 36 (6): 1389– 1401. Bibcode : 1957BSTJ...36.1389P . doi : 10.1002/j.1538-7305.1957.tb01515.x . Archived from the original (PDF) on 18 July 2017 . Retrieved 18 July 2017 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  5. ^ V. Jarník: O jistém problému minimálním [About a certain minimal problem], Práce Moravské Přírodovědecké Společnosti, 6, 1930, pp. 57–63. (in Czech) Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  6. ^ Gass, Saul; Fu, Michael (2013). "Dijkstra's Algorithm". In Gass, Saul I; Fu, Michael C (eds.). Encyclopedia of Operations Research and Management Science . Vol. 1. Springer. doi : 10.1007/978-1-4419-1153-7 . ISBN 978-1-4419-1137-7 – via Springer Link. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  7. ^ Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2022) [1990]. "22". Introduction to Algorithms (4th ed.). MIT Press and McGraw-Hill. pp. 622– 623. ISBN 0-262-04630-X . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  8. ^ Sometimes also least-cost-first search : Nau, Dana S. (1983). "Expert computer systems" (PDF) . Computer . 16 (2). IEEE: 63– 85. doi : 10.1109/mc.1983.1654302 . S2CID 7301753 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  9. ^ Bauer, Reinhard; Delling, Daniel; Sanders, Peter; Schieferdecker, Dennis; Schultes, Dominik; Wagner, Dorothea (2010). "Combining hierarchical and goal-directed speed-up techniques for Dijkstra's algorithm" . ACM Journal of Experimental Algorithmics . 15 : 2.1. doi : 10.1145/1671970.1671976 . S2CID 1661292 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  10. ^ Goldberg, Andrew V.; Harrelson, Chris (2005). Computing the Shortest Path: A* Search Meets Graph Theory . Proceedings of the Sixteenth Annual ACM–SIAM Symposium on Discrete Algorithms (SODA). Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  11. ^ Haeupler, Bernhard; Hladík, Richard; Rozhon, Václav; Tarjan, Robert E. ; Tetek, Jakub (2025). "Bidirectional Dijkstra's algorithm is instance-optimal". In Bercea, Ioana Oriana; Pagh, Rasmus (eds.). 2025 Symposium on Simplicity in Algorithms, SOSA 2025, New Orleans, LA, USA, January 13–15, 2025 . Society for Industrial and Applied Mathematics. pp. 202– 215. arXiv : 2410.14638 . doi : 10.1137/1.9781611978315.16 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  12. ^ Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2009). Introduction to Algorithms (3rd ed.). MIT Press. pp. 658– 663. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  13. ^ Geisberger, Robert; Sanders, Peter; Schultes, Dominik; Delling, Daniel (2008). Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks . Experimental Algorithms (WEA 2008). Lecture Notes in Computer Science. Vol. 5038. Springer. pp. 319– 333. doi : 10.1007/978-3-540-68552-4_24 . ISBN 978-3-540-68548-7 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  14. ^ Sanders, Peter; Schultes, Dominik (2005). Highway Hierarchies Hasten Exact Shortest Path Queries . Algorithms – ESA 2005. Lecture Notes in Computer Science. Vol. 3669. Springer. pp. 568– 579. doi : 10.1007/11561071_51 . ISBN 978-3-540-29118-3 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  15. ^ Haeupler, Bernhard; Hladík, Richard; Rozhoň, Václav; Tarjan, Robert; Tětek, Jakub (28 October 2024). "Universal Optimality of Dijkstra via Beyond-Worst-Case Heaps". arXiv : 2311.11793 [ cs.DS ]. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  16. ^ Brubaker, Ben (25 October 2024). "Computer Scientists Establish the Best Way to Traverse a Graph" . Quanta Magazine . Retrieved 9 December 2024 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  17. ^ Richards, Hamilton. "Edsger Wybe Dijkstra" . A.M. Turing Award . Association for Computing Machinery . Retrieved 16 October 2017 . At the Mathematical Centre a major project was building the ARMAC computer. For its official inauguration in 1956, Dijkstra devised a program to solve a problem interesting to a nontechnical audience: Given a network of roads connecting cities, what is the shortest route between two designated cities? Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  18. ^ Schrijver, Alexander (2012). "On the history of the shortest path problem" (PDF) . Optimization Stories . Documenta Mathematica Series. Vol. 6. pp. 155– 167. doi : 10.4171/dms/6/19 . ISBN 978-3-936609-58-5 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  19. ^ Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs" (PDF) . Numerische Mathematik . 1 : 269– 271. CiteSeerX 10.1.1.165.7577 . doi : 10.1007/BF01386390 . S2CID 123284777 . {{ cite journal }} : Cite uses deprecated parameter |citeseerx= ( help ) Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  20. ^ a b c Frana, Phil (August 2010). "An Interview with Edsger W. Dijkstra". Communications of the ACM . 53 (8): 41– 47. doi : 10.1145/1787234.1787249 . S2CID 27009702 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  21. ^ Dijkstra, Edsger W., Reflections on "A note on two problems in connexion with graphs (PDF) Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  22. ^ a b Ahuja et al. 1990 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  23. ^ a b Cormen et al. 2001 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  24. ^ Dial 1969 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  25. ^ Dijkstra 1959 , p. 270. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  26. ^ Fredman & Tarjan 1984 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  27. ^ a b Fredman & Tarjan 1987 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  28. ^ Leyzorek et al. 1957 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  29. ^ Raman 1997 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  30. ^ Thorup 2000 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  31. ^ Szcześniak, Ireneusz; Jajszczyk, Andrzej; Woźna-Szcześniak, Bożena (2019). "Generic Dijkstra for optical networks". Journal of Optical Communications and Networking . 11 (11): 568– 577. arXiv : 1810.04481 . doi : 10.1364/JOCN.11.000568 . S2CID 52958911 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  32. ^ Szcześniak, Ireneusz; Woźna-Szcześniak, Bożena (2023), "Generic Dijkstra: Correctness and tractability", NOMS 2023-2023 IEEE/IFIP Network Operations and Management Symposium , pp. 1– 7, arXiv : 2204.13547 , doi : 10.1109/NOMS56928.2023.10154322 , ISBN 978-1-6654-7716-1 , S2CID 248427020 Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  33. ^ Observe that p < dist[ u ] cannot ever hold because of the update dist[ v ] ← alt when updating the queue. See https://cs.stackexchange.com/questions/118388/dijkstra-without-decrease-key for discussion. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  34. ^ a b Thomas, Mark (30 May 2020). "Bidirectional Dijkstra" . UCL Mathematics Blog . Retrieved 3 October 2025 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  35. ^ a b Russell, Stuart ; Norvig, Peter (2009) [1995]. Artificial Intelligence: A Modern Approach (3rd ed.). Prentice Hall. pp. 75, 81. ISBN 978-0-13-604259-4 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  36. ^ Chen, M.; Chowdhury, R. A.; Ramachandran, V.; Roche, D. L.; Tong, L. (2007). Priority Queues and Dijkstra's Algorithm – UTCS Technical Report TR-07-54 – 12 October 2007 (PDF) . Austin, Texas: The University of Texas at Austin, Department of Computer Sciences. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  37. ^ Denardo, E.V. (2003). Dynamic Programming: Models and Applications . Mineola, NY: Dover Publications . ISBN 978-0-486-42810-9 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  38. ^ a b c Felner, Ariel (2011). Position Paper: Dijkstra's Algorithm versus Uniform Cost Search or a Case Against Dijkstra's Algorithm . Proc. 4th Int'l Symp. on Combinatorial Search. Archived from the original on 18 February 2020 . Retrieved 12 February 2015 . In a route-finding problem, Felner finds that the queue can be a factor 500–600 smaller, taking some 40% of the running time. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  39. ^ a b c d e Mehlhorn, Kurt ; Sanders, Peter (2008). "Chapter 10. Shortest Paths" (PDF) . Algorithms and Data Structures: The Basic Toolbox . Springer. doi : 10.1007/978-3-540-77978-0 . ISBN 978-3-540-77977-3 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  40. ^ Sniedovich, M. (2006). "Dijkstra's algorithm revisited: the dynamic programming connexion" (PDF) . Journal of Control and Cybernetics . 35 (3): 599– 620. Online version of the paper with interactive computational modules. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  41. ^ Sniedovich, M. (2010). Dynamic Programming: Foundations and Principles . Francis & Taylor . ISBN 978-0-8247-4099-3 . Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
  42. ^ Wagner, Dorothea; Willhalm, Thomas (2007). Speed-up techniques for shortest-path computations . STACS. pp. 23– 36. Wikipedia revision 1378301409, retrieved 2026-11-19 · 1795042800.0
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Dijkstra's algorithm". 1791057649. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1378301409

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·