public LazyPrimMinimumSpanTree(EdgeWeightedGraph g)
 {
     pq     = new MinPriorityQueue <Edge>(g.Ecount);
     marked = new bool[g.Vcount];
     mst    = new Queue <Edge>();
     Visit(g, 0);
     while (!pq.IsEmpty())
     {
         var e = pq.DelMin();
         int v = e.Either;
         int 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);
         }
     }
 }
Beispiel #2
0
        public void MinPriorityQueueTest()
        {
            int[] xs    = { 3, 2, 1, 6, 5, 9, 4 };
            var   queue = new MinPriorityQueue <int>(xs.Length);

            for (int i = 0; i < xs.Length; i++)
            {
                queue.Insert(xs[i]);
            }
            Quick <int> .SimpleSort(xs);

            for (int i = 0; i < xs.Length; i++)
            {
                int v = queue.DelMin();
                Assert.Equal(v, xs[i]);
            }
        }
        public void MinPriorityQueueTest_InitWithKey()
        {
            int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Shuffle.Do(a);

            MinPriorityQueue <int> pq = new MinPriorityQueue <int>(a);

            Assert.IsFalse(pq.IsEmpty());
            Assert.AreEqual(10, pq.Size());

            for (int i = 0; i < 10; i++)
            {
                int result = pq.Min();
                Assert.AreEqual(i, result);
                result = pq.DelMin();
                Assert.AreEqual(i, result);
            }
            Assert.IsTrue(pq.IsEmpty());
            Assert.AreEqual(0, pq.Size());
        }
        public void MinPriorityQueueTest_Comparer()
        {
            Point[] points = new Point[10];
            for (int i = 0; i < 10; i++)
            {
                points[i] = new Point(i, 10 - i);
            }
            Shuffle.Do(points);

            MinPriorityQueue <Point> pq = new MinPriorityQueue <Point>(Point.X_ORDER);

            foreach (var value in points)
            {
                pq.Insert(value);
            }
            Assert.IsFalse(pq.IsEmpty());
            Assert.AreEqual(10, pq.Size());

            Point[] expectPoints = new Point[10];
            for (int i = 0; i < 10; i++)
            {
                expectPoints[i] = new Point(i, 10 - i);
            }
            Shuffle.Do(expectPoints);
            HeapSort.Sort(expectPoints, Point.X_ORDER, SortOrder.ASC);

            for (int i = 0; i < 10; i++)
            {
                var result = pq.Min();
                Assert.AreEqual(expectPoints[i].X, result.X);
                Assert.AreEqual(expectPoints[i].Y, result.Y);
                result = pq.DelMin();
                Assert.AreEqual(expectPoints[i].X, result.X);
                Assert.AreEqual(expectPoints[i].Y, result.Y);
            }
            Assert.IsTrue(pq.IsEmpty());
            Assert.AreEqual(0, pq.Size());
        }
Beispiel #5
0
        public KruskalMinimumSpanTree(EdgeWeightedGraph g)
        {
            mst = new List <Edge>();
            var pq = new MinPriorityQueue <Edge>(g.Ecount);

            foreach (var e in g.Edges())
            {
                pq.Insert(e);
            }
            var uf = new UF(g.Vcount);

            while (!pq.IsEmpty() && mst.Count < g.Vcount - 1)
            {
                var e = pq.DelMin();
                int v = e.Either;
                int w = e.Other(v);
                if (uf.Connected(v, w))
                {
                    continue;
                }
                uf.Union(v, w);
                mst.Add(e);
            }
        }