home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ingzp26a / route.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.8 KB  |  159 lines

  1. /* $Id: Route.java,v 1.2 1996/03/31 11:43:02 djun Exp djun $
  2.  
  3.    File: Route.java
  4.  
  5.    Defines the class Route.   A route is a sequence of Carriers.
  6.  
  7.    Author: Djun M. Kim
  8.    Copyright (c) 1996 Djun M. Kim.  All rights reserved.
  9.  
  10.  
  11. */
  12.  
  13. import Carrier;
  14. import java.util.*;
  15.  
  16.  
  17. // A Route is a sequence of Carriers
  18. class Route {
  19.  
  20.     private Map parent;
  21.  
  22.     public static final int MAXINT = 2147483647;    // largest "int"
  23.  
  24.     private Vector segments;    // the carriers comprising the route.
  25.     private String name;        
  26.     private int length;        
  27.     private String description;  
  28.  
  29.     // Constructor - returns an empty route.
  30.     Route(Map parent) {
  31.     this.parent = parent;
  32.     segments = new Vector();
  33.     name = new String("");
  34.     description = new String("");
  35.     length = 0;
  36.     }
  37.  
  38.     public String print(Route r) {
  39.     String routestr = new String();
  40.     for (Enumeration e = segments.elements(); e.hasMoreElements(); ) {
  41.         routestr = routestr + ((Carrier) e.nextElement()).getName();
  42.         }
  43.     return routestr;
  44.     }
  45.  
  46.     public void addSegment(Carrier c) {        
  47.     length = length + c.getLength().intValue();
  48.     segments.addElement(c);
  49.     }
  50.  
  51.     public String getName() {
  52.     return name;
  53.     }
  54.  
  55.     public int length() {
  56.     return length;
  57.     }
  58.  
  59.  
  60.     /*------------------------------------------------------------
  61.             Routing/Shortest Path
  62.       ------------------------------------------------------------*/
  63.  
  64.     // Get the minimum distance between two locations.  
  65.     // Returns MAXINT if there is no carrier between the locations.  
  66.     // At the moment this assumes that there is only one type of carrier. 
  67.     // But the locations can be connected by multiple carriers.
  68.     public int dist(Location v, Location u) {
  69.     int distance = MAXINT;
  70.     for (Enumeration e = v.exits.elements(); e.hasMoreElements();) {
  71.         Carrier c = (Carrier)e.nextElement();
  72.         if (c.sink == u) {
  73.         distance = java.lang.Math.min(distance, c.length.intValue());
  74.         }
  75.     }
  76.     return distance;
  77.     }
  78.  
  79.  
  80.     // Get the shortest carrier between two locations.
  81.     // Returns null if there is no carrier between the locations.  
  82.     // At the moment this assumes that there is only one type of carrier. 
  83.     // But the locations can be connected by multiple carriers.
  84.     public Carrier minCarrier(Location v, Location u) {
  85.     Carrier min_c = new Carrier(parent, null, null, MAXINT, 0);
  86.     for (Enumeration e = v.exits.elements(); e.hasMoreElements();) {
  87.         Carrier c = (Carrier)e.nextElement();
  88.         if (c.sink == u) {
  89.         if (c.getLength().intValue() < min_c.getLength().intValue()) {
  90.             min_c = c;
  91.         }
  92.         }
  93.     }
  94.     return min_c;
  95.     }
  96.  
  97.     // Get shortest route between two locations on map.  Returns an
  98.     // empty route if no route exists.  Uses Dijkstra's algorithm...
  99.     public Route shortestPath(Location source, Location dest, Hashtable Locations) {
  100.     Hashtable length = new Hashtable();    // upper bound on length of shortest path
  101.     Hashtable shortest = new Hashtable();    // shortest path found so far - indexed by loc
  102.     Vector Examined = new Vector();        // locations examined so far
  103.     Vector Unexamined = new Vector();    
  104.     Route r = new Route(parent);
  105.     Location ux = new Location(parent, 0, 0);    // unexamined location
  106.     int lux = 0;                // length of path to ux.
  107.     if (source != null && dest != null) {
  108.         // Initialize length of "known" paths; unexamined locations
  109.         for (Enumeration enum = Locations.elements(); 
  110.            enum.hasMoreElements(); ) {
  111.         Location loc = (Location) enum.nextElement();
  112.         if (loc.getName() != "Carrier") {
  113.             length.put(loc, new Integer(dist(source, loc)));
  114.             Route s = new Route(parent);
  115.             s.addSegment(minCarrier(source, loc));
  116.             shortest.put(loc, s);
  117.             Unexamined.addElement(loc);
  118.         }
  119.         }
  120.         length.put(source, new Integer(0));
  121.         shortest.put(source, new Route(parent));
  122.         Examined.addElement(source);
  123.         Unexamined.removeElement(source);
  124.         while (!Unexamined.isEmpty()) {
  125.     found_least:for (Enumeration unex = Unexamined.elements(); unex.hasMoreElements(); ) {
  126.             ux = (Location)unex.nextElement();
  127.     next:        for (Enumeration ex = Examined.elements(); ex.hasMoreElements(); ) {
  128.             boolean found = true;
  129.             Location x = (Location)ex.nextElement();
  130.             lux = ((Integer)length.get(ux)).intValue();
  131.             int lx =  ((Integer)length.get(x)).intValue();
  132.             if (lux >= lx) {
  133.                 found = false;
  134.                 break next; 
  135.             }
  136.             if (found) break found_least;
  137.             }
  138.         }
  139.             Examined.addElement(ux);
  140.         Unexamined.removeElement(ux);
  141.         for (Enumeration unex = Unexamined.elements(); unex.hasMoreElements(); ) {
  142.             Location v = (Location)unex.nextElement();
  143.             int lv = ((Integer)length.get(v)).intValue();
  144.             int testlen = ((Integer)length.get(ux)).intValue() + dist(ux, v);
  145.             if (testlen < lv) {
  146.             length.put(v, new Integer(testlen));
  147.             Route s = (Route)shortest.get(ux);
  148.             s.addSegment(minCarrier(ux, v));
  149.             shortest.put(v, s);            // update shortest path to v
  150.             }
  151.         }
  152.         }
  153.         }
  154.         return r;
  155.     }
  156.    
  157. }
  158.  
  159.