Algorithm Search⌘K

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 M[0..m−1][0..n−1]M[0..m-1][0..n-1] with m,n≥1m, n \ge 1. For a≤ba \le b and e≤de \le d let S(a,b,e,d)=∑i=ab∑j=edM[i][j]S(a, b, e, d) = \sum_{i=a}^{b} \sum_{j=e}^{d} M[i][j], and let S∗S^* be its maximum over all such quadruples. We use the correctness of Kadane's algorithm: on an array C[0..n−1]C[0..n-1] with n≥1n \ge 1, KADANE returns (s,e,d)(s, e, d) with e≤de \le d, ∑j=edC[j]=s\sum_{j=e}^{d} C[j] = s, and ss the largest sum of a non-empty subarray of CC.

Lemma 1 (band sums). In the iteration (a,b)(a, b) of the inner loop, after line L5, C[j]=∑i=abM[i][j]C[j] = \sum_{i=a}^{b} M[i][j] for every jj.

Proof. By induction on bb for fixed aa. Line L3 sets C[j]=0C[j] = 0, the empty sum. If C[j]=∑i=ab−1M[i][j]C[j] = \sum_{i=a}^{b-1} M[i][j] before line L5 (for b=ab = a the sum is empty), line L5 adds M[b][j]M[b][j]. ■\blacksquare

Lemma 2 (one band). In the iteration (a,b)(a, b), line L6 returns s=max⁡e≤dS(a,b,e,d)s = \max_{e \le d} S(a, b, e, d) and columns e≤de \le d with S(a,b,e,d)=sS(a, b, e, d) = s.

Proof. By Lemma 1, S(a,b,e,d)=∑j=ed∑i=abM[i][j]=∑j=edC[j]S(a, b, e, d) = \sum_{j=e}^{d} \sum_{i=a}^{b} M[i][j] = \sum_{j=e}^{d} C[j], after exchanging the finite sums. So the rectangles with rows a..ba..b correspond one to one, with equal sums, to the non-empty subarrays of CC. The correctness of Kadane's algorithm gives the claim. ■\blacksquare

Claim. After the iteration (a,b)(a, b), best=max⁡{S(a′,b′,e,d)}best = \max\{S(a', b', e, d)\} over all bands (a′,b′)(a', b') visited so far and all e≤de \le d, and RR is a rectangle with this sum.

Proof. Before the first iteration, no band is visited and best=−∞best = -\infty. In iteration (a,b)(a, b), Lemma 2 gives the best sum ss of the band and its columns, and line L7 replaces (best,R)(best, R) when ss is larger, so the claim holds after the iteration. ■\blacksquare

Theorem. COLUMN-SWEEP returns S∗S^* and a rectangle that reaches it.

Proof. The loops of lines L2 and L4 visit each pair 0≤a≤b≤m−10 \le a \le b \le m - 1 exactly once, and every rectangle has its rows in exactly one such band. By the claim after the last iteration, best=S∗best = S^* and RR reaches it. There are m(m+1)/2m(m+1)/2 iterations, each with Θ(n)\Theta(n) work, so the algorithm stops after Θ(m2n)\Theta(m^2 n) steps. ■\blacksquare

Back to Bentley's maximum sum submatrix algorithm