Slope Trick Through Isotonic Regression: Why the Heap Contains Two Copies
Slope trick compresses a convex piecewise-linear DP by storing where its derivative changes. Instead of presenting a general data structure first, we will derive a compact algorithm for a constrained fitting problem.
Given observations , choose real values minimizing . This is isotonic regression with absolute loss. The observations need not be sorted, and sorting them would change the problem.
The DP function and its shape
Let be the minimum cost for the first observations when the final chosen value is exactly . Then
The prefix minimum of a convex function preserves its decreasing portion and flattens everything after a minimum. Adding an absolute value preserves convexity and introduces two units of derivative change at .
For this recurrence it is convenient to store the prefix minimum . Its slope is never positive. Represent it using a constant minimum and left-facing hinges:
Here . The multiset fits in a max heap. Its largest breakpoint is the left edge of the flat minimum region.
Derive the heap update
Adding and taking another prefix minimum yields a compact rule: insert twice, remove the largest breakpoint , and increase the minimum by . Since one inserted value is , this increase is nonnegative.
Equivalently, insert once; if the heap maximum exceeds , replace that maximum by another copy of and pay the difference. This second version avoids an unnecessary push and pop when no violation occurs.
long long cost = 0;std::priority_queue<long long> heap;for (long long a : observations) { heap.push(a); if (heap.top() > a) { cost += heap.top() - a; heap.pop(); heap.push(a); }}Why not merely keep one copy of each observation? The heap stores derivative breakpoints, not the original data. A descending pair may force two earlier constraints to share the same breakpoint. Treating it as a set, or deleting duplicates, destroys that information.
trace and exact small-state comparison
For observations , the prefix optimum costs are . One optimal fit is . Another is . The optimum need not be unique.
The lab plots the exact DP for integer endpoints on this small instance. Because observations are integers, an integer optimum exists. It runs the direct recurrence, not the heap algorithm, so it is useful as an independent explanation and check.
Beyond this one recurrence
A full slope-trick structure keeps left and right breakpoint heaps, plus the minimum. Prefix minimization discards the right-facing hinges; suffix minimization discards the left-facing ones. Shifting a function translates its breakpoints. A sliding minimum can often be represented with separate lazy offsets for the heaps.
These extensions solve movement-constrained convex DPs, but they need their own derivations. The single-heap code above does not solve every problem containing absolute values. In particular, weighted observations introduce slope jumps larger than one and may require multiplicity counts rather than literally inserting a breakpoint billions of times.
The algorithm uses time and storage. long long is needed for accumulated cost, and coordinate differences must also fit its range. The companion isotonic_cost is checked against exhaustive small endpoint DPs on randomized inputs.
Exercise. Require for a fixed integer . Transform and to remove the gap requirement. Prove that the absolute losses are preserved, then reuse the same heap without changing its internals.
Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline