Misplaced Pages

Dijkstra's algorithm: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 15:43, 10 April 2009 editDigwuren (talk | contribs)11,308 edits Python is one of the closest things to runnable pseudocode. Having Python implementation serves our reader well and increases his understanding of the topic at hand.← Previous edit Latest revision as of 08:00, 23 December 2024 edit undoPanamitsu (talk | contribs)Autopatrolled, Extended confirmed users84,744 editsm Reverted 2 edits by 2405:201:3002:2123:3C05:5D87:F97F:D459 (talk) to last revision by Salix albaTags: Twinkle Undo 
Line 1: Line 1:
{{short description|Algorithm for finding shortest paths}}
{{Infobox Algorithm
|class=] {{Distinguish|Dykstra's projection algorithm}}
{{Use dmy dates|date=December 2019}}
|image=]
{{Infobox algorithm|class=]<br>]<br>]<ref>Controversial, see {{cite journal|author1=Moshe Sniedovich|title=Dijkstra's algorithm revisited: the dynamic programming connexion|journal=Control and Cybernetics|date=2006|volume=35|pages=599–620|url=https://www.infona.pl/resource/bwmeta1.element.baztech-article-BAT5-0013-0005/tab/summary}} and ].</ref>|image=Dijkstra Animation.gif|caption=Dijkstra's algorithm to find the shortest path between ''a'' and ''b''. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors.|data=]<br>Usually used with ] or ] for optimization{{sfn|Cormen|Leiserson|Rivest|Stein|2001}}{{sfn|Fredman|Tarjan|1987}}|time=<math>\Theta(|E| + |V| \log|V|)</math>{{sfn|Fredman|Tarjan|1987}}|best-time=|average-time=|space=|optimal=|complete=}}
|caption = Dijkstra's algorithm runtime
'''Dijkstra's algorithm''' ({{IPAc-en|ˈ|d|aɪ|k|s|t|r|ə|z}} {{respell|DYKE|strəz}}) is an ] for finding the ] between ] in a weighted ], which may represent, for example, a ]. It was conceived by ] ] in 1956 and published three years later.<ref>{{cite web |last=Richards |first=Hamilton |title=Edsger Wybe Dijkstra |url=http://amturing.acm.org/award_winners/dijkstra_1053701.cfm |access-date=16 October 2017 |website=A.M. Turing Award |publisher=Association for Computing Machinery |quote=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?}}</ref><ref name="Dijkstra Interview2">{{cite journal |last=Frana |first=Phil |date=August 2010 |title=An Interview with Edsger W. Dijkstra |journal=Communications of the ACM |volume=53 |issue=8 |pages=41–47 |doi=10.1145/1787234.1787249 |s2cid=27009702 |doi-access=}}</ref><ref name="Dijkstra19592">{{cite journal |last1=Dijkstra |first1=E. W. |author-link=Edsger W. Dijkstra |year=1959 |title=A note on two problems in connexion with graphs |url=https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=40368327ACB1D1FFF45671886D563916?doi=10.1.1.165.7577&rep=rep1&type=pdf |journal=Numerische Mathematik |volume=1 |pages=269–271 |citeseerx=10.1.1.165.7577 |doi=10.1007/BF01386390 |s2cid=123284777}}</ref>
|data=]
|time= <math>O(|E| + |V| \log|V|)</math>
Dijkstra's algorithm finds the shortest path from a given source node to every other node.<ref name="mehlhorn">{{cite book |last1=Mehlhorn |first1=Kurt |author1-link=Kurt Mehlhorn |title=Algorithms and Data Structures: The Basic Toolbox |last2=Sanders |first2=Peter |author2-link=Peter Sanders (computer scientist) |publisher=Springer |year=2008 |isbn=978-3-540-77977-3 |chapter=Chapter 10. Shortest Paths |doi=10.1007/978-3-540-77978-0 |chapter-url=http://people.mpi-inf.mpg.de/~mehlhorn/ftp/Toolbox/ShortestPaths.pdf}}</ref>{{rp|196–206}} It can be used to find the shortest path to a specific destination node, by terminating the algorithm after determining the shortest path to the destination node. 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 ]s, most notably ] (Intermediate System to Intermediate System) and ] (Open Shortest Path First). It is also employed as a ] in algorithms such as ].
|best-time=
|average-time=
|space=
|optimal=
|complete=
}}
{{graph search algorithm}}
'''Dijkstra's algorithm''', conceived by Dutch ] ] in 1959, <ref>]: . In ''Numerische Mathematik'', 1 (1959), S. 269–271.</ref> is a ] that solves the single-source ] for a ] with nonnegative ] path costs, producing a ]. This algorithm is often used in ].


The algorithm uses a ] data structure for selecting the shortest paths known so far. Before more advanced priority queue structures were discovered, Dijkstra's original algorithm ran in <math>\Theta(|V|^2)</math> ], where <math>|V|</math> is the number of nodes.<ref>{{Cite book |last=Schrijver |first=Alexander |title=Optimization Stories |chapter=On the history of the shortest path problem |date=2012 |chapter-url=http://ftp.gwdg.de/pub/misc/EMIS/journals/DMJDMV/vol-ismp/32_schrijver-alexander-sp.pdf |series=Documenta Mathematica Series |volume=6 |pages=155–167 |doi=10.4171/dms/6/19 |isbn=978-3-936609-58-5}}</ref>{{sfn|Leyzorek|Gray|Johnson|Ladew|1957}} {{harvnb|Fredman|Tarjan|1984}} proposed a ] priority queue to optimize the running time complexity to <math>\Theta(|E|+|V|\log|V|)</math>. This is ] the fastest known single-source ] for arbitrary ]s with unbounded non-negative weights. However, specialized cases (such as bounded/integer weights, directed acyclic graphs etc.) can be ]. If preprocessing is allowed, algorithms such as ] can be up to seven orders of magnitude faster.
For a given source ] (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network ]s, most notably ] and ] (Open Shortest Path First).


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 ], provided the subsequent labels (a subsequent label is produced when traversing an edge) are ] non-decreasing.<ref name="Generic Dijkstra2">{{cite journal |last1=Szcześniak |first1=Ireneusz |last2=Jajszczyk |first2=Andrzej |last3=Woźna-Szcześniak |first3=Bożena |year=2019 |title=Generic Dijkstra for optical networks |journal=Journal of Optical Communications and Networking |volume=11 |issue=11 |pages=568–577 |arxiv=1810.04481 |doi=10.1364/JOCN.11.000568 |s2cid=52958911}}</ref><ref name="Generic Dijkstra correctness2">{{citation |last1=Szcześniak |first1=Ireneusz |title=NOMS 2023-2023 IEEE/IFIP Network Operations and Management Symposium |pages=1–7 |year=2023 |chapter=Generic Dijkstra: Correctness and tractability |arxiv=2204.13547 |doi=10.1109/NOMS56928.2023.10154322 |isbn=978-1-6654-7716-1 |s2cid=248427020 |last2=Woźna-Szcześniak |first2=Bożena}}</ref>
==Algorithm==


In many fields, particularly ], Dijkstra's algorithm or a variant offers a ] and is formulated as an instance of the more general idea of ].{{r|felner}}
Let's call the node we are starting with an '''initial node'''. Let a '''distance of a node X''' be the distance from the '''initial node''' to it. Dijkstra's algorithm will assign some initial distance values and will try to improve them step-by-step.


== History ==
# Assign to every node a distance value. Set it to zero for our initial node and to infinity for all other nodes.
{{blockquote|What is the shortest way to travel from ] to ], in general: from given city to given city. ], which I designed in about twenty minutes. One morning I was shopping in ] with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. In fact, it was published in '59, three years later. The publication is still readable, it is, in fact, quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one of the advantages of designing without pencil and paper is that you are almost forced to avoid all avoidable complexities. Eventually, that algorithm became to my great amazement, one of the cornerstones of my fame.|Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001<ref name="Dijkstra Interview2"/>}}
# Mark all nodes as unvisited. Set initial node as current.
Dijkstra thought about the shortest path problem while working as a programmer at the ] in 1956. He wanted to demonstrate the capabilities of the new ARMAC computer.<ref>{{cite web |date=2007 |title=ARMAC |url=http://www-set.win.tue.nl/UnsungHeroes/machines/armac.html |url-status=dead |archive-url=https://web.archive.org/web/20131113021126/http://www-set.win.tue.nl/UnsungHeroes/machines/armac.html |archive-date=13 November 2013 |website=Unsung Heroes in Dutch Computing History}}</ref> His objective was to choose a problem and a computer solution that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (he limited it to 64, so that 6 bits would be sufficient to encode the city number).<ref name="Dijkstra Interview2" /> A year later, he came across another problem advanced by hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the machine's back panel. As a solution, he re-discovered ] (known earlier to ], and also rediscovered by ]).<ref name="EWD841a2">{{citation |last1=Dijkstra |first1=Edsger W. |title=Reflections on "A note on two problems in connexion with graphs |url=https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD841a.PDF}}</ref><ref>{{citation |last=Tarjan |first=Robert Endre |title=Data Structures and Network Algorithms |volume=44 |page=75 |year=1983 |series=CBMS_NSF Regional Conference Series in Applied Mathematics |publisher=Society for Industrial and Applied Mathematics |quote=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. |author-link=Robert Endre Tarjan}}</ref> Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.<ref>{{cite journal |last1=Prim |first1=R.C. |date=1957 |title=Shortest connection networks and some generalizations |url=http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Prim1957.pdf |url-status=dead |journal=Bell System Technical Journal |volume=36 |issue=6 |pages=1389–1401 |bibcode=1957BSTJ...36.1389P |doi=10.1002/j.1538-7305.1957.tb01515.x |archive-url=https://web.archive.org/web/20170718230207/http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Prim1957.pdf |archive-date=18 July 2017 |access-date=18 July 2017}}</ref><ref>V. Jarník: ''O jistém problému minimálním'' , Práce Moravské Přírodovědecké Společnosti, 6, 1930, pp.&nbsp;57–63. (in Czech)</ref>
# For current node, consider all its unvisited neighbours and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance.
# When we are done considering all neighbours of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal.
# Set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3


== Algorithm ==
==Description of the algorithm==
] ] problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are the visited ones, with color representing the distance: the greener, the closer. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular ] as Dijkstra's algorithm uses a ] of picking the shortest known path so far.]]
The algorithm requires a starting node, and node ''N,'' with a distance between the starting node and ''N''. Dijkstra's algorithm starts with infinite distances and tries to improve them step by step:


# Create a ] of all unvisited nodes: the unvisited set.
Suppose you create a knotted web of strings, with each knot corresponding to a node, and the strings corresponding to the edges of the web: the length of each string is proportional to the weight of each edge. Now you compress the web into a small pile without making any knots or tangles in it. You then grab your starting knot and pull straight up. As new knots start to come up with the original, you can measure the straight up-down distance to these knots: this must be the shortest distance from the starting node to the destination node. The acts of "pulling up" and "measuring" must be abstracted for the computer, but the general idea of the algorithm is the same: you have two sets, one of knots that are on the table, and another of knots that are in the air. Every step of the algorithm, you take the closest knot from the table and pull it into the air, and mark it with its length. If any knots are left on the table when you're done, you mark them with the distance infinity.
# Assign to every node a distance from start value: for the starting node, it is zero, and for all other nodes, it is infinity, since initially no path is known to these nodes. During execution, the distance of a node ''N'' is the length of the shortest path discovered so far between the starting node and ''N''.<ref>{{Cite encyclopedia |encyclopedia=Encyclopedia of Operations Research and Management Science |publisher=Springer |date=2013 |editor1-last=Gass |editor1-first=Saul I |volume=1 |doi=10.1007/978-1-4419-1153-7 |isbn=978-1-4419-1137-7 |last2=Fu |first2=Michael |last1=Gass |first1=Saul |chapter=Dijkstra's Algorithm |editor2-first=Michael C |editor2-last=Fu |via=Springer Link |doi-access=free}}</ref>
# From the unvisited set, select the current node to be the one with the smallest (finite) distance; initially, this is the starting node (distance zero). If the unvisited set is empty, or contains only nodes with infinite distance (which are unreachable), then the algorithm terminates by skipping to step 6. If the only concern is the path to a target node, the algorithm terminates once the current node is the target node. Otherwise, the algorithm continues.
# 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 to step 3.
# Once the loop exits (steps 3–5), every visited node contains its shortest distance from the starting node.


== Description ==
Or, using a street map, suppose you're marking over the streets (tracing the street with a marker) in a certain order, until you have a route marked in from the starting point to the destination. The order is conceptually simple: from all the street intersections of the already marked routes, find the closest unmarked intersection - closest to the starting point (the "greedy" part). It's the whole marked route to the intersection, plus the street to the new, unmarked intersection. Mark that street to that intersection, draw an arrow with the direction, then repeat. Never mark to any intersection twice. When you get to the destination, follow the arrows backwards. There will be only one path back against the arrows, the shortest one.
{{Hatnote|Note: For ease of understanding, this discussion uses the terms intersection, road and map – however, in formal terminology these terms are vertex, edge and graph, respectively.}}
The shortest path between two ] on a city map can be found by this algorithm using pencil and paper. Every intersection is listed on a separate line: one is the starting point and is labeled (given a distance of) 0. Every other intersection is initially labeled with a distance of infinity. This is done to note that no path to these intersections has yet been established. At each iteration one intersection becomes the current intersection. For the first iteration, this is the starting point.


From the current intersection, the distance to every ] (directly-connected) intersection is assessed by summing the label (value) of the current intersection and the distance to the neighbor and then ] the neighbor with the lesser of that sum and the neighbor's existing label. I.e., the neighbor is relabeled if the path to it through the current intersection is shorter than previously assessed paths. If so, mark the road to the neighbor with an arrow pointing to it, and erase any other arrow that points to it. After the distances to each of the current intersection's neighbors have been assessed, the current intersection is marked as visited. The unvisited intersection with the smallest label becomes the current intersection and the process repeats until all nodes with labels less than the destination's label have been visited.
==Pseudocode==


Once no unvisited nodes remain with a label smaller than the destination's label, the remaining arrows show the shortest path.
In the following algorithm, the code <code>u := node in ''Q'' with smallest dist</code>, searches for the vertex <var>u</var> in the vertex set <var>Q</var> that has the least <var>dist</var> value. That vertex is removed from the set <var>Q</var> and returned to the user. <code>dist_between(u, v)</code> calculates the length between the two neighbor-nodes <var>u</var> and <var>v</var>. <var>alt</var> on line 11 is the length of the path from the root node to the neighbor node <var>v</var> if it were to go through <var>u</var>. If this path is shorter than the current shortest path recorded for <var>v</var>, that current path is replaced with this <var>alt</var> path. The <var>previous</var> array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to the source.

==Pseudocode==
In the following ], {{mono|dist}} is an array that contains the current distances from the {{mono|<var>source</var>}} to other vertices, i.e. {{mono|dist}} is the current distance from the source to the vertex {{mono|<var>u</var>}}. The {{mono|prev}} 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 {{mono|u ← vertex in ''Q'' with min dist}}, searches for the vertex {{mono|<var>u</var>}} in the vertex set {{mono|<var>Q</var>}} that has the least {{mono|dist}} value. {{mono|Graph.Edges(<var>u</var>, <var>v</var>)}} returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes {{mono|<var>u</var>}} and {{mono|<var>v</var>}}. The variable {{mono|<var>alt</var>}} on line 14 is the length of the path from the {{mono|<var>source</var>}} node to the neighbor node {{mono|<var>v</var>}} if it were to go through {{mono|<var>u</var>}}. If this path is shorter than the current shortest path recorded for {{mono|<var>v</var>}}, then the distance of {{mono|<var>v</var>}} is updated to {{mono|<var>alt</var>}}.<ref name=" mehlhorn" />
. 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''): 1 '''function''' Dijkstra(''Graph'', ''source''):
2
2 '''for each''' vertex ''v'' in ''Graph'': ''// Initializations''
3 '''for each''' vertex ''v'' in ''Graph.Vertices'':
3 dist := infinity ''// Unknown distance function from source to v''
4 previous := undefined ''// Previous node in optimal path from source'' 4 dist INFINITY
5 prev ← UNDEFINED
5 dist := 0 ''// Distance from source to source''
6 ''Q'' := the set of all nodes in ''Graph'' 6 add ''v'' to ''Q''
7 dist ← 0
''// All nodes in the graph are unoptimized - thus are in Q''
8
7 '''while''' ''Q'' '''is not''' empty: ''// The main loop''
8 ''u'' := vertex in ''Q'' with smallest dist 9 '''while''' ''Q'' is not empty:
9 '''if''' dist = infinity: 10 ''u'' ← vertex in ''Q'' with minimum dist
11 remove u from ''Q''
10 '''break''' ''// all remaining vertices are inaccessible''
11 remove ''u'' from ''Q'' 12
12 '''for each''' neighbor ''v'' of ''u'': ''// where v has not yet been removed from Q. 13 '''for each''' neighbor ''v'' of ''u'' still in ''Q'':
13 ''alt'' := dist + dist_between(''u'', ''v'') 14 ''alt'' dist + Graph.Edges(''u'', ''v'')
14 '''if''' ''alt'' < dist: ''// Relax (u,v,a)'' 15 '''if''' ''alt'' < dist:
16 dist ''alt''
15 dist := ''alt'' 17 prev ''u''
18
16 previous := ''u''
17 '''return''' previous 19 '''return''' dist, prev


If we are only interested in a shortest path between vertices <var>source</var> and <var>target</var>, we can terminate the search at line 10 if <var>u</var> = <var>target</var>. To find the shortest path between vertices {{mono|<var>source</var>}} and {{mono|<var>target</var>}}, the search terminates after line 10 if {{mono|<var>u</var> {{=}} <var>target</var>}}. The shortest path from {{mono|<var>source</var>}} to {{mono|<var>target</var>}} can be obtained by reverse iteration:
Now we can read the shortest path from <var>source</var> to <var>target</var> by iteration:


1 ''S'' := empty sequence 1 ''S'' empty sequence
2 ''u'' := ''target'' 2 ''u'' ''target''
3 '''while''' previous is defined: 3 '''if''' prev is defined '''or''' ''u'' = ''source'': ''// Proceed if the vertex is reachable''
4 '''while''' ''u'' is defined: ''// Construct the shortest path with a stack S''
4 insert ''u'' at the beginning of ''S''
5 insert ''u'' at the beginning of ''S'' ''// Push the vertex onto the stack''
5 ''u'' := previous
6 ''u'' ← prev ''// Traverse from target to source''


Now sequence <var>S</var> is the list of vertices constituting one of the shortest paths from <var>target</var> to <var>source</var>, or the empty sequence if no path exists. Now sequence {{mono|<var>S</var>}} is the list of vertices constituting one of the shortest paths from {{mono|<var>source</var>}} to {{mono|<var>target</var>}}, or the empty sequence if no path exists.


A more general problem would be to find all the shortest paths between <var>source</var> and <var>target</var> (there might be several different ones of the same length). Then instead of storing only a single node in each entry of previous we would store all nodes satisfying the relaxation condition. For example, if both <var>r</var> and <var>source</var> connect to <var>target</var> and both of them lie on different shortest paths through <var>target</var> (because the edge cost is the same in both cases), then we would add both <var>r</var> and <var>source</var> to previous. When the algorithm completes, previous data structure will actually describe a graph that is a subset of the original graph with some edges removed. Its key property will be that if the algorithm was run with some starting node, then every path from that node to any other node in the new graph will be the shortest path between those nodes in the original graph, and all paths of that length from the original graph will be present in the new graph. Then to actually find all these short paths between two given nodes we would use a path finding algorithm on the new graph, such as depth-first search. A more general problem is to find all the shortest paths between {{mono|<var>source</var>}} and {{mono|<var>target</var>}} (there might be several of the same length). Then instead of storing only a single node in each entry of {{mono|prev}} all nodes satisfying the relaxation condition can be stored. For example, if both {{mono|<var>r</var>}} and {{mono|<var>source</var>}} connect to {{mono|<var>target</var>}} and they lie on different shortest paths through {{mono|<var>target</var>}} (because the edge cost is the same in both cases), then both {{mono|<var>r</var>}} and {{mono|<var>source</var>}} are added to {{mono|prev}}. When the algorithm completes, {{mono|prev}} data structure describes a graph that is a subset of the original graph with some edges removed. Its key property is that if the algorithm was run with some starting node, then every path from that node to any other node in the new graph is the shortest path between those nodes graph, and all paths of that length from the original graph are present in the new graph. Then to actually find all these shortest paths between two given nodes, a path finding algorithm on the new graph, such as ] would work.


===Using a priority queue===
== Running time==
A min-priority queue is an abstract data type that provides 3 basic operations: {{mono|add_with_priority()}}, {{mono|decrease_priority()}} and {{mono|extract_min()}}. As mentioned earlier, using such a data structure can lead to faster computing times than using a basic queue. Notably, ]{{sfn|Fredman|Tarjan|1984}} or ] offer optimal implementations for those 3 operations. As the algorithm is slightly different in appearance, it is mentioned here, in pseudocode as well:


1 '''function''' Dijkstra(''Graph'', ''source''):
An upper bound of the running time of Dijkstra's algorithm on a graph with edges ''E'' and vertices ''V'' can be expressed as a function of |''E''| and |''V''| using the ].
2 create vertex priority queue Q
3
4 dist ← 0 ''// Initialization''
5 ''Q''.add_with_priority(''source'', 0) ''// associated priority equals dist''
6
7 '''for each''' vertex ''v'' in ''Graph.Vertices'':
8 '''if''' ''v'' ≠ ''source''
9 prev ← UNDEFINED ''// Predecessor of v''
10 dist ← INFINITY ''// Unknown distance from source to v''
11 Q.add_with_priority(v, INFINITY)
12
13
14 '''while''' ''Q'' is not empty: ''// The main loop''
15 ''u'' ← ''Q''.extract_min() ''// Remove and return best vertex''
16 '''for each''' neighbor ''v'' of ''u'': ''// Go through all v neighbors of u''
17 ''alt'' ← dist + Graph.Edges(''u'', ''v'')
18 '''if''' ''alt'' < dist:
19 prev ← ''u''
20 dist ← ''alt''
21 ''Q''.decrease_priority(''v'', ''alt'')
22
23 '''return''' dist, prev


Instead of filling the priority queue with all nodes in the initialization phase, it is possible to initialize it to contain only ''source''; then, inside the <code>'''if''' ''alt'' < dist</code> block, the {{mono|decrease_priority()}} becomes an {{mono|add_with_priority()}} operation.<ref name=" mehlhorn" />{{rp|198}}
For any implementation of set ''Q'' the running time is ''O''(|E|*''decrease_key_in_Q'' + |V|*''extract_minimum_in_Q''), where ''decrease_key_in_Q'' and ''extract_minimum_in_Q'' are times needed to perform that operation in set ''Q''.


Yet another alternative is to add nodes unconditionally to the priority queue and to instead check after extraction (<code>''u'' ← ''Q''.extract_min()</code>) that it isn't revisiting, or that no shorter connection was found yet in the <code>if alt < dist</code> block. This can be done by additionally extracting the associated priority <code>''p''</code> from the queue and only processing further <code>'''if''' ''p'' == dist</code> inside the <code>'''while''' ''Q'' is not empty</code> loop.<ref name="Note2">Observe that {{mono|''p'' < dist}} cannot ever hold because of the update {{mono|dist ← ''alt''}} when updating the queue. See https://cs.stackexchange.com/questions/118388/dijkstra-without-decrease-key for discussion.</ref>
The simplest implementation of the Dijkstra's algorithm stores vertices of set ''Q'' in an ordinary linked list or array, and operation Extract-Min(''Q'') is simply a linear search through all vertices in ''Q''. In this case, the running time is ''O''(|''V''|<sup>2</sup>+|''E''|)=''O''(|''V''|<sup>2</sup>).


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.<ref name="chen_072">{{cite book |last1=Chen |first1=M. |url=http://www.cs.sunysb.edu/~rezaul/papers/TR-07-54.pdf |title=Priority Queues and Dijkstra's Algorithm – UTCS Technical Report TR-07-54 – 12 October 2007 |last2=Chowdhury |first2=R. A. |last3=Ramachandran |first3=V. |last4=Roche |first4=D. L. |last5=Tong |first5=L. |publisher=The University of Texas at Austin, Department of Computer Sciences |year=2007 |location=Austin, Texas |ref=chen}}</ref>
For ]s, that is, graphs with fewer than |''V''|<sup>2</sup> edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of ]s and using a ], ], or ] as a ] to implement the Extract-Min function efficiently. With a binary heap, the algorithm requires ''O''((|''E''|+|''V''|) log |''V''|) time (which is dominated by ''O''(|''E''| log |''V''|) assuming every vertex is connected, that is, |''E''| ≥ |''V''| - 1), and the ] improves this to <math>O(|E| + |V| \log|V|)</math>.


== Python implementation == == Proof ==
To prove the ] of Dijkstra's algorithm, ] can be used on the number of visited nodes.<ref>{{Introduction to Algorithms|edition=4|pages=622–623|chapter=22}}</ref>


''Invariant hypothesis'': For each visited node {{mono|v}}, {{code|dist}} is the shortest distance from {{mono|source}} to {{mono|v}}, and for each unvisited node {{mono|u}}, {{code|dist}} is the shortest distance from {{mono|source}} to {{mono|u}} when traveling via visited nodes only, or infinity if no such path exists. (Note: we do not assume {{code|dist}} is the actual shortest distance for unvisited nodes, while {{code|dist}} is the actual shortest distance)
<source lang="python">
import heapq
from collections import defaultdict


=== Base case ===
class Edge(object):
The base case is when there is just one visited node, {{mono|source}}. Its distance is defined to be zero, which is the shortest distance, since negative weights are not allowed. Hence, the hypothesis holds.
def __init__(self, start, end, weight):
self.start, self.end, self.weight = start, end, weight


=== Induction ===
# For heapq.
Assuming that the hypothesis holds for <math>k</math> visited nodes, to show it holds for <math>k+1</math> nodes, let {{mono|u}} be the next visited node, i.e. the node with minimum {{code|dist}}. The claim is that {{code|dist}} is the shortest distance from {{mono|source}} to {{mono|u}}.
def __cmp__(self, other): return cmp(self.weight, other.weight)


The proof is by contradiction. If a shorter path were available, then this shorter path either contains another unvisited node or not.
class Graph(object):
def __init__(self):
# The adjacency list.
self.adj = defaultdict(list)


* In the former case, let {{mono|w}} be the first unvisited node on this shorter path. By induction, the shortest paths from {{mono|source}} to {{mono|u}} and {{mono|w}} through visited nodes only have costs {{code|dist}} and {{code|dist}} respectively. This means the cost of going from {{mono|source}} to {{mono|u}} via {{mono|w}} has the cost of at least {{code|dist}} + the minimal cost of going from {{mono|w}} to {{mono|u}}. As the edge costs are positive, the minimal cost of going from {{mono|w}} to {{mono|u}} is a positive number. However, {{code|dist}} is at most {{code|dist}} because otherwise w would have been picked by the priority queue instead of u. This is a contradiction, since it has already been established that {{code|dist}} + a positive number < {{code|dist}}.
def add_e(self, start, end, weight = 0):
* In the latter case, let {{mono|w}} be the last but one node on the shortest path. That means {{code|dist + Graph.Edges < dist}}. That is a contradiction because by the time {{mono|w}} is visited, it should have set {{code|dist}} to at most {{code|dist + Graph.Edges}}.
self.adj.append(Edge(start, end, weight))
self.adj.append(Edge(end, start, weight))


For all other visited nodes {{mono|v}}, the {{code|dist}} is already known to be the shortest distance from {{mono|source}} already, because of the inductive hypothesis, and these values are unchanged.
def s_path(self, src):
"""
Returns the distance to every vertex from the source and the
array representing, at index i, the node visited before
visiting node i. This is in the form (dist, previous).
"""
dist, visited, previous, queue = {src: 0}, {}, {},
heapq.heappush(queue, (dist,src))
while len(queue) > 0:
distance, current = heapq.heappop(queue)
if current in visited:
continue
visited = True


After processing {{mono|u}}, it is still true that for each unvisited node {{mono|w}}, {{code|dist}} is the shortest distance from {{mono|source}} to {{mono|w}} using visited nodes only. Any shorter path that did not use {{mono|u}}, would already have been found, and if a shorter path used {{mono|u}} it would have been updated when processing {{mono|u}}.
for edge in self.adj:
relaxed = dist + edge.weight
end = edge.end
if end not in dist or relaxed < dist:
previous, dist = current, relaxed
heapq.heappush(queue, (dist,end))
return dist, previous
</source>


After all nodes are visited, the shortest path from {{mono|source}} to any node {{mono|v}} consists only of visited nodes. Therefore, {{code|dist}} is the shortest distance.
For the example graph in the we do:


== Running time ==
<source lang="python">
Bounds of the running time of Dijkstra's algorithm on a graph with edges ''{{mvar|E}}'' and vertices ''{{mvar|V}}'' can be expressed as a function of the number of edges, denoted <math>|E|</math>, and the number of vertices, denoted <math>|V|</math>, using ]. The complexity bound depends mainly on the data structure used to represent the set ''{{mvar|Q}}''. In the following, upper bounds can be simplified because <math>|E|</math> is <math>O(|V|^2)</math> for any simple graph, but that simplification disregards the fact that in some problems, other upper bounds on <math>|E|</math> may hold.
g = Graph()
g.add_e(1,2,4)
g.add_e(1,4,1)
g.add_e(2,1,74)
g.add_e(2,3,2)
g.add_e(2,5,12)
g.add_e(3,2,12)
g.add_e(3,10,12)
g.add_e(3,6,74)
g.add_e(4,7,22)
g.add_e(4,5,32)
g.add_e(5,8,33)
g.add_e(5,4,66)
g.add_e(5,6,76)
g.add_e(6,10,21)
g.add_e(6,9,11)
g.add_e(7,3,12)
g.add_e(7,8,10)
g.add_e(8,7,2)
g.add_e(8,9,72)
g.add_e(9,10,7)
g.add_e(9,6,31)
g.add_e(9,8,18)
g.add_e(10,6,8)


For any data structure for the vertex set ''{{mvar|Q}}'', the running time i s:{{sfn|Cormen|Leiserson|Rivest|Stein|2001}}
# Find a shortest path from vertex 'a' (1) to 'j' (10).
dist, prev = g.s_path(1)
# Trace the path back using the prev array.
path, current, end = , 10, 10
while current in prev:
path.insert(0, prev)
current = prev


: <math>\Theta(|E| \cdot T_\mathrm{dk} + |V| \cdot T_\mathrm{em}),</math>
print path
print dist
</source>


where <math>T_\mathrm{dk}</math> and <math>T_\mathrm{em}</math> are the complexities of the ''decrease-key'' and ''extract-minimum'' operations in ''{{mvar|Q}}'', respectively.
Output:

(namely a, b, c)
The simplest version of Dijkstra's algorithm stores the vertex set ''{{mvar|Q}}'' as a linked list or array, and edges as an ] or ]. In this case, extract-minimum is simply a linear search through all vertices in ''{{mvar|Q}}'', so the running time is <math>\Theta(|E| + |V|^2) = \Theta(|V|^2)</math>.
18

For ]s, that is, graphs with far fewer than <math>|V|^2</math> edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a ], ], ], ] or a priority heap as a ] 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 ''{{mvar|Q}}'' changes. With a self-balancing binary search tree or binary heap, the algorithm requires

: <math>\Theta((|E| + |V|) \log |V|)</math>

time in the worst case; for connected graphs this time bound can be simplified to <math>\Theta( | E | \log | V | )</math>. The ] improves this to

: <math>\Theta(|E| + |V| \log|V|).</math>

When using binary heaps, the ] time complexity is lower than the worst-case: assuming edge costs are drawn independently from a common ], the expected number of ''decrease-key'' operations is bounded by <math>\Theta(|V| \log (|E|/|V|))</math>, giving a total running time of<ref name=" mehlhorn" />{{rp|199–200}}

: <math>O\left(|E| + |V| \log \frac{|E|}{|V|} \log |V|\right).</math>

===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).{{r|mehlhorn}}{{rp|198}} This variant has the same worst-case bounds as the common variant, but maintains a smaller priority queue in practice, speeding up queue operations.<ref name="felner">{{cite conference |last=Felner |first=Ariel |year=2011 |title=Position Paper: Dijkstra's Algorithm versus Uniform Cost Search or a Case Against Dijkstra's Algorithm |url=http://www.aaai.org/ocs/index.php/SOCS/SOCS11/paper/view/4017/4357 |conference=Proc. 4th Int'l Symp. on Combinatorial Search |archive-url=https://web.archive.org/web/20200218150924/https://www.aaai.org/ocs/index.php/SOCS/SOCS11/paper/view/4017/4357 |archive-date=18 February 2020 |access-date=12 February 2015 |url-status=dead}} In a route-finding problem, Felner finds that the queue can be a factor 500–600 smaller, taking some 40% of the running time.</ref>

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{{r|felner}}<ref name="aima">{{Cite AIMA|3|pages=75, 81}}</ref><ref>Sometimes also ''least-cost-first search'': {{cite journal |last=Nau |first=Dana S. |year=1983 |title=Expert computer systems |url=https://www.cs.umd.edu/~nau/papers/nau1983expert.pdf |journal=Computer |publisher=IEEE |volume=16 |issue=2 |pages=63–85 |doi=10.1109/mc.1983.1654302 |s2cid=7301753}}</ref> and can be expressed in pseudocode as<!--NOTE TO EDITORS: Don't get confused by "uniform" in the name and remove costs from the pseudocode. UCS includes costs and is different from breadth-first search.-->
'''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 {{math|''C''<sup>*</sup>}} is the length of the shortest path from the start node to any node satisfying the "goal" predicate, each edge has cost at least ''{{mvar|ε}}'', and the number of neighbors per node is bounded by ''{{mvar|b}}'', then the algorithm's worst-case time and space complexity are both in {{math|''O''(''b''<sup>1+⌊''C''<sup>*</sup> {{frac}} ''ε''⌋</sup>)}}.{{r|aima}}

Further optimizations for the single-target case include ] variants, goal-directed variants such as the ] (see {{slink||Related problems and algorithms}}), 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 {{math|''s''–''t''}} routing to connecting ''{{mvar|s}}'' and ''{{mvar|t}}'' to their respective "]" followed by shortest-path computation between these transit nodes using a "highway".<ref name="speedup2">{{cite conference |last1=Wagner |first1=Dorothea |last2=Willhalm |first2=Thomas |year=2007 |title=Speed-up techniques for shortest-path computations |conference=STACS |pages=23–36}}</ref> Combinations of such techniques may be needed for optimal practical performance on specific problems.<ref>{{cite journal |last1=Bauer |first1=Reinhard |last2=Delling |first2=Daniel |last3=Sanders |first3=Peter |last4=Schieferdecker |first4=Dennis |last5=Schultes |first5=Dominik |last6=Wagner |first6=Dorothea |year=2010 |title=Combining hierarchical and goal-directed speed-up techniques for Dijkstra's algorithm |url=https://publikationen.bibliothek.kit.edu/1000014952 |journal=J. Experimental Algorithmics |volume=15 |pages=2.1 |doi=10.1145/1671970.1671976 |s2cid=1661292}}</ref>

=== Optimality for comparison-sorting by distance ===
As well as simply computing distances and paths, Dijkstra's algorithm can be used to sort vertices by their distances from a given starting vertex.
In 2023, Haeupler, Rozhoň, Tětek, Hladík, and ] (one of the inventors of the 1984 heap), proved that, for this sorting problem on a positively-weighted directed graph, a version of Dijkstra's algorithm with a special heap data structure has a runtime and number of comparisons that is within a constant factor of optimal among ] algorithms for the same sorting problem on the same graph and starting vertex but with variable edge weights. To achieve this, they use a comparison-based heap whose cost of returning/removing the minimum element from the heap is logarithmic in the number of elements inserted after it rather than in the number of elements in the heap.<ref>{{cite arXiv |last1=Haeupler |first1=Bernhard |title=Universal Optimality of Dijkstra via Beyond-Worst-Case Heaps |date=2024-10-28 |eprint=2311.11793 |last2=Hladík |first2=Richard |last3=Rozhoň |first3=Václav |last4=Tarjan |first4=Robert |last5=Tětek |first5=Jakub|class=cs.DS }}</ref><ref>{{Cite web |last=Brubaker |first=Ben |date=2024-10-25 |title=Computer Scientists Establish the Best Way to Traverse a Graph |url=https://www.quantamagazine.org/computer-scientists-establish-the-best-way-to-traverse-a-graph-20241025/ |access-date=2024-12-09 |website=Quanta Magazine |language=en}}</ref>

===Specialized variants===
When arc weights are small integers (bounded by a parameter <math>C</math>), specialized queues can be used for increased speed. The first algorithm of this type was Dial's algorithm{{Sfn|Dial|1969}} for graphs with positive integer edge weights, which uses a ] to obtain a running time <math>O(|E|+|V|C)</math>. The use of a ] as the priority queue brings the complexity to <math>O(|E|\log\log C)</math> .{{sfn|Ahuja|Mehlhorn|Orlin|Tarjan|1990}} Another interesting variant based on a combination of a new ] and the well-known Fibonacci heap runs in time <math>O(|E|+|V|\sqrt{\log C})</math> .{{sfn|Ahuja|Mehlhorn|Orlin|Tarjan|1990}} Finally, the best algorithms in this special case run in <math>O(|E|\log\log|V|)</math>{{sfn|Thorup|2000}} time and <math>O(|E| + |V|\min\{(\log|V|)^{1/3+\varepsilon}, (\log C)^{1/4+\varepsilon}\})</math> time.{{sfn|Raman|1997}}


== Related problems and algorithms == == Related problems and algorithms ==
Dijkstra's original algorithm can be extended with modifications. For example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution.


Dijkstra's algorithm is usually the working principle behind ]s. ] and ] are the most common.
The functionality of Dijkstra's original algorithm can be extended with a variety of modifications. For example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution.


Unlike Dijkstra's algorithm, the ] can be used on graphs with negative edge weights, as long as the graph contains no ] reachable from the source vertex ''s''. The presence of such cycles means that no shortest path can be found, since the label becomes lower each time the cycle is traversed. (This statement assumes that a "path" is allowed to repeat vertices. In ] that is normally not allowed. In ] it often is allowed.) It is possible to adapt Dijkstra's algorithm to handle negative weights by combining it with the Bellman-Ford algorithm (to remove negative edges and detect negative cycles): ].
Dijkstra's algorithm is usually the working principle behind ]s, ] and ] being the most common ones.


The ] is a generalization of Dijkstra's algorithm that reduces the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the distance to the target.
Unlike Dijkstra's algorithm, the ] can be used on graphs with negative edge weights, as long as the graph contains no ] reachable from the source vertex ''s''. (The presence of such cycles means there is no shortest path, since the total weight becomes lower each time the cycle is traversed.)


The process that underlies Dijkstra's algorithm is similar to the ] process used in ]. Prim's purpose is to find a ] that connects all nodes in the graph; Dijkstra is concerned with only two nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual edges.
The ] is a generalization of Dijkstra's algorithm that cuts down on the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the "distance" to the target.


] can be viewed as a special-case of Dijkstra's algorithm on unweighted graphs, where the priority queue degenerates into a ] queue.
The process that underlies Dijkstra's algorithm is similar to the greedy process used in ]. Prim's purpose is to find a ] for a graph.


The ] can be viewed as a continuous version of Dijkstra's algorithm which computes the geodesic distance on a triangle mesh.
For the solution of nonconvex cost trees (typical for ''real-world'' costs exhibiting ]) one solution allowing application of this algorithm is to successively divide the problem into convex subtrees (using bounding linear costs) and to pursue subsequent divisions using ] methods. Such methods have been superseded by more efficient direct methods{{Fact|date=January 2009}}.


=== Dynamic programming perspective ===
== See also ==
From a ] point of view, Dijkstra's algorithm is a successive approximation scheme that solves the dynamic programming functional equation for the shortest path problem by the '''Reaching''' method.<ref name="sniedovich_062">{{cite journal |last=Sniedovich |first=M. |year=2006 |title=Dijkstra's algorithm revisited: the dynamic programming connexion |url=http://matwbn.icm.edu.pl/ksiazki/cc/cc35/cc3536.pdf |journal=Journal of Control and Cybernetics |volume=35 |issue=3 |pages=599–620}} </ref><ref name="denardo_032">{{cite book |last=Denardo |first=E.V. |title=Dynamic Programming: Models and Applications |publisher=] |year=2003 |isbn=978-0-486-42810-9 |location=Mineola, NY}}</ref><ref name="sniedovich_102">{{cite book |last=Sniedovich |first=M. |title=Dynamic Programming: Foundations and Principles |publisher=] |year=2010 |isbn=978-0-8247-4099-3}}</ref>


In fact, Dijkstra's explanation of the logic behind the algorithm:{{sfn|Dijkstra|1959|p=270}}
* ]
{{blockquote|'''Problem 2.''' Find the path of minimum total length between two given nodes {{mvar|P}} and {{mvar|Q}}.
* ]
* ]
* ]
* ]
* ]
* ]
* ]
* ]


We use the fact that, if {{mvar|R}} is a node on the minimal path from {{mvar|P}} to {{mvar|Q}}, knowledge of the latter implies the knowledge of the minimal path from {{mvar|P}} to {{mvar|R}}.}}
== Notes ==
is a paraphrasing of ] ] in the context of the shortest path problem.


== See also ==
<references/>

* ]
* ]
* ]
* ]
* ]
* ]
* ]

==Notes==
<references responsive="1"></references>


== References == == References ==


* ], ], ], and ]. '']'', Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 24.3: Dijkstra's algorithm, pp.595&ndash;601. * {{cite book |last1=Cormen |first1=Thomas H. |author1-link=Thomas H. Cormen |title=Introduction to Algorithms |title-link=Introduction to Algorithms |last2=Leiserson |first2=Charles E. |author2-link=Charles E. Leiserson |last3=Rivest |first3=Ronald L. |author3-link=Ronald L. Rivest |last4=Stein |first4=Clifford |author4-link=Clifford Stein |publisher=] and ] |year=2001 |isbn=0-262-03293-7 |edition=Second |pages=595–601 |chapter=Section 24.3: Dijkstra's algorithm}}
* {{cite journal |last=Dial |first=Robert B. |year=1969 |title=Algorithm 360: Shortest-path forest with topological ordering |journal=] |volume=12 |issue=11 |pages=632–633 |doi=10.1145/363269.363610 |s2cid=6754003 |doi-access=free}}
* ], and ]. 1998. Shortest Path Algorithms: An Evaluation Using Real Road Networks. '']'' 32(1): 65-73.
* {{cite conference |last1=Fredman |first1=Michael Lawrence |author-link1=Michael Fredman |last2=Tarjan |first2=Robert E. |author-link2=Robert Tarjan |year=1984 |title=Fibonacci heaps and their uses in improved network optimization algorithms |conference=25th Annual Symposium on Foundations of Computer Science |publisher=] |pages=338–346 |doi=10.1109/SFCS.1984.715934}}
* {{cite journal |last1=Fredman |first1=Michael Lawrence |author-link1=Michael Fredman |last2=Tarjan |first2=Robert E. |author-link2=Robert Tarjan |year=1987 |title=Fibonacci heaps and their uses in improved network optimization algorithms |journal=Journal of the Association for Computing Machinery |volume=34 |issue=3 |pages=596–615 |doi=10.1145/28869.28874 |s2cid=7904683 |doi-access=free}}
* {{cite journal |last1=Zhan |first1=F. Benjamin |last2=Noon |first2=Charles E. |date=February 1998 |title=Shortest Path Algorithms: An Evaluation Using Real Road Networks |journal=] |volume=32 |issue=1 |pages=65–73 |doi=10.1287/trsc.32.1.65 |s2cid=14986297}}
* {{cite book |last1=Leyzorek |first1=M. |title=Investigation of Model Techniques – First Annual Report – 6 June 1956 – 1 July 1957 – A Study of Model Techniques for Communication Systems |last2=Gray |first2=R. S. |last3=Johnson |first3=A. A. |last4=Ladew |first4=W. C. |last5=Meaker, Jr. |first5=S. R. |last6=Petry |first6=R. M. |last7=Seitz |first7=R. N. |publisher=Case Institute of Technology |year=1957 |location=Cleveland, Ohio}}
* {{cite journal |last1=Knuth |first1=D.E. |author-link1=Donald Knuth |year=1977 |title=A Generalization of Dijkstra's Algorithm |journal=] |volume=6 |pages=1–5 |doi=10.1016/0020-0190(77)90002-3 |number=1}}
* {{cite journal |last1=Ahuja |first1=Ravindra K. |last2=Mehlhorn |first2=Kurt |last3=Orlin |first3=James B. |last4=Tarjan |first4=Robert E. |date=April 1990 |title=Faster Algorithms for the Shortest Path Problem |url=https://dspace.mit.edu/bitstream/1721.1/47994/1/fasteralgorithms00sloa.pdf |journal=Journal of the ACM |volume=37 |pages=213–223 |doi=10.1145/77600.77615 |s2cid=5499589 |hdl-access=free |number=2 |hdl=1721.1/47994}}
* {{cite journal |last1=Raman |first1=Rajeev |year=1997 |title=Recent results on the single-source shortest paths problem |journal=SIGACT News |volume=28 |issue=2 |pages=81–87 |doi=10.1145/261342.261352 |s2cid=18031586 |doi-access=free}}
* {{cite journal |last1=Thorup |first1=Mikkel |year=2000 |title=On RAM priority Queues |journal=SIAM Journal on Computing |volume=30 |issue=1 |pages=86–109 |doi=10.1137/S0097539795288246 |s2cid=5221089}}
* {{cite journal |last1=Thorup |first1=Mikkel |year=1999 |title=Undirected single-source shortest paths with positive integer weights in linear time |url=http://www.diku.dk/~mthorup/PAPERS/sssp.ps.gz |journal=Journal of the ACM |volume=46 |issue=3 |pages=362–394 |doi=10.1145/316542.316548 |s2cid=207654795 |doi-access=free}}


== External links == == External links ==
{{Commons category|Dijkstra's algorithm}}


* , ], University of Minnesota, Minneapolis
*
* * , ], The Clean Code Blog
{{Edsger Dijkstra}}{{Graph traversal algorithms}}{{Optimization algorithms|combinatorial|state=autocollapse}}
*
*
* , graph data structures and algorithms for .Net


]
]
] ]
] ]
Line 215: Line 248:
] ]
] ]
]

]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]

Latest revision as of 08:00, 23 December 2024

Algorithm for finding shortest paths Not to be confused with Dykstra's projection algorithm.

Dijkstra's algorithm
Dijkstra's algorithm to find the shortest path between a and b. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors.
ClassSearch algorithm
Greedy algorithm
Dynamic programming
Data structureGraph
Usually used with priority queue or heap for optimization
Worst-case performance Θ ( | E | + | V | log | V | ) {\displaystyle \Theta (|E|+|V|\log |V|)}

Dijkstra's algorithm (/ˈdaɪkstrəz/ DYKE-strəz) 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 the destination node. 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 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 Θ ( | V | 2 ) {\displaystyle \Theta (|V|^{2})} time, where | V | {\displaystyle |V|} is the number of nodes. Fredman & Tarjan 1984 proposed a Fibonacci heap priority queue to optimize the running time complexity to Θ ( | E | + | V | log | V | ) {\displaystyle \Theta (|E|+|V|\log |V|)} . 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.

History

What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to given city. It is the algorithm for the shortest path, which I designed in about twenty minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. In fact, it was published in '59, three years later. The publication is still readable, it is, in fact, quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one of the advantages of designing without pencil and paper is that you are almost forced to avoid all avoidable complexities. Eventually, that algorithm became to my great amazement, one of the cornerstones of my fame.

— Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001

Dijkstra thought about the shortest path problem while working as a programmer at the Mathematical Center in Amsterdam in 1956. He wanted to demonstrate the capabilities of the new ARMAC computer. His objective was to choose a problem and a computer solution that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (he limited it to 64, so that 6 bits would be sufficient to encode the city number). A year later, he came across another problem advanced by hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the machine's back panel. As a solution, he re-discovered Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim). Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.

Algorithm

Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a target node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are the visited ones, with color representing the distance: the greener, the closer. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic of picking the shortest known path so far.

The algorithm requires a starting node, and node N, with a distance between the starting node and N. Dijkstra's algorithm starts with infinite distances and tries to improve them step by step:

  1. Create a set of all unvisited nodes: the unvisited set.
  2. Assign to every node a distance from start value: for the starting node, it is zero, and for all other nodes, it is infinity, since initially no path is known to these nodes. During execution, the distance of a node N is the length of the shortest path discovered so far between the starting node and N.
  3. From the unvisited set, select the current node to be the one with the smallest (finite) distance; initially, this is the starting node (distance zero). If the unvisited set is empty, or contains only nodes with infinite distance (which are unreachable), then the algorithm terminates by skipping to step 6. If the only concern is the path to a target node, the algorithm terminates once the current node is the target node. Otherwise, the algorithm continues.
  4. 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).
  5. 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 to step 3.
  6. Once the loop exits (steps 3–5), every visited node contains its shortest distance from the starting node.

Description

Note: For ease of understanding, this discussion uses the terms intersection, road and map – however, in formal terminology these terms are vertex, edge and graph, respectively.

The shortest path between two intersections on a city map can be found by this algorithm using pencil and paper. Every intersection is listed on a separate line: one is the starting point and is labeled (given a distance of) 0. Every other intersection is initially labeled with a distance of infinity. This is done to note that no path to these intersections has yet been established. At each iteration one intersection becomes the current intersection. For the first iteration, this is the starting point.

From the current intersection, the distance to every neighbor (directly-connected) intersection is assessed by summing the label (value) of the current intersection and the distance to the neighbor and then relabeling the neighbor with the lesser of that sum and the neighbor's existing label. I.e., the neighbor is relabeled if the path to it through the current intersection is shorter than previously assessed paths. If so, mark the road to the neighbor with an arrow pointing to it, and erase any other arrow that points to it. After the distances to each of the current intersection's neighbors have been assessed, the current intersection is marked as visited. The unvisited intersection with the smallest label becomes the current intersection and the process repeats until all nodes with labels less than the destination's label have been visited.

Once no unvisited nodes remain with a label smaller than the destination's label, the remaining arrows show the shortest path.

Pseudocode

In the following pseudocode, dist is an array that contains the current distances from the source to other vertices, i.e. dist is the current distance from the source to the vertex u. The prev 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 u ← vertex in Q with min dist, searches for the vertex u in the vertex set Q that has the least dist value. Graph.Edges(u, v) returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes u and v. The variable alt on line 14 is the length of the path from the source node to the neighbor node v if it were to go through u. If this path is shorter than the current shortest path recorded for v, then the distance of v is updated to alt.

A demo of Dijkstra's algorithm based on Euclidean distance. Red lines are the shortest path covering, i.e., connecting u and prev. 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 ← INFINITY
 5          prev ← UNDEFINED
 6          add v to Q
 7      dist ← 0
 8     
 9      while Q is not empty:
10          u ← vertex in Q with minimum dist
11          remove u from Q
12         
13          for each neighbor v of u still in Q:
14              alt ← dist + Graph.Edges(u, v)
15              if alt < dist:
16                  dist ← alt
17                  prev ← u
18
19      return dist, prev

To find the shortest path between vertices source and target, the search terminates after line 10 if u = target. The shortest path from source to target can be obtained by reverse iteration:

1  S ← empty sequence
2  utarget
3  if prev is defined or u = source:          // Proceed if the vertex is reachable
4      while u is defined:                       // Construct the shortest path with a stack S
5          insert u at the beginning of S        // Push the vertex onto the stack
6          u ← prev                           // Traverse from target to source

Now sequence S is the list of vertices constituting one of the shortest paths from source to target, or the empty sequence if no path exists.

A more general problem is to find all the shortest paths between source and target (there might be several of the same length). Then instead of storing only a single node in each entry of prev all nodes satisfying the relaxation condition can be stored. For example, if both r and source connect to target and they lie on different shortest paths through target (because the edge cost is the same in both cases), then both r and source are added to prev. When the algorithm completes, prev data structure describes a graph that is a subset of the original graph with some edges removed. Its key property is that if the algorithm was run with some starting node, then every path from that node to any other node in the new graph is the shortest path between those nodes graph, and all paths of that length from the original graph are present in the new graph. Then to actually find all these shortest paths between two given nodes, a path finding algorithm on the new graph, such as depth-first search would work.

Using a priority queue

A min-priority queue is an abstract data type that provides 3 basic operations: add_with_priority(), decrease_priority() and extract_min(). As mentioned earlier, using such a data structure can lead to faster computing times than using a basic queue. Notably, Fibonacci heap or Brodal queue offer optimal implementations for those 3 operations. As the algorithm is slightly different in appearance, it is mentioned here, in pseudocode as well:

1   function Dijkstra(Graph, source):
2       create vertex priority queue Q
3
4       dist ← 0                          // Initialization
5       Q.add_with_priority(source, 0)            // associated priority equals dist
6
7       for each vertex v in Graph.Vertices:
8           if vsource
9               prev ← UNDEFINED               // Predecessor of v
10              dist ← INFINITY                // Unknown distance from source to v
11              Q.add_with_priority(v, INFINITY)
12
13
14      while Q is not empty:                     // The main loop
15          uQ.extract_min()                   // Remove and return best vertex
16          for each neighbor v of u:             // Go through all v neighbors of u
17              alt ← dist + Graph.Edges(u, v)
18              if alt < dist:
19                  prev ← u
20                  dist ← alt
21                  Q.decrease_priority(v, alt)
22
23      return dist, prev

Instead of filling the priority queue with all nodes in the initialization phase, it is possible to initialize it to contain only source; then, inside the if alt < dist block, the decrease_priority() becomes an add_with_priority() operation.

Yet another alternative is to add nodes unconditionally to the priority queue and to instead check after extraction (uQ.extract_min()) that it isn't revisiting, or that no shorter connection was found yet in the if alt < dist block. This can be done by additionally extracting the associated priority p from the queue and only processing further if p == dist 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.

Proof

To prove the correctness of Dijkstra's algorithm, mathematical induction can be used on the number of visited nodes.

Invariant hypothesis: For each visited node v, dist is the shortest distance from source to v, and for each unvisited node u, dist 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 is the actual shortest distance for unvisited nodes, while dist 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.

Induction

Assuming that the hypothesis holds for k {\displaystyle k} visited nodes, to show it holds for k + 1 {\displaystyle k+1} nodes, let u be the next visited node, i.e. the node with minimum dist. The claim is that dist is the shortest distance from source to u.

The proof is by contradiction. If a shorter path were available, then this shorter path either contains another unvisited node or not.

  • In the former case, let w be the first unvisited node on this shorter path. By induction, the shortest paths from source to u and w through visited nodes only have costs dist and dist respectively. This means the cost of going from source to u via w has the cost of at least dist + 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 is at most dist because otherwise w would have been picked by the priority queue instead of u. This is a contradiction, since it has already been established that dist + a positive number < dist.
  • In the latter case, let w be the last but one node on the shortest path. That means dist + Graph.Edges < dist. That is a contradiction because by the time w is visited, it should have set dist to at most dist + Graph.Edges.

For all other visited nodes v, the dist 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 is still true that for each unvisited node w, dist is the shortest distance from source to w using visited nodes only. Any shorter path that did not use u, would already have been found, and if a shorter path used u it would have been updated when processing u.

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

Running time

Bounds of the running time of Dijkstra's algorithm on a graph with edges E and vertices V can be expressed as a function of the number of edges, denoted | E | {\displaystyle |E|} , and the number of vertices, denoted | V | {\displaystyle |V|} , using big-O notation. The complexity bound depends mainly on the data structure used to represent the set Q. In the following, upper bounds can be simplified because | E | {\displaystyle |E|} is O ( | V | 2 ) {\displaystyle O(|V|^{2})} for any simple graph, but that simplification disregards the fact that in some problems, other upper bounds on | E | {\displaystyle |E|} may hold.

For any data structure for the vertex set Q, the running time i s:

Θ ( | E | T d k + | V | T e m ) , {\displaystyle \Theta (|E|\cdot T_{\mathrm {dk} }+|V|\cdot T_{\mathrm {em} }),}

where T d k {\displaystyle T_{\mathrm {dk} }} and T e m {\displaystyle T_{\mathrm {em} }} are the complexities of the decrease-key and extract-minimum operations in Q, respectively.

The simplest version of Dijkstra's algorithm stores the vertex set Q 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 Q, so the running time is Θ ( | E | + | V | 2 ) = Θ ( | V | 2 ) {\displaystyle \Theta (|E|+|V|^{2})=\Theta (|V|^{2})} .

For sparse graphs, that is, graphs with far fewer than | V | 2 {\displaystyle |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 Q changes. With a self-balancing binary search tree or binary heap, the algorithm requires

Θ ( ( | E | + | V | ) log | V | ) {\displaystyle \Theta ((|E|+|V|)\log |V|)}

time in the worst case; for connected graphs this time bound can be simplified to Θ ( | E | log | V | ) {\displaystyle \Theta (|E|\log |V|)} . The Fibonacci heap improves this to

Θ ( | E | + | V | log | V | ) . {\displaystyle \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 Θ ( | V | log ( | E | / | V | ) ) {\displaystyle \Theta (|V|\log(|E|/|V|))} , giving a total running time of

O ( | E | + | V | log | E | | V | log | V | ) . {\displaystyle O\left(|E|+|V|\log {\frac {|E|}{|V|}}\log |V|\right).}

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 C 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 b, then the algorithm's worst-case time and space complexity are both in O(b).

Further optimizations for the single-target case include bidirectional variants, goal-directed variants such as the A* algorithm (see § Related problems and algorithms), 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 st routing to connecting s and t 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.

Optimality for comparison-sorting by distance

As well as simply computing distances and paths, Dijkstra's algorithm can be used to sort vertices by their distances from a given starting vertex. In 2023, Haeupler, Rozhoň, Tětek, Hladík, and Tarjan (one of the inventors of the 1984 heap), proved that, for this sorting problem on a positively-weighted directed graph, a version of Dijkstra's algorithm with a special heap data structure has a runtime and number of comparisons that is within a constant factor of optimal among comparison-based algorithms for the same sorting problem on the same graph and starting vertex but with variable edge weights. To achieve this, they use a comparison-based heap whose cost of returning/removing the minimum element from the heap is logarithmic in the number of elements inserted after it rather than in the number of elements in the heap.

Specialized variants

When arc weights are small integers (bounded by a parameter C {\displaystyle 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 O ( | E | + | V | C ) {\displaystyle O(|E|+|V|C)} . The use of a Van Emde Boas tree as the priority queue brings the complexity to O ( | E | log log C ) {\displaystyle O(|E|\log \log C)} . Another interesting variant based on a combination of a new radix heap and the well-known Fibonacci heap runs in time O ( | E | + | V | log C ) {\displaystyle O(|E|+|V|{\sqrt {\log C}})} . Finally, the best algorithms in this special case run in O ( | E | log log | V | ) {\displaystyle O(|E|\log \log |V|)} time and O ( | E | + | V | min { ( log | V | ) 1 / 3 + ε , ( log C ) 1 / 4 + ε } ) {\displaystyle O(|E|+|V|\min\{(\log |V|)^{1/3+\varepsilon },(\log C)^{1/4+\varepsilon }\})} time.

Related problems and algorithms

Dijkstra's original algorithm can be extended with modifications. For example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution.

Dijkstra's algorithm is usually the working principle behind link-state routing protocols. OSPF and IS-IS are the most common.

Unlike Dijkstra's algorithm, the Bellman–Ford algorithm can be used on graphs with negative edge weights, as long as the graph contains no negative cycle reachable from the source vertex s. The presence of such cycles means that no shortest path can be found, since the label becomes lower each time the cycle is traversed. (This statement assumes that a "path" is allowed to repeat vertices. In graph theory that is normally not allowed. In theoretical computer science it often is allowed.) It is possible to adapt Dijkstra's algorithm to handle negative weights by combining it with the Bellman-Ford algorithm (to remove negative edges and detect negative cycles): Johnson's algorithm.

The A* algorithm is a generalization of Dijkstra's algorithm that reduces the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the distance to the target.

The process that underlies Dijkstra's algorithm is similar to the greedy process used in Prim's algorithm. Prim's purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only two nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual edges.

Breadth-first search can be viewed as a special-case of Dijkstra's algorithm on unweighted graphs, where the priority queue degenerates into a FIFO queue.

The fast marching method can be viewed as a continuous version of Dijkstra's algorithm which computes the geodesic distance on a triangle mesh.

Dynamic programming perspective

From a dynamic programming point of view, Dijkstra's algorithm is a successive approximation scheme that solves the dynamic programming functional equation for the shortest path problem by the Reaching method.

In fact, Dijkstra's explanation of the logic behind the algorithm:

Problem 2. Find the path of minimum total length between two given nodes P and Q. We use the fact that, if R is a node on the minimal path from P to Q, knowledge of the latter implies the knowledge of the minimal path from P to R.

is a paraphrasing of Bellman's Principle of Optimality in the context of the shortest path problem.

See also

Notes

  1. Controversial, see Moshe Sniedovich (2006). "Dijkstra's algorithm revisited: the dynamic programming connexion". Control and Cybernetics. 35: 599–620. and below part.
  2. ^ Cormen et al. 2001.
  3. ^ Fredman & Tarjan 1987.
  4. 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?
  5. ^ 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.
  6. Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs". Numerische Mathematik. 1: 269–271. CiteSeerX 10.1.1.165.7577. doi:10.1007/BF01386390. S2CID 123284777.
  7. ^ 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.
  8. 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.
  9. Leyzorek et al. 1957.
  10. 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.
  11. 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
  12. ^ 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.
  13. "ARMAC". Unsung Heroes in Dutch Computing History. 2007. Archived from the original on 13 November 2013.
  14. Dijkstra, Edsger W., Reflections on "A note on two problems in connexion with graphs (PDF)
  15. 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.
  16. 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.
  17. V. Jarník: O jistém problému minimálním , Práce Moravské Přírodovědecké Společnosti, 6, 1930, pp. 57–63. (in Czech)
  18. 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.
  19. Fredman & Tarjan 1984.
  20. Observe that p < dist cannot ever hold because of the update dist ← alt when updating the queue. See https://cs.stackexchange.com/questions/118388/dijkstra-without-decrease-key for discussion.
  21. 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.
  22. Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2022) . "22". Introduction to Algorithms (4th ed.). MIT Press and McGraw-Hill. pp. 622–623. ISBN 0-262-04630-X.
  23. ^ Russell, Stuart; Norvig, Peter (2009) . Artificial Intelligence: A Modern Approach (3rd ed.). Prentice Hall. pp. 75, 81. ISBN 978-0-13-604259-4.
  24. 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.
  25. Wagner, Dorothea; Willhalm, Thomas (2007). Speed-up techniques for shortest-path computations. STACS. pp. 23–36.
  26. 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". J. Experimental Algorithmics. 15: 2.1. doi:10.1145/1671970.1671976. S2CID 1661292.
  27. 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 .
  28. Brubaker, Ben (25 October 2024). "Computer Scientists Establish the Best Way to Traverse a Graph". Quanta Magazine. Retrieved 9 December 2024.
  29. Dial 1969.
  30. ^ Ahuja et al. 1990.
  31. Thorup 2000.
  32. Raman 1997.
  33. 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.
  34. Denardo, E.V. (2003). Dynamic Programming: Models and Applications. Mineola, NY: Dover Publications. ISBN 978-0-486-42810-9.
  35. Sniedovich, M. (2010). Dynamic Programming: Foundations and Principles. Francis & Taylor. ISBN 978-0-8247-4099-3.
  36. Dijkstra 1959, p. 270.

References

External links

Edsger Dijkstra
Works
Main research
areas
Related
people
Graph and tree traversal algorithms
Search
Shortest path
Minimum spanning tree
List of graph search algorithms
Optimization: Algorithms, methods, and heuristics
Unconstrained nonlinear
Functions
Gradients
Convergence
Quasi–Newton
Other methods
Hessians
Graph of a strictly concave quadratic function with unique maximum.
Optimization computes maxima and minima.
Constrained nonlinear
General
Differentiable
Convex optimization
Convex
minimization
Linear and
quadratic
Interior point
Basis-exchange
Combinatorial
Paradigms
Graph
algorithms
Minimum
spanning tree
Shortest path
Network flows
Metaheuristics
Categories: