Dijkstra Algorithm

Dijkstra algorithm finds the shortest path from a certain vertex on a directed graph without negative weights.

Steps

  1. We have S as start vertex
  2. Create a priority queue PQ, and add S to PQ
  3. Set the distance to S be 0, and to others be ∞
  4. Take every closest vertex from PQ:
    1. Skip this vertex if it is visited
    2. Set this vertex to be visited
    3. For every edge connected to this vertex, update the distance and the path to the edge's target if the distance can be shorter
  5. After PQ being empty, we get the shortest paths from S to every vertex on the graph

Image Demo

Links

Wikipedia page of Dijkstra's algorithm
UW-Madison CS 400 Reading of Graphs