Пример #1
0
 public LazyPrimMST(EdgeWeightedGraph G)
     : base(G)
 {
     _marked = new bool[G.V()];
     _mst    = new Queue <Edge>();
     _pq     = new MinPQ <Edge>(G.E());
     Visit(G, 0);
     while (!_pq.IsEmpty())
     {
         Edge e = _pq.RemoveMin();
         var  v = e.Either();
         var  w = e.Other(v);
         if (_marked[v] && _marked[w])
         {
             continue;
         }
         _mst.Enqueue(e);
         if (!_marked[v])
         {
             Visit(G, v);
         }
         if (!_marked[w])
         {
             Visit(G, w);
         }
     }
 }
Пример #2
0
 public KruskalMST(EdgeWeightedGraph G) : base(G)
 {
     _pq  = new MinPQ <Edge>(G.E());
     _mst = new Queue <Edge>();
     _uf  = new UF(G.V());
     foreach (var e in G.Edges())
     {
         _pq.Insert(e);
     }
     while (!_pq.IsEmpty() && _mst.Count < G.V() - 1)
     {
         var e = _pq.RemoveMin();
         var v = e.Either();
         var w = e.Other(v);
         if (_uf.Connected(v, w))
         {
             continue;
         }
         _uf.Union(v, w);
         _mst.Enqueue(e);
     }
 }