M2 T1 RodCutting DynamicProg
M2 T1 RodCutting DynamicProg
Dynamic Programming
Rod-cutting – Problem
Dynamic Programming
• Dynamic Programming (DP) is an algorithm design
technique for optimization problems.
No, for e.g. by greedy algorithm, optimal revenue of a 4 inch rod should be
33+1=34 which we get by cutting a rod of length 3 first (which has the
maximum value of 11) and then taking the remaining rod of length 1 (i.e., the
cutting lengths here are in order: (3,1)).
However, the optimal cutting is: (2,2) which gives us optimal revenue of
Example: Rod Cutting
Can we use a brute-force/exhaustive algorithm that tries all possible
cuts of a rod of length n?
Question: in how many different ways can we cut a rod of length n?
For a rod of length 4:
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
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Example: Rod Cutting
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Example: Rod Cutting
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Example: Rod Cutting
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Example: Rod Cutting
In general, for any n:
rn = max(pn, p1+ rn-1, p2+ rn-2, p3+ rn-3, …, pn-1+ r1)
Þ 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
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
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Time Complexity of Top Down algorithm
Let’s call Cut-Rod(p, 4), to see the effects on a simple case:
Specifically:
• Note that navigating the whole tree requires 2n function calls
•However, no more than n different values need to be computed or used.
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Example: Rod Cutting
Length i 1 2 3 4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30
Bottom-Up Approach: Dynamic programming
j times for each value of jɛ {1, 2,…, n}. So it is executed 1+2+…+n = Θ(n2)
The most heavily executed statement in bottom-up approach is line 6 which is executed
{ }
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