Code Fragment: Dijkstra2



public:
  void run(const Vertex& s) {				// run Dijkstra's alg
    VertexIterator vertices = graph.vertices(); 
    while (vertices.hasNext()) {			// initialize vertices
      Vertex u = vertices.nextVertex();
      int u_dist = INFINITE;				// distance = infinity
      if (u == s) u_dist = 0;				// ...except source
      setDist(u, u_dist);
      Locator u_loc = Q.insertItem(u_dist, u);		// put vertex into Q
      setLoc(u, u_loc);					// save its locator
    }
    while (!Q.isEmpty()) {				// main processing loop
      Locator  u_loc   = Q.min();			// get closest vertex
      Vertex   u       = getVertex(u_loc);
      int      u_dist  = getDist(u_loc);		// ...and its distance
      Q.remove(u_loc);					// remove it from Q
      setDist(u, u_dist);				// set final distance
      destroyLoc(u);					// remove the locator
      if (u_dist == INFINITE) continue;			// ignore if unreachable
      EdgeIterator edges = graph.incidentEdges(u);
      while (edges.hasNext()) {				// visit u's neighbors
        Edge e = edges.nextEdge();
        Vertex z = graph.opposite(u,e);
        if (hasLoc(z)) {				// if z is not finished
          int      e_weight  = weight(e);		// get edge weight
          Locator  z_loc     = getLoc(z);
          int      z_dist    = getDist(z_loc);		// get distance to z
          if (u_dist + e_weight < z_dist)  		// relaxation of (u,z)
            Q.replaceKey(z_loc, u_dist + e_weight);
        }
      }
    }
  }