2021-11-06 19:00:01 +00:00
|
|
|
= Dijkstra's Algorithm =
|
|
|
|
|
|
|
|
Dijkstra's Algorithm was created by Edsger W. Dijkstra, and is an algorithm
|
|
|
|
for finding the *shortest path between any two nodes in a graph*. This can be
|
|
|
|
thought of finding the best route from point A to point B on something like a
|
|
|
|
road map. This is also called a *routing protocol*.
|
|
|
|
|
2022-04-04 17:00:02 +00:00
|
|
|
Dijkstra's Algorithm is a [[greedy_algorithm]].
|
2021-11-06 19:00:01 +00:00
|
|
|
|
|
|
|
== Algorithm ==
|
|
|
|
|
|
|
|
1) Choose some initial node to start from. Create a set of distances from this
|
|
|
|
node to all other nodes, and set their distance to infinite. This algorithm
|
|
|
|
will change these distances and attempt to find the fastest way to each node.
|
|
|
|
2) Mark all nodes as 'unvisited'. All of these nodes are part of a set.
|
|
|
|
3) For the current node, consider all of its unvisited neighbors and calculate
|
|
|
|
the distance from the start node to this node.
|
|
|
|
- For example, if the current node is marked as a distance of 6 from the
|
|
|
|
starting node, and the edge of neighbor _N1_ has a distance of 2, then
|
2021-11-06 19:15:01 +00:00
|
|
|
N1's distance is 6+2 = 8. If the node was previously marked as having a
|
|
|
|
different value, keep the smallest of the two values.
|
|
|
|
4) Once we have checked all of a nodes neighbors, remove it from the unvisited
|
2022-04-04 17:00:02 +00:00
|
|
|
set. A visited node is never checked again.
|
2021-11-06 19:15:01 +00:00
|
|
|
5) If searching for a specific node, the algorithm can stop when the desired
|
2022-04-04 17:00:02 +00:00
|
|
|
node is no longer in the 'unvisted' set.
|
2021-11-06 19:15:01 +00:00
|
|
|
6) If not, select the unvisted node with the smalled distance, and set it as
|
|
|
|
the current node, and go back to step 3
|
2021-11-06 19:00:01 +00:00
|
|
|
|
|
|
|
|