KEMBAR78
dynamic programming Rod cutting class | PPT
DYNAMIC PROGRAMMING
BY
D.Y.L.N CHARYULU
2
Dynamic programming
Problem: Let’s consider the calculation of Fibonacci numbers:
F(n) = F(n-2) + F(n-1)
with seed values F(1) = 1, F(2) = 1
or F(0) = 0, F(1) = 1
What would a series look like:
0, 1, 1, 2, 3, 4, 5, 8, 13, 21, 34, 55, 89, 144, …
What is the obvious challenge here?
https://www.cs.usfca.edu/~galles/visualization/DPFib.html
3
Dynamic programming
What’s the problem?
4
Memoization:
Fib(n)
{
if (n == 0)
return M[0];
if (n == 1)
return M[1];
if (Fib(n-2) is not already calculated)
call Fib(n-2);
if(Fib(n-1) is already calculated)
call Fib(n-1);
//Store the ${n}^{th}$ Fibonacci no. in memory & use previous results.
M[n] = M[n-1] + M[n-2]
Return M[n];
}
5
already calculated …
6
Dynamic programming
- Main approach: recursive, holds answers to a sub problem in a
table, can be used without recomputing.
-Can be formulated both via recursion and saving results in a
table (memoization). Typically, we first formulate the recursive
solution and then turn it into recursion plus dynamic
programming via memoization or bottom-up.
-”programming” as in tabular not programming code
7
Example: rod cutting
We are given prices pi and rods of length i:
length i 1 2 3 4 5 6 7 8 9 10
price pi 1 5 8 9 10 17 17 20 24 30
Question: We are given a rod of length n, and want to maximize revenue, by
cutting up the rod into pieces and selling each of the pieces.
Example: we are given a 4 inches rod. Best solution to cut up? We’ll first list the
solutions:
8
We’ll first list the solutions:
1.) Cut into 2 pieces of length 2:
2.) Cut into 4 pieces of length 1:
3-4.) Cut into 2 pieces of length 1 and 3 (3 and 1):
5.) Keep length 4:
6-8.) Cut into 3 pieces, length 1, 1 and 2 (and all the different orders)
Total: 8 cases for n = 4 (= 2n-1
). We can slightly reduce by always requiring
cuts in non-decreasing order. But still a lot!
p2 + p2 = 5+ 5 =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
p1 + p1 + p1 + p1 =1+1+1+1= 4
p1 + p3 =1+8 = 9 p3 + p1 = 8+1= 9
p4 = 9
p1 + p1 + p2 = 7 p1 + p2 + p1 = 7 p2 + p1 + p1 = 7
9
Note: We’ve computed a brute force solution; all possibilities for this simple small
example. But we want more optimal solution!
One solution:
i n-i
recurse on further
What are we doing?
- Cut rod into length i and n-i
-Only remainder n-i can be further cut (recursed)
We need to define:
a.) Maximum revenue for log of size n: rn (that is the solution we want to find).
b.) Revenue (price) for single log of length i: pi
10
i n-i
recurse on further
Example: If we cut log into length i and n-i:
Revenue: pi + rn-i Can be seen by recursing on n-i
What are we going to do?
There are many possible choices of i:
rn = max
p1 + rn−1
p1 + rn−2
...
pn +r0














11
Recursive (top-down) pseudo code:
Problem? Slow runtime (it’s essential brute force)!
But why? Cut-rod calls itself repeatedly with the same parameter values (tree):
- Node label: size of the subproblem
called on
- Can be seen by eye that many
subproblems are called repeatedly
(subproblem overlap)
- Number of nodes exponential in n
(2n
). therefore exponential number
of calls.
12
Therefore … Dynamic Programing Approach:
•Recursive solution is inefficient, since it repeatedly calculates a solution of the
same subproblem (overlapping subproblem).
•Instead, solve each subproblem only once AND save its solution. Next time we
encounter the subproblem look it up in a hashtable or an array (Memoization,
recursive top-down solution).
•We will also talk about a second solution where we save the solution of subproblems
Of increasing size (i.e. in order) in an array. Each time we will fall back on solutions that
we obtained in previous steps and stored in an array (bottom-up solution).
13
Recursive top-down solution: Cut-Rod with Memoization
•Step 1 Initialization:
Creates array for holding memoized
results, and initialized to minus infinity.
Then calls the main auxiliary function.
14
Step 2: The main auxiliary function, which goes through the lengths, computes
answers to subproblems and memoizes if subproblem not yet encountered:
15
Bottom-up solution: Bottom Up Cut-Rod
Each time we use previous values form arrays:
Check if value already known or memoized
vv
Compute maximum revenue
if it hasn’t already been
computed.
vv Saving value
16
Run time: For bottom-up and top-down approach O(n2
)
Why (double nested loop)?
We can also view the subproblems encountered in graph form.
•We reduce the previous tree that included all the subproblems
repeatedly
• each vertex represents
subproblem of a given size
• Vertex label: subproblem size
• Edges from x to y: We need
a solution for subproblem x when
solving subproblem y
17
Run time: Can be seen as number of edges O(n2
)
Note: Run time is a combination of number of items in table (n) and work per item (n).
The work per item because of the max operation (needed even if the table is filled
And we just take values from the table) is proportional to n as in the number of edges
in graph.
Matrix Chain Multiplication
Matrix Chain Multiplication
• Given : a chain of matrices {A1,A2,…,An}.
• Once all pairs of matrices are parenthesized, they can be
multiplied by using the standard algorithm as a sub-routine.
• A product of matrices is fully parenthesized if it is either a
single matrix or the product of two fully parenthesized matrix
products, surrounded by parentheses. [Note: since matrix
multiplication is associative, all parenthesizations yield the same
product.]
Matrix Chain Multiplication cont.
• For example, if the chain of matrices is {A, B, C, D},
the product A, B, C, D can be fully parenthesized in 5
distinct ways:
(A ( B ( C D ))),
(A (( B C ) D )),
((A B ) ( C D )),
((A ( B C )) D),
((( A B ) C ) D ).
• The way the chain is parenthesized can have a
dramatic impact on the cost of evaluating the
product.
Matrix Chain Multiplication Optimal
Parenthesization
• Example: A[30][35], B[35][15], C[15][5]
minimum of A*B*C
A*(B*C) = 30*35*5 + 35*15*5 = 7,585
(A*B)*C = 30*35*15 + 30*15*5 = 18,000
• How to optimize:
– Brute force – look at every possible way to parenthesize :
Ω(4n
/n3/2
)
– Dynamic programming – time complexity of Ω(n3
) and
space complexity of Θ(n2
).
Matrix Chain Multiplication Structure
of Optimal Parenthesization
• For n matrices, let Ai..j be the result of AiAi+1….Aj
• An optimal parenthesization of AiAi+1…Ansplits the
product between Ak and Ak+1 where 1  k < n.
• Example, k = 4 (A1A2A3A4)(A5A6)
Total cost of A1..6 = cost of A1..4 plus total
cost of multiplying these two matrices
together.
Matrix Chain Multiplication
Overlapping Sub-Problems
• Overlapping sub-problems helps in reducing the running
time considerably.
– Create a table M of minimum Costs
– Create a table S that records index k for each optimal sub-problem
– Fill table M in a manner that corresponds to solving the
parenthesization problem on matrix chains of increasing length.
– Compute cost for chains of length 1 (this is 0)
– Compute costs for chains of length 2
A1..2, A2..3, A3..4, …An-1…n
– Compute cost for chain of length n
A1..n
Each level relies on smaller sub-strings

dynamic programming Rod cutting class

  • 1.
  • 2.
    2 Dynamic programming Problem: Let’sconsider the calculation of Fibonacci numbers: F(n) = F(n-2) + F(n-1) with seed values F(1) = 1, F(2) = 1 or F(0) = 0, F(1) = 1 What would a series look like: 0, 1, 1, 2, 3, 4, 5, 8, 13, 21, 34, 55, 89, 144, … What is the obvious challenge here? https://www.cs.usfca.edu/~galles/visualization/DPFib.html
  • 3.
  • 4.
    4 Memoization: Fib(n) { if (n ==0) return M[0]; if (n == 1) return M[1]; if (Fib(n-2) is not already calculated) call Fib(n-2); if(Fib(n-1) is already calculated) call Fib(n-1); //Store the ${n}^{th}$ Fibonacci no. in memory & use previous results. M[n] = M[n-1] + M[n-2] Return M[n]; }
  • 5.
  • 6.
    6 Dynamic programming - Mainapproach: recursive, holds answers to a sub problem in a table, can be used without recomputing. -Can be formulated both via recursion and saving results in a table (memoization). Typically, we first formulate the recursive solution and then turn it into recursion plus dynamic programming via memoization or bottom-up. -”programming” as in tabular not programming code
  • 7.
    7 Example: rod cutting Weare given prices pi and rods of length i: length i 1 2 3 4 5 6 7 8 9 10 price pi 1 5 8 9 10 17 17 20 24 30 Question: We are given a rod of length n, and want to maximize revenue, by cutting up the rod into pieces and selling each of the pieces. Example: we are given a 4 inches rod. Best solution to cut up? We’ll first list the solutions:
  • 8.
    8 We’ll first listthe solutions: 1.) Cut into 2 pieces of length 2: 2.) Cut into 4 pieces of length 1: 3-4.) Cut into 2 pieces of length 1 and 3 (3 and 1): 5.) Keep length 4: 6-8.) Cut into 3 pieces, length 1, 1 and 2 (and all the different orders) Total: 8 cases for n = 4 (= 2n-1 ). We can slightly reduce by always requiring cuts in non-decreasing order. But still a lot! p2 + p2 = 5+ 5 =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 p1 + p1 + p1 + p1 =1+1+1+1= 4 p1 + p3 =1+8 = 9 p3 + p1 = 8+1= 9 p4 = 9 p1 + p1 + p2 = 7 p1 + p2 + p1 = 7 p2 + p1 + p1 = 7
  • 9.
    9 Note: We’ve computeda brute force solution; all possibilities for this simple small example. But we want more optimal solution! One solution: i n-i recurse on further What are we doing? - Cut rod into length i and n-i -Only remainder n-i can be further cut (recursed) We need to define: a.) Maximum revenue for log of size n: rn (that is the solution we want to find). b.) Revenue (price) for single log of length i: pi
  • 10.
    10 i n-i recurse onfurther Example: If we cut log into length i and n-i: Revenue: pi + rn-i Can be seen by recursing on n-i What are we going to do? There are many possible choices of i: rn = max p1 + rn−1 p1 + rn−2 ... pn +r0              
  • 11.
    11 Recursive (top-down) pseudocode: Problem? Slow runtime (it’s essential brute force)! But why? Cut-rod calls itself repeatedly with the same parameter values (tree): - Node label: size of the subproblem called on - Can be seen by eye that many subproblems are called repeatedly (subproblem overlap) - Number of nodes exponential in n (2n ). therefore exponential number of calls.
  • 12.
    12 Therefore … DynamicPrograming Approach: •Recursive solution is inefficient, since it repeatedly calculates a solution of the same subproblem (overlapping subproblem). •Instead, solve each subproblem only once AND save its solution. Next time we encounter the subproblem look it up in a hashtable or an array (Memoization, recursive top-down solution). •We will also talk about a second solution where we save the solution of subproblems Of increasing size (i.e. in order) in an array. Each time we will fall back on solutions that we obtained in previous steps and stored in an array (bottom-up solution).
  • 13.
    13 Recursive top-down solution:Cut-Rod with Memoization •Step 1 Initialization: Creates array for holding memoized results, and initialized to minus infinity. Then calls the main auxiliary function.
  • 14.
    14 Step 2: Themain auxiliary function, which goes through the lengths, computes answers to subproblems and memoizes if subproblem not yet encountered:
  • 15.
    15 Bottom-up solution: BottomUp Cut-Rod Each time we use previous values form arrays: Check if value already known or memoized vv Compute maximum revenue if it hasn’t already been computed. vv Saving value
  • 16.
    16 Run time: Forbottom-up and top-down approach O(n2 ) Why (double nested loop)? We can also view the subproblems encountered in graph form. •We reduce the previous tree that included all the subproblems repeatedly • each vertex represents subproblem of a given size • Vertex label: subproblem size • Edges from x to y: We need a solution for subproblem x when solving subproblem y
  • 17.
    17 Run time: Canbe seen as number of edges O(n2 ) Note: Run time is a combination of number of items in table (n) and work per item (n). The work per item because of the max operation (needed even if the table is filled And we just take values from the table) is proportional to n as in the number of edges in graph.
  • 18.
  • 19.
    Matrix Chain Multiplication •Given : a chain of matrices {A1,A2,…,An}. • Once all pairs of matrices are parenthesized, they can be multiplied by using the standard algorithm as a sub-routine. • A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses. [Note: since matrix multiplication is associative, all parenthesizations yield the same product.]
  • 20.
    Matrix Chain Multiplicationcont. • For example, if the chain of matrices is {A, B, C, D}, the product A, B, C, D can be fully parenthesized in 5 distinct ways: (A ( B ( C D ))), (A (( B C ) D )), ((A B ) ( C D )), ((A ( B C )) D), ((( A B ) C ) D ). • The way the chain is parenthesized can have a dramatic impact on the cost of evaluating the product.
  • 21.
    Matrix Chain MultiplicationOptimal Parenthesization • Example: A[30][35], B[35][15], C[15][5] minimum of A*B*C A*(B*C) = 30*35*5 + 35*15*5 = 7,585 (A*B)*C = 30*35*15 + 30*15*5 = 18,000 • How to optimize: – Brute force – look at every possible way to parenthesize : Ω(4n /n3/2 ) – Dynamic programming – time complexity of Ω(n3 ) and space complexity of Θ(n2 ).
  • 22.
    Matrix Chain MultiplicationStructure of Optimal Parenthesization • For n matrices, let Ai..j be the result of AiAi+1….Aj • An optimal parenthesization of AiAi+1…Ansplits the product between Ak and Ak+1 where 1  k < n. • Example, k = 4 (A1A2A3A4)(A5A6) Total cost of A1..6 = cost of A1..4 plus total cost of multiplying these two matrices together.
  • 23.
    Matrix Chain Multiplication OverlappingSub-Problems • Overlapping sub-problems helps in reducing the running time considerably. – Create a table M of minimum Costs – Create a table S that records index k for each optimal sub-problem – Fill table M in a manner that corresponds to solving the parenthesization problem on matrix chains of increasing length. – Compute cost for chains of length 1 (this is 0) – Compute costs for chains of length 2 A1..2, A2..3, A3..4, …An-1…n – Compute cost for chain of length n A1..n Each level relies on smaller sub-strings