TheAlgorithms/C-Sharp · error · InvalidOperationException

Value cannot be increased

Error message

Value cannot be increased

What it means

FibonacciHeap.DecreaseKey throws InvalidOperationException "Value cannot be increased" when the new key k compares greater than the node's current key (k.CompareTo(x.Key) > 0). A decrease-key operation only accepts equal or smaller values; increasing would break the Fibonacci heap invariants.

Solutions

  1. Verify k is less than or equal to the node's current key before calling.
  2. If an increase is needed, remove and re-Push the node instead.
  3. Check comparator/parameter order so new value is the second argument.

Example fix

// before
heap.DecreaseKey(node, newVal); // newVal > node.Key
// after
if (newVal.CompareTo(node.Key) <= 0)
{
    heap.DecreaseKey(node, newVal);
}
else
{
    heap.Remove(node); // or delete+reinsert path
}
Defensive patterns

Strategy: validation

Validate before calling

if (newVal.CompareTo(node.Key) <= 0) { heap.DecreaseKey(node, newVal); }

Try / catch

try { heap.DecreaseKey(node, k); } catch (InvalidOperationException) { /* attempted increase; remove+reinsert instead */ }

Prevention

When it happens

Trigger: Calling DecreaseKey(x, k) where k > x.Key numerically / by comparison order.

Common situations: Inverting the priority direction (min-heap vs max-heap thinking), passing the old and new values swapped, or recomputing a distance that grew.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/974cdc6c8f838c83. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs:237

    ///     k must be less than x.Key, increasing the key of an item is not supported.
    /// </remarks>
    /// <param name="x">The item you want to reduce in value.</param>
    /// <param name="k">The new value for the item.</param>
    public void DecreaseKey(FHeapNode<T> x, T k)
    {
        if (MinItem == null)
        {
            throw new ArgumentException($"{nameof(x)} is not from the heap");
        }

        if (x.Key == null)
        {
            throw new ArgumentException("x has no value");
        }

        if (k.CompareTo(x.Key) > 0)
        {
            throw new InvalidOperationException("Value cannot be increased");
        }

        x.Key = k;
        var y = x.Parent;
        if (y != null && x.Key.CompareTo(y.Key) < 0)
        {
            Cut(x, y);
            CascadingCut(y);
        }

        if (x.Key.CompareTo(MinItem.Key) < 0)
        {
            MinItem = x;
        }
    }

    /// <summary>
    ///     Remove x from the child list of y.

View on GitHub (pinned to 96e2905cab)