public void MinPQTest() { string[] graphItems = { "4 5 0.35", "4 7 0.37", "5 7 0.28", "0 7 0.16", "1 5 0.32", "0 4 0.38", "2 3 0.17", "1 7 0.19", "0 2 0.26", "1 2 0.36", "1 3 0.29", "2 7 0.34", "6 2 0.40", "3 6 0.52", "6 0 0.58", "6 4 0.93" }; var graph = new EdgeWeightedGraph(8, 16, graphItems); var pq = new MinPQ<Edge>(); foreach (Edge edge in graph.Edges()) { pq.insert(edge); } while (pq.isEmpty() == false) { pq.delMin(); } }
public KruskalMST(EdgeWeightedGraph ewg) { this.mst = new Queue(); MinPQ minPQ = new MinPQ(); Iterator iterator = ewg.edges().iterator(); while (iterator.hasNext()) { Edge edge = (Edge)iterator.next(); minPQ.insert(edge); } UF uF = new UF(ewg.V()); while (!minPQ.isEmpty() && this.mst.size() < ewg.V() - 1) { Edge edge = (Edge)minPQ.delMin(); int num = edge.either(); int i = edge.other(num); if (!uF.connected(num, i)) { uF.union(num, i); this.mst.enqueue(edge); this.weight += edge.weight(); } } if (!KruskalMST.s_assertionsDisabled && !this.check(ewg)) { throw new AssertionError(); } }
public static void Main(string[] arg) { string filename = "words3.txt"; string[] a = Util.readWords(filename); MaxPQ<string> pqMax = new MaxPQ<string>(); foreach (var item in a) { pqMax.insert(item); } // se vor afisa cuvintele in ordine descrescatoare Console.WriteLine("MaxPQ"); while (!pqMax.isEmpty()) { Console.WriteLine(pqMax.delMax()); } MinPQ<string> pqMin = new MinPQ<string>(); foreach (var item in a) { pqMin.insert(item); } // se vor afisa cuvintele in ordine crescatoare Console.WriteLine("\nMinPQ"); while (!pqMin.isEmpty()) { Console.WriteLine(pqMin.delMin()); } }
private Queue <Edge> mst = new Queue <Edge>(); // edges in MST public KruskalMST(EdgeWeightedGraph G) { // more efficient to build heap by passing array of edges EdgeComparer comparer = new EdgeComparer(); MinPQ <Edge> pq = new MinPQ <Edge>((Comparer <Edge>)comparer); foreach (Edge e in G.edges()) { pq.insert(e); } // run greedy algorithm UF uf = new UF(G.V()); while (!pq.isEmpty() && mst.size() < G.V() - 1) { Edge e = pq.delMin(); int v = e.either(); int w = e.other(v); if (!uf.connected(v, w)) { // v-w does not create a cycle uf.union(v, w); // merge v and w components mst.Enqueue(e); // add edge e to mst weight += e.Weight(); } } }
/** * Simulates the system of particles for the specified amount of time. * * @param limit the amount of time */ public void simulate(double limit) { // initialize PQ with collision events and redraw event pq = new MinPQ<Event>(); for (int i = 0; i < particles.length; i++) { predict(particles[i], limit); } pq.insert(new Event(0, null, null)); // redraw event // the main event-driven simulation loop while (!pq.isEmpty()) { // get impending event, discard if invalidated Event e = pq.delMin(); if (!e.isValid()) continue; Particle a = e.a; Particle b = e.b; // physical collision, so update positions, and then simulation clock for (int i = 0; i < particles.length; i++) particles[i].move(e.time - t); t = e.time; // process event if (a != null && b != null) a.bounceOff(b); // particle-particle collision else if (a != null && b == null) a.bounceOffVerticalWall(); // particle-wall collision else if (a == null && b != null) b.bounceOffHorizontalWall(); // particle-wall collision else if (a == null && b == null) redraw(limit); // redraw event // update the priority queue with new collisions involving a or b predict(a, limit); predict(b, limit); } }
public LazyPrimMST(EdgeWeightedGraph graph) { pq = new MinPQ<Edge>(); marked = new bool[graph.V]; mst = new Queue<Edge>(); visit(graph, 0); while (!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.Either, w = e.Other(v); if (marked[v] && marked[w]) continue; mst.Enqueue(e); if (!marked[v]) visit(graph, v); if (!marked[w]) visit(graph, w); } }
private Move expand(MinPQ <Move> moves) { if (moves.isEmpty()) { return(null); } Move bestMove = moves.delMin(); if (bestMove.board.isGoal()) { return(bestMove); } foreach (Board neighbor in bestMove.board.neighbors()) { if (bestMove.previous == null || !neighbor.equals(bestMove.previous.board)) { moves.insert(new Move(neighbor, bestMove)); } } return(null); }
private Queue<Edge> mst = new Queue<Edge>(); // edges in MST /** * Compute a minimum spanning tree (or forest) of an edge-weighted graph. * @param G the edge-weighted graph */ public KruskalMST(EdgeWeightedGraph G) { // more efficient to build heap by passing array of edges MinPQ<Edge> pq = new MinPQ<Edge>(); for (Edge e : G.edges()) { pq.insert(e); } // run greedy algorithm UF uf = new UF(G.V()); while (!pq.isEmpty() && mst.size() < G.V() - 1) { Edge e = pq.delMin(); int v = e.either(); int w = e.other(v); if (!uf.connected(v, w)) { // v-w does not create a cycle uf.union(v, w); // merge v and w components mst.enqueue(e); // add edge e to mst weight += e.weight(); } } // check optimality conditions assert check(G); }
private void prim(EdgeWeightedGraph G, int s) { scan(G, s); while (!pq.isEmpty()) { // better to stop when mst has V-1 edges Edge e = pq.delMin(); // smallest edge on pq int v = e.either(), w = e.other(v); // two endpoints if (marked[v] && marked[w]) { continue; // lazy, both v and w already scanned } mst.Enqueue(e); // add e to MST weight += e.Weight(); if (!marked[v]) { scan(G, v); // v becomes part of tree } if (!marked[w]) { scan(G, w); // w becomes part of tree } } }
// run Prim's algorithm private void prim(EdgeWeightedGraph G, int s) { scan(G, s); while (!pq.isEmpty()) { // better to stop when mst has V-1 edges Edge e = pq.delMin(); // smallest edge on pq int v = e.either(), w = e.other(v); // two endpoints assert marked[v] || marked[w];