コード例 #1
0
        public void MergeTest()
        {
            FibonacciHeap <int, string> heap  = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            FibonacciHeap <int, string> heap2 = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;

            for (int i = 11; i > 0; i--)
            {
                heap.Enqueue(i, i.ToString());
                heap2.Enqueue(i * 11, i.ToString());
                count += 2;
            }
            heap2.Merge(heap);
            int?lastValue = null;

            foreach (var value in heap2.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                if (lastValue > value.Key)
                {
                    Assert.Fail("Heap condition has been violated");
                }
                lastValue = value.Key;
                count--;
            }
            Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued");
        }
コード例 #2
0
        public void IncreasingIncreaseKeyCascadeCut()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            lastValue = heap.Top.Priority;
            heap.Dequeue();
            count--;
            heap.ChangeKey(cells[5], 10);
            string s = (heap as FibonacciHeap <int, string>).DrawHeap();

            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                if (lastValue > value.Key)
                {
                    Assert.Fail("Heap condition has been violated: {0} > {1}", lastValue, value.Key);
                }
                lastValue = value.Key;
                count--;
            }
            Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued");
        }
コード例 #3
0
        public void DeleteKey()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            heap.Dequeue();
            var DeletedCell = cells[8];

            heap.Delete(DeletedCell);
            count -= 2;
            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.IsFalse(lastValue > value.Key, "Heap condition has been violated");
                Assert.AreNotEqual(DeletedCell, value, "Found item that was deleted");
                lastValue = value.Key;
                count--;
            }
            Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued");
        }
コード例 #4
0
        public void SimpleEnqueDequeDecreasing()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;

            for (int i = 0; i < 10; i++)
            {
                heap.Enqueue(i, i.ToString());
                count++;
            }
            int?lastValue = null;

            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                if (lastValue < value.Key)
                {
                    Assert.Fail("Heap condition has been violated");
                }
                lastValue = value.Key;
                count--;
            }
            Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued");
        }
コード例 #5
0
        public void ChangeKeyToSelf()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            heap.ChangeKey(cells[0], 0);
            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                if (lastValue < value.Key)
                {
                    Assert.Fail("Heap condition has been violated");
                }
                lastValue = value.Key;
                count--;
            }
            Assert.AreEqual(count, 0, "Not all elements enqueued were dequeued");
        }
コード例 #6
0
        public void MergeTest()
        {
            FibonacciHeap <int, string> heap  = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            FibonacciHeap <int, string> heap2 = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;

            for (int i = 11; i > 0; i--)
            {
                heap.Enqueue(i, i.ToString());
                heap2.Enqueue(i * 11, i.ToString());
                count += 2;
            }
            heap2.Merge(heap);
            int?lastValue = null;

            foreach (var value in heap2.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue > value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #7
0
        public void IncreasingIncreaseKeyCascadeCut()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            lastValue = heap.Top.Priority;
            heap.Dequeue();
            count--;
            heap.ChangeKey(cells[5], 10);
            string s = (heap as FibonacciHeap <int, string>).DrawHeap();

            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue > value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #8
0
        public void DeleteKey()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            heap.Dequeue();
            var DeletedCell = cells[8];

            heap.Delete(DeletedCell);
            count -= 2;
            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue > value.Key);
                //TODO FIXME Assert.NotEqual(DeletedCell, value);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #9
0
        public void IncreaseKeyOnDecreasing()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            heap.ChangeKey(cells[0], 100);
            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                if (lastValue < value.Key)
                {
                    // Assert.Fail("Heap condition has been violated");
                    //FIXME TODO
                }
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #10
0
        public void ChangeKeyToSelf()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; i++)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                count++;
            }
            int?lastValue = null;

            heap.ChangeKey(cells[0], 0);
            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue < value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #11
0
        public void DecreaseKeyOnIncreasing()
        {
            var heap  = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; ++i)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                ++count;
            }

            int?lastValue = null;

            heap.ChangeKey(cells[9], -1);
            foreach (KeyValuePair <int, string> value in heap.GetDestructiveEnumerator())
            {
                if (lastValue is null)
                {
                    lastValue = value.Key;
                }

                if (lastValue > value.Key)
                {
                    Assert.Fail("Heap condition has been violated.");
                }

                lastValue = value.Key;
                --count;
            }

            Assert.AreEqual(0, count, "Not all elements enqueued were dequeued.");
        }
コード例 #12
0
        public void SimpleEnqueueDequeDecreasing()
        {
            var heap  = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;

            for (int i = 0; i < 10; ++i)
            {
                heap.Enqueue(i, i.ToString());
                ++count;
            }

            int?lastValue = null;

            foreach (KeyValuePair <int, string> value in heap.GetDestructiveEnumerator())
            {
                if (lastValue is null)
                {
                    lastValue = value.Key;
                }

                if (lastValue < value.Key)
                {
                    Assert.Fail("Heap condition has been violated.");
                }

                lastValue = value.Key;
                --count;
            }
            Assert.AreEqual(0, count, "Not all elements enqueued were dequeued.");
        }
コード例 #13
0
        public void IncreasingIncreaseKeyCascadeCut()
        {
            var heap  = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            int count = 0;
            var cells = new Dictionary <int, FibonacciHeapCell <int, string> >();

            for (int i = 0; i < 10; ++i)
            {
                cells.Add(i, heap.Enqueue(i, i.ToString()));
                ++count;
            }

            int lastValue = heap.Top.Priority;

            heap.Dequeue();
            --count;
            heap.ChangeKey(cells[5], 10);
            foreach (KeyValuePair <int, string> value in heap.GetDestructiveEnumerator())
            {
                if (lastValue > value.Key)
                {
                    Assert.Fail($"Heap condition has been violated: {lastValue} > {value.Key}");
                }

                lastValue = value.Key;
                --count;
            }
            Assert.AreEqual(0, count, "Not all elements enqueued were dequeued.");
        }
コード例 #14
0
        public void NextCutOnGreaterThan()
        {
            var heap       = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            var heap2      = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            var toCutNodes = new List <FibonacciHeapCell <int, string> >();

            heap.Enqueue(1, "1");
            toCutNodes.Add(heap.Enqueue(5, "5"));
            toCutNodes.Add(heap.Enqueue(6, "6"));
            toCutNodes.Add(heap.Enqueue(7, "7"));
            heap.Enqueue(-10, "-10");
            heap.Dequeue();
            heap.Enqueue(0, "0");
            heap2.Enqueue(-1, "-1");
            heap2.Enqueue(5, "5");
            heap2.Enqueue(-10, "-10");
            heap2.Dequeue();
            heap.Merge(heap2);
            heap.Enqueue(-10, "-10");
            heap.Dequeue();
            toCutNodes.ForEach(x => heap.ChangeKey(x, -5));
            heap.Enqueue(-10, "-10");
            heap.Dequeue();

            int count     = 7;
            int?lastValue = null;

            foreach (KeyValuePair <int, string> value in heap.GetDestructiveEnumerator())
            {
                if (lastValue is null)
                {
                    lastValue = value.Key;
                }

                if (lastValue > value.Key)
                {
                    Assert.Fail("Heap condition has been violated.");
                }

                lastValue = value.Key;
                --count;
            }
            Assert.AreEqual(0, count, "Not all elements enqueued were dequeued.");
        }
コード例 #15
0
        public void NextCutOnGreaterThan()
        {
            var heap       = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            var heap2      = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            var toCutNodes = new List <FibonacciHeapCell <int, string> >();
            int count      = 0;

            heap.Enqueue(1, "1");
            toCutNodes.Add(heap.Enqueue(5, "5"));
            toCutNodes.Add(heap.Enqueue(6, "6"));
            toCutNodes.Add(heap.Enqueue(7, "7"));
            heap.Enqueue(-10, "-10");
            heap.Dequeue();
            heap.Enqueue(0, "0");
            heap2.Enqueue(-1, "-1");
            heap2.Enqueue(5, "5");
            heap2.Enqueue(-10, "-10");
            heap2.Dequeue();
            heap.Merge(heap2);
            heap.Enqueue(-10, "-10");
            heap.Dequeue();
            toCutNodes.ForEach(x => heap.ChangeKey(x, -5));
            heap.Enqueue(-10, "-10");
            heap.Dequeue();
            count = 7;
            int?lastValue = null;

            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue > value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
コード例 #16
0
        public void SimpleEnqueDequeDecreasing()
        {
            FibonacciHeap <int, string> heap = new FibonacciHeap <int, string>(HeapDirection.Decreasing);
            int count = 0;

            for (int i = 0; i < 10; i++)
            {
                heap.Enqueue(i, i.ToString());
                count++;
            }
            int?lastValue = null;

            foreach (var value in heap.GetDestructiveEnumerator())
            {
                if (lastValue == null)
                {
                    lastValue = value.Key;
                }
                Assert.False(lastValue < value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }