Ejemplo n.º 1
0
        public void consolidateTree()
        {
            FibonacciHeap heap = new FibonacciHeap();
            FibonacciNode n1 = heap.insert(1);
            FibonacciNode n2 = heap.insert(2);
            n1.MinPathValue = 23;
            n2.MinPathValue = 7;
            heap.consolidateTree(heap.root);

            Assert.AreEqual(2, heap.minNode.StationID);
            Assert.AreEqual(n2, n1.Parent);
            Assert.AreEqual(1, n2.Degree);
            Assert.AreEqual(0, n1.Degree);
            Assert.AreEqual(n1, n2.Child);

            FibonacciNode n3 = heap.insert(3);
            n3.MinPathValue = 2;
            heap.consolidateTree(heap.root);

            Assert.AreEqual(3, heap.minNode.StationID);
            Assert.AreEqual(0, n3.Degree);
            Assert.AreEqual(null, n3.Parent);
            Assert.AreEqual(null, n3.Child);
            Assert.AreEqual(n2, n1.Parent);
            Assert.AreEqual(1, n2.Degree);
            Assert.AreEqual(0, n1.Degree);
            Assert.AreEqual(n1, n2.Child);
        }
Ejemplo n.º 2
0
 public void calculateArraySize()
 {
     FibonacciHeap heap = new FibonacciHeap();
     Assert.AreEqual(6, heap.calculateArraySize(15));
     Assert.AreEqual(5, heap.calculateArraySize(7));
     Assert.AreEqual(5, heap.calculateArraySize(8));
 }
Ejemplo n.º 3
0
        public void Cut()
        {
            FibonacciHeap heap = new FibonacciHeap();
            DoubleLinkedList list = new DoubleLinkedList();
            FibonacciNode y = new FibonacciNode() { StationID = 1, MinPathValue = 10 };
            FibonacciNode x = new FibonacciNode() { StationID = 2, MinPathValue = 20 };
            x.Parent = y;
            y.Child = x;
            list.Add(y);

            heap.Cut(list, x, y);

            Assert.AreEqual(null, x.Parent);
            Assert.AreEqual(false, x.Mark);
            Assert.AreEqual(1, list.head.StationID);
            Assert.AreEqual(2, list.head.RightNode.StationID);
        }
Ejemplo n.º 4
0
        public void CascadingCut()
        {
            FibonacciHeap heap = new FibonacciHeap();
            DoubleLinkedList list = new DoubleLinkedList();
            FibonacciNode y = new FibonacciNode() { StationID = 1, MinPathValue = 10 };
            FibonacciNode x = new FibonacciNode() { StationID = 2, MinPathValue = 20, Mark = true };
            FibonacciNode z = new FibonacciNode() { StationID = 3, MinPathValue = 21, Mark = true };
            x.Parent = y;
            y.Child = x;
            z.Parent = x;
            x.Child = z;
            list.Add(y);

            heap.CascadingCut(list, z);

            Assert.AreEqual(null, x.Child);
            Assert.AreEqual(null, y.Child);
            Assert.AreEqual(null, x.Parent);
            Assert.AreEqual(null, z.Parent);
            Assert.AreEqual(1, list.head.StationID);
            Assert.AreEqual(3, list.head.RightNode.StationID);
            Assert.AreEqual(2, list.head.RightNode.RightNode.StationID);
        }
Ejemplo n.º 5
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.º 6
0
        public void Link()
        {
            FibonacciHeap heap1 = new FibonacciHeap();
            DoubleLinkedList root1 = new DoubleLinkedList();
            FibonacciNode y = new FibonacciNode() { StationID = 1, MinPathValue = 10 };
            FibonacciNode x = new FibonacciNode() { StationID = 2, MinPathValue = 5 };
            root1.Add(y);
            root1.Add(x);

            heap1.Link(root1, y, x);

            Assert.AreEqual(2, root1.head.StationID);
            Assert.AreEqual(1, x.Child.StationID);
            Assert.AreEqual(1, x.Degree);
            Assert.AreEqual(2, root1.head.RightNode.StationID);

            FibonacciHeap heap2 = new FibonacciHeap();
            DoubleLinkedList root2 = new DoubleLinkedList();
            FibonacciNode z = new FibonacciNode() { StationID = 3, MinPathValue = 10 };
            x.Child = z;
            z.Parent = x;
            x.Degree = 1;
            root1.Add(y);
            root1.Add(x);

            heap2.Link(root2, y, x);

            Assert.AreEqual(2, root1.head.StationID);
            Assert.AreEqual(3, x.Child.StationID);
            Assert.AreEqual(2, x.Degree);
            Assert.AreEqual(2, root1.head.RightNode.StationID);
            Assert.AreEqual(3, x.Child.StationID);
            Assert.AreEqual(1, x.Child.RightNode.StationID);
        }
Ejemplo n.º 7
0
        public void insertNode()
        {
            FibonacciHeap heap = new FibonacciHeap();
            heap.insert(1);

            Assert.AreEqual(1, heap.numberOfNodes);
            Assert.AreEqual(1, heap.root.head.StationID);
            Assert.AreEqual(1, heap.minNode.StationID);

            heap.insert(2);
            Assert.AreEqual(2, heap.numberOfNodes);
            Assert.AreEqual(1, heap.root.head.StationID);
            Assert.AreEqual(1, heap.minNode.StationID);

            Assert.AreEqual(2, heap.root.head.RightNode.StationID);
            Assert.AreEqual(1, heap.minNode.StationID);
        }
Ejemplo n.º 8
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.º 9
0
        public void extractMinNode()
        {
            FibonacciHeap heap = new FibonacciHeap();
            FibonacciNode n1 = heap.insert(1);
            n1.MinPathValue = 5;
            FibonacciNode n2 = heap.insert(2);
            n2.MinPathValue = 10;
            FibonacciNode n3 = heap.insert(3);
            n3.MinPathValue = 15;

            Assert.AreEqual(3, heap.numberOfNodes);

            FibonacciNode min = heap.extractMinNode();
            Assert.AreEqual(1, min.StationID);
            Assert.AreEqual(5, Convert.ToInt32(min.MinPathValue));
            Assert.AreEqual(2, heap.numberOfNodes);
            Assert.AreEqual(2, heap.minNode.StationID);

            FibonacciNode min2 = heap.extractMinNode();
            Assert.AreEqual(2, min2.StationID);
            Assert.AreEqual(10, Convert.ToInt32(min2.MinPathValue));
            Assert.AreEqual(1, heap.numberOfNodes);
            Assert.AreEqual(3, heap.minNode.StationID);

            FibonacciNode min3 = heap.extractMinNode();
            Assert.AreEqual(3, min3.StationID);
            Assert.AreEqual(15, Convert.ToInt32(min3.MinPathValue));
            Assert.AreEqual(0, heap.numberOfNodes);
            Assert.AreEqual(null, heap.minNode);
        }
Ejemplo n.º 10
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.º 11
0
        //return a path with min weight
        public static List<PathStop> shortestPathWithFibonacci(Dictionary<int, Dictionary<int, decimal>> adjListWithWeight, int startSID, int endSID)
        {
            FibonacciHeap Q = new FibonacciHeap();
            Dictionary<int, FibonacciNode> QCopy = new Dictionary<int, FibonacciNode>();
            List<int> R = new List<int>();
            List<FibonacciNode> S = new List<FibonacciNode>();
            //initialize start node
            FibonacciNode start = Q.insert(startSID);
            start.MinPathValue = 0;
            QCopy.Add(startSID, start);

            while (Q.numberOfNodes != 0)
            {
                FibonacciNode u = Q.extractMinNode();
                R.Add(u.StationID);
                S.Add(u);
                if (R.Contains(endSID))
                {
                    break;  //if extracted list contains the end station id, stop algorithm
                }
                foreach (int vId in adjListWithWeight[u.StationID].Keys)
                {
                    FibonacciNode v = null;
                    if (!R.Contains(vId)) //if the node is not in extracted list
                    {
                        if (!Q.stationIDs.Contains(vId))//if the node is not reached yet
                        {
                            v = Q.insert(vId);
                            QCopy.Add(vId, v);
                        }
                        else
                        {
                            v = QCopy[vId]; //The node is reached

                        }
                        decimal w = adjListWithWeight[u.StationID][vId];
                        relax(u, v, w, Q, QCopy);
                    }

                }
            }

            List<PathStop> route = new List<PathStop>();
            if (R.Contains(endSID))
            {
                route = buildRoute(S, endSID);
            }
            return route;
        }
Ejemplo n.º 12
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;
     }
 }
Ejemplo n.º 13
0
        public void relax()
        {
            FibonacciHeap Q = new FibonacciHeap();
            FibonacciNode u = Q.insert(1);
            u.MinPathValue = 5;
            FibonacciNode v = Q.insert(2);
            v.MinPathValue = 6;
            Dictionary<int, FibonacciNode> QCopy = new Dictionary<int, FibonacciNode>();
            QCopy.Add(1, u);
            QCopy.Add(2, v);
            decimal w = 3;

            FindPath.relax(u, v, w, Q, QCopy);
            Assert.AreEqual(6, Convert.ToInt32(v.MinPathValue));
            Assert.AreEqual(null, v.lastStop);
            Assert.AreEqual(6, Convert.ToInt32(QCopy[2].MinPathValue));

            v.MinPathValue = 10;
            FindPath.relax(u, v, w, Q, QCopy);

            Assert.AreEqual(8, Convert.ToInt32(v.MinPathValue));
            Assert.AreEqual(u, v.lastStop);
            Assert.AreEqual(8, Convert.ToInt32(QCopy[2].MinPathValue));
        }