The document discusses the dynamic programming approach to solving the Fibonacci numbers problem and the rod cutting problem. It explains that dynamic programming formulations first express the problem recursively but then optimize it by storing results of subproblems to avoid recomputing them. This is done either through a top-down recursive approach with memoization or a bottom-up approach by filling a table with solutions to subproblems of increasing size. The document also introduces the matrix chain multiplication problem and how it can be optimized through dynamic programming by considering overlapping subproblems.
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.
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