Katana VentraIP

Dijkstra's algorithm

Dijkstra's algorithm (/ˈdkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.[4][5][6]

Not to be confused with Dykstra's projection algorithm.

Class

Graph
Usually used with priority queue or heap for optimization[2][3]

[3]

Dijkstra's algorithm finds the shortest path from a given source node to every other node.[7]: 196–206  It can also be used to find the shortest path to a specific destination node, by terminating the algorithm once the shortest path to the destination node is known. For example, if the nodes of the graph represent cities, and the costs of edges represent the average 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 other 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 time, where is the number of nodes.[8] The idea of this algorithm is also given in Leyzorek et al. 1957. Fredman & Tarjan 1984 proposed using a Fibonacci heap priority queue to optimize the running time complexity to . 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 indeed be improved further, as detailed in Specialized variants. Additionally, 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.[9][10]


In many fields, particularly artificial intelligence, Dijkstra's algorithm or a variant of it is known as uniform cost search and formulated as an instance of the more general idea of best-first search.[11]

In the former case, let w be the first unvisited node on this shorter path. By the induction hypothesis, the shortest paths from source to u and w through visited nodes only have costs dist[u] and dist[w] respectively. This means the cost of going from source to u via w has the cost of at least dist[w] + the minimal cost of going from w to u. As the edge costs are positive, the minimal cost of going from w to u is a positive number. However, dist[u] is at most dist[w] because otherwise w would have been picked by the priority queue instead of v. This is a contradiction, since it has already been established that dist[w] + a positive number < dist[u].

To prove the correctness of Dijkstra's algorithm, we proceed by mathematical induction on the number of visited nodes.[21]


Invariant hypothesis: For each visited node v, dist[v] is the shortest distance from source to v, and for each unvisited node u, dist[u] is the shortest distance from source to u when traveling via visited nodes only, or infinity if no such path exists. (Note: we do not assume dist[u] is the actual shortest distance for unvisited nodes, while dist[v] is the actual shortest distance)[[


Base case:


The base case is when there is just one visited node, source. Its distance is defined to be zero, which is the shortest distance, since negative weights are not allowed. Hence, the hypothesis holds.


Inductive step:


Assume the hypothesis holds for visited nodes. We wish to show it holds for nodes. Let u be the next visited node according to the algorithm, i.e. the node with minimum dist[u]. We claim that dist[u] is the shortest distance from source to u.


To prove this claim, we proceed by contradiction. If there were a shorter path, then this shorter path either contains another unvisited node or not.


For all other visited nodes v, the dist[v] is already known to be the shortest distance from source already, because of the inductive hypothesis, and these values are unchanged.


After processing u, it will still be true that for each unvisited node w, dist[w] will be the shortest distance from source to w using visited nodes only. If there were a shorter path that did not use u, we would have found it previously, and if there were a shorter path using u we would have updated it when processing u.


After all nodes are visited, the shortest path from source to any node v consists only of visited nodes. Therefore, dist[v] is the shortest distance.

A* search algorithm

Bellman–Ford algorithm

Euclidean shortest path

Floyd–Warshall algorithm

Johnson's algorithm

Longest path problem

Parallel all-pairs shortest path algorithm

; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). "Section 24.3: Dijkstra's algorithm". Introduction to Algorithms (Second ed.). MIT Press and McGraw–Hill. pp. 595–601. ISBN 0-262-03293-7.

Cormen, Thomas H.

Dial, Robert B. (1969). . Communications of the ACM. 12 (11): 632–633. doi:10.1145/363269.363610. S2CID 6754003.

"Algorithm 360: Shortest-path forest with topological ordering [H]"

; Tarjan, Robert E. (1984). Fibonacci heaps and their uses in improved network optimization algorithms. 25th Annual Symposium on Foundations of Computer Science. IEEE. pp. 338–346. doi:10.1109/SFCS.1984.715934.

Fredman, Michael Lawrence

; Tarjan, Robert E. (1987). "Fibonacci heaps and their uses in improved network optimization algorithms". Journal of the Association for Computing Machinery. 34 (3): 596–615. doi:10.1145/28869.28874. S2CID 7904683.

Fredman, Michael Lawrence

Zhan, F. Benjamin; Noon, Charles E. (February 1998). "Shortest Path Algorithms: An Evaluation Using Real Road Networks". . 32 (1): 65–73. doi:10.1287/trsc.32.1.65. S2CID 14986297.

Transportation Science

Leyzorek, M.; Gray, R. S.; Johnson, A. A.; Ladew, W. C.; Meaker, Jr., S. R.; Petry, R. M.; Seitz, R. N. (1957). Investigation of Model Techniques – First Annual Report – 6 June 1956 – 1 July 1957 – A Study of Model Techniques for Communication Systems. Cleveland, Ohio: Case Institute of Technology.

(1977). "A Generalization of Dijkstra's Algorithm". Information Processing Letters. 6 (1): 1–5. doi:10.1016/0020-0190(77)90002-3.

Knuth, D.E.

Ahuja, Ravindra K.; Mehlhorn, Kurt; Orlin, James B.; Tarjan, Robert E. (April 1990). (PDF). Journal of the ACM. 37 (2): 213–223. doi:10.1145/77600.77615. hdl:1721.1/47994. S2CID 5499589.

"Faster Algorithms for the Shortest Path Problem"

Raman, Rajeev (1997). . SIGACT News. 28 (2): 81–87. doi:10.1145/261342.261352. S2CID 18031586.

"Recent results on the single-source shortest paths problem"

Thorup, Mikkel (2000). "On RAM priority Queues". SIAM Journal on Computing. 30 (1): 86–109. :10.1137/S0097539795288246. S2CID 5221089.

doi

Thorup, Mikkel (1999). . Journal of the ACM. 46 (3): 362–394. doi:10.1145/316542.316548. S2CID 207654795.

"Undirected single-source shortest paths with positive integer weights in linear time"

Charles Babbage Institute, University of Minnesota, Minneapolis

Oral history interview with Edsger W. Dijkstra

Robert Cecil Martin, The Clean Code Blog

Implementation of Dijkstra's algorithm using TDD