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[change | change source]
- 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.
References[change | change source]
- Cormen, Thomas H.; 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.