Algorithm Search⌘K

Proofs: Kadane's algorithm

These are the human proofs of Kadane's algorithm, at full length.

Kadane's 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. For 1≤j≤n1 \le j \le n let E(j)=max⁡1≤i≤j∑k=ijA[k]E(j) = \max_{1 \le i \le j} \sum_{k=i}^{j} A[k], the largest sum of a subarray ending at jj, and let B(j)=max⁡1≤i≤l≤j∑k=ilA[k]B(j) = \max_{1 \le i \le l \le j} \sum_{k=i}^{l} A[k], the largest sum of a subarray of A[1..j]A[1..j]. Write S(i,l)=∑k=ilA[k]S(i, l) = \sum_{k=i}^{l} A[k].

Lemma 1. E(1)=A[1]E(1) = A[1], and E(j)=A[j]+max⁡(E(j−1),0)E(j) = A[j] + \max(E(j-1), 0) for j≥2j \ge 2.

Proof. The subarrays ending at jj are A[j..j]A[j..j], with sum A[j]A[j], and A[i..j]A[i..j] for 1≤i≤j−11 \le i \le j - 1, with sum S(i,j−1)+A[j]S(i, j-1) + A[j]. The largest of the second kind is E(j−1)+A[j]E(j-1) + A[j]. So E(j)=max⁡(A[j],E(j−1)+A[j])=A[j]+max⁡(0,E(j−1))E(j) = \max(A[j], E(j-1) + A[j]) = A[j] + \max(0, E(j-1)). ■\blacksquare

Lemma 2. B(1)=A[1]B(1) = A[1], and B(j)=max⁡(B(j−1),E(j))B(j) = \max(B(j-1), E(j)) for j≥2j \ge 2.

Proof. A subarray of A[1..j]A[1..j] either ends at jj or is a subarray of A[1..j−1]A[1..j-1]. ■\blacksquare

Claim. After step jj (after line L1 for j=1j = 1, after line L5 for j≥2j \ge 2):

  1. cur=E(j)cur = E(j), 1≤c≤j1 \le c \le j and S(c,j)=curS(c, j) = cur;
  2. best=B(j)best = B(j), 1≤bi≤bj≤j1 \le bi \le bj \le j and S(bi,bj)=bestS(bi, bj) = best.

Proof. By induction on jj.

Base, j=1j = 1. Line L1 sets cur=A[1]=E(1)cur = A[1] = E(1) with c=1c = 1, and best=A[1]=B(1)best = A[1] = B(1) with (bi,bj)=(1,1)(bi, bj) = (1, 1).

Step, j≥2j \ge 2. By induction, before line L3 we have cur=E(j−1)cur = E(j-1) and S(c,j−1)=E(j−1)S(c, j-1) = E(j-1).

If E(j−1)<0E(j-1) < 0, line L3 sets cur=A[j]cur = A[j] and c=jc = j. Then S(c,j)=A[j]S(c, j) = A[j], and A[j]=A[j]+max⁡(E(j−1),0)A[j] = A[j] + \max(E(j-1), 0) since the max is 00. If E(j−1)≥0E(j-1) \ge 0, line L4 sets cur=E(j−1)+A[j]cur = E(j-1) + A[j] and keeps cc, and S(c,j)=S(c,j−1)+A[j]=E(j−1)+A[j]S(c, j) = S(c, j-1) + A[j] = E(j-1) + A[j], with the max equal to E(j−1)E(j-1). In both cases cur=E(j)cur = E(j) by Lemma 1, and S(c,j)=curS(c, j) = cur with 1≤c≤j1 \le c \le j. Part 1 holds.

Before line L5, best=B(j−1)best = B(j-1) with S(bi,bj)=bestS(bi, bj) = best, by induction. If cur>bestcur > best, line L5 sets best=cur=E(j)best = cur = E(j) and (bi,bj)=(c,j)(bi, bj) = (c, j), so S(bi,bj)=E(j)S(bi, bj) = E(j) by part 1. Otherwise best=B(j−1)≥E(j)best = B(j-1) \ge E(j) is kept. In both cases best=max⁡(B(j−1),E(j))=B(j)best = \max(B(j-1), E(j)) = B(j) by Lemma 2, and part 2 holds. ■\blacksquare

Theorem. Line L6 returns (B(n),bi,bj)(B(n), bi, bj) with S(bi,bj)=B(n)S(bi, bj) = B(n), the largest sum of a non-empty subarray of AA.

Proof. The loop ends after step nn (or at once if n=1n = 1). Apply the claim with j=nj = n. The loop runs n−1n - 1 times and each step does O(1)O(1) work, so the algorithm stops after Θ(n)\Theta(n) operations. ■\blacksquare

Back to Kadane's algorithm