コード例 #1
0
        public void PopMin_1Node_CorrectNode()
        {
            var fh = new FibonacciHeap <int>();

            fh.Push(7);
            var actual = fh.PopMin();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(7, actual);

                Assert.Throws <HeapEmptyException>(() => fh.PopMin());
            });
        }
コード例 #2
0
        public void PopMin_2Nodes_CorrectNode()
        {
            var fh = new FibonacciHeap <int>();

            fh.Push(7);
            fh.Push(6);

            var first  = fh.PopMin();
            var second = fh.PopMin();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(6, first);
                Assert.AreEqual(7, second);

                Assert.Throws <HeapEmptyException>(() => fh.PopMin());
            });
        }
コード例 #3
0
        public void ProcessIntArray(int[] sequence)
        {
            var fh = new FibonacciHeap <int>();

            foreach (var s in sequence)
            {
                if (s < 0)
                {
                    var val = fh.PopMin();
                    Assert.AreEqual(System.Math.Abs(s), val);
                }
                else
                {
                    fh.Push(s);
                }
            }

            Assert.Throws <HeapEmptyException>(() => fh.PopMin());
        }
コード例 #4
0
        public void PopMin_5Nodes_CorrectNode()
        {
            var fh = new FibonacciHeap <int>();

            fh.Push(7);
            fh.Push(6);
            fh.Push(5);
            fh.Push(8);
            fh.Push(4);
            fh.Push(2);
            fh.Push(1);
            fh.Push(3);

            var first  = fh.PopMin();
            var second = fh.PopMin();
            var third  = fh.PopMin();
            var fourth = fh.PopMin();
            var fifth  = fh.PopMin();
            var sixth  = fh.PopMin();
            var seven  = fh.PopMin();
            var eigth  = fh.PopMin();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(1, first);
                Assert.AreEqual(2, second);
                Assert.AreEqual(3, third);
                Assert.AreEqual(4, fourth);
                Assert.AreEqual(5, fifth);
                Assert.AreEqual(6, sixth);
                Assert.AreEqual(7, seven);
                Assert.AreEqual(8, eigth);

                Assert.Throws <HeapEmptyException>(() => fh.PopMin());
            });
        }
コード例 #5
0
        public void Count_MultipleValuesPushedAndPopped_CorrectCount(int[] sequence)
        {
            var fh    = new FibonacciHeap <int>();
            var count = 0;

            foreach (var value in sequence)
            {
                if (value < 0)
                {
                    var val = fh.PopMin();
                    Assert.AreEqual(--count, fh.Count);
                }
                else
                {
                    fh.Push(value);
                    count++;
                }
            }
        }
コード例 #6
0
        public static FibonacciHeap <int> ProcessIntArray(int[] sequence)
        {
            var fh = new FibonacciHeap <int>();

            foreach (var s in sequence)
            {
                if (s < 0)
                {
                    var val = fh.PopMin();
                    Assert.AreEqual(System.Math.Abs(s), val);
                }
                else
                {
                    fh.Push(s);
                }
            }

            return(fh);
        }
コード例 #7
0
        public void FibonacciHeap_ShouldOrderLargeDataset()
        {
            var random = new Random(123);

            var numbers = Enumerable
                          .Range(0, 8)
                          .Select(_ => random.Next(1, 1000000))
                          .Distinct()
                          .ToList();

            var heap = new FibonacciHeap <int>();

            foreach (var i in numbers)
            {
                heap.Push(i);
            }

            foreach (var i in numbers.OrderBy(n => n))
            {
                Assert.AreEqual(i, heap.PopMin());
            }
        }
コード例 #8
0
        public void PopMin_EmptyHeap_Exception()
        {
            var fh = new FibonacciHeap <int>();

            Assert.Throws <HeapEmptyException>(() => fh.PopMin());
        }
コード例 #9
0
        public void FibonacciHeap_ShouldPerform()
        {
            var stringBuilder = new System.Text.StringBuilder();

            stringBuilder.AppendLine();
            var mod    = 4;
            var random = new Random(123);

            var numbers = Enumerable
                          .Range(0, 200000)
                          .Select(_ => random.Next(1, 1000000))
                          .Distinct()
                          .ToList();


            var baselineTimer = Stopwatch.StartNew();
            var queueB        = new BaselinePriorityQueue <int>();

            for (var i = 0; i < numbers.Count; i++)
            {
                queueB.Push(numbers[i]);
                if (i % mod == 0)
                {
                    queueB.PopMin();
                }
            }
            baselineTimer.Stop();
            var queueElapsed = baselineTimer.ElapsedMilliseconds;

            var heapTimer = Stopwatch.StartNew();
            var heap      = new FibonacciHeap <int>();

            for (var i = 0; i < numbers.Count; i++)
            {
                heap.Push(numbers[i]);
                if (i % mod == 0)
                {
                    heap.PopMin();
                }
            }
            heapTimer.Stop();
            var heapElapsed = heapTimer.ElapsedMilliseconds;

            stringBuilder.AppendLine($"Heap: {numbers.Count} processed in {heapElapsed}ms");

            // timer = Stopwatch.StartNew();
            // var list = new List<int>();
            // for (var i = 0; i < numbers.Count; i++)
            // {
            //     list.Add(numbers[i]);
            //     if (i % mod == 0) {
            //         list.Remove(list.Min());
            //     }
            // }
            // timer.Stop();
            // stringBuilder.AppendLine($"List: {numbers.Count} processed in {timer.ElapsedMilliseconds}ms");


            var variance = ((double)heapElapsed / (double)queueElapsed) * (double)100;

            stringBuilder.AppendLine($"Queue: {numbers.Count} processed in {queueElapsed}ms");

            stringBuilder.AppendLine();
            stringBuilder.AppendLine($"variance {heapElapsed - queueElapsed}ms ({Math.Round(variance, 2)}%)");
            stringBuilder.AppendLine();

            Console.WriteLine(stringBuilder.ToString());

            // Assert.Less(heapElapsed, queueElapsed);
        }