Exemple #1
0
        public static SP GetSp(EdgeWeightedDigraph G, int s)
        {
            SP sp;

            sp = new DijkstraSP(G, s);
            return(sp);
        }
Exemple #2
0
 private void Relax(EdgeWeightedDigraph G, int v)
 {
     foreach (var e in G.Adj(v))
     {
         var w = e.To();
         if (_distTo[w] > _distTo[v] + e.Weight())
         {
             _distTo[w] = _distTo[v] + e.Weight();
             _edgeTo[w] = e;
         }
     }
 }
Exemple #3
0
 public DijkstraSP(EdgeWeightedDigraph G, int s) : base(G, s)
 {
     _pq = new IndexMinPQ <double>(G.E());
     for (int i = 0; i < G.V(); i++)
     {
         _distTo[i] = double.PositiveInfinity;
     }
     _distTo[s] = 0d;
     _pq.Insert(0, 0d);
     while (!_pq.IsEmpty())
     {
         var w = _pq.RemoveMin();
         Relax(G, w);
     }
 }
Exemple #4
0
 private void Relax(EdgeWeightedDigraph G, int v)
 {
     foreach (var e in G.Adj(v))
     {
         var w = e.To();
         if (_distTo[w] > _distTo[v] + e.Weight())
         {
             _distTo[w] = _distTo[v] + e.Weight();
             _edgeTo[w] = e;
             if (_pq.Contains(w))
             {
                 _pq.Change(w, _distTo[w]);
             }
             else
             {
                 _pq.Insert(w, _distTo[w]);
             }
         }
     }
 }
Exemple #5
0
        public void RunSp()
        {
            EdgeWeightedDigraph G = new EdgeWeightedDigraph("tinyEWD.txt");
            int s  = 0;
            SP  sp = Factory.GetSp(G, s);

            for (int t = 0; t < G.V(); t++)
            {
                Console.Write(s + " to " + t);
                Console.Write(" ({0:F2}): ", sp.DistTo(t));
                if (sp.HasPathTo(t))
                {
                    foreach (DirectedEdge e in sp.PathTo(t))
                    {
                        Console.Write(e + " ");
                    }
                }
                Console.WriteLine();
            }
        }