// Textbook fragment 13.16 /** The actual execution of Dijkstra's algorithm. * @param v source vertex. */ protected void dijkstraVisit (Vertex v) { // store all the vertices in priority queue Q for (Vertex u: graph.vertices()) { int u_dist; if (u==v) u_dist = 0; else u_dist = INFINITE; Entry> u_entry = Q.insert(u_dist, u); // autoboxing u.put(ENTRY, u_entry); } // grow the cloud, one vertex at a time while (!Q.isEmpty()) { // remove from Q and insert into cloud a vertex with minimum distance Entry> u_entry = Q.min(); Vertex u = u_entry.getValue(); int u_dist = u_entry.getKey(); Q.remove(u_entry); // remove u from the priority queue u.put(DIST,u_dist); // the distance of u is final u.remove(ENTRY); // remove the entry decoration of u if (u_dist == INFINITE) continue; // unreachable vertices are not processed // examine all the neighbors of u and update their distances for (Edge e: graph.incidentEdges(u)) { Vertex z = graph.opposite(u,e); Entry> z_entry = (Entry>) z.get(ENTRY); if (z_entry != null) { // check that z is in Q, i.e., not in the cloud int e_weight = (Integer) e.get(WEIGHT); int z_dist = z_entry.getKey(); if ( u_dist + e_weight < z_dist ) // relaxation of edge e = (u,z) Q.replaceKey(z_entry, u_dist + e_weight); } } } }