Exemplo n.º 1
0
        /// <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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Relax the edge and update the priority queue if needed.
        /// </summary>
        /// <param name="edge">the edge</param>
        /// <param name="source">the source vertex where the edge leads from</param>
        private void Relax(WeightedGraphEdge edge, int source)
        {
            int v, w;

            if (source == edge.From)
            {
                v = edge.From;
                w = edge.To;
            }
            else
            {
                v = edge.To;
                w = edge.From;
            }
            if (distTo[w] > distTo[v] + edge.Cost)
            {
                distTo[w] = distTo[v] + edge.Cost;
                edgeTo[w] = edge;
                if (pq.Contains(w))
                {
                    pq.DecreaseKey(w, distTo[w]);
                }
                else
                {
                    pq.Insert(w, distTo[w]);
                }
            }
        }
Exemplo n.º 3
0
        /**
         * Relax the edge and update the priority queue if needed.
         * @param edge the edge
         * @param source the source vertex where the edge leads from
         */
        private void Relax(WeightedGraphEdge edge, int source)
        {
            int v, w;

            if (source == edge.From)
            {
                v = edge.From;
                w = edge.To;
            }
            else
            {
                v = edge.To;
                w = edge.From;
            }
            // UnityEngine.Debug.Log("find distance from " + w+" to "+v);
            if (distTo[w] > distTo[v] + edge.Cost)
            {
                distTo[w] = distTo[v] + edge.Cost;
                edgeTo[w] = edge;
                if (pq.Contains(w))
                {
                    pq.DecreaseKey(w, distTo[w]);
                }
                else
                {
                    pq.Insert(w, distTo[w]);
                }
            }
        }
Exemplo n.º 4
0
 // 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;
 }