Exemplo n.º 1
0
	public DijkstraSP(EdgeWeightedDigraph G , int s){
		edgeTo =new DirectedEdge[G.V()];
		distTo =new double[G.V()];
		pq = new  IndexMinPQ<Double>(G.V());

		for(int v = 0 ; v < G.V() ; v++) distTo[v] = Double.POSITIVE_INFINITY;
		distTo[s]=0;
		pq.insert(s,0.0);
		while(!pq.isEmpty()){
			relax(G,pq.delMin());
		}
	}
Exemplo n.º 2
0
  public PrimMST(EdgeWeightedGraph G) {
    edgeTo = new Edge[G.V()];
    distTo = new double[G.V()];
    marked = new boolean[G.V()];

    for (int v = 0; v < G.V(); v++) distTo[v] = Double.POSITIVE_INFINITY;
    pq = new IndexMinPQ<Double>(G.V());

    distTo[0] = 0.0;
    pq.insert(0, 0.0); // Initialize pq with 0, weight 0.
    while (!pq.isEmpty()) visit(G, pq.delMin()); // Add closest vertex to tree.
  }
Exemplo n.º 3
0
 private void prim(int s, StringBuilder sb) {
   distTo[s] = 0;
   pq.insert(s, distTo[s]);
   while (!pq.isEmpty()) {
     int v = pq.delMin();
     if (v != s) {
       Route r = edgeTo[v];
       String str = String.format("%s, %s : %d\n", r.fst(), r.snd(), r.distance());
       sb.append(str);
     }
     scan(v);
   }
 }
Exemplo n.º 4
0
  // merge together the sorted input streams and write the sorted result to standard output
  private static void merge(In[] streams) {
    int N = streams.length;
    IndexMinPQ<String> pq = new IndexMinPQ<String>(N);
    for (int i = 0; i < N; i++) if (!streams[i].isEmpty()) pq.insert(i, streams[i].readString());

    // Extract and print min and read next from its stream.
    while (!pq.isEmpty()) {
      StdOut.print(pq.minKey() + " ");
      int i = pq.delMin();
      if (!streams[i].isEmpty()) pq.insert(i, streams[i].readString());
    }
    StdOut.println();
  }
Exemplo n.º 5
0
 public MyPrimMST(EdgeWeightedGraph G) {
   this.G = G;
   pq = new IndexMinPQ<Double>(G.V());
   disTo = new double[G.V()];
   edges = new Bag<Edge>();
   for (int i = 0; i < G.V(); ++i) disTo[i] = Double.POSITIVE_INFINITY;
   edgeTo = new int[G.V()];
   marked = new boolean[G.V()];
   disTo[0] = 0.0;
   visit(0);
   while (!pq.isEmpty()) {
     int v = pq.delMin();
     weight += disTo[v];
     edges.add(new Edge(v, edgeTo[v], disTo[v]));
     visit(v);
   }
 }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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);
 }