# Mincost q 8 return mincost matrix chain

• No School
• AA 1
• 34

This preview shows page 16 - 28 out of 34 pages.

MinCost = q } 8. return MinCost; }
Matrix Chain Multiplication Problem A recursive algorithm may encounter each sub problem many times in different branches of its recursion tree. Instead of computing the same solution again and again, we can use the memoization strategy.
Matrix Chain Multiplication Problem Top Down Recursive Approach with Memoization Memoized-Matrix-Chain(p) { n = length[p] – 1; for(i= 1; i <= n; i++) for(j = i; j <= n; j++) m[i, j] = return LookUp-Chain(p, 1, n); }
Matrix Chain Multiplication Problem Top Down Recursive Approach with Memoization LookUp-Chain(p, i, j) { if( m[i, j] < ) return m[i, j] if(i == j) m[i, j] = 0; else { for(k = i; k <= j-1; k++) { q = LookUp-Chain(p, I, k) + LookUp-Chain(p, k+1, j) + p i- 1 p k p j if(q < m[i, j]) { m[i, j] = q; s[i, j] = k; } } } }
Matrix Chain Multiplication Problem Bottom Up Heuristic Approach To keep track of how to construct an optimal solution, let us define s[i, j] to be a value of k at which we can split the product A i ,A i+1 , … A j to obtain an optimal parenthesization.i.e. s[i, j] equals a value k such that m[i, j] = m[i, k] + m[k+1, j] + p i-1 p k p j .
Matrix Chain Multiplication Problem Bottom Up Approach with Memoization Matrix-Chain-Order(p) { n = length(p) –1 for(i = 1; i <= n; i++) m[i, i] = 0; for(l = 2; l <= n; l ++) for(i = l; i<=n-l+1; i++) { j = i + l – 1; m[i, j] = for(k = i; k<= j-1; k++) { q = m[i, k] + m[k+1, j] + p i-1 p k p j if (q < m[i, j]) { m[i, j] = q; s[i, j] = k; } } } return m and s; }
Matrix Chain Multiplication Problem Bottom Up Approach with Memoization Matrix-Chain-Multiply(A , s, i, j) { if (j > i) { X = Matrix-Chain-Multiply(A, s, i, s[i, j]); Y = Matrix-Chain-Multiply(A, s, s[i, j]+1, j); return Matrix-Multiply(X, Y); } else return A i }
Longest Common Subsequence (LCS) Problem Given two sequences of symbols, X and Y, determine the longest subsequence of symbols that appears in both X and Y. Example: X = <A, B, C, B, D, A, B> Y = <B, D, C, A, B, A> LCS of X, Y? <B, C, B, A> <B, D, A, B>
1. Enumerate all subsequences of X. 2. Each subsequence of X corresponds to a subset of indices {1, 2, ….,m} of X. 3. There are 2 m subsequences of X, so this approach requires exponential time. Longest Common Subsequence (LCS) Problem
Longest Common Subsequence (LCS) 7 1 2 3 4 5 6 i 0 j 0 1 2 3 4 5 6 y j B D C A B A x i A B C B D A B 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 2 2 0 1 1 2 2 2 2 0 1 1 2 2 3 3 0 1 2 2 2 3 3 0 1 2 2 3 3 4 0 1 2 2 3 4 4
LCS – Algorithm: Constructing an LCS Table b[1..m, 1..n] can be used to construct an LCS. Initial invocation is Print_LCS(b, X, length[X], length[Y]). Print_LCS(b[], X[], i, j) { if(i = = 0 or j = = 0) return 0; if(b[i, j] = = “ // common elements { Print_LCS(b, X, I-1, j-1); print xi; } else if (b[I, j] = = “ ”) Print_LCS(b, X, I-1, j); else Print_LCS(b, X, I, j-1); } Devise a memoized version of LCS_Length();
Longest Common Subsequence (LCS) Problem Dynamic Programming Approach I.

#### You've reached the end of your free preview.

Want to read all 34 pages?

• Fall '19

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern