Sub topic 17
When Subproblems Overlap: Divide and Conquer Versus Dynamic Programming
This chapter assumes you can trace recursive functions, read Big-O notation, and follow the merge sort and maximum subarray chapters. Both of those algorithms split their input into pieces that do not share any elements. This chapter covers what happens when subproblems do share work, and how to tell which technique a problem needs.
Disjoint Versus Overlapping Subproblems
In merge sort, the call on a[0:4] and the call on a[4:8] never touch the same elements. Each subproblem in the recursion tree is solved once. That is the setting where plain divide and conquer works well.
Some recurrences produce the same subproblem along many different paths. Fibonacci is the standard example: fib(n) = fib(n-1) + fib(n-2). Both branches go on to call fib(n-3), and the repetition grows exponentially as you go deeper.
Measuring the Waste
The code below counts how many calls each version makes.
from functools import lru_cache
calls = 0
def fib_naive(n):
global calls
calls += 1
if n < 2:
return n
return fib_naive(n - 1) + fib_naive(n - 2)
@lru_cache(maxsize=None)
def fib_memo(n):
if n < 2:
return n
return fib_memo(n - 1) + fib_memo(n - 2)
print(fib_naive(20), calls) # 6765 21891
print(fib_memo(20)) # 6765
print(fib_memo.cache_info().currsize) # 21The naive version makes 21,891 calls, and that count grows like O(φⁿ) with φ ≈ 1.618. The memoized version solves each of the 21 distinct subproblems once, so it runs in O(n). Dynamic programming is this idea: solve each distinct subproblem once and store the answer, either top-down with a cache or bottom-up with a table.
Choosing a Technique
| Question | Divide and conquer | Dynamic programming |
|---|---|---|
| Do subproblems share inputs? | No, they are disjoint | Yes, they repeat |
| How many distinct subproblems? | Proportional to the size of the recursion tree | Polynomial, often far fewer than the recursive calls |
| Extra memory | Recursion stack and buffers | A cache or table of every subproblem |
| Examples | Merge sort, quickselect, Karatsuba | Fibonacci, edit distance, knapsack |
To decide, write the recurrence and list the arguments each call passes down. If two different branches can reach the same argument tuple, you have overlap and should memoize. If every call works on a fresh, non-overlapping slice, a cache only adds memory and gives no speedup.
Exercise
The binomial coefficient satisfies C(n, k) = C(n-1, k-1) + C(n-1, k), with C(n, 0) = C(n, n) = 1. Should you implement it as plain divide and conquer or with memoization? How many distinct subproblems are there?
Show answer
Use memoization. Both branches can reach C(n-2, k-1), so the subproblems overlap and the plain recursion takes exponential time. The distinct subproblems are the pairs (i, j) with 0 ≤ j ≤ k and j ≤ i ≤ n, which is O(nk) pairs. That gives O(nk) time with a cache or table.
Exercise
Does the divide-and-conquer maximum subarray algorithm from the earlier chapter benefit from memoization?
Show answer
No. Each call works on a range [lo, hi), and the recursion splits ranges into disjoint halves, so no range is ever requested twice. A cache would only use memory. The O(n) Kadane's algorithm is a different, dynamic-programming formulation of the problem.