Docsity

Memoization and Dynamic Programming: Raise2 Function and Pascal's Triangle, Papers of Algorithms and Programming

Toggle sidebar
bg1
1
Wellesley College โ—Š CS231 Algorithms โ—Š November 1, 1996
Handout #25
MEMOIZATION AND DYNAMIC PROGRAMMING
Reading: CLR Chapter 16
Below is a recursive function Raise2 that computes 2n .
Raise2(n)
if n = 0 then
return 1
else
return 2 * Raise2(n-1)
For any input, can draw a function call tree in which each node is labelled by Raise2(i)
for some i, and each Raise(i-1) is a child of Raise(i). E.g. Draw Raise2(3):
Suppose we had a machine that didn't have a multiply operator. Then we might have
Raise2-Slow(n)
if n = 0 then
return 1
else
return Raise2(n-1) + Raise2(n-1)
What is the recurrence relation and solution for the running-time of Raise2-Slow?
Draw Raise2-Slow(3):
The reason Raise2-Slow is so slow is that it resolves the same subproblems many times.
We can make it fast again by remembering the result of a subproblem once it is solved.
This effectively "glues" together nodes with the same label in the function call tree to
form a DAG (Directed Acyclic Graph). In this case, we can remember the subproblem
result in a local variable:
Raise2-Fast1(n)
if n = 0 then
return 1
else
subresult <- Raise2(n-1)
return subresult + subresult
pf3

Partial preview of the text

Download Memoization and Dynamic Programming: Raise2 Function and Pascal's Triangle and more Papers Algorithms and Programming in PDF only on Docsity!

Wellesley College โ—Š CS231 Algorithms โ—Š November 1, 1996

Handout

MEMOIZATION AND DYNAMIC PROGRAMMING

Reading: CLR Chapter 16

Below is a recursive function Raise2 that computes 2n.

Raise2(n) if n = 0 then return 1 else return 2 * Raise2(n-1)

For any input, can draw a function call tree in which each node is labelled by Raise2(i)

for some i, and each Raise(i-1) is a child of Raise(i). E.g. Draw Raise2(3):

Suppose we had a machine that didn't have a multiply operator. Then we might have

Raise2-Slow(n) if n = 0 then return 1 else return Raise2(n-1) + Raise2(n-1)

What is the recurrence relation and solution for the running-time of Raise2-Slow?

Draw Raise2-Slow(3):

The reason Raise2-Slow is so slow is that it resolves the same subproblems many times.

We can make it fast again by remembering the result of a subproblem once it is solved.

This effectively "glues" together nodes with the same label in the function call tree to

form a DAG (Directed Acyclic Graph). In this case, we can remember the subproblem

result in a local variable:

Raise2-Fast1(n) if n = 0 then return 1 else subresult <- Raise2(n-1) return subresult + subresult

More generally, we need an auxiliary table to remember results.

Raise2-Fast2(n) R <- new array[0..n] for i <- 0 to n do R[i] <- 0 return Raise2-Memo(R,n)

Raise2-Memo(R,n) if R[n] = 0 then if n = 0 then return 1 else R[n] <- Raise2-Memo(R,n-1) + Raise2-Memo(R,n-1) return R[n]

This strategy of remembering the results of subproblems in a table is called

memoization (not memo r ization!)

Pascal's Triangle

Recall Pascal's triangle. Each element is the sum of the two elements above it, except for

edge elements, which are 1:

The following Pascal function computes the kth element of the nth row (where both k and

n are 0-based):

Pascal(n,k) if k = 0 or k = n then return 1 else return Pascal(n-1,k-1) + Pascal(n-1, k)