Exemplo n.º 1
0
        public Dijstra(GridWorld G, int s)
        {
            world  = G;
            this.s = s;
            int V = G.VertexCount;

            marked = new bool[V];
            edgeTo = new DirectedWeightedEdge[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);
                }
            }
        }
Exemplo n.º 2
0
        private void Relax(GridWorld G, DirectedWeightedEdge 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]);
                }
            }
        }