private static void merge(int[] streams) { int n = streams.Length; IndexMinPQ <string> pq = new IndexMinPQ <string>(n); for (int i = 0; i < n; i++) { if (!streams[i].Equals(null)) { pq.insert(i, streams[i].ToString()); } } // Extract and print min and read next from its stream. while (!pq.isEmpty()) { print(pq.minKey() + " "); int i = pq.delMin(); if (!streams[i].Equals(null)) { pq.insert(i, streams[i].ToString()); } } }
// run Prim's algorithm in graph G, starting from vertex s private void prim(EdgeWeightedGraph G, int s) { distTo[s] = 0.0; pq.insert(s, distTo[s]); while (!pq.isEmpty()) { int v = pq.delMin(); scan(G, v); } }
private void Visit(EdgeWeightedGraph graph, int v) { _marked[v] = true; foreach (Edge edge in graph.Adj(v)) { int w = edge.Other(v); if (_marked[w]) { continue; } if (edge.Weight < _distTo[w]) { _edgeTo[w] = edge; _distTo[w] = edge.Weight; if (_pq.contains(w)) { _pq.change(w, _distTo[w]); } else { _pq.insert(w, _distTo[w]); } } } }
// 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(); }
private void visit(EdgeWeightedGraph G, int v) { mark[v] = true; foreach (Edge e in G.adj(v)) { int w = e.other(v); if (mark[w]) { continue; } if (e.getWeight() < distTo[w]) { edgeTo[w] = e; distTo[w] = e.getWeight(); if (pq.contains(w)) { pq.change(w, distTo[w]); } else { pq.insert(w, distTo[w]); } } } }
private IndexMinPQ<Double> pq; // priority queue of vertices /** * Computes a shortest-paths tree from the source vertex {@code s} to every * other vertex in the edge-weighted graph {@code G}. * * @param G the edge-weighted digraph * @param s the source vertex * @throws IllegalArgumentException if an edge weight is negative * @throws IllegalArgumentException unless {@code 0 <= s < V} */ public DijkstraUndirectedSP(EdgeWeightedGraph G, int s) { for (Edge e : G.edges()) { if (e.weight() < 0) throw new IllegalArgumentException("edge " + e + " has negative weight"); } distTo = new double[G.V()]; edgeTo = new Edge[G.V()]; validateVertex(s); for (int v = 0; v < G.V(); v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s] = 0.0; // relax vertices in order of distance from s pq = new IndexMinPQ<Double>(G.V()); pq.insert(s, distTo[s]); while (!pq.isEmpty()) { int v = pq.delMin(); for (Edge e : G.adj(v)) relax(e, v); } // check optimality conditions assert check(G, s); }
// relax edge e and update pq if changed private void relax(Edge e, int v) { int w = e.other(v); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert(w, distTo[w]); } }
// relax edge e and update pq if changed private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert(w, distTo[w]); } }
public PrimMST(EdgeWeightedGraph G) { edgeTo = new Edge[G.v()]; distTo = new double[G.v()]; mark = new bool[G.v()]; for (int v = 0; v < G.v(); v++) { distTo[v] = double.MaxValue; } pq = new IndexMinPQ <double>(4); distTo[0] = 0.0; pq.insert(0, 0.0); while (!pq.isEmpty()) { visit(G, pq.delMin()); } }
public PrimeMST(EdgeWeightedGraph graph) { _edgeTo = new Edge[graph.V]; _distTo = new double[graph.V]; _marked = new bool[graph.V]; for (int i = 0; i < graph.V; i++) { _distTo[i] = double.PositiveInfinity; } _pq = new IndexMinPQ <double>(graph.V); _distTo[0] = 0.0; _pq.insert(0, 0.0); while (!_pq.isEmpty()) { Visit(graph, _pq.delMin()); } }
private IndexMinPQ <double> pq; // priority queue of vertices public DijkstraSP(EdgeWeightedDigraph G, int s) { foreach (DirectedEdge e in G.edges()) { if (e.Weight() < 0) { throw new System.Exception("edge " + e + " has negative weight"); } } distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()]; validateVertex(s); for (int v = 0; v < G.V(); v++) { distTo[v] = double.PositiveInfinity; } distTo[s] = 0.0; // relax vertices in order of distance from s IndexMinPQComparer comparator = new IndexMinPQComparer(); pq = new IndexMinPQ <double>(G.V(), (Comparer <double>)comparator); pq.insert(s, distTo[s]); while (!pq.isEmpty()) { int v = pq.delMin(); foreach (DirectedEdge e in G.Adj(v)) { relax(e); } } }