// 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(); }
// 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]); } }
private void relax(EdgeWeightedDigraph G ,int v){ for(DirectedEdge e:G.adj(V)){ int w = e.to(); if(distTo[w]>distTo[v]+e.weight()){ distTo[w] = distTo[v]+e.weight(); edgeTo[w] = e; if(pq.contains(w)) pq.change(w,distTo[w]); else pq.insert(w,distTo[w]); } } }
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]); } }
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()); } }
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. }
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); } }
public void visit(int v) { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue; if (disTo[w] > e.weight()) { disTo[w] = e.weight(); if (!pq.contains(w)) { pq.insert(w, e.weight()); } else pq.changeKey(w, e.weight()); edgeTo[w] = v; } } }
private void visit(EdgeWeightedGraph G, int v) // Add v to tree; update data structures. { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue; // v-w is ineligible. if (e.weight() < distTo[w]) // Edge e is new best connection from tree to w. { edgeTo[w] = e; distTo[w] = e.weight(); if (pq.contains(w)) pq.change(w, distTo[w]); else pq.insert(w, distTo[w]); } } }
private void scan(int v) { marked[v] = true; for (Route r : adj[v]) { int w = r.other(cities[v]).id() - 1; // index of other city in route if (marked[w]) { continue; // v-w is obsolete edge } if (r.distance() < distTo[w]) { distTo[w] = r.distance(); edgeTo[w] = r; if (pq.contains(w)) { pq.change(w, distTo[w]); } else { pq.insert(w, distTo[w]); } } } }
// 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); }
// 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); }