Example #1
0
 public static void computePaths(City source) {
   source.minDistance = 0.;
   PriorityQueue<City> cityQueue = new PriorityQueue<City>();
   cityQueue.add(source);
   while (!cityQueue.isEmpty()) {
     City u = cityQueue.poll();
     // Visit each edge exiting u
     for (Route e : u.neighbours) {
       City v = e.target;
       double weight = e.weight;
       double distanceThroughU = u.minDistance + weight;
       if (distanceThroughU < v.minDistance) {
         cityQueue.remove(v);
         v.minDistance = distanceThroughU;
         v.previous = u;
         cityQueue.add(v);
       }
     }
   }
 }