Ejemplo n.º 1
0
        public void runUnpassedData()
        {
            List<int> dataList = readDataFromFile();
            decimal preValue = 0;
            decimal currentExtractValue = 0;
            FibonacciHeap heap = new FibonacciHeap();
            for (int i = 0; i < dataList.Count; i++)
            {
                FibonacciNode node = heap.insert(i+1);
                heap.DecreasingKey(heap.root, node, dataList[i]);
                Assert.AreEqual(i + 1, heap.numberOfNodes);
            }

            dataList.Sort();

            List<FibonacciNode> listNode = new List<FibonacciNode>();
            int size = dataList.Count;
            for (int i = 0; i < dataList.Count; i++)
            {
                FibonacciNode node = heap.extractMinNode();
                listNode.Add(node);
                Assert.AreEqual(size - (i + 1), heap.numberOfNodes);
                currentExtractValue = node.MinPathValue;
                if (currentExtractValue < preValue)
                {
                    Assert.Fail();
                }
                preValue = currentExtractValue;

            }

            for (int i = 0; i < dataList.Count; i++)
            {
                Assert.AreEqual(dataList[i], Convert.ToInt32(listNode[i].MinPathValue));
            }
        }
Ejemplo n.º 2
0
        public void DecreasingKey()
        {
            FibonacciHeap heap = new FibonacciHeap();
            FibonacciNode x = heap.insert(1);
            x.MinPathValue = 10;
            heap.DecreasingKey(heap.root, x, 5);

            Assert.AreEqual(5, Convert.ToInt32(x.MinPathValue));
            Assert.AreEqual(1, heap.root.Size);

            FibonacciNode y = heap.insert(2);
            y.MinPathValue = 26;
            y.Parent = x;
            x.Child = y;
            heap.DecreasingKey(heap.root, y, 20);
            Assert.AreEqual(20, Convert.ToInt32(y.MinPathValue));
            Assert.AreEqual(2, heap.root.Size);
        }
Ejemplo n.º 3
0
        public void extract_minWithRandomNumbers()
        {
            List<int> dataList = new List<int>();
            try
            {
                FibonacciHeap heap = new FibonacciHeap();
                Random r = new Random();
                int ran = 0;
                decimal preValue = 0;
                decimal currentExtractValue = 0;
                int size = 300; //can be adjusted
                for (int i = 0; i < size; i++)
                {
                    FibonacciNode node = heap.insert(i + 1);
                    ran = r.Next(1, 100);
                    heap.DecreasingKey(heap.root, node, ran);
                    dataList.Add(ran);
                    Assert.AreEqual(i + 1, heap.numberOfNodes);
                }

                for (int i = 0; i < size; i++)
                {
                    FibonacciNode node = heap.extractMinNode();
                    Assert.AreEqual(size - (i + 1), heap.numberOfNodes);
                    currentExtractValue = node.MinPathValue;
                    if (currentExtractValue < preValue)
                    {
                        Assert.Fail();
                    }
                    preValue = currentExtractValue;

                }
            }
            catch (IndexOutOfRangeException e)
            {
                writeDataToFile(e.Message, dataList);
                throw new Exception();
            }
            catch (AssertFailedException e)
            {
                writeDataToFile(e.Message, dataList);
                throw new Exception();
            }
        }
Ejemplo n.º 4
0
 public static void relax(FibonacciNode u, FibonacciNode v, decimal w, FibonacciHeap Q, Dictionary<int, FibonacciNode> QCopy)
 {
     if (v.MinPathValue > u.MinPathValue + w)
     {
         Q.DecreasingKey(Q.root, v, u.MinPathValue + w);
         QCopy[v.StationID].MinPathValue = u.MinPathValue + w;
         v.lastStop = u;
     }
 }