Algorithm


Dijkstra's Algorithm:

  1. Initialize the distance of the source node to 0 and all other nodes to infinity.
  2. Create a priority queue and add the source node with a distance of 0.
  3. While the priority queue is not empty:
    1. Extract the node with the smallest distance from the priority queue.
    2. For each neighboring node of the extracted node:
      1. Calculate the tentative distance to the neighbor.
      2. If the tentative distance is smaller than the currently known distance, update it.
      3. Add the neighbor to the priority queue with the updated distance.
  4. Repeat until all nodes have been processed or the shortest path to the target node is found.

  5. Example:

    Dijkstra's Algorithm Example

    Fig. 1: Step-by-Step Process of Dijkstra's Algorithm

    Note: Dijkstra's Algorithm works only with graphs that have non-negative edge weights.