30 lines
1.4 KiB
Plaintext
30 lines
1.4 KiB
Plaintext
= 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*.
|
|
|
|
Dijkstra's Algorithm is a [[greedy_algorithm]].
|
|
|
|
== 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
|
|
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
|
|
set. A visited node is never checked again.
|
|
5) If searching for a specific node, the algorithm can stop when the desired
|
|
node is no longer in the 'unvisted' set.
|
|
6) If not, select the unvisted node with the smalled distance, and set it as
|
|
the current node, and go back to step 3
|
|
|
|
|