Exemple #1
0
 private void Visit(EdgeWeightedGraph G, int v)
 {
     _marked[v] = true;
     foreach (var e in G.Adj(v))
     {
         var w = e.Other(v);
         if (_marked[w])
         {
             continue;
         }
         if (_distTo[w] > e.Weight())
         {
             _edgeTo[w] = e;
             _distTo[w] = e.Weight();
             if (_pq.Contains(w))
             {
                 _pq.Change(w, e.Weight());
             }
             else
             {
                 _pq.Insert(w, e.Weight());
             }
         }
     }
 }
Exemple #2
0
 public SimplePrimMST(EdgeWeightedGraph G)
     : base(G)
 {
     _marked = new bool[G.V()];
     _edgeTo = new Edge[G.V()];
     _pq     = new IndexMinPQ <double>(G.E());
     _pq.Insert(0, 0d);
     while (!_pq.IsEmpty())
     {
         int v = _pq.RemoveMin();
         Visit(G, v);
     }
 }
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
 public PrimMST(EdgeWeightedGraph G)
     : base(G)
 {
     _marked = new bool[G.V()];
     _edgeTo = new Edge[G.V()];
     _distTo = new double[G.V()];
     _pq     = new IndexMinPQ <double>(G.E());
     for (int v = 0; v < G.V(); v++)
     {
         _distTo[v] = double.PositiveInfinity;
     }
     _distTo[0] = 0d;
     _pq.Insert(0, 0d);
     while (!_pq.IsEmpty())
     {
         int v = _pq.RemoveMin();
         Visit(G, v);
     }
 }
Exemple #5
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]);
             }
         }
     }
 }