Beispiel #1
0
 public void PopTest()
 {
     simpleHeap = new Heap <int>();
     heap       = new HeapWithIndices <int>();
     for (int i = 1; i <= 10; ++i)
     {
         simpleHeap.Add(i);
     }
     for (int i = 1; i <= 10; ++i)
     {
         heap.Add(i);
     }
     Assert.AreEqual(1, simpleHeap.Peek);
     Assert.AreEqual(1, heap.Peek.Value);
     simpleHeap.Add(-1);
     heap.Add(-1);
     Assert.AreEqual(-1, simpleHeap.Peek);
     Assert.AreEqual(-1, heap.Peek.Value);
     for (int i = 0; i < 5; ++i)
     {
         simpleHeap.Pop();
     }
     for (int i = 0; i < 5; ++i)
     {
         heap.Pop();
     }
     Assert.AreEqual(5, simpleHeap.Peek);
     Assert.AreEqual(5, heap.Peek.Value);
 }
Beispiel #2
0
        public void HeapWithDictionaryStressTest(int k)
        {
            HeapWithIndices <long, long> heap = new HeapWithIndices <long, long>(10);
            SortedSet <long>             keys = new SortedSet <long>();
            Random rand = new Random(55);

            for (int i = 0; i < k; ++i)
            {
                if (rand.Next() % 3 == 1)
                {
                    if (rand.Next() % 2 == 0)
                    {
                        if (heap.Count > 0)
                        {
                            keys.Remove(heap.Pop().Key);
                        }
                    }
                    else
                    {
                        if (keys.Count == 0)
                        {
                            continue;
                        }
                        heap.Remove(keys.First());
                        keys.Remove(keys.First());
                    }
                }
                else
                {
                    keys.Add(i);
                    heap.Add(rand.Next(), i);
                }
            }
        }
Beispiel #3
0
        public void ModifyTest()
        {
            Heap <int> simpleHeap = new Heap <int>();
            HeapWithIndices <int, String> heap = new HeapWithIndices <int, string>();

            for (int i = 0; i < 10; ++i)
            {
                simpleHeap.Add(i);
            }
            for (int i = 0; i <= 10; ++i)
            {
                heap.Add(i, i.ToString());
            }
            simpleHeap.ModifyTop(12);
            Assert.AreEqual(1, simpleHeap.Peek);
            simpleHeap.ModifyTop(-11);
            Assert.AreEqual(-11, simpleHeap.Peek);
            for (int i = 0; i < 9; ++i)
            {
                simpleHeap.Pop();
            }
            Assert.AreEqual(12, simpleHeap.Peek);
            heap.Modify("10", -1111);
            Assert.AreEqual(-1111, heap.Peek.Value);
            Assert.AreEqual("10", heap.Peek.Key);
            heap.Pop();
            Assert.AreEqual("0", heap.Peek.Key);
            Assert.AreEqual(0, heap.Peek.Value);
            for (int i = 0; i < 9; ++i)
            {
                heap.Pop();
            }
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(9, heap.Peek.Value);
            heap.ModifyTop(1111);
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(1111, heap.Peek.Value);
        }
Beispiel #4
0
        public void HeapWithDictionaryStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, long> priorityQueue = new SortedDictionary <long, long>();
            Dictionary <long, long>       dictionary    = new Dictionary <long, long>();
            HeapWithIndices <long, long>  myHeap        = new HeapWithIndices <long, long>(10);
            long   lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                if (rand.Next() % 3 == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        long key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                else
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    myHeap.Add(x, (long)i);
                    priorityQueue.Add(x, (long)i);
                    dictionary.Add((long)i, x);
                    lastKey = i;
                }
                if (rand.Next() % 3 == 0)
                {
                    if (rand.Next() % 2 == 0)
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                priorityQueue.Remove(dictionary[lastKey]);
                                myHeap.Remove((long)lastKey);
                                dictionary.Remove(lastKey);
                            }
                        }
                    }
                    else
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (rand.Next() % 2 == 0)
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = myHeap.PeekKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.ModifyTop(newTop);
                            }
                            else if (myHeap.ContainsKey((long)lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }
Beispiel #5
0
        public void HeapWithIndicesStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, int> priorityQueue = new SortedDictionary <long, int>();
            Dictionary <int, long>       dictionary    = new Dictionary <int, long>();
            HeapWithIndices <long>       myHeap        = new HeapWithIndices <long>(10);
            int    lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                int type = rand.Next() % 3;
                if (type == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        int  key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                if (rand.NextDouble() > 0.1)
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    int y = myHeap.Add(x);
                    priorityQueue.Add(x, y);
                    dictionary.Add(y, x);
                    lastKey = y;
                }
                if (type == 1)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (dictionary.ContainsKey(lastKey))
                        {
                            priorityQueue.Remove(dictionary[lastKey]);
                            myHeap.Remove(lastKey);
                            dictionary.Remove(lastKey);
                        }
                    }
                }
                if (type == 2)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (rand.NextDouble() < 0.5)
                        {
                            long newTop = rand.Next();
                            while (priorityQueue.ContainsKey(newTop))
                            {
                                newTop = rand.Next();
                            }

                            int key = myHeap.PeekKey;
                            priorityQueue.Remove(dictionary[key]);
                            dictionary.Remove(key);
                            priorityQueue.Add(newTop, key);
                            dictionary.Add(key, newTop);
                            myHeap.ModifyTop(newTop);
                        }
                        else
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }
                                int key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public void HeapWithIndicesTest(int numberOfIterations, int heapSize)
        {
            List <int>                 actions  = new List <int>();
            List <int>                 actions1 = new List <int>();
            Random                     rand     = new Random(55);
            HeapWithIndices <long>     heap     = new HeapWithIndices <long>(new LongComparer());
            ListHeapWithIndices <long> list     = new ListHeapWithIndices <long>(new LongComparer());

            System.Collections.Generic.HashSet <int> listKeys = new System.Collections.Generic.HashSet <int>();
            System.Collections.Generic.HashSet <int> heapKeys = new System.Collections.Generic.HashSet <int>();
            for (int i = 0; i < heapSize; ++i)
            {
                int x = rand.Next() % 100;
                list.Add(x);
                heap.Add(x);
                actions.Add(x);
                actions1.Add(x);
            }
            for (int i = 0; i < numberOfIterations; ++i)
            {
                if (i < heapSize)
                {
                    actions[i] = actions[i] + rand.Next() % 10000000;
                }
                else
                {
                    actions.Add(actions[i - heapSize] + rand.Next() % 10000000);
                }
                if (i < heapSize)
                {
                    actions1[i] = actions1[i] + rand.Next() % 10000000;
                }
                else
                {
                    actions1.Add(actions1[i - heapSize] + rand.Next() % 10000000);
                }
            }
            long      heapSum = 0;
            long      listSum = 0;
            Stopwatch timer   = new Stopwatch();

            timer.Start();
            for (int i = 0; i < actions.Count; ++i)
            {
                ListHeapWithIndices <long> .HeapEntry heapEntry = list.Pop();
                listSum += heapEntry.Value;
                listKeys.Remove(heapEntry.Key);
                listKeys.Add(list.Add(actions[i]));
                if (i % 10 == 0)
                {
                    int key = listKeys.First();
                    listKeys.Remove(key);
                    list.Remove(key);
                    listKeys.Add(list.Add(actions1[i]));
                }
            }

            timer.Stop();

            System.Console.WriteLine(timer.ElapsedMilliseconds);

            timer.Reset();
            timer.Start();
            for (int i = 0; i < actions.Count; ++i)
            {
                HeapWithIndices <long> .HeapEntry heapEntry = heap.Pop();
                heapSum += heapEntry.Value;
                heapKeys.Remove(heapEntry.Key);
                heapKeys.Add(heap.Add(actions[i]));
                if (i % 10 == 0)
                {
                    int key = heapKeys.First();
                    heapKeys.Remove(key);
                    heap.Remove(key);
                    heapKeys.Add(heap.Add(actions1[i]));
                }
            }

            timer.Stop();
            System.Console.WriteLine(timer.ElapsedMilliseconds);

            Assert.AreEqual(heapSum, listSum);
        }