What is Dijkstra's algorithm?
[[File:Dijkstra Animation.gif|thumb|Dijkstra's algorithm to find the shortest path between ''a'' and ''b''. It picks the unvisited node 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.]] '''Dijkstra's algorithm''' is a method to find the shortest paths between nodes in a [[graph]]. It is faster than many other ways to do this, but it needs all of the distances between nodes in the graph to be zero or more. == Algorithm == * Keep doing these steps: ** Find the thing that you have not yet drawn a mark next to that has the smallest number written next to it ** Draw a mark next to this thing ** Do these steps for each other thing connected to this place: *** Add the number written next to this thing and the distance of the connection together *** If the connected thing does not have a number written next to it, write the new number and the name of this thing next to the connected thing *** If the connected thing has a number written next to it and the new number is smaller than the written number: **** Draw a line over what is already written **** Write the new number and the name of this thing instead ** Stop when you have drawn a mark next to every thing in the list When you have done all of these steps you can use the list to find the shortest way from the first thing to any other thing. First write down the other thing. Then keep doing these steps: * Find the last thing you wrote down in the list * Write down the thing written next to it * Stop when you find the first thing The connections between the things you have written down are the shortest way from the first thing to the other thing. ==Pseudocode== In this [[pseudocode]] algorithm, the code {{mono|u ← vertex in ''Q'' with min dist[u]}}, searches for the vertex {{mono|<var>u</var>}} in the vertex set {{mono|<var>Q</var>}} that has the least {{mono|dist[<var>u</var>]}} value. {{mono|length(<var>u</var>, <var>v<