The Science Library has one copy of the first edition of this book, which I have placed on reserve (this material appeared as Ch. 7 of that edition). I have also made several copies of Chapter 8 from my own copy of the second edition of the text, and those will hopefully be placed on reserve as well.
The problem is to take as input an array of n values (some of which may be negative), and to find a contiguous subarray which has the maximum sum. For example, consider the array:
| 31 | -41 | 59 | 26 | -53 | 58 | 97 | -93 | -23 | 84 |
We consider five distinct algorithms for solving this problem.
maxsofar = 0;
for (i = 0; i < n; i++)
for (j = i; j < n; j++) {
sum = 0;
for (k = i; k <= j; k++)
sum += x[k];
if (sum > maxsofar)
maxsofar = sum;
}
maxsofar = 0;
for (i = 0; i < n; i++) {
sum = 0;
for (j = i; j < n; j++) {
sum += x[j]; // sum is that of x[i..j]
if (sum > maxsofar)
maxsofar = sum;
}
}
maxsofar = 0;
cumarr[-1] = 0;
for (i = 0; i < n; i++)
cumarr[i] = cumarr[i-1] + x[i];
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
sum = cumarr[j] - cumarr[i-1]; // sum is that of x[i..j]
if (sum > maxsofar)
maxsofar = sum;
}
}
float recmax(int l, int u)
if (l > u) /* zero elements */
return 0;
if (l == u) /* one element */
return max(0, x[l]);
m = (l+u) / 2;
/* find max crossing to left */
lmax = sum = 0;
for (i = m; i >= l; i--) {
sum += x[i];
if (sum > lmax)
lmax = sum;
}
/* find max crossing to right */
rmax = sum = 0;
for (i = m+1; i <= u; i++) {
sum += x[i];
if (sum > rmax)
rmax = sum;
}
return max(lmax + rmax,
max(recmax(l, m),
recmax(m+1, u)));
}
The toplevel recursion is invoked as recmax(0,n-1).
maxsofar = 0
maxendinghere = 0;
for (i = 0; i < n; i++) {
maxhere = max(maxhere + x[i], 0)
maxsofar = max(maxsofar, maxhere)
}