Beispiel #1
0
        private void FindPath(EdgeWeightedDigraph digraph, int start)
        {
            priorityQueue.Insert(start, 0f);

            while (!priorityQueue.IsEmpty)
            {
                Relax(digraph, priorityQueue.DeleteMax());
            }
        }
Beispiel #2
0
 protected virtual void Relax(EdgeWeightedDigraph digraph, int vertex)
 {
     foreach (var edge in digraph.Adjacent(vertex))
     {
         int w = edge.W;
         if (distanceTo[w] > distanceTo[vertex] + edge.Weight)
         {
             distanceTo[w] = distanceTo[vertex] + edge.Weight;
             edgeTo[w]     = edge;
         }
     }
 }
Beispiel #3
0
        protected SP(EdgeWeightedDigraph digraph, int start)
        {
            this.start = start;
            edgeTo     = new DirectedEdge[digraph.VertexCount];
            distanceTo = new float[digraph.VertexCount];

            for (int v = 0; v < digraph.VertexCount; v++)
            {
                distanceTo[v] = float.MaxValue;
            }
            distanceTo[start] = 0f;
        }
Beispiel #4
0
        protected override void Relax(EdgeWeightedDigraph digraph, int vertex)
        {
            foreach (var edge in digraph.Adjacent(vertex))
            {
                int w = edge.W;
                if (distanceTo[w] > distanceTo[vertex] + edge.Weight)
                {
                    distanceTo[w] = distanceTo[vertex] + edge.Weight;
                    edgeTo[w]     = edge;

                    if (priorityQueue.Contains(w))
                    {
                        priorityQueue.Change(w, distanceTo[w]);
                    }
                    else
                    {
                        priorityQueue.Insert(w, distanceTo[w]);
                    }
                }
            }
        }
Beispiel #5
0
        private static EdgeWeightedDigraph CreateGraph()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(8);

            digraph.AddEdge(4, 5, 35f);
            digraph.AddEdge(5, 4, 35f);
            digraph.AddEdge(4, 7, 37f);
            digraph.AddEdge(5, 7, 28f);
            digraph.AddEdge(7, 5, 28f);

            digraph.AddEdge(5, 1, 32f);
            digraph.AddEdge(0, 4, 38f);
            digraph.AddEdge(0, 2, 26f);
            digraph.AddEdge(7, 3, 39f);
            digraph.AddEdge(1, 3, 29f);

            digraph.AddEdge(2, 7, 34f);
            digraph.AddEdge(6, 2, 40f);
            digraph.AddEdge(3, 6, 52f);
            digraph.AddEdge(6, 0, 58f);
            digraph.AddEdge(6, 4, 93f);

            return(digraph);
        }
Beispiel #6
0
 public Dijkstra(EdgeWeightedDigraph digraph, int start) : base(digraph, start)
 {
     priorityQueue = new IndexPriorityQueue <float>(digraph.VertexCount);
     FindPath(digraph, start);
 }