Sub topic 3
Maximum Subarray: Handling the Crossing Case
This chapter assumes you can trace recursive Python functions, read Big-O notation, and apply the master theorem from the previous chapter.
Maximum Subarray
The best contiguous slice can sit in one of three places. It can lie in the left half, lie in the right half, or cross the midpoint. The first two cases are recursive calls. The crossing case is the combine step. It takes the best sum that ends at mid - 1, scanning leftward, and adds the best sum that starts at mid, scanning rightward. Both scans are linear, so T(n) = 2T(n/2) + O(n) = O(n log n).
def max_subarray(a, lo, hi):
if hi - lo == 1:
return a[lo]
mid = (lo + hi) // 2
best = max(max_subarray(a, lo, mid), max_subarray(a, mid, hi))
s, left = 0, float('-inf')
for i in range(mid - 1, lo - 1, -1):
s += a[i]
left = max(left, s)
s, right = 0, float('-inf')
for i in range(mid, hi):
s += a[i]
right = max(right, s)
return max(best, left + right)
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(max_subarray(nums, 0, len(nums))) # 6A common error is to let the crossing sums be empty. Both sides must contain at least one element. Otherwise the "crossing" result is really a one-sided result, and an all-negative input can return the wrong answer.
Fewer or Smaller Subproblems
Most speedups in divide and conquer come from cutting the number of recursive calls or shrinking their size.
| Algorithm | Key idea | Recurrence | Result |
|---|---|---|---|
| Fast exponentiation | Compute x^(n/2) once and square it | T(n/2) + O(1) | O(log n) |
| Karatsuba | 3 half-size products instead of 4 | 3T(n/2) + O(n) | O(n^1.585) |
| Strassen | 7 block products instead of 8 | 7T(n/2) + O(n²) | O(n^2.807) |
| Quickselect | Recurse into one side only | T(~n/2) + O(n) expected | O(n) expected |
| Median of medians | Use the median of group-of-5 medians as the pivot | T(n/5) + T(7n/10) + O(n) | O(n) worst case |
| Closest pair | Check a strip around the split, about 7 neighbors per point | 2T(n/2) + O(n) | O(n log n) |
def power(x, n):
if n == 0:
return 1
half = power(x, n // 2)
return half * half if n % 2 == 0 else half * half * x
print(power(3, 13)) # 1594323Median of medians works because 1/5 + 7/10 < 1. The total work therefore shrinks geometrically from one level of recursion to the next.
Divide and Conquer Versus Dynamic Programming
Divide and conquer fits when the subproblems are disjoint. If the same subproblems come up again and again, as in naive Fibonacci, plain recursion repeats that work exponentially. Caching the results or building a table turns the solution into dynamic programming.
In Practice
Exercise
Naive recursive fib(n) calls fib(n-1) and fib(n-2). Why is it a poor divide-and-conquer algorithm, and what fixes it?
Show answer
The subproblems overlap, so the same values are recomputed and the running time grows exponentially. Memoizing the results or filling a table (dynamic programming) brings it to O(n).
Exercise
Using the master theorem, solve T(n) = 7T(n/2) + O(n²).
Show answer
a = 7 and b = 2, so n^(log2 7) ≈ n^2.807, which grows faster than n². Case 1 applies, and T(n) = O(n^2.807).