Example #1
0
        public void consolidateTree(DoubleLinkedList root)
        {
            FibonacciNode[] A = new FibonacciNode[calculateArraySize(numberOfNodes)];
            //FibonacciNode[] A = new FibonacciNode[1000000];
            for (int i = 0; i < A.Length; i++)
            {
                A[i] = null;
            }

            FibonacciNode w = root.head; //pointer for tranverse nodes

            int size = root.Size;
            for (int i = 0; i < size; i++)
            {
                FibonacciNode x = w;

                int d = x.Degree;
                while (A[d] != null)
                {
                    FibonacciNode y = A[d];

                    if (x.MinPathValue > y.MinPathValue)
                    {
                        //exchange x and y
                        FibonacciNode c = x;
                        x = y;
                        y = c;
                        //w = w.LeftNode;//set the pointer back to the node on the left
                    }
                    if ( w==y)
                    {
                        w = w.LeftNode;
                    }
                    Link(root, y, x); //x (w) is to be deleted in root list
                    //w = x; //TODO could be bug

                    A[d] = null;
                    d += 1;
                }
                A[d] = x;
                w = w.RightNode; //pointer continue move to next node
            }

            minNode = null;
            root = new DoubleLinkedList();
            for (int j = 0; j < A.Length; j++)
            {
                if (A[j] != null)
                {
                    root.Add(A[j]);
                    if (minNode == null || A[j].MinPathValue < minNode.MinPathValue)
                    {
                        minNode = A[j];
                    }
                }
            }
        }
Example #2
0
        public void Delete(FibonacciNode node)
        {
            if (node == head)
            {
                head = node.RightNode;
            }
            FibonacciNode leftNode = node.LeftNode;
            FibonacciNode rightNode = node.RightNode;
            leftNode.RightNode = rightNode;
            rightNode.LeftNode = leftNode;

            Size--;
        }
Example #3
0
 public void CascadingCut(DoubleLinkedList list, FibonacciNode y)
 {
     FibonacciNode z = y.Parent;
     if (z != null)
     {
         if (y.Mark == false)
         {
             y.Mark = true;
         }
         else
         {
             Cut(list, y, z);
             CascadingCut(list, z);
         }
     }
 }
Example #4
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);
        }
Example #5
0
 public void Add(FibonacciNode node)
 {
     if (head == null)
     {
         head = node;
         node.LeftNode = node;
         node.RightNode = node;
     }
     else
     {
         FibonacciNode lastNode = head.LeftNode;
         lastNode.RightNode = node;
         node.RightNode = head;
         node.LeftNode = lastNode;
         head.LeftNode = node;
     }
     Size++;
 }
Example #6
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);
        }
        public void AddDoubleLinkedList()
        {
            DoubleLinkedList dll = new DoubleLinkedList();
            FibonacciNode n1= new FibonacciNode(){StationID = 1};
            FibonacciNode n2= new FibonacciNode(){StationID = 2};
            FibonacciNode n3 = new FibonacciNode() { StationID = 3 };
            dll.Add(n1);
            Assert.AreEqual(1, dll.head.StationID);
            Assert.AreEqual(1, dll.head.RightNode.StationID);
            Assert.AreEqual(1, dll.head.LeftNode.StationID);

            dll.Add(n2);
            Assert.AreEqual(1, dll.head.StationID);
            Assert.AreEqual(2, dll.head.RightNode.StationID);
            Assert.AreEqual(1, dll.head.RightNode.RightNode.StationID);

            dll.Add(n3);

            Assert.AreEqual(1, dll.head.StationID);
            Assert.AreEqual(2, dll.head.RightNode.StationID);
            Assert.AreEqual(3, dll.head.RightNode.RightNode.StationID);
            Assert.AreEqual(1, dll.head.LeftNode.RightNode.StationID);
        }
Example #8
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);
        }
Example #9
0
        //remove node y from root list, and link node y to x
        public void Link(DoubleLinkedList root, FibonacciNode y, FibonacciNode x)
        {
            root.Delete(y);
            //add y as a child of x
            if (x.Child == null)
            {
                y.Parent = x;
                x.Child = y;
                y.RightNode = y;
                y.LeftNode = y;
            }
            else
            {
                FibonacciNode lastNode = x.Child.LeftNode;
                lastNode.RightNode = y;
                y.RightNode = x.Child;
                y.LeftNode = lastNode;
                x.Child.LeftNode = y;

                y.Parent = x;
            }
            x.Degree++;
            y.Mark = false;
        }
Example #10
0
 public FibonacciNode insert(int id)
 {
     FibonacciNode node = new FibonacciNode() { StationID = id };
     if (minNode == null || node.MinPathValue < minNode.MinPathValue)
     {
         minNode = node;
     }
     root.Add(node);
     stationIDs.Add(id);
     numberOfNodes++;
     return node;
 }
Example #11
0
 public void DecreasingKey(DoubleLinkedList list, FibonacciNode x, decimal k)
 {
     if (x.MinPathValue < k)
     {
         throw new SystemException("The new node min value is greater than current node value.");
     }
     x.MinPathValue = k;
     FibonacciNode y = x.Parent;
     if (y != null && x.MinPathValue < y.MinPathValue)
     {
         Cut(list, x, y);
         CascadingCut(list, y);
     }
     if (x.MinPathValue < minNode.MinPathValue)
     {
         minNode = x;
     }
 }
Example #12
0
        public void Cut(DoubleLinkedList list, FibonacciNode x, FibonacciNode y)
        {
            //remove x from y child least
            if (x == x.LeftNode)
            {
                y.Child = null;
            }
            else
            {

                if (x == y.Child)
                {
                    y.Child = x.RightNode;
                }

                FibonacciNode leftNode = x.LeftNode;
                FibonacciNode rightNode = x.RightNode;
                leftNode.RightNode = rightNode;
                rightNode.LeftNode = leftNode;

            }
            y.Degree--;

            //add x to the list
            list.Add(x);
            //FibonacciNode lastNode = list.head.LeftNode;
            //lastNode.RightNode = x;
            //x.LeftNode = lastNode;
            //x.RightNode = list.head;
            //list.head.LeftNode = x;
            //list.Size++;

            x.Parent = null;
            x.Mark = false;
        }
Example #13
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;
     }
 }
Example #14
0
        public void buildRoute()
        {
            List<FibonacciNode> S = new List<FibonacciNode>();
            FibonacciNode s = new FibonacciNode() { StationID = 1, MinPathValue = 0, lastStop = null };
            FibonacciNode y = new FibonacciNode() { StationID = 3, MinPathValue = 5, lastStop = s };
            FibonacciNode t = new FibonacciNode() { StationID = 2, MinPathValue = 8, lastStop = y };
            FibonacciNode x = new FibonacciNode() { StationID = 4, MinPathValue = 9, lastStop = t };
            FibonacciNode z = new FibonacciNode() { StationID = 5, MinPathValue = 7, lastStop = y };
            S.Add(s);
            S.Add(t);
            S.Add(y);
            S.Add(x);
            S.Add(z);

            List<PathStop> route = new List<PathStop>();
            route = FindPath.buildRoute(S, 1);
            Assert.AreEqual(1, route[0].stationID);
            Assert.AreEqual(0, Convert.ToInt32(route[0].driveHour));

            route = FindPath.buildRoute(S, 2);
            Assert.AreEqual(1, route[0].stationID);
            Assert.AreEqual(0, Convert.ToInt32(route[0].driveHour));
            Assert.AreEqual(3, route[1].stationID);
            Assert.AreEqual(5, Convert.ToInt32(route[1].driveHour));
            Assert.AreEqual(2, route[2].stationID);
            Assert.AreEqual(8, Convert.ToInt32(route[2].driveHour));

            route = FindPath.buildRoute(S, 4);
            Assert.AreEqual(1, route[0].stationID);
            Assert.AreEqual(0, Convert.ToInt32(route[0].driveHour));
            Assert.AreEqual(3, route[1].stationID);
            Assert.AreEqual(5, Convert.ToInt32(route[1].driveHour));
            Assert.AreEqual(2, route[2].stationID);
            Assert.AreEqual(8, Convert.ToInt32(route[2].driveHour));
            Assert.AreEqual(4, route[3].stationID);
            Assert.AreEqual(9, Convert.ToInt32(route[3].driveHour));

            route = FindPath.buildRoute(S, 3);
            Assert.AreEqual(1, route[0].stationID);
            Assert.AreEqual(0, Convert.ToInt32(route[0].driveHour));
            Assert.AreEqual(3, route[1].stationID);
            Assert.AreEqual(5, Convert.ToInt32(route[1].driveHour));

            route = FindPath.buildRoute(S, 5);
            Assert.AreEqual(1, route[0].stationID);
            Assert.AreEqual(0, Convert.ToInt32(route[0].driveHour));
            Assert.AreEqual(3, route[1].stationID);
            Assert.AreEqual(5, Convert.ToInt32(route[1].driveHour));
            Assert.AreEqual(5, route[2].stationID);
            Assert.AreEqual(7, Convert.ToInt32(route[2].driveHour));
        }