VisuAl

Dijkstra's Algorithm

Djikstra's algorithm finds the shortest path between two nodes in a graph. After a start and a finish node are defined, all of the nodes are placed into a set for unvisited nodes. Each node is given a distance upon creation, infinity, and the node that is set as the start has its distance set to 0. Next, the algorithm can begin solving for the shortest distance.

  1. The set of unvisited nodes is sorted by distance from least to greatest
  2. The first node in the unvisited set is removed and set as the current node
  3. If the current node is a wall, move onto the next node
  4. If the current node's distance is infinity, this means that the finish node is unreachable and the set of visited nodes is returned
  5. If the current node is the finish node, return the set of visited nodes
  6. Otherwise, the current node is marked as visited. Next, it is added to the set of visited nodes. Finally, the current neighbor's neighbors have their distances set to the current node's distance +1 and their previous node set to the current node
  7. Finally, steps 1-6 are repeated until either the finish node has been found, the entire grid has been traversed, or the distance of the current node is infinity, indicating that the finish is unreachable

If the finish node has been found, its previous nodes are traced back to the start and the shortest path has been found

More Info...