[prev] [index] [next]

Implementation of Dijkstra's Algorithm

C-like description of algorithm:

void shortestPath(Graph g, Vertex s)
{
   VertexSet vSet = {s}; // start vertex
   float dist[g->nV] = {inf,inf,...};
   Vertex pred[g->nV] = {-1,-1,...};
   EdgeSet eSet = edges(g)
   while (!empty(es)) {
      find (edge e) satisfying {
         e = (s,t,w), s ∈ vSet
         w is min weight of all such edges
      }
      dist[t] = dist[s] + w;  pred[t] = s;
      vSet += w;  eSet -= e
   }
   // dist[] and pred[] now set appropriately
}