public void Initialize() { _target = new IndexMinPQ<Distance>(4); _target.Insert(2, new Distance { V = 2, Dist = 1 }); _target.Insert(1, new Distance { V = 1, Dist = 2 }); _target.Insert(0, new Distance { V = 0, Dist = 3 }); }
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); } }
/// <summary> /// run Prim's algorithm in graph G, starting from vertex s /// </summary> /// <param name="g"></param> /// <param name="s"></param> private void Prim(EdgeWeightedGraph g, int s) { _distTo[s] = 0.0; _pq.Insert(s, _distTo[s]); while (!_pq.IsEmpty()) { var v = _pq.DelMin(); Scan(g, v); } }
/// <summary> /// Demo test the <c>IndexMinPQ</c> data type.</summary> /// <param name="args">Place holder for user arguments</param> /// public static void MainTest(string[] args) { // insert a bunch of strings string[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; IndexMinPQ <string> pq = new IndexMinPQ <string>(strings.Length); for (int i = 0; i < strings.Length; i++) { pq.Insert(i, strings[i]); } // delete and print each key while (!pq.IsEmpty) { int i = pq.DelMin(); Console.WriteLine(i + " " + strings[i]); } Console.WriteLine(); // reinsert the same strings for (int i = 0; i < strings.Length; i++) { pq.Insert(i, strings[i]); } // print each key using the iterator foreach (int i in pq) { Console.WriteLine(i + " " + strings[i]); } while (!pq.IsEmpty) { Console.WriteLine("Min k={0} at {1}", pq.MinKey, pq.MinIndex); Console.WriteLine("Removed {0}", pq.DelMin()); } }
private void Visit(EdgeWeightedGraph graph, int vertex) { marked[vertex] = true; foreach (var edge in graph.Adj(vertex)) { int otherVertex = edge.Other(vertex); if (marked[otherVertex]) { continue; } if (!(edge.Weight < distTo[otherVertex])) { continue; } edgeTo[otherVertex] = edge; distTo[otherVertex] = edge.Weight; if (prioryQueue.Contains(otherVertex)) { prioryQueue.ChangeKey(otherVertex, distTo[otherVertex]); } else { prioryQueue.Insert(otherVertex, distTo[otherVertex]); } } }
public DijkstraSP(EdgeWeightedDigraph g, int s) { foreach (DirectedEdge e in g.Edges()) { if (e.Weight() < 0) { throw new ArgumentOutOfRangeException("graph edge must have nonegative weights"); } } distTo = new double[g.V]; edgeTo = new DirectedEdge[g.V]; for (int v = 0; v < g.V; v++) { distTo[v] = double.PositiveInfinity; } distTo[s] = 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(); foreach (DirectedEdge e in g.Adj(v)) { relax(e); } } }
/// <summary> /// Dijkstra algorithm (shortest path) based on graph g for start vertice s. Positive cycles are allowed in shortest path algorithm /// </summary> /// <param name="g">Graph for search</param> /// <param name="s">Start vertice</param> public static PathStats ShortestPath(GraphBase g, int s) { var ps = new PathStats(g.V); for (var i = 0; i < ps.Dist.Length; i++) { ps.Dist[i] = int.MaxValue; ps.Prev[i] = -1; } ps.Dist[s] = 0;//start vertice var pq = new IndexMinPQ<Distance>(ps.Dist.Length); for (int i = 0; i < ps.Dist.Length; i++) { pq.Insert(i, new Distance { V = i, Dist = ps.Dist[i] }); } while (!pq.IsEmpty()) { var v = pq.DelRoot(); foreach (var e in g.Adjacent(v)) { if (ps.Dist[e.V2] > ps.Dist[v] + e.Weight) { ps.Dist[e.V2] = ps.Dist[v] + e.Weight; ps.Prev[e.V2] = v; pq.ChangeKey(e.V2, new Distance { V = e.V2, Dist = ps.Dist[e.V2] }); } } } return ps; }
public Dijkstra(WeightedDiGraph G, int s) { this.s = s; int V = G.V(); marked = new bool[V]; edgeTo = new Edge[V]; cost = new double[V]; for (var i = 0; i < V; ++i) { cost[i] = Double.MaxValue; } cost[s] = 0; pq = new IndexMinPQ <Double>(V); pq.Insert(s, 0); while (!pq.IsEmpty) { var v = pq.DelMin(); marked[v] = true; foreach (var e in G.adj(v)) { Relax(G, e); } } }
private void Visit(IEdgeWeightGraph g, int v) { marked[v] = true; foreach (var e in g.Adj(v)) { int w = e.Other(v); if (marked[w]) { continue; } if (e.Weight < distTo[w]) //更新最佳的边 { edgeTo[w] = e; distTo[w] = e.Weight; if (pq.Contains(w)) { pq.ChangeKey(w, distTo[w]); } else { pq.Insert(w, distTo[w]); } } } }
private readonly IndexMinPQ<Double> _pq; // priority queue of vertices #endregion Fields #region Constructors /// <summary> /// Computes a shortest-paths tree from the source vertex <tt>s</tt> to every /// other vertex in the edge-weighted graph <tt>G</tt>. /// </summary> /// <param name="g">g the edge-weighted graph</param> /// <param name="s">s the source vertex</param> /// <exception cref="ArgumentException">if an edge weight is negative</exception> /// <exception cref="ArgumentException">unless 0 <= <tt>s</tt> <= <tt>V</tt> - 1</exception> public DijkstraUndirectedSP(EdgeWeightedGraph g, int s) { foreach (var e in g.Edges()) { if (e.Weight < 0) throw new ArgumentException($"edge {e} has negative weight"); } _distTo = new double[g.V]; _edgeTo = new EdgeW[g.V]; for (var v = 0; v < g.V; v++) _distTo[v] = double.PositiveInfinity; _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()) { var v = _pq.DelMin(); foreach (var e in g.Adj(v)) Relax(e, v); } // check optimality conditions //assert check(G, s); }
public void Visit(EdgeWeightedGraph g, int v) { marked[v] = true; foreach (var edge in g.Adj(v)) { int v2 = edge.Other(v); if (marked[v2]) { continue; } if (edge.Weight < distanceTo[v2]) { edgeTo[v2] = edge; distanceTo[v2] = edge.Weight; if (pq.Contains(v2)) { pq.Change(v2, distanceTo[v2]); } else { pq.Insert(v2, distanceTo[v2]); } } } }
private readonly IndexMinPQ <Double> _pq; // priority queue of vertices /// <summary> /// Computes a shortest-paths tree from the source vertex <tt>s</tt> to every other /// vertex in the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="g">g the edge-weighted digraph</param> /// <param name="s">s the source vertex</param> /// <exception cref="ArgumentException">if an edge weight is negative</exception> /// <exception cref="ArgumentException">unless 0 <= <tt>s</tt> <= <tt>V</tt> - 1</exception> public DijkstraSP(EdgeWeightedDigraph g, int s) { foreach (var e in g.Edges()) { if (e.Weight < 0) { throw new ArgumentException($"edge {e} has negative weight"); } } _distTo = new double[g.V]; _edgeTo = new DirectedEdge[g.V]; for (var v = 0; v < g.V; v++) { _distTo[v] = double.PositiveInfinity; } _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()) { var v = _pq.DelMin(); foreach (var e in g.Adj(v)) { Relax(e); } } // check optimality conditions //assert check(G, s); }
public DijkstraSP(IEdgeWeightedDIgraph G, int s) : base(G, s) { pq = new IndexMinPQ <double>(G.V); pq.Insert(s, 0.0); while (!pq.IsEmpty) { Relax(G, pq.DeleteMin()); } }
// add all items to copy of heap // takes linear time since already in heap order so no keys move public HeapIEnumerator(IndexMinPQ <Key> minpq) { innerPQ = new IndexMinPQ <Key>(minpq.Count); for (int i = 1; i <= minpq.N; i++) { innerPQ.Insert(minpq.pq[i], minpq.keys[minpq.pq[i]]); } copy = innerPQ; }
private void Relax(EdgeWeightedDigraph g, int v) { foreach (var e in g.Adj(v)) { int w = e.To; if (_distTo[v] + e.Weight < _distTo[w]) { _edgeTo[w] = e; _distTo[w] = _distTo[v] + e.Weight; if (_pq.Contains(e.To)) { _pq.Change(e.To, _distTo[w]); } else { _pq.Insert(e.To, _distTo[w]); } } } }
public void Visit(EdgeWeightedGraph g, int v) { marked[v] = true; foreach (var edge in g.Adj(v)) { if (!marked[edge.Other(v)]) { Edge temp = new Edge(edge.V1, edge.V2, edge.CurrentValue); pq.Insert(0, temp); } } }
public DifkstraSP(EdgeWeightedDigraph g, int s) { _edgeTo = new DirectedEdge[g.V]; _distTo = Enumerable.Repeat(double.PositiveInfinity, g.V).ToArray(); _pq = new IndexMinPQ <double>(g.V); _distTo[s] = 0.0; _pq.Insert(s, 0.0); while (!_pq.IsEmpty()) { Relax(g, _pq.DelMin()); } }
public void IndexMinPQ_Enqueue() { var q = new IndexMinPQ<int>(items.Length); var minItem = int.MaxValue; for (int i = 0; i < items.Length; i++) { // After adding each item, the min item should be on top q.Insert(i, items[i]); minItem = Math.Min(items[i], minItem); Assert.AreEqual(q.MinKey(), minItem); } }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - tinyPQ.txt"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "tinyIndexPQ.txt"; break; case "quit": return; default: return; } var @in = new In($"Files\\Sorting\\{fieName}"); var words = @in.ReadAllStrings(); //var list = words.Select(word => new StringComparable(word)).ToList(); //var listComparable = list.Cast<IComparable>().ToList(); //var arrayComparable = list.Cast<IComparable>().ToArray(); var listStrings = words.ToList(); var pq = new IndexMinPQ <string>(listStrings.Count); //Fill Priority Queue for (var i = 0; i < listStrings.Count; i++) { pq.Insert(i, listStrings[i]); } // print results foreach (var item in pq) { Console.WriteLine(pq.KeyOf(item)); } Console.ReadLine(); }
public void IndexMinPQTest1() { const int MaxSize = 8; const double MinValue = 3.9; const double MaxValue = MinValue * MaxSize + 32; int index; // MinValue index == 3, MaxValue index == 4 double[] items = { MinValue * 2, MinValue * 3, MinValue * 4, MinValue, MaxValue, MinValue * 5, MinValue * 6, MinValue * 7 }; StdRandom.Seed = 101; IndexMinPQ <double> pq = new IndexMinPQ <double>(MaxSize); index = StdRandom.Uniform(items.Length); Assert.IsFalse(pq.Contains(index)); Assert.IsTrue(pq.IsEmpty); Assert.AreEqual(0, pq.Count); try { index = pq.DelMin(); Assert.Fail("Failed to catch exception"); } catch (InvalidOperationException) { } for (int i = 0; i < items.Length; i++) { pq.Insert(i, items[i]); } Assert.AreEqual(items.Length, pq.Count); Assert.AreEqual(MinValue, pq.MinKey); Assert.AreEqual(3, pq.MinIndex); Assert.AreEqual(MaxValue, pq.KeyOf(4)); index = StdRandom.Uniform(items.Length); Assert.AreEqual(items[index], pq.KeyOf(index)); pq.ChangeKey(1, pq.MinKey * 0.9); // make it the smallest item Assert.AreEqual(1, pq.MinIndex); pq.DecreaseKey(3, pq.MinKey * 0.87); Assert.AreEqual(3, pq.MinIndex); pq.Delete(3); Assert.AreNotEqual(3, pq.MinIndex); Assert.AreEqual(1, pq.DelMin()); }
public DijkstraAlgorithm(EdgeWeightedDigraph digraph, int source) : base(digraph, source) { //pq maintain a set of vertex need to deal with. IndexMinPQ <double> pq = new IndexMinPQ <double>(digraph.V); pq.Insert(source, distTo[source]); while (!pq.IsEmpty()) { //when v pops up, the distance and path to v have been confirmed int v = pq.DelMin(); foreach (DirectedEdge e in digraph.Adj(v)) { Relax(pq, e); } } }
private IndexMinPQ <double> pq; //有效的横切边 public PrimeMST(IEdgeWeightGraph G) { edgeTo = new Edge[G.V]; distTo = new double[G.V]; marked = new bool[G.V]; for (int v = 0; v < G.V; v++) { distTo[v] = Double.PositiveInfinity; } pq = new IndexMinPQ <double>(G.V); distTo[0] = 0.0; pq.Insert(0, 0.0); //顶点0权重初始化 while (!pq.IsEmpty) { Visit(G, pq.DeleteMin()); } }
/// <summary> /// relax edge e and update pq if changed /// </summary> /// <param name="e"></param> /// <param name="v"></param> private void Relax(EdgeW e, int v) { var 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]); } } }
public void IndexMinPQ_Dequeue() { var q = new IndexMinPQ<int>(items.Length); for (int i = 0; i < items.Length; i++) { q.Insert(i, items[i]); } var sortedItems = new List<int>(items); sortedItems.Sort(); foreach (var item in sortedItems) { // The top of the queue returns the items in sorted order var minIndex = q.DelMin(); Assert.AreEqual(items[minIndex], item); } }
private readonly decimal[] _distTo; // distTo[v] = distance of shortest s->v path #endregion Fields #region Constructors /** * Computes a shortest paths tree from <tt>s</tt> to every other vertex in * the edge-weighted digraph <tt>G</tt>. * @param G the edge-weighted digraph * @param s the source vertex * @throws IllegalArgumentException if an edge weight is negative * @throws IllegalArgumentException unless 0 ≤ <tt>s</tt> ≤ <tt>V</tt> - 1 */ public ShortestNeuronPath(NeuronGraph G, int s) { _distTo = new decimal[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) _distTo[v] = decimal.MaxValue; _distTo[s] = 0.0M; // relax vertices in order of distance from s pq = new IndexMinPQ<decimal>(G.V()); pq.Insert(s, _distTo[s]); while (!pq.IsEmpty()) { int v = pq.DelMin(); foreach (DirectedEdge e in G.adj(v)) relax(e); } }
/// <summary> /// relax edge e and update pq if changed /// </summary> /// <param name="e"></param> 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]); } } }
void IndexMinPQTest() { var strArr = FileHandler.ReadFileAsStrArr("words3.txt"); strArr.Show(); var pq = new IndexMinPQ <string>(strArr.Length); for (int i = 0; i < strArr.Length; i++) { pq.Insert(i, strArr[i]); } while (!pq.IsEmpty()) { Console.WriteLine(pq.DelMin()); } Console.WriteLine(); Console.ReadKey(); }
public PrimMinSpanningTree(EdgeWeightedGraph g) { edgeTo = new Edge[g.VertexCount]; distanceTo = new double[g.VertexCount]; marked = new bool[g.VertexCount]; pq = new IndexMinPQ <double>(g.VertexCount); for (int i = 0; i < g.VertexCount; i++) { distanceTo[i] = double.MaxValue; } distanceTo[0] = 0; pq.Insert(0, 0); while (!pq.IsEmpty()) { Visit(g, pq.DeleteMin()); } }
void Relax(EdgeWeightedDirectedGraph graph, int vertex) { foreach (var edge in graph.Adj(vertex)) { int v2 = edge.End; if (DistTo[v2] > DistTo[vertex] + edge.Weight) { DistTo[v2] = DistTo[vertex] + edge.Weight; EdgeTo[v2] = edge; if (pq.Contains(v2)) { pq.Change(v2, DistTo[v2]); } pq.Insert(v2, DistTo[v2]); } } }
public DijkstraShortestPath(EdgeWeightedDirectedGraph graph, int start) { EdgeTo = new DirectedEdge[graph.VertexCount]; DistTo = new double[graph.VertexCount]; pq = new IndexMinPQ <double>(graph.VertexCount); for (int i = 0; i < graph.VertexCount; i++) { DistTo[i] = double.PositiveInfinity; DistTo[start] = 0; pq.Insert(start, 0); while (!pq.IsEmpty()) { Relax(graph, pq.DeleteMin()); } } }
private void Visit(WeightedGraph G, int v) { marked[v] = true; foreach (var e in G.adj(v)) { int w = e.other(v); if (!marked[w]) { if (!pq.Contains(w)) { pq.Insert(w, e); } else { pq.DecreaseKey(w, e); } } } }
/// <summary> /// relax edge e and update pq if changed /// relax defined as follows: to relax an edge v-->w means to test whether the best known way from s to w is to go from s to v, then take the edge from v to w.If so, update our data structures to indicate that /// </summary> /// <param name="e"></param> private void relax(DirectedEdge e) { int v = e.From(); 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.DecreaseKey(w, distTo[w]); } else { pq.Insert(w, distTo[w]); } } }
private void Relax(WeightedDiGraph G, Edge e) { int v = e.from(); int w = e.to(); if (cost[w] > cost[v] + e.Weight) { cost[w] = cost[v] + e.Weight; edgeTo[w] = e; if (!pq.Contains(w)) { pq.Insert(w, cost[w]); } else { pq.DecreaseKey(w, cost[w]); } } }
protected void Relax(IndexMinPQ <double> pq, DirectedEdge e) { int v = e.From; 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.DecreaseKey(w, distTo[w]); } else { //this will never used pq.Insert(w, distTo[w]); } } }
public DijkstraSP(EdgeWeightedDigraph graph, int startVertex) { edgeTo = new DirectedEdge[graph.Vertices]; distTo = new double[graph.Vertices]; priorityQueue = new IndexMinPQ <double>(graph.Vertices); for (int v = 0; v < graph.Vertices; v++) { distTo[v] = double.MaxValue; } distTo[startVertex] = 0.0f; priorityQueue.Insert(startVertex, 0.0f); while (!priorityQueue.IsEmpty()) { Relax(graph, priorityQueue.DelMin()); } }
private void Relax(IEdgeWeightedDIgraph g, int v) { foreach (var e in 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.ChangeKey(w, distTo[w]); } else { pq.Insert(w, distTo[w]); } } } }