/// <summary> /// Computes a shortest paths tree from <tt>s</tt> to every other vertex in the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="graph">the edge-weighted digraph</param> /// <param name="source">the source vertex</param> public DijkstraDirectedSearch(EdgeWeightedDirectedGraph graph, int source) { for (int i = graph.NumberOfEdges - 1; i >= 0; i--) { WeightedGraphEdge e = graph.GetEdge(i); if (e.Cost < 0) { throw new RPGException(ErrorMessage.INTERNAL_BAD_ARGUMENT, "edge " + e + " has negative weight"); } } distTo = new double[graph.NumberOfVertices]; edgeTo = new WeightedGraphEdge[graph.NumberOfVertices]; for (int v = 0; v < graph.NumberOfVertices; v++) { distTo[v] = Double.PositiveInfinity; } distTo[source] = 0.0; // relax vertices in order of distance from s pq = new IndexMinPQ <Double>(graph.NumberOfVertices); pq.Insert(source, distTo[source]); while (!pq.IsEmpty) { int v = pq.DelMin(); WeightedGraphEdge[] adj = graph.GetVertexAdjacencies(v); for (int i = adj.Length - 1; i >= 0; i--) { Relax(adj[i], v); } } // check optimality conditions Debug.Assert(Check(graph, source)); }
// 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; }
public void Reset() { innerPQ = copy; }