Download presentation
Presentation is loading. Please wait.
Published byหทัย พันธุเมธา Modified over 5 years ago
1
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
2
Matrix Chain Order Code
Matrix-Chain-Order(p) // p is an array containing pi-1 to pj n = p.length – // Number of matrices in chain Let m[1..n, 1..n] and s[1..n – 1, 2..n] be new tables for i = 1 to n m[i, i] = 0 // Single matrices take 0 multiplications for b = 2 to n // b is length of chain for i = 1 to n – b // all possible starting indices for length b j = i + b // Ending index of chain of length b m[i, j] = ∞ // Large value to start to find minimum for k = i to j - 1 // Try all possible splits of this chain q = m[i, k] + m[k + 1, j] + pi-1pkpj // smaller chains are // already computed if q < m[i, j] // If minimum value, then store it m[i, j] = q s[i, j] = k return m and s
3
Example Find the best parenthesization for multiplying the following chain: A1A2A3A4, where A1 is 3x3, A2 is 3x2, A3 is 2x2 and A4 is 2x4. p = <3, 3, 2, 2, 4> j j m[i, j] s[i, j] 1 2 3 4 1 2 3 4 i i
4
Running time of Matrix-Chain-Order
... for b = 2 to n // b is length of chain for i = 1 to n – b // all possible starting indices for length b j = i + b // Ending index of chain of length b m[i, j] = ∞ // Large value to start to find minimum for k = i to j - 1 // Try all possible splits of this chain What is the running time?
5
4. Construct optimal solution from computed information
We have computed the minimum number of multiplications: m[1, n] Now we want to know the parenthesization that gives this minimum. To find it, we use the array, s. s[i, j] records k for the optimal split in each subarray: Ai .. j becomes (Ai .. k)(Ak+1 .. j) To split A1 .. n, use s[1, n] value for k: A1 .. n = A1 .. s[1, n]As[1,n]+1 ..n We recursively split each of the subsequences using the array, s to find the optimal k at which to split each one. (Homework: Write this algorithm).
6
Memoization Memoization is a variation of dynamic programming.
Idea: Write the program like the classic recursive program, except: Each time you compute a solution, store it in a table. Before you compute a solution, check the table to see if it's already been computed. If it's there, use the table entry. Advantages: Memoization offers the efficiency of dynamic programming. It maintains the top-down recursive strategy.
7
Recursive Matrix Chain
The standard recursive programs can be inefficient: Recursive-Matrix-Chain(p, i, j) if i = j return 0 m[i, j] = infinity for k= i to j-1 do q = Recursive-Matrix-Chain(p, i, k) + Recursive-Matrix-Chain(p, k+1, j) + pi-1pkpj if q < m[i, j] m[i, j] = q return m[i, j]
8
Example: RMC for Matrix Chain of Length 4
Recursion tree for chain of length 4 (We will do this in class). RMC(p, 1, 4)
9
Memoized-Matrix-Chain
Memoized-Matrix-Chain(p) n = length[p] -1 for i = 1 to n for j = 1 to n m[i, j] = infinity return Lookup-Chain(p, 1, n) Lookup-Chain(p, i, j) if m[i, j] < infinity then return m[i, j] if i = j then m[i, j] = 0 else for k = i to j-1 q = Lookup-Chain(p, i, k) + Lookup-Chain(p, k+1, j) + pi-1pkpj if q < m[i, j] then m[i, j] = q
10
Running time of MMC If all the table entries were filled in except the one we are evaluating, a call to Lookup-Chain would take O(n) (single for loop). We compute one table entry for 1/2 the cells in an nxn table. # of entries = ? Each computation takes O(n) (from above). Total computation time = ? Comparison with dynamic programming: Dynamic programming generally faster by constant factor. In situations where not every subproblem is computed, memoization only solves those that are needed. Dynamic programming solves all the subproblems.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.