Dynamic programming remembers what it has already worked out, so that no sub-problem is solved twice.

Overlapping Sub-problems

Consider the Fibonacci numbers defined by F(0) = 0, F(1) = 1 and F(n) = F(n − 1) + F(n − 2). The plain recursive program computing F(5) calls F(4) and F(3); F(4) again calls F(3) and F(2); so F(3) is computed twice, F(2) three times, and so on. The number of calls grows exponentially, about 1.618ⁿ. The sub-problems overlap: the same ones recur. A problem suits dynamic programming when it has optimal substructure (the best answer is built from best answers to sub-problems) and overlapping sub-problems.

Definition: Dynamic programming. A technique that solves a problem by breaking it into overlapping sub-problems, solving each only once and storing the results in a table so that they can be reused.

The Two Styles

Top-down with memoization keeps the recursive program but stores each result in a table on first computation and returns the stored value on later calls. Bottom-up tabulation fills the table iteratively from the smallest sub-problems upwards, so that when a value is needed its parts are already there. Bottom-up avoids recursion overhead and often allows the table to be shrunk. The usual steps are: define the sub-problem; write the recurrence; fix the base cases; choose the order of filling; and finally read off the answer (and, if needed, trace back to recover the solution itself).

Fibonacci by Dynamic Programming

Using a table F[0..n], set F[0] = 0, F[1] = 1 and for i from 2 to n set F[i] = F[i − 1] + F[i − 2]. The loop performs n − 1 additions, so the time is Θ(n) instead of exponential, and space is Θ(n); since only the last two values are needed, the space can be cut to O(1) with two variables. For n = 30 the naive method makes about 2.7 million calls, the tabulation just 29 additions. The values grow fast: F(30) = 832,040 and F(90) is near 2.9 × 10¹⁸, so large n needs big-integer arithmetic.

Greedy, Divide and Conquer and Dynamic Programming Compared

Divide and conquer uses independent sub-problems; dynamic programming uses overlapping ones and stores answers; greedy makes one choice and never revisits it. Dynamic programming usually examines all choices at each step, which makes it slower than greedy but correct for many problems on which greedy fails, such as the 0/1 knapsack.

Table 3.1: Three design techniques

Technique Sub-problems Choices Stores results
Divide and conquer Independent All, by recursion No
Greedy One at a time Single local choice No
Dynamic programming Overlapping All, with reuse Yes, in a table

Example 3.1: Fibonacci by tabulation

Problem: Compute F(8) using a table and count the additions.

Solution:

  1. F[0] = 0, F[1] = 1.
  2. F[2] = 1, F[3] = 2, F[4] = 3, F[5] = 5, F[6] = 8, F[7] = 13, F[8] = 21.
  3. Additions performed for i = 2 to 8: 7 = n − 1.
  4. The naive recursion would make 2·F(9) − 1 = 67 calls for the same value.

Result: F(8) = 21 using 7 additions instead of 67 calls.

def fib_table(n):
    F = [0, 1] + [0] * (n - 1)
    for i in range(2, n + 1):
        F[i] = F[i - 1] + F[i - 2]
    return F[n]
def naive_calls(n):
    return 1 if n < 2 else naive_calls(n - 1) + naive_calls(n - 2) + 1
print("F(8) =", fib_table(8), " naive calls:", naive_calls(8))
print("F(30) =", fib_table(30), " additions: 29")
print("naive calls for n = 25:", naive_calls(25))

Output

F(8) = 21  naive calls: 67
F(30) = 832040  additions: 29
naive calls for n = 25: 242785

Common mistakes

  • Using plain recursion where sub-problems overlap, causing exponential time.
  • Filling the table in an order in which needed values are not yet computed.
  • Confusing memoization (top-down) with tabulation (bottom-up).
  • Forgetting the base cases F(0) and F(1).

Key takeaways

  • Dynamic programming needs optimal substructure and overlapping sub-problems.
  • Store each sub-result once: memoization (top-down) or tabulation (bottom-up).
  • Fibonacci takes Θ(n) with a table instead of exponential time.
  • Steps: define, recurrence, base cases, filling order, answer.

Practice questions

  1. What two properties make a problem suitable for dynamic programming?
  2. Difference between memoization and tabulation?
  3. How many additions does tabulated Fibonacci need for F(n)?

Answers to the practice questions

  1. Optimal substructure and overlapping sub-problems.
  2. Memoization is top-down recursion with a cache; tabulation fills the table bottom-up iteratively.
  3. n − 1.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit III