# Dynamic Programming - Memoization

### Memoized Solutions - Overview

- Memoization is a technique for improving the performance of
recursive algorithms

- It involves rewriting the recursive algorithm so that
as answers to problems are found, they are stored in an array.

- Recursive calls can look up results in the array rather than having to recalculate
them

- Memoization improves performance because partial results are never calculated twice

- Example: fibonacci

### Fibonacci - Simple Recursive Solution

- fibb(0) = 0, fibb(1) = 1; fibb(n) = fibb(n-1) + fibb(n-2)

- Obvious algorithm:
- Code (precondition: n ≥ 0):

fibb(n)
if n < 2
return n
else
return fibb(n-1) + fibb(n-2)

Performance:
- Recurrence: $T(n) = ...$
- $T(n)$ is exponential
- Actually, $T(n) = \Theta(\text{fibb}(n)$
- $\text{fibb}(n) \simeq 1.6^n$

### Fibonacci: Iterative Bottom-Up Solution

- Compute a table of values of fibb(0) up to fibb(n)

fibb(n): -- Precond: n ≥ 0
A: Array(0..n) of Natural;
A(0) := 0;
if n > 0 then
A(1) := 1;
for i in 2 .. n loop
A(i) := A(i-1) + A(i-2);
end if;
return A(n)

What is the performance?

We figured out the optimum order to fill the array:
- Always had solutions to subproblems when we needed them

### Fibonacci: Memoized, Recursive Top-Down Solution

- A linear recursive algorithm - uses memoization
- Do recursion until reach base case, then fill array while returning:

fib(n):
A: Array(0..n) of Natural := (0=>0, 1=>1, others=>0);
return fib-aux(n, A)
fib-aux(n, A: in out array):
if A(n) = 0 and then n /= 0 then -- Check if A(n) is calculated yet
A(n) := fib-aux(n-1, A) + A(n-2); -- Store solution
end if;
end if
return A(n) -- Return solution
client:
put(fib(5));

How many times is fib-aux called?
This is a top down solution
We arrange the recursion so that A(n-2) is calculated before it is needed
- This technique is called
** memoization**

### Memoized Programs - Summary

- Memoized programs store results of smaller subproblems

- Results of smaller subproblems are used in solving larger problems

- No subproblem result is calculated twice

- Now: the Rod Cuttin Problem

Back to Dynamic Programming