0% found this document useful (0 votes)
18 views35 pages

M2 T1 RodCutting DynamicProg

Design and alalysis of algorithm

Uploaded by

Aarav Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views35 pages

M2 T1 RodCutting DynamicProg

Design and alalysis of algorithm

Uploaded by

Aarav Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Lecture 11

Dynamic Programming
Rod-cutting – Problem
Dynamic Programming
• Dynamic Programming (DP) is an algorithm design
technique for optimization problems.

• Like divide and conquer (D&C), DP solves problems by


combining solutions to sub-problems.

• Unlike D&C, sub-problems are not independent.


• Sub-problems may share sub-sub-problems
Dynamic Programming
The term Dynamic Programming comes from Control
Theory, not computer science. Programming refers to the
use of tables (arrays) to construct a solution.
In dynamic programming we usually reduce time by
increasing the amount of space
We solve the problem by solving sub-problems of
increasing size and saving each optimal solution in a table
(usually).
The table is then used for finding the optimal solution to
larger problems.
Time is saved since each sub-problem is solved only once.
Designing a DP Algorithm
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution in a bottom up
fashion.
4. Construct an optimal solution from computed
information.
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

Cutting is free (simplifying assumption)


Problem: given a table of prices pi determine the maximum
revenue rn obtainable by cutting up the given 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
Example: Rod Cutting
Problem: given a table of prices pi determine the maximum revenue rn
obtainable by cutting the given rod (of length n) and selling the pieces.
Can we use a greedy algorithm (like fractional knapsack) that always takes
the length with highest price/length value?
Length i 1 2 3 4
Price pi 1 20 33 36
Value pi/i 1 10 11 9

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

How can we construct a recurrence relation for rn?

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

Maximum revenue obtainable by cutting a rod of length n, r n = ? (for n = 1)

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

Maximum revenue obtainable by cutting a rod of length n, r n = ? (for n = 2)

r2 = max(p2, p1+r1) = max(5, 1+1) = 5

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

Maximum revenue obtainable by cutting a rod of length n, r n = ? (for n = 3)

r3 = max(p3, p1+r2 , p2+r1) = max(8, 1+5, 5+1) = 8

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

Maximum revenue obtainable by cutting a rod of length n, r n = ? (for n =4)

r4 = max(p4, p1+r3, p2+r2, p3+r1) = max(9, 1+8, 5+5, 8+1) = 10

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

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

 The recurrence relation: rn = max1≤i≤n(pi + rn-i) shows that the Rod

Cutting problem has optimal substructure property:

an optimal solution to the problem (here rn) contains within it optimal

solution to sub problems (here rn-i).

This recurrence relation can be implemented as a simple top-down


recursive procedure
Top Down algorithm for Rod Cutting

Computing the recursion leads to re-computing some numbers (overlapping


subproblems) again and again – how many?

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:

T(n) = total # of calls made to CUT-ROD for an initial call of CUT-ROD(p,n)


= The # of nodes for a recursion tree corresponding to a rod of size n = ?

Solution: CUT-ROD(p,n) calls CUT-ROD(p, n-i) for i = 1,2,3, …, n, i.e., CUT-ROD(p,n)


calls CUT-ROD(p, j) for j = 0,1,2, …, n-1

T 0  c,T (n) c   j 0 T ( j ) O(2 ),n 1.


n 1 n
Extra Explanation on
How to decrease time?
We have a problem: “reasonable size” problems are not solvable in “reasonable
time” (but, in this case, they are solvable in “reasonable space”).

Specifically:
• Note that navigating the whole tree requires 2n function calls
•However, no more than n different values need to be computed or used.

We exploit these observations to come up with two ways to decrease time:


• Memoized Top-down algorithm: store the value of rk (once computed by a call
of CUT-ROD) in a table and reuse it in later calls of CUT-ROD as needed. This
technique allows us to compute rk only once.

The technique of storing and reusing values computed by a previous call of a


recursive function is called “memoizing” (i.e., writing yourself a memo).

• Bottom Up Algorithm: Compute small subproblems first, then gradually solve


larger and larger subproblems by using the pre-computed solutions of smaller
subproblems. For e.g., for rod-cutting problem: compute r 0, r1, r2, …., rn
Memoized Top-down Algorithm for
Rod-Cutting Problem
X X X

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

Maximum revenue obtainable by cutting a rod of length n, r n = ? (for n =4)

r4 = max(p4, p1+r3, p2+r2, p3+r1) = max(9, 1+8, 5+5, 8+1) = 10

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

Store 2 tables : revenue and S(optimal sequence of cuts

after the maximum revenue is obtained)

S => is used to record the optimal size of the first piece

to cut off for each rod length j.


Bottom Up Algorithm

Both memoized top-down and bottom-up approach takes Θ(n2) time.


Why??

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

times. Similarly, memoized top-down


Simulation: Rod Cutting
Length 1 2 3 4 5 6 7
i
Price pi 1 5 8 9 10 12 17

We begin by constructing (by hand) the optimal solutions for i = 1, …, 10:


r0 = 0
r1 = p 1 = 1 (no cuts)
r2 = max(p2, p1+r1) = max(5,1+1) = 5 (no cuts)
r3 = max(p3, p2+r1, p1+r2) = max(8,5+1,1+5) = 8 (no cuts)
r4 = max(p4, p3+r1, p2+r2, p1+r3) = max(9, 8+1, 5+5, 1+8) = 10
r5 = ?
r6 = ?
r7 = ?
Simulation: Rod Cutting
Length 1 2 3 4 5 6 7
i
Price pi 1 5 8 9 10 12 17

We begin by constructing (by hand) the optimal solutions for i = 1, …, 10:


r0 = 0
r 1 = p1 = 1 (no cuts)
r2 = max(p2, p1+r1) = max(5,1+1) = 5 (no cuts)
r3 = max(p3, p2+r1, p1+r2) = max(8,5+1,1+5) = 8 (no cuts)
r4 = max(p4, p3+r1, p2+r2, p1+r3) = max(9, 8+1, 5+5, 1+8) = 10
r5 = max{p5, p4+r1, p3+r2, p2+r3, p1+r4}
= max(10, 9+1, 8+5, 5+8, 1+10) = 13
r6 = max{p6, p5+r1, p4+r2, p3+r3, p2+r4, p1+r5}
= max{12,11,14,16,15,14} = 16
r7 = p2 + r5 = 5 + 13 = 18
Reconstructing a Solution
Reconstructing a Solution
Simulation: Rod Cutting
Length i 1 2 3 4 5 6 7
Price pi 1 5 8 9 10 12 17
si 1 2 3 2 2 3 2

We begin by constructing (by hand) the optimal solutions for i = 1, …, 10:


r0 = 0
r 1 = p1 = 1 (no cuts)
r2 = max(p2, p1+r1) = max(5,1+1) = 5 (no cuts)
r3 = max(p3, p2+r1, p1+r2) = max(8,5+1,1+5) = 8 (no cuts)
r4 = max(p4, p3+r1, p2+r2, p1+r3) = max(9, 8+1, 5+5, 1+8) = 10
r5 = max{p5, p4+r1, p3+r2, p2+r3, p1+r4}
= max(10, 9+1, 8+5, 5+8, 1+10) = 13
r6 = max{p6, p5+r1, p4+r2, p3+r3, p2+r4, p1+r5}
= max{12,11,14,16,15,14} = 16
r7 = p2 + r5 = 5 + 13 = 18
Simulation: Rod Cutting
Length i 1 2 3 4 5 6 7
Price pi 1 5 8 9 10 12 17
si 1 2 3 2 2 3 2

Optimal Cutting lengths of a rod of length 7:


s7 = 2
Þ 2, optimal cutting of a rod of remaining length (7-2) is: s7-2 = s5
Þ s5 = 2, optimal cutting of a rod of remaining length (5-2) is: s5-2 = s3
Þ s3= 3, remaining length = 3-3 = 0; so stop

Therefore optimal cutting lengths of a rod of length 7 is: 2,2,3


Multiple Rod Cutting
You are given m rods of different lengths
A rod of length i inches will be sold for pi dollars

Problem: given a table of prices pi determine the maximum


revenue obtainable by cutting m rods 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: 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,j1 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

What are the base cases?


j n-j
A possible cut
i

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

Can you write memoized top-down and bottom-up algorithms


to solve this problem?
j n-j
A possible cut
i

m-i

You might also like