예제 #1
0
        /// <summary>
        ///   Deletes the specified item from this heap.
        /// </summary>
        /// <param name="item">Item to be deleted.</param>
        /// <exception cref="InvalidOperationException">
        ///   This heap is empty.
        /// </exception>
        public void Delete(IPriorityQueueItem <T> item)
        {
            if (this.IsEmpty())
            {
                throw new InvalidOperationException("This heap is empty.");
            }

            if (item is FibonacciHeapItem <T> )
            {
                FibonacciHeapItem <T> fibHeapItem = (FibonacciHeapItem <T>)item;

                // Cut the edge joining the containing node x to its parent p.
                FibonacciHeapNode <T> x = fibHeapItem.ContainingNode;

                if (x.Parent == null)
                {
                    // x is originally a root - just remove it from the list of roots.
                    if (x == this.minimumNode)
                    {
                        this.DeleteMin();
                        return;
                    }
                    else
                    {
                        x.LeftSibling.RightSibling = x.RightSibling;
                        x.RightSibling.LeftSibling = x.LeftSibling;
                    }
                }
                else
                {
                    /*
                     * As x is not a root, remove it from the list of children of
                     * its parent and decrease its parent's rank.
                     */
                    x.CutEdgeToParent(false, this);

                    /*
                     * Form a new list of roots by concatenating the list of children of
                     * x with the original list of roots.
                     */
                    if (x.SomeChild != null)
                    {
                        this.minimumNode.RightSibling.LeftSibling = x.SomeChild.LeftSibling;
                        x.SomeChild.LeftSibling.RightSibling      = this.minimumNode.RightSibling;

                        this.minimumNode.RightSibling = x.SomeChild;
                        x.SomeChild.LeftSibling       = this.minimumNode;
                    }
                }

                this.count--;
            }
            else
            {
                throw new ArgumentException("Can work only on Fibonacci Heap items.", "item");
            }
        }
예제 #2
0
        /// <summary>
        ///   Decreases the key of the specified item in this heap by subtracting
        ///   the passed non-negative real number <c>delta</c>.
        /// </summary>
        /// <param name="item">Item to decrease the key of.</param>
        /// <param name="delta">Non-negative real number to be subtracted from the item's key.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        ///   <c>delta</c> is negative.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///   This heap is empty.
        /// </exception>
        public void DecreaseKey(IPriorityQueueItem <T> item, double delta)
        {
            if (delta < 0)
            {
                throw new ArgumentOutOfRangeException("delta", "delta has to be non-negative.");
            }

            if (this.IsEmpty())
            {
                throw new InvalidOperationException("This heap is empty.");
            }

            if (item is FibonacciHeapItem <T> )
            {
                FibonacciHeapItem <T> fibHeapItem = (FibonacciHeapItem <T>)item;

                // Subtract delta from the key of the passed item.
                fibHeapItem.Key -= delta;

                // Cut the edge joining the containing node x to its parent p.
                FibonacciHeapNode <T> x = fibHeapItem.ContainingNode;

                if (x.Parent != null)
                {
                    /*
                     * If x is not a root, remove it from the list of children of
                     * its parent, decrease its parent's rank, and add it to the list
                     * of roots of this heap in order to preserve the heap order.
                     */
                    x.CutEdgeToParent(true, this);
                }

                // Redefine the minimum node of this heap, if necessary.
                if (fibHeapItem.Key < this.minimumNode.Item.Key)
                {
                    this.minimumNode = x;
                }
            }
            else
            {
                throw new ArgumentException("Can only work on Fibonacci Heap items.", "item");
            }
        }