A visual demonstration of Dijkstra's algorithm

Current Queue
  1. Assign to every node a distance value. Set it to zero for the initial node and to infinity for all other nodes.
  2. Mark all nodes as unvisited, except for the initial node.
  3. Queue the inital node.
  4. While the queue is not empty:
    1. Pop the top of the queue, making it the current node.
    2. For each unvisited neighbor of the current node: (shown as unvisited)
      1. Set the distance to the unvisited node equal to the distance to the current node, plus the distance from the current node to the unvisited node (in a square graph, the distance between any adjacent nodes is 1).
      2. Mark the unvisited node as visited.
      3. Queue the node.
  5. Once the queue is empty, the algorithm is finished.

This demonstration was created by Ryan Patterson on January 29, 2009. Licensed under Creative Commons License.