TheAlgorithms/C-Sharp · error · ArgumentException
New value is not than old value.
Error message
New value is not {(sorting != Sorting.Descending ? "less" : "greater")} than old value. What it means
PairingHeap.UpdateKey throws ArgumentException when the new value compares as greater than (or equal order-inverting for) the old value relative to the heap's sorting direction. For a min-heap the new key must be less than the old (and greater for a max-heap/descending sort); the heap only supports decrease-key / increase-key in one direction.
Solutions
- For a min-heap, only pass a newValue smaller than the current value (and larger for max-heap)
- Verify argument order: UpdateKey(currentValue, newValue), not reversed
- Catch ArgumentException if your algorithm legitimately may attempt both directions and skip/re-insert instead
Example fix
// before
heap.UpdateKey(node, node.Distance + 5); // increases key in min-heap
// after
if (comparer.Compare(node.Distance + 5, node.Distance) < 0)
heap.UpdateKey(node, node.Distance + 5);
else { heap.Delete(node); heap.Insert(node with distance); } // re-insert instead Defensive patterns
Strategy: validation
Validate before calling
if (comparer.Compare(newValue, currentValue) > 0) { /* re-insert instead of UpdateKey */ } else { heap.UpdateKey(currentValue, newValue); } Try / catch
try { heap.UpdateKey(currentValue, newValue); } catch (ArgumentException) { /* wrong direction: fall back to delete+insert */ } Prevention
- For min-heaps only decrease keys; for max-heaps only increase keys
- Double-check currentValue/newValue argument order
- If keys can move either way, remove and re-insert instead of UpdateKey
When it happens
Trigger: Calling UpdateKey(current, newValue) where comparer.Compare(newValue, currentValue) > 0 on a min-heap — i.e. attempting to increase a key in a min-heap, or decrease a key in a max-heap (Sorting.Descending).
Common situations: Dijkstra relaxation writing the wrong distance; mixing up min-heap and max-heap usage; changing sort direction without inverting the update logic; passing arguments in the wrong order (currentValue and newValue swapped).
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Current value is not present in this heap.
- Key " " already in tree!
- The sequence may only contain ones or zeros
- Capacity must be at least 1.
- Heap is empty!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/638180233ef6aea7.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Heap/PairingHeap/PairingHeap.cs:60
Count--;
return minMax.Value;
}
/// <summary>
/// Update heap key [O(log(n))].
/// </summary>
public void UpdateKey(T currentValue, T newValue)
{
if (!mapping.ContainsKey(currentValue))
{
throw new ArgumentException("Current value is not present in this heap.");
}
var node = mapping[currentValue]?.Where(x => x.Value.Equals(currentValue)).FirstOrDefault();
if (comparer.Compare(newValue, node!.Value) > 0)
{
throw new ArgumentException($"New value is not {(sorting != Sorting.Descending ? "less" : "greater")} than old value.");
}
UpdateNodeValue(currentValue, newValue, node);
if (node == root)
{
return;
}
DeleteChild(node);
root = RebuildHeap(root, node);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}View on GitHub (pinned to 96e2905cab)