TOPICS
Search

Dijkstra's Algorithm


Dijkstra's algorithm is an algorithm for finding a graph geodesic, i.e., the shortest path between two graph vertices in a graph. It functions by constructing a shortest-path tree from the initial vertex to every other vertex in the graph.

The algorithm is implemented in the Wolfram Language as FindShortestPath[g, Method -> "Dijkstra"].

The worst-case running time for the Dijkstra algorithm on a graph with n nodes and m edges is O(n^2) because it allows for directed cycles. It even finds the shortest paths from a source node s to all other nodes in the graph. This is basically O(n^2) for node selection and O(m) for distance updates. While O(n^2) is the best possible complexity for dense graphs, the complexity can be improved significantly for sparse graphs.

The bottleneck in Dijkstra's algorithm is node selection. For graphs with nonnegative integer edge lengths bounded by C, Dial's bucket-queue implementation replaces repeated minimum selection by bucket lookup and gives a running time of O(m+nC), which is especially useful for sparse graphs when C is small (Dial 1969).

Haeupler et al. (2024) proved that Dijkstra's algorithm is universally optimal both in its running time and number of comparisons when combined with a sufficiently efficient heap data structure. Informally speaking, this universal optimality result says that Dijkstra's algorithm is optimal for each fixed graph topology under worst-case edge weights (Haeupler et al. 2024). This result provides a powerful beyond-worst-case performance guarantee for graph algorithms.

In the standard worst-case setting, Duan et al. (2025) gave a deterministic O(mln^(2/3)n)-time algorithm for the single-source shortest path problem on directed graphs with nonnegative real edge weights in the comparison-addition model. This breaks the O(m+nlnn) time bound of Dijkstra's algorithm on sparse graphs, showing that Dijkstra's algorithm is not worst-case optimal for sparse directed single-source shortest paths in this model.

With slight modifications, Dijkstra's algorithm can be used as a reverse algorithm that maintains minimum spanning trees for the sink node. With further modifications, it can be extended to become bidirectional.

Because road networks can be modeled by weighted graphs, Dijkstra's algorithm and its variants are foundational in route planning and pathfinding systems such as digital maps (Whiting and Hillier 1960, Veritasium 2026).

In the Season 3 episode "Money For Nothing" (2007) of the television crime drama NUMB3RS, mathematics professor Charlie Eppes uses Dijkstra's algorithm to find the most likely escape routes out of Los Angeles for a hijacked truck that is carrying millions of dollars in cash and medical supplies and also two kidnapping victims.


See also

All-Pairs Shortest Path, Bellman-Ford Algorithm, Floyd-Warshall Algorithm, Graph Distance, Graph Geodesic, Reaching Algorithm, Shortest Path, Shortest Path Problem

Portions of this entry contributed by Andreas Lauschke

Portions of this entry contributed by Len Goodman

Explore with Wolfram|Alpha

References

Dial, R. B. "Algorithm 360: Shortest-Path Forest with Topological Ordering [H]." Comm. ACM 12, 632-633, 1969.Dijkstra, E. W. "A Note on Two Problems in Connection with Graphs." Numerische Math. 1, 269-271, 1959.Duan, R.; Mao, J.; Mao, X.; Shu, X.; and Yin, L. "Breaking the Sorting Barrier for Directed Single-Source Shortest Paths." In STOC '25: Proceedings of the 57th Annual ACM Symposium on Theory of Computing. New York: ACM, pp. 36-44, 2025. https://doi.org/10.1145/3717823.3718179.Haeupler, B.; Hladík, R.; Rozhoň, V., Tarjan, R.; and Tetek, J. "Universal Optimality of Dijkstra via Beyond-Worst-Case Heaps." 9 Apr 2024. https://arxiv.org/abs/2311.11793.Skiena, S. "Dijkstra's Algorithm." §6.1.1 in Implementing Discrete Mathematics: Combinatorics and Graph Theory with Mathematica. Reading, MA: Addison-Wesley, pp. 225-227, 1990.Veritasium. "How Does Google Maps Actually Work?" May 30, 2026. https://www.youtube.com/watch?v=kS-CGkiPetQ.Whiting, P. D. and Hillier, J. A. "A Method for Finding the Shortest Route through a Road Network." Operational Res. Quart. 11, 37-40, 1960.

Referenced on Wolfram|Alpha

Dijkstra's Algorithm

Cite this as:

Goodman, Len; Lauschke, Andreas; and Weisstein, Eric W. "Dijkstra's Algorithm." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/DijkstrasAlgorithm.html

Subject classifications