Ejemplo n.º 1
0
 // Option 7
 public void addRoute(String c1, String c2, int distance, double price) {
   if (c1.equals(c2) || distance < 0 || price < 0) {
     System.out.println("Invalid route info");
     return;
   }
   City city1 = null;
   City city2 = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c1)) {
       city1 = cities[i];
     }
     if (cities[i].name().equals(c2)) {
       city2 = cities[i];
     }
   }
   if (city1 == null || city2 == null) {
     System.out.println("Invalid city choice(s)");
     return;
   }
   Route r = new Route(city1, city2, distance, price);
   routes.add(r);
   adj[city1.id() - 1].add(r);
   adj[city2.id() - 1].add(r);
   numRoutes++;
 }
Ejemplo n.º 2
0
 // relax edge e and update pq if changed
 private void relaxC(Route r, int v) {
   City city2 = r.other(cities[v]);
   int w = city2.id() - 1;
   if (costTo[w] > costTo[v] + r.price()) {
     costTo[w] = costTo[v] + r.price();
     edgeTo[w] = r;
     if (costPQ.contains(w)) costPQ.change(w, costTo[w]);
     else costPQ.insert(w, costTo[w]);
   }
 }
Ejemplo n.º 3
0
 private void relaxD(Route r, int v) {
   // relax edge e and update pq if changed
   City city2 = r.other(cities[v]);
   int w = city2.id() - 1;
   if (distTo[w] > distTo[v] + r.distance()) {
     distTo[w] = distTo[v] + r.distance();
     edgeTo[w] = r;
     if (pq.contains(w)) pq.change(w, distTo[w]);
     else pq.insert(w, distTo[w]);
   }
 }
Ejemplo n.º 4
0
 public void removeRoute(City city1, City city2) {
   Route route = null;
   for (Route r : adj[city1.id() - 1]) {
     if (r.other(city1).equals(city2)) {
       route = r;
       break;
     }
   }
   adj[city1.id() - 1].remove(route);
   adj[city2.id() - 1].remove(route);
   routes.remove(route);
   numRoutes--;
 }
Ejemplo n.º 5
0
 // Option 10
 public void removeCity(String c) {
   City city = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c)) {
       city = cities[i];
     }
   }
   if (city == null) {
     System.out.println("Invalid city choice");
     return;
   }
   // Remove all routes connected to the city
   for (Route r : adj[city.id() - 1]) {
     City other = r.other(city);
     adj[other.id() - 1].remove(r);
     routes.remove(r);
     numRoutes--;
   }
   cities[city.id() - 1] = null;
   adj[city.id() - 1] = null;
   numCities--;
   // Shift and resize arrays as necessary
   shiftCities(city.id() - 1);
   shiftAdj(city.id() - 1);
   if (numCities < cities.length / 2) { // halve the lengths of the arrays
     resizeCities(cities.length / 2);
     resizeAdj(cities.length / 2);
   }
 }
Ejemplo n.º 6
0
 // Option 5
 public void shortestByHops(String c1, String c2) {
   System.out.println("FEWEST HOPS from " + c1 + " to " + c2);
   System.out.println("---------------------------------------------");
   City city1 = null;
   City city2 = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c1)) {
       city1 = cities[i];
     }
     if (cities[i].name().equals(c2)) {
       city2 = cities[i];
     }
   }
   if (c1.equals(c2) || city1 == null || city2 == null) {
     System.out.println("Invalid city choice(s)");
     return;
   }
   marked = new boolean[numCities];
   distTo = new int[numCities];
   edgeTo = new Route[numCities];
   for (int i = 0; i < numCities; i++) distTo[i] = Integer.MAX_VALUE;
   bfs(city1.id() - 1);
   if (distTo[city2.id() - 1] == Integer.MAX_VALUE) {
     System.out.println("No path");
     return;
   }
   System.out.printf("Fewest hops from %s to %s is %d\n", c1, c2, distTo[city2.id() - 1]);
   City currCity = city2;
   for (Route r = edgeTo[city2.id() - 1]; r != null; r = edgeTo[currCity.id() - 1]) {
     System.out.print(currCity + " ");
     currCity = r.other(currCity);
   }
   System.out.println(currCity);
 }
Ejemplo n.º 7
0
 // Option 4
 public void shortestByCost(String c1, String c2) {
   System.out.println("SHORTEST COST PATH from " + c1 + " to " + c2);
   System.out.println("--------------------------------------------------------");
   City city1 = null;
   City city2 = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c1)) {
       city1 = cities[i];
     }
     if (cities[i].name().equals(c2)) {
       city2 = cities[i];
     }
   }
   if (c1.equals(c2) || city1 == null || city2 == null) {
     System.out.println("Invalid city choice(s)");
     return;
   }
   costTo = new double[numCities];
   edgeTo = new Route[numCities];
   for (int i = 0; i < numCities; i++) costTo[i] = Double.POSITIVE_INFINITY;
   costTo[city1.id() - 1] = 0;
   // relax vertices in order of distance from s
   costPQ = new IndexMinPQ<Double>(numCities);
   costPQ.insert(city1.id() - 1, costTo[city1.id() - 1]);
   while (!costPQ.isEmpty()) {
     int v = costPQ.delMin();
     for (Route r : adj[v]) relaxC(r, v);
   }
   if (costTo[city2.id() - 1] == Double.POSITIVE_INFINITY) {
     System.out.println("No path");
     return;
   }
   System.out.printf("Shortest cost from %s to %s is %.2f\n", c1, c2, costTo[city2.id() - 1]);
   System.out.println("Path with edges (in reverse order):");
   City currCity = city2;
   for (Route r = edgeTo[city2.id() - 1]; r != null; r = edgeTo[currCity.id() - 1]) {
     System.out.print(currCity + " " + r.price() + " ");
     currCity = r.other(currCity);
   }
   System.out.println(currCity);
 }
Ejemplo n.º 8
0
 // Option 3
 public void shortestByDistance(String c1, String c2) {
   System.out.println("SHORTEST DISTANCE PATH from " + c1 + " to " + c2);
   System.out.println("--------------------------------------------------------");
   City city1 = null;
   City city2 = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c1)) {
       city1 = cities[i];
     }
     if (cities[i].name().equals(c2)) {
       city2 = cities[i];
     }
   }
   if (c1.equals(c2) || city1 == null || city2 == null) {
     System.out.println("Invalid city choice(s)");
     return;
   }
   distTo = new int[numCities];
   edgeTo = new Route[numCities];
   for (int i = 0; i < numCities; i++) distTo[i] = Integer.MAX_VALUE;
   distTo[city1.id() - 1] = 0;
   // relax vertices in order of distance from s
   pq = new IndexMinPQ<Integer>(numCities);
   pq.insert(city1.id() - 1, distTo[city1.id() - 1]);
   while (!pq.isEmpty()) {
     int v = pq.delMin();
     for (Route r : adj[v]) relaxD(r, v);
   }
   if (distTo[city2.id() - 1] == Integer.MAX_VALUE) {
     System.out.println("No path");
     return;
   }
   System.out.printf("Shortest distance from %s to %s is %d\n", c1, c2, distTo[city2.id() - 1]);
   System.out.println("Path with edges (in reverse order):");
   City currCity = city2;
   for (Route r = edgeTo[city2.id() - 1]; r != null; r = edgeTo[currCity.id() - 1]) {
     System.out.print(currCity + " " + r.distance() + " ");
     currCity = r.other(currCity);
   }
   System.out.println(currCity);
 }
Ejemplo n.º 9
0
  @RequestMapping(
      value = "/getCityApi",
      method = {RequestMethod.GET, RequestMethod.POST})
  public String getCityApi(
      HttpServletRequest request,
      @RequestParam(value = "locationname") String locationname,
      ModelMap model) {

    Map<Object, Object> map = new HashMap<Object, Object>();
    JSONArray jSONArray = new JSONArray();
    try {
      String status = "active";
      List<City> cityList = cityService.getCityApi(locationname, status);
      if (cityList != null && cityList.size() > 0) {
        for (int i = 0; i < cityList.size(); i++) {
          City city = (City) cityList.get(i);
          String cityName = (String) city.getCity();
          String stateName = (String) city.getState();

          int ID = (Integer) (city.getId());
          JSONObject jSONObject = new JSONObject();

          jSONObject.put("id", ID);
          jSONObject.put("text", cityName);
          jSONArray.put(jSONObject);
        }
        utilities.setSuccessResponse(response, jSONArray.toString());
      } else {
        throw new ConstException(ConstException.ERR_CODE_NO_DATA, ConstException.ERR_MSG_NO_DATA);
      }
    } catch (Exception ex) {
      logger.error("getCity :" + ex.getMessage());
      utilities.setErrResponse(ex, response);
    }
    model.addAttribute("model", jSONArray.toString());
    return "home";
  }
Ejemplo n.º 10
0
 private void recPaths(double maxCost, double currCost, City currCity) {
   // Backtrack if above max cost
   if (currCost > maxCost) {
     marked[currCity.id() - 1] = false;
     return;
   }
   // Print current path before continuing along routes
   if (edgeTo[currCity.id() - 1] != null) {
     System.out.printf("Cost: %.0f Path (reversed): ", currCost);
     City temp = currCity;
     for (Route r = edgeTo[temp.id() - 1]; r != null; r = edgeTo[temp.id() - 1]) {
       System.out.printf("%s %.0f ", temp, r.price());
       temp = r.other(temp);
     }
     System.out.println(temp);
   }
   // Recursion
   marked[currCity.id() - 1] = true;
   for (Route r : adj[currCity.id() - 1]) {
     City other = r.other(currCity);
     // Don't follow route if other city already in path
     if (!marked[other.id() - 1]) {
       edgeTo[other.id() - 1] = r;
       recPaths(maxCost, currCost + r.price(), other);
     }
   }
   // traversed all paths from currCity, backtrack to previous city
   marked[currCity.id() - 1] = false;
 }