Dynamic Programming: CSE 301: Combinatorial Optimization
Dynamic Programming: CSE 301: Combinatorial Optimization
F(9)
F(8) F(7)
F(4) F(3)
+ +
F(1) F(0)
Same subproblem is
solved multiple times
A Top-down Algorithm that memorizes previous
solutions (memoized top-down approach)
Algorithm Fib(n):
for i = 0 to n //Non-memoized Top-Down Approach
Algorithm Fibo(n):
F[i] = -∞
if n ≤ 1 then
Fib_rec(n) return n
else
Algorithm Fib_rec(n): return Fibo(n-1)+Fibo(n-2)
if F[n]≥0 then
return F[n]
if n ≤ 1 then
F[n] = n
else
F[n] = Fib_rec(n-1)+Fib_rec(n-2)
return F[n]
Time complexity?
Each of the F[i] values (i={0, 1,…, n}) is computed only once. So time is O(n).
Recursion Tree of Memoized Algorithm
Fib(5)
Fib_rec(5)
+
Fib_rec(4) Fib_rec(3)
+
Fib_rec(3)
Fib_rec(2)
+
Fib_rec(2) Fib_rec(1)
+
Fib_rec(1) Fib_rec(0)
A Bottom-up (Iterative) Algorithm
Algorithm fib_bu(n):
for i = 0 to n
if i ≤ 1 then
F[i] = i
else
F[i] = F[i-1] + F[i-2]
return F[n]
Time complexity?
The for loop here runs O(n) times and all the other (first and last)
statements take O(1) time. So total time is O(n).
Summery
Problem with the recursive Fib algorithm:
Each subproblem was solved for many times!
• Algorithm rec(n,r):
• if C[n,r] != -∞ then
• return C[n,r]
• if r = 1 then // base case 1
• C[n,r] = n
• else if r = n or r = 0 then // base case 2
• C[n,r] = 1
• else //general case
C[n,r] = rec(n-1, r-1)+rec(n-1, r)
return C[n,r]
Bottom-up Algorithm
Algorithm Combination (n,r):
for i = 0 to n
for j = 0 to min(i,r)
if j = 1 then
• C[i,j] = i
• else if j = i or j = 0 then
• C[i,j] = 1
• else //general case
C[i,j] = C[i-1, j-1]+C[i-1, j]
return C[n,r]
Time Complexity:
Θ (nr), for both memorized top-down & bottom-up algorithms
Example: Rod Cutting
You are given a rod of length n ≥ 0 (n in inches)
A rod of length i inches will be sold for pi dollars
24 - 1 = 2 3 = 8
Yes but the cost would be too high!!! There are 2n-1 possible ways to cut a rod of
length n (Exponential). We cannot try all possibilities for "large“ n; the exhaustive
approach isn’t practically feasible.
Example: Rod Cutting
Let us find a way to solve the problem recursively:
Let rn = the maximum revenue obtainable from a rod of length n
Advice: when you don’t know what to do next, start with a simple
example and hope that some idea will click…
Example: Rod Cutting
r1 = p1 = 1
Example: Rod Cutting
Þ rn = max(pn+r0, p1+ rn-1, p2+ rn-2, p3+ rn-3, …, pn-1+ r1) :[Let, r0 = 0]
Þ rn = max1≤i≤n(pi + rn-i)
In other words,
• maximal revenue rn is obtained by cutting the rod into smaller pieces of lengths: i
and (n-i), for some value of i (for which pi+rn-i is maximum) where1≤i≤n and
ri = pi, i.e., the piece of length i need no more cut (because cutting it into smaller
pieces will not increase its revenue) whereas
the piece of length (n-i) may need more cut; but we have already calculated the
maximal revenue, rn-i of a rod of length (n-i) before calculating rn.. We can use that
Optimal Substructure Property of Rod Cutting Problem
Specifically:
• Note that navigating the whole tree requires 2n function calls
•However, no more than n different values need to be computed or used.
X
Memoized Top-down Algorithm for
Rod-Cutting Problem
Time: Θ(n2)
Each r[i], for i = 1,2,3, …, n, is computed only once and this
computation takes Θ(n) time (due to the for loop in lines 6-7).
As we compute n values of r[i] (for i=1,2,…,n), total time is Θ(n2)
Bottom Up Algorithm
Length i 1 2 3 4 5 6 7 8
Price pi 1 5 8 9 10 12 17 19
si
Idea: apply the DP algorithm for rod-cutting on the longest rod and
then use its solution (r,s) to compute optimal cutting and optimal
revenue of each rod.
Rod Cutting having cutting-cost
You are given a rod of length n
A rod of length i inches will be sold for pi dollars
Each cut costs you c dollars.
Problem: given a table of prices pi determine the maximum
revenue obtainable by cutting a rod of length n and selling the
pieces.
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Idea: Add cutting cost in the recurrence relation of rod-cutting:
rn = max1≤i≤n(pi + rn-i - c) for n>0; r0 = 0
Chocolate Cutting/Breaking
You are given a Mimi chocolate having m*n blocks
In each step, we can break one piece of chocolate into two
rectangular pieces horizontally/vertically (so no L shaped
piece ever appears). A chocolate of i*j blocks can be sold for
pi,j dollars. Given pi,j determine the maximum revenue rm,n
obtainable by breaking a chocolate with m*n blocks and then
selling the pieces.
Price pi,j 1 2 3 4 5 6 7 8 9 10
1 1 5 8 9 10 17 17 20 24 30
2 5 6 18 22 31 35 37 39 40 45
3 8 18 22 34 37 39 42 43 45 48
4 9 22 34 40 44 47 48 50 52 53
Chocolate Cutting
Recurrence relation:
{ }
rm,n = max max{pi,n+rm-i,n}, max{pm,j+rm,n-j} , when n>0
1≤i≤n 1≤j≤n
m-i
Chocolate Cutting
Recurrence relation:
{ }
rm,n = max max{pi,n+rm-i,n}, max{pm,j+rm,n-j} , when n>0
1≤i≤n 1≤j≤n
m-i
Properties of DP Problems
How can one tell if a DP algorithm will be able to solve an optimization
problem i.e., whether a problem is a “DP problem”?
There are 2 properties exhibited by most DP Problems (problems
solvable via a DP algorithm):
1. Overlapping Subproblem Property: To solve a large problem, the
same subproblem is required to be solved again & again
• It implies that a we can save time by solving each subproblem exactly once
& saving that in a table, so that whenever that solution is required later, we
can simply look up that table and reuse that value
2. Optimal Sub-structure Property: Optimal solution of a problem
depends on optimal solution(s) of its subproblem(s)
• It implies that the optimal solution(s) of subproblem(s) can be combined
together to obtain the optimal solution of the problem itself