Exemplo n.º 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");
            }
        }
Exemplo n.º 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");
            }
        }
Exemplo n.º 3
0
        public void Remove(IPriorityQueueItem <T> item)
        {
            // Check
            if (item.Index < 0 || item.Index >= _heap.Count)
            {
                throw new ArgumentException("handle does not exist in heap.", "item");
            }
            if (item.Item is ValueType)
            {
                if (!_heap[item.Index].Item.Equals(item.Item))
                {
                    throw new ArgumentException(string.Format("handle has unexpected item: heap {0} vs handle {1}.", _heap[item.Index].Item, item.Item));
                }
            }
            else
            {
                if (!ReferenceEquals(_heap[item.Index].Item, item.Item))
                {
                    throw new ArgumentException(string.Format("handle has unexpected item: heap {0} vs handle {1}.", _heap[item.Index].Item, item.Item));
                }
            }

            Remove(item.Index);
        }
Exemplo n.º 4
0
 /// <summary>
 ///   Decreases the key of the specified item in this heap to the passed
 ///   non-negative real number.
 /// </summary>
 /// <param name="item">Item to decrease the key of.</param>
 /// <param name="newKey">Item's new key.</param>
 /// <exception cref="ArgumentOutOfRangeException">
 ///   The resulting key would be greater than the current one.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 ///   This heap is empty.
 /// </exception>
 public void DecreaseKeyTo(IPriorityQueueItem <T> item, double newKey)
 {
     this.DecreaseKey(item, item.Key - newKey);
 }