Computing a Running Minimum
A running minimum stores the best value encountered at each point of a traversal and requires a valid empty-input policy.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Start from observed data
For 8, 3, 5, and minus 1, each new minimum is 8, then 3, then minus 1. Starting at zero would manufacture a minimum before a negative number appears. Use the first item or an explicit missing value.
Preserve a witness if useful
A result can include an index as well as the minimum. Updating on strictly smaller values retains the first position at a tie. Updating on smaller or equal values retains the last. An indexed traversal can keep that witness with the value.[1]
Prove the remembered value is an observed minimum
A scan over 5, minus 2, and 1 can start with five, update to minus two, and leave that result unchanged at one. After processing each position, the stored value is the smallest among those read. The first observed value satisfies that statement for a one-element prefix. Comparing it with the next element preserves the claim after expanding the prefix. An invariant at exit then converts the prefix claim to the full result.
Keep a position when it matters which value won
On scores 4, 2, 2, the first minimum appears at position one. Updating only for strictly smaller values retains it through position two. Using a smaller-or-equal comparison instead saves the last tie. Indexed iteration lets the result include the chosen position.
Use tests with ties, all positive values, and an allowed empty source. A fabricated initial zero would falsely win on all positive input. The empty result can be a documented missing value, an error, or an initial value supplied by the caller. Choose it according to the domain rather than relying on an arbitrary extreme.