private IEnumerable <Edge> GetMinimumSpanningTreeEdgesBad(Dictionary <char, List <Edge> > g) { var h = new BinaryMinHeap <char>(); var vte = new Dictionary <char, Edge>(); // Fill Heap var isFirst = true; foreach (char key in g.Keys) { if (isFirst) { h.Add(new BinaryMinHeap <char> .Node { Id = key, Weight = 0 }); isFirst = false; } else { h.Add(new BinaryMinHeap <char> .Node { Id = key, Weight = int.MaxValue }); } } var result = new List <Edge>(); while (h.HasItem()) { var v = h.ExtractMinimum(); vte.TryGetValue(v.Id, out Edge ste); if (ste != null) { result.Add(ste); } foreach (Edge e in g[v.Id]) { char adj = e.V2; if (!h.Contains(adj)) { continue; } var node = h.GetNode(adj); if (node.Weight > e.Weight) { node.Weight = e.Weight; h.Decrease(node); if (!vte.ContainsKey(node.Id)) { vte.Add(node.Id, e); } vte[node.Id] = e; } } } return(result); }
public void Add_and_remove_single() { _emptyIntMinHeap.Add(55); Assert.IsTrue(_emptyIntMinHeap.Contains(55)); Assert.AreEqual(55, _emptyIntMinHeap.PeekMin()); Assert.AreEqual(1, _emptyIntMinHeap.Size); Assert.AreEqual(55, _emptyIntMinHeap.RemoveMin()); Assert.AreEqual(0, _emptyIntMinHeap.Size); Assert.IsFalse(_emptyIntMinHeap.Contains(55)); }
public void GivenKeepMin2Items_AddInReverseOrder_ShouldHold3Not4() { _intHeap = BinaryMinHeap <int> .CreateWithSizeLimit(new IntComparer(), 2); Assert.AreEqual(0, _intHeap.Add(4)); Assert.AreEqual(0, _intHeap.Add(3)); Assert.AreEqual(0, _intHeap.Add(2)); // level 2 is now full, should discard on next add Assert.AreEqual(4, _intHeap.Add(1)); }
public static IEnumerable <object[]> GetSampleBinaryHeap() { var sut = new BinaryMinHeap <char>(); var a = new BinaryMinHeap <char> .Node { Id = 'a', Weight = -1 }; var b = new BinaryMinHeap <char> .Node { Id = 'b', Weight = 2 }; var c = new BinaryMinHeap <char> .Node { Id = 'c', Weight = 6 }; var d = new BinaryMinHeap <char> .Node { Id = 'd', Weight = 4 }; var e = new BinaryMinHeap <char> .Node { Id = 'e', Weight = 5 }; var f = new BinaryMinHeap <char> .Node { Id = 'f', Weight = 7 }; var g = new BinaryMinHeap <char> .Node { Id = 'g', Weight = 8 }; sut.Add(a); sut.Add(b); sut.Add(c); sut.Add(d); sut.Add(e); sut.Add(f); sut.Add(g); yield return(new object[] { sut }); }
public static void CheckOrderInHeap_DecreasingOrder_ReturnsTrue() { BinaryMinHeap <long> minHeap = new BinaryMinHeap <long>(Comparer <long> .Default); minHeap.Add(10); minHeap.Add(9); minHeap.Add(8); minHeap.Add(7); minHeap.Add(6); minHeap.Add(5); minHeap.Add(4); minHeap.Add(3); minHeap.Add(2); minHeap.Add(1); var isRightOrder = IsRightOrderInHeap <long>(minHeap); Assert.True(isRightOrder); }
public void CheckOrderInHeap_RandomOrder_ReturnsTrue() { BinaryMinHeap <long> minHeap = new BinaryMinHeap <long>(Comparer <long> .Default); minHeap.Add(23); minHeap.Add(42); minHeap.Add(4); minHeap.Add(16); minHeap.Add(8); minHeap.Add(1); minHeap.Add(3); minHeap.Add(100); minHeap.Add(5); minHeap.Add(7); var isRightOrder = IsRightOrderInHeap <long>(minHeap); Assert.IsTrue(isRightOrder); }
public void ReplacingMinElementAndCheckingIfExtractedInSortedOrder() { var heap = new BinaryMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } heap.ReplaceMin(int.MaxValue); heap.ReplaceMin(int.MaxValue); heap.ReplaceMin(int.MaxValue); int removedElements = 0; var min = heap.PeekMin(); while (!heap.IsEmpty) { if (min > heap.PeekMin()) { Assert.Fail(); } min = heap.PopMin(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }
public void AddingElementsWithCustomComparerAndCheckingIfExtractedInSortedOrder() { //Creating heap with reversed comparer var heap = new BinaryMinHeap <int>(Comparer <int> .Create( (x, y) => y.CompareTo(x))); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } int removedElements = 0; // because of the reversed comparer var max = heap.PeekMin(); while (!heap.IsEmpty) { if (max < heap.PeekMin()) { Assert.Fail(); } max = heap.PopMin(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }
public void TestBinaryMinHeapDecrease() { var sut = new BinaryMinHeap <char>(); var a = new BinaryMinHeap <char> .Node { Id = 'a', Weight = -1 }; var b = new BinaryMinHeap <char> .Node { Id = 'b', Weight = 2 }; var c = new BinaryMinHeap <char> .Node { Id = 'c', Weight = 6 }; var d = new BinaryMinHeap <char> .Node { Id = 'd', Weight = 4 }; var e = new BinaryMinHeap <char> .Node { Id = 'e', Weight = 5 }; var f = new BinaryMinHeap <char> .Node { Id = 'f', Weight = 7 }; var g = new BinaryMinHeap <char> .Node { Id = 'g', Weight = 8 }; sut.Add(a); sut.Add(b); sut.Add(c); sut.Add(d); sut.Add(e); sut.Add(f); sut.Add(g); f.Weight = -2; sut.Decrease(f); g.Weight = -3; sut.Decrease(g); var expected = new[] { 'g', 'f', 'a', 'b', 'd', 'e', 'c' }; var actual = expected.Select(_ => sut.ExtractMinimum()); //foreach (BinaryMinHeap<char>.Node node in actual) //{ // _output.WriteLine(node.ToString()); //} Assert.True(expected.SequenceEqual(actual.Select(_ => _.Id))); }
public void AddAndRemoveElements() { var r = new Random(); const int totalElements = 10000; var elements = Enumerable.Range(1, totalElements).Select(x => r.Next(totalElements)).ToList(); var target = new BinaryMinHeap<int>(); foreach (var i in elements) { target.Add(i); } Assert.AreEqual(totalElements, target.Count); elements.Sort(); for (var i = 0; i < totalElements; i++) { Assert.AreEqual(elements[i], target.RemoveMin()); } }
public static void DoTest() { BinaryMinHeap <long> minHeap = new BinaryMinHeap <long>(Comparer <long> .Default); minHeap.Add(23); minHeap.Add(42); minHeap.Add(4); minHeap.Add(16); minHeap.Add(8); minHeap.Add(15); minHeap.Add(9); minHeap.Add(55); minHeap.Add(0); minHeap.Add(34); minHeap.Add(12); minHeap.Add(2); minHeap.Add(93); minHeap.Add(14); minHeap.Add(27); var array = minHeap.ToArray(); Assert.True(array.Length == minHeap.Count, "Wrong size."); var list = minHeap.ToList(); Assert.True(list.Count == minHeap.Count, "Wrong size."); array.HeapSortDescending(); var maxHeap = minHeap.ToMaxHeap(); Assert.True(maxHeap.Peek() == array[0], "Wrong maximum."); }
public void ConvertingToBinaryMaxHeap() { var minHeap = new BinaryMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { minHeap.Add(el); addedElements++; el += i; } } if (minHeap.Count != addedElements) { Assert.Fail(); } // Binary min heap with reversed comparer. Have to be the same as the max heap var reversedMinHeap = new BinaryMinHeap <int>(Comparer <int> .Create( (x, y) => y.CompareTo(x))); reversedMinHeap.Heapify(minHeap.ToArray()); var maxHeap = minHeap.ToMaxHeap(); if (maxHeap.Count != reversedMinHeap.Count) { Assert.Fail(); } var max1 = reversedMinHeap.PeekMin(); var max2 = maxHeap.PeekMax(); int removedElements = 0; while (!reversedMinHeap.IsEmpty && !maxHeap.IsEmpty) { if (max1 < reversedMinHeap.PeekMin()) { Assert.Fail(); } if (max2 < maxHeap.PeekMax()) { Assert.Fail(); } max1 = reversedMinHeap.PopMin(); max2 = maxHeap.PopMax(); removedElements++; if (max1 != max2) { Assert.Fail(); } } Assert.IsTrue(reversedMinHeap.IsEmpty && maxHeap.IsEmpty && reversedMinHeap.Count == 0 && maxHeap.Count == 0 && addedElements == removedElements); }
public void MergingTwoHeapsAndCheckingIfExtractedInSortedOrder() { var heap1 = new BinaryMinHeap <int>(); var heap2 = new BinaryMinHeap <int>(); int maxElementInFirstHeap = 100000; int minElementInFirstHeap = 0; int addedElementsInFirstHeap = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minElementInFirstHeap; while (el <= maxElementInFirstHeap) { heap1.Add(el); addedElementsInFirstHeap++; el += i; } } int maxElementInSecondHeap = 50000; int minElementInSecondHeap = -50000; int addedElementsInSecondHeap = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minElementInSecondHeap; while (el <= maxElementInSecondHeap) { heap2.Add(el); addedElementsInSecondHeap++; el += i; } } if (heap1.Count != addedElementsInFirstHeap) { Assert.Fail("first heap incorrect count"); } if (heap2.Count != addedElementsInSecondHeap) { Assert.Fail("second heap incorrect count"); } var oldHeap = new BinaryMinHeap <int>(); oldHeap.Heapify(heap1.ToArray()); heap1.Merge(heap2); heap2.Merge(oldHeap); int mergedHeapElements = addedElementsInFirstHeap + addedElementsInSecondHeap; if (heap1.Count != mergedHeapElements) { Assert.Fail("merged first with second incorect count"); } if (heap2.Count != mergedHeapElements) { Assert.Fail("merged second with first incorrect count"); } var min1 = heap1.PeekMin(); var min2 = heap2.PeekMin(); if (min1 != min2) { Assert.Fail("merged heaps min element is different"); } int removedElements = 0; while (!heap1.IsEmpty && !heap2.IsEmpty) { if (min1 > heap1.PeekMin()) { Assert.Fail(); } if (min2 > heap2.PeekMin()) { Assert.Fail(); } min1 = heap1.PopMin(); min2 = heap2.PopMin(); removedElements++; if (min1 != min2) { Assert.Fail("merged heaps min element is different"); } } Assert.IsTrue(heap1.IsEmpty && heap2.IsEmpty && heap1.Count == 0 && heap2.Count == 0 && mergedHeapElements == removedElements); }
public void AddingAfterClearingHeap() { var heap = new BinaryMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } heap.Clear(); if (heap.Count != 0) { Assert.Fail(); } addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el < maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } int removedElements = 0; var min = heap.PeekMin(); while (!heap.IsEmpty) { if (min > heap.PeekMin()) { Assert.Fail(); } min = heap.PopMin(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }
public static void DoTest () { BinaryMinHeap<long> minHeap = new BinaryMinHeap<long> (Comparer<long>.Default); minHeap.Add (23); minHeap.Add (42); minHeap.Add (4); minHeap.Add (16); minHeap.Add (8); minHeap.Add (15); minHeap.Add (9); minHeap.Add (55); minHeap.Add (0); minHeap.Add (34); minHeap.Add (12); minHeap.Add (2); minHeap.Add (93); minHeap.Add (14); minHeap.Add (27); var array = minHeap.ToArray (); Debug.Assert (array.Length == minHeap.Count, "Wrong size."); var list = minHeap.ToList (); Debug.Assert (list.Count == minHeap.Count, "Wrong size."); array.HeapSortDescending(); var maxHeap = minHeap.ToMaxHeap (); Debug.Assert (maxHeap.Peek() == array[0], "Wrong maximum."); }