示例#1
0
 void Relax(EdgeWeightedDiagraph g, int v)
 {
     foreach (DirectedEdge e in g.AdjList(v))
     {
         int w = e.To();
         if (distTo[w] < distTo[v] + e.Weight())
         {
             distTo[w] = distTo[v] + e.Weight();
             edgeTo[w] = e;
         }
     }
 }
示例#2
0
        //Bellman Ford has the complexity of O(VE)
        public BellmanFordSP(EdgeWeightedDiagraph ewg, int source)
        {
            vertexNum = ewg.V();
            distTo    = new double[vertexNum];
            edgeTo    = new DirectedEdge[vertexNum];

            for (int i = 0; i < vertexNum; i++)
            {
                distTo[i] = double.PositiveInfinity;
            }
            distTo[source] = 0.0;
        }
示例#3
0
        public AcyclicSP(EdgeWeightedDiagraph ewg, int source)
        {
            edgeTo = new DirectedEdge[ewg.V()];
            distTo = new double[ewg.V()];
            for (int i = 0; i < ewg.V(); i++)
            {
                distTo[i] = double.PositiveInfinity;
            }
            distTo[source] = 0;
            TopologicalOrder tlOrder = new TopologicalOrder(ewg);

            foreach (int v in tlOrder.Order())
            {
                Relax(ewg, v);
            }
        }
示例#4
0
        public DijkstraSP(EdgeWeightedDiagraph g, int source)
        {
            vertexNumber = g.V();
            distTo       = new double[vertexNumber];
            for (int i = 0; i < vertexNumber; i++)
            {
                distTo[i] = double.PositiveInfinity;
            }
            distTo[source] = 0.0;

            edgeTo = new DirectedEdge[vertexNumber];

            pq = new IndexMinPQ <double>(vertexNumber);
            pq.Insert(source, 0.0);

            while (!pq.IsEmpty())
            {
                int current = pq.DelMin();
                Relax(g, current);
            }
        }
示例#5
0
 void Relax(EdgeWeightedDiagraph g, int v)
 {
     foreach (DirectedEdge e in g.AdjList(v))
     {
         int    w      = e.To();
         double weight = e.Weight();
         if (distTo[w] > distTo[v] + weight)
         {
             //relax
             edgeTo[w] = e;
             distTo[w] = distTo[v] + weight;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
示例#6
0
 void Relax(EdgeWeightedDiagraph ewg, int v)
 {
 }