Jump to content

Dijkstra's algorithm

From Simple English Wikipedia, the free encyclopedia
Revision as of 21:40, 30 June 2020 by Pikaryaa (talk | changes) (added diagram)
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.


Dijkstra's algorithm is an algorithm that works on groups of things connected by distances. It finds the shortest ways to move from one first thing to each other thing in the graph. It is faster than many other ways to do this, but it needs all of the distances connecting the things to be zero or more.

Steps next to the first thing

  • 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

  • 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.
  • https://github.com/xtaci/algorithms/blob/master/include/dijkstra.h

Other websites