Ejemplo n.º 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;
                }
                Assert.False(lastValue > value.Key);
                lastValue = value.Key;
                count--;
            }
            Assert.Equal(0, count);
        }
Ejemplo n.º 2
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");
        }
Ejemplo n.º 3
0
        public void FibonacciHeapMergeTestMinHeaps()
        {
            // create two minheaps based on the priority value of the elements.
            FibonacciHeap <HeapElement> heap1 = new FibonacciHeap <HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true);
            FibonacciHeap <HeapElement> heap2 = new FibonacciHeap <HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true);

            FillHeap(heap1, 100);
            FillHeap(heap2, 100);
            Assert.AreEqual(100, heap1.Count);
            Assert.AreEqual(100, heap2.Count);
            // merge heap1 into heap2. heap2 will be empty afterwards
            heap1.Merge(heap2);
            Assert.AreEqual(200, heap1.Count);
            Assert.AreEqual(0, heap2.Count);

            // check if they are inserted correctly
            HeapElement previous = heap1.ExtractRoot();
            HeapElement current  = heap1.ExtractRoot();

            while (current != null)
            {
                Assert.IsTrue(previous.Priority <= current.Priority);
                previous = current;
                current  = heap1.ExtractRoot();
            }
            // heap1 should be empty as well
            Assert.AreEqual(0, heap1.Count);
        }
Ejemplo n.º 4
0
        public void Merge()
        {
            var heap  = new FibonacciHeap <int, string>(HeapDirection.Increasing);
            var 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 (KeyValuePair <int, string> value in heap2.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.");
        }
Ejemplo n.º 5
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.");
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
		public void FibonacciHeapMergeTestMinHeaps()
		{
			// create two minheaps based on the priority value of the elements. 
			FibonacciHeap<HeapElement> heap1 = new FibonacciHeap<HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true);
			FibonacciHeap<HeapElement> heap2 = new FibonacciHeap<HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true);

			FillHeap(heap1, 100);
			FillHeap(heap2, 100);
			Assert.AreEqual(100, heap1.Count);
			Assert.AreEqual(100, heap2.Count);
			// merge heap1 into heap2. heap2 will be empty afterwards
			heap1.Merge(heap2);
			Assert.AreEqual(200, heap1.Count);
			Assert.AreEqual(0, heap2.Count);

			// check if they are inserted correctly
			HeapElement previous = heap1.ExtractRoot();
			HeapElement current = heap1.ExtractRoot();
			while(current != null)
			{
				Assert.IsTrue(previous.Priority <= current.Priority);
				previous = current;
				current = heap1.ExtractRoot();
			}
			// heap1 should be empty as well
			Assert.AreEqual(0, heap1.Count);
		}