Algorithm Search⌘K

Proofs: Maximum subarray divide and conquer

These are the human proofs of Maximum subarray divide and conquer, at full length.

The divide-and-conquer algorithm returns a maximum-sum subarray full

This proof shows: correctness.

We use the pseudocode of the page on A[1..n]A[1..n] with n≥1n \ge 1. Write S(i,j)=A[i]+⋯+A[j]S(i, j) = A[i] + \dots + A[j] for i≤ji \le j. For lo≤hilo \le hi let M(lo,hi)=max⁡{S(i,j):lo≤i≤j≤hi}M(lo, hi) = \max\{S(i, j) : lo \le i \le j \le hi\}.

Lemma 1 (crossing scan). In a call with lo<hilo < hi and mid=⌊(lo+hi)/2⌋mid = \lfloor (lo + hi)/2 \rfloor, after lines L4 to L6 we have lo≤ci≤mid<cj≤hilo \le ci \le mid < cj \le hi, S(ci,cj)=sCS(ci, cj) = sC, and

sC=max⁡{S(i,j):lo≤i≤mid<j≤hi}.sC = \max\{S(i, j) : lo \le i \le mid < j \le hi\}.

Proof. Since lo<hilo < hi, we have lo≤mid<hilo \le mid < hi, so both loops run at least once. In line L4 the running sum tt equals S(i,mid)S(i, mid) after the item A[i]A[i] is added. The loop tries every ii from midmid down to lolo and keeps the largest value lsls with an index cici that reaches it, so ls=max⁡lo≤i≤midS(i,mid)=S(ci,mid)ls = \max_{lo \le i \le mid} S(i, mid) = S(ci, mid). In the same way, line L5 gives rs=max⁡mid<j≤hiS(mid+1,j)=S(mid+1,cj)rs = \max_{mid < j \le hi} S(mid + 1, j) = S(mid + 1, cj). For i≤mid<ji \le mid < j, S(i,j)=S(i,mid)+S(mid+1,j)S(i, j) = S(i, mid) + S(mid + 1, j), and the two terms depend on different indices, so the largest such sum is ls+rs=S(ci,cj)ls + rs = S(ci, cj). ■\blacksquare

Theorem. For 1≤lo≤hi≤n1 \le lo \le hi \le n, MAXSUB(A,lo,hi)\text{MAXSUB}(A, lo, hi) stops and returns (s,i,j)(s, i, j) with lo≤i≤j≤hilo \le i \le j \le hi, S(i,j)=sS(i, j) = s and s=M(lo,hi)s = M(lo, hi).

Proof. By strong induction on m=hi−lo+1m = hi - lo + 1.

Base, m=1m = 1. Line L1 returns (A[lo],lo,lo)(A[lo], lo, lo), and A[lo..lo]A[lo..lo] is the only subarray of A[lo..hi]A[lo..hi].

Step, m≥2m \ge 2. Then lo≤mid<hilo \le mid < hi, so the two calls in line L3 are on A[lo..mid]A[lo..mid] and A[mid+1..hi]A[mid+1..hi], which are non-empty and shorter than mm. By the induction hypothesis they stop and return sL=M(lo,mid)sL = M(lo, mid) with S(iL,jL)=sLS(iL, jL) = sL, and sR=M(mid+1,hi)sR = M(mid + 1, hi) with S(iR,jR)=sRS(iR, jR) = sR. By Lemma 1, sCsC is the largest sum of a subarray that contains both A[mid]A[mid] and A[mid+1]A[mid + 1], with S(ci,cj)=sCS(ci, cj) = sC.

Every subarray A[i..j]A[i..j] of A[lo..hi]A[lo..hi] has exactly one of these forms: j≤midj \le mid, or i>midi > mid, or i≤mid<ji \le mid < j. So

M(lo,hi)=max⁡(sL,sR,sC).M(lo, hi) = \max(sL, sR, sC).

Lines L7 to L9 return a candidate whose value is at least the other two, together with its indices, and all three index pairs lie in [lo,hi][lo, hi]. ■\blacksquare

Corollary. MAXSUB(A,1,n)\text{MAXSUB}(A, 1, n) returns a maximum-sum non-empty subarray of AA.

Running time. Let T(m)T(m) be the number of additions in lines L4 and L5 over a call on mm items and all the calls under it. Then T(1)=0T(1) = 0 and T(m)=T(⌈m/2⌉)+T(⌊m/2⌋)+mT(m) = T(\lceil m/2 \rceil) + T(\lfloor m/2 \rfloor) + m. For m=2km = 2^k this gives T(m)=mlg⁡mT(m) = m \lg m by induction: T(2k)=2⋅2k−1(k−1)+2k=k2kT(2^k) = 2 \cdot 2^{k-1}(k - 1) + 2^k = k 2^k. Since TT does not decrease in mm (again by induction), T(m)=Θ(mlog⁡m)T(m) = \Theta(m \log m) for every mm, and every other line of a call costs O(1)O(1), with m−1m - 1 calls of more than one item. ■\blacksquare

Back to Maximum subarray divide and conquer