Ejemplo n.º 1
0
 public FibonacciHeap()
 {
     minNode = null;
     numberOfNodes = 0;
     root = new DoubleLinkedList();
     stationIDs = new List<int>();
 }
Ejemplo n.º 2
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];
                    }
                }
            }
        }
Ejemplo n.º 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);
         }
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
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.º 6
0
        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);
        }
Ejemplo n.º 7
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.º 8
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;
        }
Ejemplo n.º 9
0
        public FibonacciNode extractMinNode()
        {
            FibonacciNode extractNode = minNode;
            if (extractNode != null)
            {
                if (extractNode.Child != null)
                {
                    //TODO could be a bug when tranversing child list
                    FibonacciNode node = extractNode.Child;
                    for (int i = 0; i < extractNode.Degree; i++)
                    {
                        FibonacciNode nextNode = node.RightNode; //store current node's next node before the next node change to root list node
                        root.Add(node);
                        node.Parent = null;
                        node = nextNode;
                    }

                }
                root.Delete(extractNode);
                numberOfNodes--;
                if (extractNode == extractNode.RightNode)
                {
                    //if extractNode is the only node in the root
                    root = new DoubleLinkedList();
                    minNode = null;
                }
                else
                {
                    minNode = extractNode.RightNode; //not necessary a minimum node
                    consolidateTree(root);
                }

            }
            return extractNode;
        }
Ejemplo n.º 10
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;
     }
 }
Ejemplo n.º 11
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;
        }