Algorithm Search⌘K

Proofs: Sliding window maximum with monotonic deque

These are the human proofs of Sliding window maximum with monotonic deque, at full length.

The monotonic deque reports the maximum of every window of length k full

This proof shows: correctness, termination, complexity.

We use the pseudocode of the page, with 1≤k≤n1 \le k \le n. For 0≤r≤n−10 \le r \le n - 1 let Wr={max⁡(0,r−k+1),…,r}W_r = \{\max(0, r-k+1), \ldots, r\}. An index i∈Wri \in W_r is a leader of WrW_r if A[i]>A[j]A[i] > A[j] for every jj with i<j≤ri < j \le r. Let Λr\Lambda_r be the list of the leaders of WrW_r in increasing order of index.

Lemma 1. The values of Λr\Lambda_r strictly decrease, rr is the last element of Λr\Lambda_r, and the first element of Λr\Lambda_r is an index ii with A[i]=max⁡j∈WrA[j]A[i] = \max_{j \in W_r} A[j].

Proof. If i<i′i < i' are both leaders, then A[i]>A[i′]A[i] > A[i'] because i<i′≤ri < i' \le r. The index rr is a leader, because there is no jj with r<j≤rr < j \le r. Let mm be the largest index in WrW_r with A[m]=max⁡WrAA[m] = \max_{W_r} A. Each jj with m<j≤rm < j \le r has A[j]<A[m]A[j] < A[m], by the choice of mm, so mm is a leader. A leader i<mi < m would need A[i]>A[m]A[i] > A[m], which is not possible. So mm is the first leader. ■\blacksquare

Lemma 2. For r≥1r \ge 1, the leaders of WrW_r are: the leaders ii of Wr−1W_{r-1} with i≥r−k+1i \ge r - k + 1 and A[i]>A[r]A[i] > A[r], and the index rr.

Proof. Take i∈Wri \in W_r with i<ri < r. Then i∈Wr−1i \in W_{r-1} and i≥r−k+1i \ge r - k + 1. The condition "A[i]>A[j]A[i] > A[j] for all i<j≤ri < j \le r" is the condition for Wr−1W_{r-1} together with A[i]>A[r]A[i] > A[r]. ■\blacksquare

Claim. After line L5 of step rr, DD equals Λr\Lambda_r from front to back. So line L6 appends max⁡WrA\max_{W_r} A for each r≥k−1r \ge k - 1, and the output is M[l]=max⁡(A[l…l+k−1])M[l] = \max(A[l \ldots l+k-1]) for l=0,…,n−kl = 0, \ldots, n - k.

Proof. By induction on rr.

Step r=0r = 0. DD is empty after line L1. Line L3 does nothing, line L4 gives D=(0)D = (0), and line L5 tests 0≤−k0 \le -k, which is false. Λ0=(0)\Lambda_0 = (0).

Step r≥1r \ge 1. By induction, D=Λr−1D = \Lambda_{r-1} before line L3. Its values strictly decrease by Lemma 1. Line L3 pops from the back while the back value is at most A[r]A[r]. Because the values decrease from front to back, the popped indices are exactly the indices of Λr−1\Lambda_{r-1} with value at most A[r]A[r], and the kept ones are those with value greater than A[r]A[r]. Line L4 appends rr. Now DD holds the leaders ii of Wr−1W_{r-1} with A[i]>A[r]A[i] > A[r], then rr. All indices of Λr−1\Lambda_{r-1} are at least r−kr - k, since they are in Wr−1W_{r-1}. The only one that can be less than r−k+1r - k + 1 is r−kr - k itself, and if it is in DD, it is at the front because the indices increase. Line L5 removes it exactly in this case. (r−kr - k is never equal to rr, so line L5 never removes the index just pushed.) By Lemma 2, D=ΛrD = \Lambda_r.

Output. By Lemma 1 the front of Λr\Lambda_r has the maximum value of WrW_r. For r≥k−1r \ge k - 1, Wr={r−k+1,…,r}W_r = \{r - k + 1, \ldots, r\} is the window that starts at l=r−k+1l = r - k + 1. So line L6 appends M[l]M[l], for l=0,1,…,n−kl = 0, 1, \ldots, n - k in order. ■\blacksquare

Why the popped indices lose nothing. An index jj popped by line L3 at step rr has A[j]≤A[r]A[j] \le A[r] and j<rj < r. Each window Wr′W_{r'} with r′≥rr' \ge r that contains jj also contains rr, because windows are intervals. So A[j]A[j] is never the strict maximum of a later window, and if it ties the maximum, A[r]A[r] has the same value. The proof above makes this exact through Lemma 2.

Termination and cost. The for loop runs nn times. Each index enters DD once, in line L4, and leaves at most once, so the total number of pops in lines L3 and L5 is at most nn, and the while loop of line L3 runs at most nn times with a pop in total. Each step ends the while loop with at most one comparison that is false. So the total number of comparisons of values in line L3 is at most 2n−22n - 2: there are at most n−1n - 1 pops at the back, since the index n−1n - 1 is never popped at the back, and at most n−1n - 1 false tests, since step 0 makes none. The time is Θ(n)\Theta(n). At each moment D⊆WrD \subseteq W_r, so ∣D∣≤k|D| \le k. ■\blacksquare

Back to Sliding window maximum with monotonic deque