/// <summary> /// Adds a value with a specified priority to the priority queue. /// </summary> /// <remarks> /// Complexity: O(log n) /// </remarks> /// <param name="value">Value to be added.</param> /// <param name="priority">Priority of the specified value.</param> public void Enqueue(T value, int priority) { //-- Create our inversely sorted tuples PriorityQueueTuple newMinEntry = new PriorityQueueTuple(SortMode.ASCENDING, value, priority); PriorityQueueTuple newMaxEntry = new PriorityQueueTuple(SortMode.DESCENDING, value, priority); //-- Add to the min heap BinomialHeap <PriorityQueueTuple> newMinHeap = new BinomialHeap <PriorityQueueTuple>(newMinEntry); //-- Add to the max heap BinomialHeap <PriorityQueueTuple> newMaxHeap = new BinomialHeap <PriorityQueueTuple>(newMaxEntry); //-- Link the new nodes together so we can access them from each other newMinHeap.TreeList.First.Value.Root.m_Counterpart = newMaxHeap.TreeList.First.Value.Root; newMaxHeap.TreeList.First.Value.Root.m_Counterpart = newMinHeap.TreeList.First.Value.Root; //-- Integrate the new size-1 heaps into the existing heaps if (null != m_MinHeap) { m_MinHeap.Union(newMinHeap); } else { m_MinHeap = newMinHeap; } if (null != m_MaxHeap) { m_MaxHeap.Union(newMaxHeap); } else { m_MaxHeap = newMaxHeap; } }
public void TC_BinomialHeap() { var heap = new BinomialHeap <double>(); HeapTest(heap, GetDoubleTestList()); Assert.AreEqual("Binomial Heap", heap.GetName()); }
/// <summary> /// Removes the root node from the specified internal tree and shuffles /// the resulting sub-trees back into this heap. /// </summary> /// <remarks> /// Complexity: O(log n) /// </remarks> /// <param name="tree">Iterater to the tree who's root we want to remove.</param> private void RemoveRootNode(LinkedListNode <BinomialTree <T> > tree) { //-- Remove the tree from this heap m_Trees.Remove(tree); //-- Rejig the heap if (0 < tree.Value.Root.Children.Count) { //-- Add that tree's sub-trees into a spearate heap BinomialHeap <T> temp = new BinomialHeap <T>(); CircularLinkedListNode <BinomialTreeNode <T> > childIter = tree.Value.Root.Children.First; do { //-- Orphan the child childIter.Value.m_Parent = null; //-- Add the children of the top node as separate trees in the temp // heap from smallest to largest order. temp.m_Trees.AddLast(new BinomialTree <T>(childIter.Value)); childIter = childIter.Next; }while(tree.Value.Root.Children.First != childIter); //-- Merge the resulting sub-trees back into this heap Union(temp); } }
//<summary> //Aplica algoritmo de Prim al grafo para hallar el MST. //El Indentificador de los nodes tienen que ser de tipo int //</summary> //<param name="graph">Grafo</param> //<returns>El MSP</returns> public static IEnumerable Run(IGraph graph) { int[] distancia = new int[graph.Vertex_Count]; IVertex[] phi = new IVertex[graph.Vertex_Count]; foreach (IVertex vertex in graph.Vertexs) { distancia[vertex.Identifier] = int.MaxValue; phi[vertex.Identifier] = null; } distancia[graph.Vertexs.First().Identifier] = 0; var pQueue = BinomialHeap <IVertex> .Build_Heap(graph.Vertexs, x => distancia[x.Identifier]); while (pQueue.Count > 0) { var current = pQueue.DeQueue(); foreach (IEdge edge in graph.Edg(current)) { if (pQueue.Contains(edge.V_2) && edge.Cost < distancia[edge.V_2.Identifier]) { distancia[edge.V_2.Identifier] = (int)edge.Cost; phi[edge.V_2.Identifier] = edge.V_1; } } } var q = graph.Vertexs.ToArray(); for (int i = 0; i < q.Length; i++) { yield return(new { V_1 = q[i], V_2 = phi[i], Cost = distancia[i] }); } }
public static void TestBinomialHeapCreation() { var heap = new BinomialHeap(); heap.Add(2); Assert.AreEqual(heap.Roots.Count, 1); heap.Add(3); Assert.AreEqual(heap.Roots.Count, 1); heap.Add(1); Assert.AreEqual(heap.Roots.Count, 2); heap.Add(4); Assert.AreEqual(heap.Roots.Count, 1); heap.Add(2); Assert.AreEqual(heap.Roots.Count, 2); heap.Add(5); Assert.AreEqual(heap.Roots.Count, 2); heap.Add(6); Assert.AreEqual(heap.Roots.Count, 3); heap.Add(7); Assert.AreEqual(heap.Roots.Count, 1); Assert.IsNotNull(heap.Roots[3]); var max = heap.GetMaximum(); Assert.AreEqual(max, 7); }
//$goals 22 //$benchmark public void insertTest(BinomialHeap binomialHeap, int value) { if (binomialHeap != null && binomialHeap.repOK()) { binomialHeap.insert(value); } }
/// <summary> /// Aplica algoritmo de Prim al grafo para hallar el MSP. /// El Indentificador de los nodes tienen que ser de tipo int /// </summary> /// <param name="graph">Grafo</param> /// <returns>El MSP</returns> public static IEnumerable<IEdge> Run(IGraph graph) { int[] distancia = new int[graph.VertexList.Count]; IVertex[] phi = new IVertex[graph.VertexList.Count]; foreach (IVertex vertex in graph.VertexList) { distancia[(int) vertex.Identifier] = int.MaxValue; phi[(int) vertex.Identifier] = null; } distancia[(int)graph.FirstVertex.Identifier] = 0; var p_Queue = BinomialHeap<int>.Build_Heap(graph.VertexList, x => distancia[(int) x.Identifier]); while (p_Queue.Count > 0) { var current = p_Queue.DeQueue(); foreach (IEdge edge in current.Out) if (p_Queue.Contains(edge.Target) && edge.Cost < distancia[(int) edge.Target.Identifier]) { distancia[(int) edge.Target.Identifier] = (int) edge.Cost; phi[(int) edge.Target.Identifier] = edge.Source; } } for (int i = 0; i < graph.VertexList.Count; i++) { yield return new EdgeResult(graph.VertexList[i], phi[i], distancia[i], false); } }
public void InsertTest3() { var heap1 = Enumerable.Range(0, 3).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); var heap = BinomialHeap <int> .Insert(3, heap1); Assert.AreEqual("[0, [2, [3]], [1]]", DumpHeap(heap)); }
public void BinomialTest2() { var heap = BinomialHeap <int> .Empty; for (var i = 0; i < 0x100; i++) { heap = BinomialHeap <int> .Insert(1, heap); var dumpHeap = DumpHeap(heap); // Console.WriteLine(dumpHeap); var blocks = dumpHeap.Split(';'); var j = 0; var p = 0; for (var k = i + 1; k > 0; k >>= 1, j++) { if (k % 2 == 0) { continue; } var q = (int)Math.Pow(2, j); var block = blocks[p++]; Assert.AreEqual(q, Counters.CountChar(block, '1') - 1); } } }
public void FindMinTest() { var heap = Enumerable.Range(0, 8).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); var min = BinomialHeap <int> .FindMin(heap); Assert.AreEqual(0, min); }
public void DeleteLotsOfMinsTest() { const int size = 1000; var random = new Random(3456); var heap = BinomialHeap <int> .Empty; for (var i = 0; i < size; i++) { heap = BinomialHeap <int> .Insert(random.Next(size), heap); } var last = 0; var count = 0; while (!BinomialHeap <int> .IsEmpty(heap)) { var next = BinomialHeap <int> .FindMin(heap); heap = BinomialHeap <int> .DeleteMin(heap); Assert.IsTrue(last <= next); last = next; count++; } Assert.AreEqual(size, count); }
//$goals 10 //$benchmark public void extractMinTest(BinomialHeap binomialHeap) { if (binomialHeap != null && binomialHeap.repOK()) { BinomialHeapNode ret_val = binomialHeap.extractMin(); } }
//$goals 3 //$benchmark public void findMinTest(BinomialHeap binomialHeap) { if (binomialHeap != null && binomialHeap.repOK()) { int ret_val = binomialHeap.findMinimum(); } }
public void InsertTest1() { var empty = BinomialHeap <int> .Empty; var heap = BinomialHeap <int> .Insert(0, empty); Assert.AreEqual("[0]", DumpHeap(heap)); }
//$goals 3 //$benchmark public void decreaseKeyNodeTest(BinomialHeap binomialHeap, BinomialHeapNode node, int new_value) { if (binomialHeap != null && binomialHeap.repOK()) { binomialHeap.decreaseKeyNode(node, new_value); } }
public void MergeTest2() { var empty = BinomialHeap <int> .Empty; var heap2 = Enumerable.Range(0, 8).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); var heap = BinomialHeap <int> .Merge(empty, heap2); Assert.AreSame(heap2, heap); }
public void MergeTest5() { var heap1 = Enumerable.Range(0, 4).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); var heap2 = Enumerable.Range(10, 4).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); var heap = BinomialHeap <int> .Merge(heap1, heap2); Assert.AreEqual("[0, [10, [12, [13]], [11]], [2, [3]], [1]]", DumpHeap(heap)); }
public void DeleteMinTest() { var heap = Enumerable.Range(0, 8).Aggregate(BinomialHeap <int> .Empty, (current, i) => BinomialHeap <int> .Insert(i, current)); heap = BinomialHeap <int> .DeleteMin(heap); Assert.AreEqual("[1]; [2, [3]]; [4, [6, [7]], [5]]", DumpHeap(heap)); }
public static void Print <TNode, TKey, TValue>([NotNull] this BinomialHeap <TNode, TKey, TValue> thisValue) where TNode : BinomialNode <TNode, TKey, TValue> { Console.WriteLine(); Console.WriteLine("Count: " + thisValue.Count); Console.WriteLine(); thisValue.WriteTo(Console.Out); }
public void EmptyTest() { var empty = BinomialHeap <int> .Empty; Assert.IsTrue(BinomialHeap <int> .IsEmpty(empty)); var heap = BinomialHeap <int> .Insert(0, empty); Assert.IsFalse(BinomialHeap <int> .IsEmpty(heap)); }
public void Min_BinomialHeap_Test() { int nodeCount = 1000 * 10; BinomialHeap <int> minHeap = new BinomialHeap <int>(); for (int i = 0; i <= nodeCount; i++) { minHeap.Insert(i); } for (int i = 0; i <= nodeCount; i++) { minHeap.UpdateKey(i, i - 1); } int min = 0; for (int i = 0; i <= nodeCount; i++) { min = minHeap.Extract(); Assert.AreEqual(min, i - 1); } //IEnumerable tests. Assert.AreEqual(minHeap.Count, minHeap.Count()); var rnd = new Random(); var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList(); foreach (var item in testSeries) { minHeap.Insert(item); } for (int i = 0; i < testSeries.Count; i++) { var decremented = testSeries[i] - rnd.Next(0, 1000); minHeap.UpdateKey(testSeries[i], decremented); testSeries[i] = decremented; } testSeries.Sort(); for (int i = 0; i < nodeCount - 2; i++) { min = minHeap.Extract(); Assert.AreEqual(testSeries[i], min); } //IEnumerable tests. Assert.AreEqual(minHeap.Count, minHeap.Count()); }
public void Max_BinomialHeap_Test() { int nodeCount = 1000 * 10; var tree = new BinomialHeap <int>(SortDirection.Descending); for (int i = 0; i <= nodeCount; i++) { tree.Insert(i); } for (int i = 0; i <= nodeCount; i++) { tree.UpdateKey(i, i + 1); } int max = 0; for (int i = nodeCount; i >= 0; i--) { max = tree.Extract(); Assert.AreEqual(max, i + 1); } //IEnumerable tests. Assert.AreEqual(tree.Count, tree.Count()); var rnd = new Random(); var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList(); foreach (var item in testSeries) { tree.Insert(item); } for (int i = 0; i < testSeries.Count; i++) { var incremented = testSeries[i] + rnd.Next(0, 1000); tree.UpdateKey(testSeries[i], incremented); testSeries[i] = incremented; } testSeries = testSeries.OrderByDescending(x => x).ToList(); for (int i = 0; i < nodeCount - 2; i++) { max = tree.Extract(); Assert.AreEqual(testSeries[i], max); } //IEnumerable tests. Assert.AreEqual(tree.Count, tree.Count()); }
public void AddTest() { var heap = new BinomialHeap(); heap.Add(7); heap.Add(5); heap.Add(4); heap.Add(8); const int expectedCount = 4; Assert.IsTrue(heap.Count == expectedCount, "Incorrect number of items added"); }
public void ExtractMinTest() { var heap = new BinomialHeap(); heap.Add(7); heap.Add(5); heap.Add(4); heap.Add(8); heap.TryExtractMin(out var min); const int expectedMin = 4; Assert.IsTrue(min == expectedMin, "Extracted the wrong item"); }
public void RemoveTest() { var heap = new BinomialHeap(); heap.Add(7); heap.Add(5); heap.Add(4); heap.Add(8); heap.Remove(4); const int expectedCount = 3; Assert.IsTrue(heap.Count == expectedCount, "Incorrect number of items after deletion"); }
private static HNode CreateHeap(IEnumerable <HNode> input) { var heap = BinomialHeap <HNode> .Build_Heap(input, x => x.Count); // O(n) amortized cost while (heap.Count > 1) { var x = heap.DeQueue(); var y = heap.DeQueue(); heap.EnQueue(new HNode { Left = x, Rigth = y, Key = char.Parse(" "), Count = x.Count + y.Count }, x.Count + y.Count); } return(heap.DeQueue()); }
public void BinomialTest1() { var heap = BinomialHeap <int> .Empty; for (var i = 0; i < 16; i++) { heap = BinomialHeap <int> .Insert(i, heap); var dumpHeap = DumpHeap(heap); // Console.WriteLine(dumpHeap); var semicolons = Counters.CountChar(dumpHeap, ';'); Assert.AreEqual(Counters.CountBinaryOnes(i + 1), semicolons); } }
public void DecreaseKeyTest() { const int newMinKey = 2; var heap = new BinomialHeap(); heap.Add(7); heap.Add(5); heap.Add(4); heap.Add(8); heap.DecreaseKey(4, newMinKey); heap.TryExtractMin(out var min); Assert.IsTrue(min == newMinKey, "The specified key has not changed"); }
public static ISearchHeap getHeapByParam(int param) { ISearchHeap heapStructure = null; switch (param) { case 1: heapStructure = new RedBlackTreeHeap(); break; case 2: heapStructure = new FibonacciHeap(); break; case 3: heapStructure = new FibonacciHeap2(); break; case 4: heapStructure = new RegularBinaryHeap(); break; case 5: heapStructure = new RegularTernaryHeap(); break; case 6: heapStructure = new BinomialHeap(); break; case 7: heapStructure = new LeftistHeap(); break; default: break; } return(heapStructure); //heapStructure = new Heaps.RedBlackTreeHeap(); //ISearchHeap heapStructure = new Heaps.FibonacciHeap1(); //ISearchHeap heapStructure = new Heaps.FibonacciHeap2(); //ISearchHeap heapStructure = new Heaps.RegularBinaryHeap(); //ISearchHeap heapStructure = new Heaps.RegularTernaryHeap(); //ISearchHeap heapStructure = new Heaps.BinomialHeap(); //ISearchHeap heapStructure = new Heaps.LeftistHeap(); }
private static string DumpTree <T>(BinomialHeap <T> .Tree tree) where T : IComparable <T> { var result = new StringBuilder(); result.Append("["); result.Append(tree.Root); if (!FunProgLib.lists.List <BinomialHeap <T> .Tree> .IsEmpty(tree.List)) { foreach (var node1 in tree.List) { result.Append(", "); result.Append(DumpTree(node1)); } } result.Append("]"); return(result.ToString()); }
public Crawler() { ipCache = new Dictionary<string, IPHostEntry>(); frontier = new Queue<Uri>(); backQueues = new Queue<Uri>[256]; domainBackQueues = new Dictionary<string, int>(); backQueueHeap = new BinomialHeap<DateTime, string>(); //backQueueHeap = new SortedDictionary<DateTime, string>(); crawledURIs = new HashSet<Uri>(); foreach (Uri seed in Seeds) { EnqueueURI(seed); } }
//$goals 3 //$benchmark public void findMinTest(BinomialHeap binomialHeap) { if (binomialHeap!=null && binomialHeap.repOK()) { int ret_val = binomialHeap.findMinimum(); } }
//$goals 3 //$benchmark public void decreaseKeyNodeTest(BinomialHeap binomialHeap, BinomialHeapNode node, int new_value) { if (binomialHeap!=null && binomialHeap.repOK()) { binomialHeap.decreaseKeyNode(node, new_value); } }
//$goals 10 //$benchmark public void extractMinTest(BinomialHeap binomialHeap) { if (binomialHeap!=null && binomialHeap.repOK()) { BinomialHeapNode ret_val = binomialHeap.extractMin(); } }
//$goals 22 //$benchmark public void insertTest(BinomialHeap binomialHeap, int value) { if (binomialHeap!=null && binomialHeap.repOK()) { binomialHeap.insert(value); } }