Algorithm Search⌘K

Proofs: Monotonic deque over prefix sums

These are the human proofs of Monotonic deque over prefix sums, at full length.

The monotonic deque over prefix sums finds a shortest subarray with sum at least K full

This proof shows: correctness.

We use the pseudocode of the page on A[0..n−1]A[0..n-1] and KK, with P[0]=0P[0] = 0 and P[t]=A[0]+⋯+A[t−1]P[t] = A[0] + \dots + A[t-1]. A pair (i,j)(i, j) with 0≤i<j≤n0 \le i < j \le n is valid if P[j]−P[i]≥KP[j] - P[i] \ge K, which says that A[i..j)A[i..j) has sum at least KK. Let ℓ∗\ell^* be the smallest j−ij - i over valid pairs, or ∞\infty if there is none.

Lemma 1 (order). After line L7 of each step, the indices in QQ increase from front to back, and so do their prefix sums, strictly.

Proof. By induction on the steps. Pops keep the property. Line L7 pushes jj, which is larger than every index in QQ. After line L6 either QQ is empty or P[back(Q)]<P[j]P[\text{back}(Q)] < P[j], so the prefix sums still strictly increase. ■\blacksquare

Lemma 2 (soundness). Every pair recorded in line L5 is valid.

Proof. Line L5 runs only when the test of line L4 holds, that is P[j]−P[i]≥KP[j] - P[i] \ge K for the popped front ii. The index ii was pushed at an earlier step, so i<ji < j. ■\blacksquare

So best≥ℓ∗best \ge \ell^* at the end, with best=∞best = \infty if no pair is ever recorded.

Lemma 3 (completeness). If ℓ∗<∞\ell^* < \infty, some pair of length ℓ∗\ell^* is recorded.

Proof. Choose a valid pair (i∗,j∗)(i^*, j^*) with j∗−i∗=ℓ∗j^* - i^* = \ell^*. The index i∗i^* is pushed in step i∗i^*. We show that it is still in QQ when step j∗j^* starts.

Suppose it is popped at the front in a step tt with i∗<t<j∗i^* < t < j^*. Then (i∗,t)(i^*, t) is valid by Lemma 2 and t−i∗<ℓ∗t - i^* < \ell^*, a contradiction.

Suppose it is popped at the back in a step tt with i∗<t<j∗i^* < t < j^*. Then P[t]≤P[i∗]P[t] \le P[i^*], so P[j∗]−P[t]≥P[j∗]−P[i∗]≥KP[j^*] - P[t] \ge P[j^*] - P[i^*] \ge K, and (t,j∗)(t, j^*) is valid with j∗−t<ℓ∗j^* - t < \ell^*, a contradiction.

So i∗i^* is in QQ at the start of step j∗j^*. By Lemma 1, every index ii in front of i∗i^* has P[i]<P[i∗]≤P[j∗]−KP[i] < P[i^*] \le P[j^*] - K, so it passes the test of line L4. Hence the loop of lines L4 and L5 pops all of them, then reaches i∗i^*, which passes the test, and pops it with the pair (i∗,j∗)(i^*, j^*). At that point best≥ℓ∗best \ge \ell^*, so bestbest becomes ℓ∗\ell^* unless it is already ℓ∗\ell^*. ■\blacksquare

Theorem. SHORTEST-AT-LEAST returns best=ℓ∗best = \ell^*, and if ℓ∗<∞\ell^* < \infty it returns a valid pair RR of length ℓ∗\ell^*.

Proof. By Lemma 2, every value given to bestbest is the length of a valid pair, so best≥ℓ∗best \ge \ell^* always, and RR is valid whenever it is set. By Lemma 3, best≤ℓ∗best \le \ell^* at the end. The algorithm stops because each index is pushed once and popped at most once, so the loops of lines L4 and L6 run at most n+1n + 1 times each in total. ■\blacksquare

Back to Monotonic deque over prefix sums