Algorithm Search⌘K

Proofs: Monotonic deque

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

The monotonic deque keeps the strict suffix minima and returns the minimum of the queue full

This proof shows: correctness.

Claim. Start from NEW and apply any sequence of PUSH, POP and MIN, where POP and MIN are called only when h<th < t. Then before and after each operation, the invariant below holds, and each call of MIN returns min⁡{xh,…,xt−1}\min\{x_h, \ldots, x_{t-1}\}. The total time of nn operations is O(n)O(n).

Notation. xjx_j is the item of the jj-th PUSH, counting from 0. Q=(xh,…,xt−1)Q = (x_h, \ldots, x_{t-1}) is the queue. For h≤j<th \le j < t, call jj a strict suffix minimum of QQ if xj<xmx_j < x_m for all mm with j<m<tj < m < t. Let M(h,t)M(h, t) be the set of these positions.

(I) The positions of the pairs of DD, from the front to the back, are the elements of M(h,t)M(h, t) in increasing order, and the pair at position jj holds xjx_j.

Fact 1. Under (I), the items of DD strictly increase from the front to the back. Proof. If j<j′j < j' are both in M(h,t)M(h, t), then j<j′<tj < j' < t, so xj<xj′x_j < x_{j'} by the definition for jj. □\square

Fact 2. If h<th < t, then t−1∈M(h,t)t - 1 \in M(h, t), so DD is not empty. Proof. For j=t−1j = t - 1 there is no mm with t−1<m<tt - 1 < m < t, so the condition holds. □\square

Step 1: NEW. Line L1 sets h=t=0h = t = 0 and DD empty. M(0,0)M(0, 0) is empty, so (I) holds.

Step 2: PUSH. Assume (I) before PUSH(x)(x), and let xt=xx_t = x. After the call the counters are hh and t+1t + 1. We show

M(h,t+1)={ j∈M(h,t):xj<x }∪{t}.M(h, t + 1) = \{\, j \in M(h, t) : x_j < x \,\} \cup \{t\}.

Take jj with h≤j<th \le j < t. Then j∈M(h,t+1)j \in M(h, t + 1) exactly when xj<xmx_j < x_m for all mm with j<m<tj < m < t and also xj<xt=xx_j < x_t = x, that is, when j∈M(h,t)j \in M(h, t) and xj<xx_j < x. And t∈M(h,t+1)t \in M(h, t + 1) by Fact 2.

By Fact 1, the pairs of DD with an item ≥x\ge x form a suffix of DD (a back part): if a pair has item ≥x\ge x, every pair behind it has a larger item. The loop of lines L2 and L3 removes pairs from the back while the back item is ≥x\ge x, so it stops exactly when this suffix is gone, or when DD is empty. The remaining pairs are the positions j∈M(h,t)j \in M(h, t) with xj<xx_j < x, in increasing order. Line L4 appends (t,x)(t, x), and tt is larger than each remaining position. Line L5 sets the counter to t+1t + 1. So (I) holds for M(h,t+1)M(h, t + 1).

The loop ends: each round removes one pair, and DD has finitely many pairs.

Step 3: POP. Assume (I) and h<th < t. After the call the counters are h+1h + 1 and tt. For jj with h+1≤j<th + 1 \le j < t, the condition for j∈Mj \in M does not depend on hh, so M(h+1,t)=M(h,t)∖{h}M(h + 1, t) = M(h, t) \setminus \{h\}. By (I), the least position in DD is the front, and every position in DD is at least hh. So h∈M(h,t)h \in M(h, t) exactly when the front has position hh. Line L6 removes the front in exactly this case, and line L7 increases hh. By Fact 2, DD is not empty, so line L6 can read the front. So (I) holds after POP.

Step 4: MIN. Assume (I) and h<th < t. Let μ=min⁡Q\mu = \min Q, and let j∗j^\ast be the largest position in {h,…,t−1}\{h, \ldots, t - 1\} with xj∗=μx_{j^\ast} = \mu. Each mm with j∗<m<tj^\ast < m < t has xm≥μx_m \ge \mu and xm≠μx_m \ne \mu, so xm>μx_m > \mu, and j∗∈M(h,t)j^\ast \in M(h, t). Let ff be the position of the front of DD. By (I), f≤j∗f \le j^\ast. If f<j∗f < j^\ast, then xf<xj∗=μx_f < x_{j^\ast} = \mu by the definition of MM for ff, which is impossible because μ\mu is the minimum of QQ. So f=j∗f = j^\ast, and line L8 returns xj∗=μx_{j^\ast} = \mu. MIN changes nothing, so (I) still holds.

Step 5: the time. Lines L1, L4 to L8 cost O(1)O(1) for each call. Each round of the loop of lines L2 and L3 removes one pair, and each test that fails ends the loop, once per PUSH. A pair enters DD only at line L4, once per PUSH, and leaves at most once. So over nn operations, the loop runs at most nn rounds in total, and the total time is O(n)O(n). In the terms of the potential Φ=∣D∣≥0\Phi = |D| \ge 0 with Φ=0\Phi = 0 after NEW: a PUSH that removes pp pairs costs p+1p + 1 units and changes Φ\Phi by 1−p1 - p, so its amortized cost is 22; POP and MIN cost one unit and do not increase Φ\Phi.

Conclusion. By induction on the number of operations, (I) holds after each operation, by Steps 1 to 4, and each MIN returns the minimum of the queue. The total time of nn operations is O(n)O(n) by Step 5. ■\blacksquare

Back to Monotonic deque