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 . Then before and after each operation, the invariant below holds, and each call of MIN returns . The total time of operations is .
Notation. is the item of the -th PUSH, counting from 0. is the queue. For , call a strict suffix minimum of if for all with . Let be the set of these positions.
(I) The positions of the pairs of , from the front to the back, are the elements of in increasing order, and the pair at position holds .
Fact 1. Under (I), the items of strictly increase from the front to the back. Proof. If are both in , then , so by the definition for .
Fact 2. If , then , so is not empty. Proof. For there is no with , so the condition holds.
Step 1: NEW. Line L1 sets and empty. is empty, so (I) holds.
Step 2: PUSH. Assume (I) before PUSH, and let . After the call the counters are and . We show
Take with . Then exactly when for all with and also , that is, when and . And by Fact 2.
By Fact 1, the pairs of with an item form a suffix of (a back part): if a pair has item , 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 , so it stops exactly when this suffix is gone, or when is empty. The remaining pairs are the positions with , in increasing order. Line L4 appends , and is larger than each remaining position. Line L5 sets the counter to . So (I) holds for .
The loop ends: each round removes one pair, and has finitely many pairs.
Step 3: POP. Assume (I) and . After the call the counters are and . For with , the condition for does not depend on , so . By (I), the least position in is the front, and every position in is at least . So exactly when the front has position . Line L6 removes the front in exactly this case, and line L7 increases . By Fact 2, is not empty, so line L6 can read the front. So (I) holds after POP.
Step 4: MIN. Assume (I) and . Let , and let be the largest position in with . Each with has and , so , and . Let be the position of the front of . By (I), . If , then by the definition of for , which is impossible because is the minimum of . So , and line L8 returns . MIN changes nothing, so (I) still holds.
Step 5: the time. Lines L1, L4 to L8 cost 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 only at line L4, once per PUSH, and leaves at most once. So over operations, the loop runs at most rounds in total, and the total time is . In the terms of the potential with after NEW: a PUSH that removes pairs costs units and changes by , so its amortized cost is ; POP and MIN cost one unit and do not increase .
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 operations is by Step 5.