Proofs: Bentley's maximum sum submatrix algorithm
These are the human proofs of Bentley's maximum sum submatrix algorithm, at full length.
The column sweep returns a maximum-sum submatrix full
This proof shows: correctness.
We use the pseudocode of the page on with . For and let , and let be its maximum over all such quadruples. We use the correctness of Kadane's algorithm: on an array with , KADANE returns with , , and the largest sum of a non-empty subarray of .
Lemma 1 (band sums). In the iteration of the inner loop, after line L5, for every .
Proof. By induction on for fixed . Line L3 sets , the empty sum. If before line L5 (for the sum is empty), line L5 adds .
Lemma 2 (one band). In the iteration , line L6 returns and columns with .
Proof. By Lemma 1, , after exchanging the finite sums. So the rectangles with rows correspond one to one, with equal sums, to the non-empty subarrays of . The correctness of Kadane's algorithm gives the claim.
Claim. After the iteration , over all bands visited so far and all , and is a rectangle with this sum.
Proof. Before the first iteration, no band is visited and . In iteration , Lemma 2 gives the best sum of the band and its columns, and line L7 replaces when is larger, so the claim holds after the iteration.
Theorem. COLUMN-SWEEP returns and a rectangle that reaches it.
Proof. The loops of lines L2 and L4 visit each pair exactly once, and every rectangle has its rows in exactly one such band. By the claim after the last iteration, and reaches it. There are iterations, each with work, so the algorithm stops after steps.