Example #1
0
        private void printNode(FibonacciHeapNode <StorageType> currentNode, int tabs, StringBuilder returnBuffer)
        {
            for (int i = 0; i < tabs; ++i)
            {
                returnBuffer.Append('\t');
            }
            returnBuffer.Append(currentNode).Append('\n');
            if (currentNode.Child != null)
            {
                printNode(currentNode.Child, tabs + 1, returnBuffer);
            }

            for (FibonacciHeapNode <StorageType> nextNode = currentNode.Right; nextNode != currentNode; nextNode = nextNode.Right)
            {
                for (int i = 0; i < tabs; ++i)
                {
                    returnBuffer.Append('\t');
                }
                returnBuffer.Append(nextNode).Append('\n');
                if (nextNode.Child != null)
                {
                    printNode(nextNode.Child, tabs + 1, returnBuffer);
                }
            }
        }
Example #2
0
        public void insert(FibonacciHeapNode <StorageType> node, StorageType key)
        {
            node.Key = key;
            if (minimum != null)
            {
                node.Left       = minimum;
                node.Right      = minimum.Right;
                minimum.Right   = node;
                node.Right.Left = node;

                if (key.CompareTo(minimum.Key) < 0)
                {
                    minimum = node;
                }
            }
            else
            {
                minimum       = node;
                minimum.Right = node;
                minimum.Left  = node;
            }

            if (++size % fiboTarget == 0)
            {
                oldFiboTarget = fiboIndex;
                fiboTarget    = FIBO_MATRIX[++fiboIndex];
                consolidate();
            }
        }
Example #3
0
        private void link(FibonacciHeapNode <StorageType> child, FibonacciHeapNode <StorageType> parent)
        {
            ///NOTE: This exception check has been removed it should NEVER occur
            //if (child == parent)
            //{
            //    throw new Exception("Unable to link a node to itself!" + child);
            //}

            child.Left.Right = child.Right;
            child.Right.Left = child.Left;

            child.Parent = parent;
            if (parent.Child != null)
            {
                child.Left         = parent.Child;
                child.Right        = parent.Child.Right;
                parent.Child.Right = child;
                child.Right.Left   = child;
            }
            else
            {
                parent.Child = child;
                child.Left   = child;
                child.Right  = child;
            }

            ++parent.Degree;
            child.Marked = false;
        }
Example #4
0
        private void cascadingCut(FibonacciHeapNode <StorageType> node)
        {
            FibonacciHeapNode <StorageType> parent = node.Parent;

            if (parent != null)
            {
                if (parent.Marked)
                {
                    cut(node, parent);
                    cascadingCut(parent);
                }
                else
                {
                    parent.Marked = true;
                }
            }
        }
Example #5
0
        public void decreaseKey(FibonacciHeapNode <StorageType> node, StorageType k)
        {
            if (k.CompareTo(node.Key) > 0)
            {
                throw new Exception("Decrease Key got larger key value");
            }

            node.Key = k;
            FibonacciHeapNode <StorageType> parent = node.Parent;

            if (parent != null && node.Key.CompareTo(parent.Key) < 0)
            {
                cut(node, parent);
                cascadingCut(parent);
            }

            if (node.Key.CompareTo(minimum.Key) < 0)
            {
                minimum = node;
            }
        }
Example #6
0
        private void cut(FibonacciHeapNode <StorageType> child, FibonacciHeapNode <StorageType> parent)
        {
            child.Left.Right = child.Right;
            child.Right.Left = child.Left;
            --parent.Degree;

            if (parent.Child == child)
            {
                parent.Child = child.Right;
            }
            if (parent.Degree == 0)
            {
                parent.Child = null;
            }

            child.Parent     = null;
            child.Left       = minimum;
            child.Right      = minimum.Right;
            minimum.Right    = child;
            child.Right.Left = child;
            child.Marked     = false;
        }
Example #7
0
 private void recursiveAdd(FibonacciHeapNode <StorageType> parentNode)
 {
     for (FibonacciHeapNode <StorageType> currentNode = parentNode.Right; currentNode != parentNode; currentNode = currentNode.Right)
     {
         vertexList.Add(currentNode);
         if (currentNode.Parent != null)
         {
             edgeList.Add(new FibonacciHeapEdge <StorageType>(currentNode, currentNode.Parent));
         }
         if (currentNode.Left != null)
         {
             edgeList.Add(new FibonacciHeapEdge <StorageType>(currentNode, currentNode.Left));
         }
         if (currentNode.Right != null)
         {
             edgeList.Add(new FibonacciHeapEdge <StorageType>(currentNode, currentNode.Right));
         }
         if (currentNode.Child != null)
         {
             edgeList.Add(new FibonacciHeapEdge <StorageType>(currentNode, currentNode.Child));
             recursiveAdd(currentNode.Child);
         }
     }
 }
Example #8
0
 public void clear()
 {
     minimum = null;
     size    = 0;
 }
Example #9
0
 public bool ContainsVertex(FibonacciHeapNode <StorageType> vertex)
 {
     return(true);
 }
Example #10
0
 public void AddVertex(FibonacciHeapNode <StorageType> vertex)
 {
     vertexList.Add(vertex);
 }
Example #11
0
 public FibonacciGraph(FibonacciHeapNode <StorageType> minimuim)
 {
     recursiveAdd(minimuim);
 }
Example #12
0
 public FibonacciHeapEdge(FibonacciHeapNode <StorageType> source, FibonacciHeapNode <StorageType> target)
 {
     this.source = source;
     this.target = target;
 }
Example #13
0
        private void consolidate()
        {
            Dictionary <int, FibonacciHeapNode <StorageType> > nodeDegreeList = new Dictionary <int, FibonacciHeapNode <StorageType> >();

            int numberRoots = 0;

            if (minimum != null)
            {
                numberRoots = 1;
                for (FibonacciHeapNode <StorageType> current = minimum.Right; current != minimum; current = current.Right)
                {
                    ++numberRoots;
                }
            }

            FibonacciHeapNode <StorageType> currentNode = minimum;

            while (numberRoots > 0)
            {
                int degree = currentNode.Degree;
                while (nodeDegreeList.ContainsKey(degree))
                {
                    FibonacciHeapNode <StorageType> child;
                    nodeDegreeList.TryGetValue(degree, out child);
                    if (child == currentNode)
                    {
                        break;
                    }

                    FibonacciHeapNode <StorageType> parent = currentNode;
                    if (currentNode.Key.CompareTo(child.Key) > 0)
                    {
                        FibonacciHeapNode <StorageType> temp = child;
                        child  = parent;
                        parent = temp;
                    }

                    if (child == minimum)
                    {
                        minimum = parent;
                    }
                    link(child, parent);
                    currentNode = parent;
                    nodeDegreeList.Remove(degree);
                    ++degree;
                }

                nodeDegreeList[degree] = currentNode;
                currentNode            = currentNode.Right;
                --numberRoots;
            }

            FibonacciHeapNode <StorageType> newMin = minimum;

            for (FibonacciHeapNode <StorageType> right = minimum.Right; right != minimum; right = right.Right)
            {
                if (right.Key.CompareTo(newMin.Key) < 0)
                {
                    newMin = right;
                }
            }
            minimum = newMin;
        }
Example #14
0
        ///
        /// The Following method is left commented out, it is the direct although marginally optimized
        ///     Implementation of removeMin() from this psuedo code: http://www.cse.yorku.ca/~aaw/Jason/FibonacciHeapAlgorithm.html
        ///

        /*
         * public FibonacciHeapNode<StorageType> removeMin()
         * {
         *  FibonacciHeapNode<StorageType> oldMin = minimum;
         *  if (oldMin != null)
         *  {
         *      int degree = oldMin.Degree;
         *      FibonacciHeapNode<StorageType> child = oldMin.Child;
         *      FibonacciHeapNode<StorageType> right;
         *      while (degree > 0)
         *      {
         *          right = child.Right;
         *
         *          child.Left.Right = child.Right;
         *          child.Right.Left = child.Left;
         *
         *          child.Left = minimum;
         *          child.Right = minimum.Right;
         *          minimum.Right = child;
         *          child.Right.Left = child;
         *
         *          child.Parent = null;
         *          child = right;
         *          --degree;
         *      }
         *
         *      minimum.Left.Right = minimum.Right;
         *      minimum.Right.Left = minimum.Left;
         *
         *      if (minimum == minimum.Right)
         *      {
         *          minimum = null;
         *      }
         *      else
         *      {
         *          minimum = minimum.Right;
         *          consolidate();
         *      }
         *
         *      --size;
         *  }
         *  return oldMin;
         * }*/
        #endregion
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public FibonacciHeapNode <StorageType> removeMin()
        {
            FibonacciHeapNode <StorageType> oldMin = minimum;

            if (oldMin != null)
            {
                if (oldMin.Degree > 0)
                {
                    FibonacciHeapNode <StorageType> child = oldMin.Child;
                    child.Parent = null;
                    for (FibonacciHeapNode <StorageType> right = child.Right; right != child; right = right.Right)
                    {
                        right.Parent = null;
                    }

                    if (minimum.Right == minimum)
                    {
                        minimum = child;
                        consolidate();
                    }
                    else
                    {
                        FibonacciHeapNode <StorageType> minLeft    = minimum.Left;
                        FibonacciHeapNode <StorageType> minRight   = minimum.Right;
                        FibonacciHeapNode <StorageType> childLeft  = child.Left;
                        FibonacciHeapNode <StorageType> childRight = child.Left.Left;

                        minLeft.Right    = childLeft;
                        minRight.Left    = childRight;
                        childLeft.Left   = minLeft;
                        childRight.Right = minRight;
                        minimum          = minRight;

                        consolidate();
                    }
                }
                else if (minimum.Right == minimum)
                {
                    minimum = null;
                }
                else
                {
                    minimum.Left.Right = minimum.Right;
                    minimum.Right.Left = minimum.Left;
                    minimum            = minimum.Right;
                    consolidate();
                }

                if (--size / oldFiboTarget == 1 && size % oldFiboTarget == 0)
                {
                    --fiboIndex;
                    fiboTarget = oldFiboTarget;
                    if (fiboIndex > 0)
                    {
                        oldFiboTarget = FIBO_MATRIX[fiboIndex];
                    }
                }
            }

            return(oldMin);
        }
Example #15
0
 public void delete(FibonacciHeapNode <StorageType> node)
 {
     decreaseKey(node, minimumKeyValue);
     removeMin();
 }