Proofs: SMAWK algorithm
These are the human proofs of SMAWK algorithm, at full length.
SMAWK reads O(n + m) entries and takes O(n + m) time full
This proof shows: time.worst, space.extra.
We count the entries of that SMAWK reads. Each other step is charged to a read or to a row or column, so the time is of the same order.
REDUCE on rows and columns reads at most entries. Each test of line L13 reads two entries. A test that is true pops a column. A column is pushed at most once, so it is popped at most once, and there are at most true tests. A test that is false, or a loop that stops because the stack is empty, ends the loop for the current column, which happens at most once per column. So there are at most tests and reads.
Lines L5 to L9 on rows and reduced columns read at most entries. Let be the values of at the passes for the even positions , and let be the stop of pass , with the last stop at most . Pass reads entries. The sum telescopes to at most .
The recurrence. Let be the largest number of reads of SMAWK on rows and columns, and . After line L2, the call works on columns, so
Let and , so . At the top level the cost outside the recursion is at most . At level , the call has rows and at most columns, so its cost outside the recursion is at most . Summing,
So SMAWK reads entries. The rest of the work is per read, plus per call to build in line L3 and to find the positions of line L7 (with an array from columns to positions, or by walking forward together with ). These are bounded by the same geometric sum, so the time is .
Lower bound. Let and . Fix any matrix whose rows are strictly decreasing from left to right, for example , and let row 0 be arbitrary. For and with we have , so the implication of total monotonicity holds, and the matrix is totally monotone for every choice of row 0. An algorithm that does not read some entry cannot tell row 0 with that entry as its only minimum from the same row with that entry larger than all others, and the two answers differ. So every correct algorithm reads all entries of row 0 on some input. It also writes answers. The worst-case time is therefore , and SMAWK is optimal up to a constant factor.
Space. The recursion has depth at most . The lists at level have rows and columns, except the columns at the top, so all lists alive at one time have entries, and the map has entries.
SMAWK returns the leftmost minimum of each row full
This proof shows: correctness, termination.
We use the pseudocode of the page. is totally monotone: for rows and columns , . The contrapositive is: . For a list of rows and a list of columns , both in increasing order and with not empty, let be the leftmost column with the least value among the columns of .
Fact 1 (monotone minima). If are rows, then .
Proof. Let and suppose . Since is the leftmost minimum of row in and is to its left, . Total monotonicity with and gives , so is not a minimum of row in , a contradiction.
Fact 2 (removing losers). Let with . Then .
Proof. Let . Each column of is in , so for , and each with has . So is the leftmost minimum of row in .
Lemma 3 (REDUCE). Let with , and let be nonempty. REDUCE stops and returns a list of columns of , in increasing order, with , such that for each .
Proof. The stack only receives columns in the order of , so it is increasing from bottom to top. Line L14 pushes only when , so always. Each pass of the loop of line L13 pops one column, and each column is pushed at most once, so the loop stops. After the first column, the stack is never empty at the end of an iteration of line L12: if line L13 empties it, line L14 pushes because .
Write from the bottom. We show this invariant, which holds after each iteration of line L12:
(I) For each position with and each , the column is not . More exactly, and .
It holds for the empty stack. A pop removes the top and leaves the rest of the stack, with the same positions, so (I) stays true. A push of at position happens after the loop of line L13 ended. If , there is nothing to show. If , the loop ended because . Since , the contrapositive of total monotonicity gives for each . So has a column to its left with a value at most as large on each of these rows, and it is not their leftmost minimum. This is (I) for position .
Now take a column that is not in the final . Then either it was popped or it was never pushed. We show that for each .
- Popped. Some later column arrived when was at position , and line L13 found . Total monotonicity gives for each , so is not a minimum of these rows. For , (I) at the time of the pop says that is not .
- Not pushed. Then the loop of line L13 ended with and , with . The contrapositive gives for each , so is not the leftmost minimum of any row.
So every stays in .
Theorem. For nonempty or empty and nonempty, SMAWK stops and returns with for each .
Proof. By induction on . If , line L1 returns the empty map, which is correct. Let and assume the claim for all shorter row lists.
Line L2 replaces by $D = $ REDUCE. By Lemma 3, is nonempty and holds for each , so by Fact 2, . It remains to compute .
The list of line L3 has rows, so by the hypothesis line L4 stops and gives for . The submatrix on the rows and the columns is totally monotone, since each of its submatrices is one of .
Lines L5 to L9 treat the rows in order. We claim that before the pass for the even position , if , and if . Line L5 makes this true for , and line L9 sets to the position of , which is the claim for . In the pass for , let . By Fact 1, when , and when . So lies in , with when . Each column of left of is not , and each column right of is not . Line L8 returns the leftmost column of least value in this range, and since is in the range and is the leftmost minimum of the whole row in , line L8 returns . Line L8 also never reads outside , because , where follows from Fact 1.
So line L10 returns for every . Each loop runs over a finite list, so the call stops.