# Why a similar observation holds for the

• No School
• AA 1
• 34

This preview shows page 10 - 17 out of 34 pages.

Why? A similar observation holds for the parenthesization of the sub chain A k+1 A k+2 …A n Thus, an optimal solution to an instance of the matrix-chain multiplication problem contains within it optimal solutions to sub problem instances.
Matrix Chain Multiplication Problem A Recursive Solution Define the value of an optimal solution recursively in terms of the optimal solutions to subproblems. For this problem, we pick as our subproblems the problems of determining the minimum cost of a parenthesization of A i A i+1 ….A j , for 1 <= i <= j <=n
A Recursive Solution Let m[i, j] be the minimum number of scalar multiplications needed to compute the matrix A i..j ; The cost of cheapest way to compute A 1..n would thus be m[1, n]
A Recursive Solution We can define m[i, j] recursively as follows: If i = j, the chain consists of just one matrix A i..i =A i , so No scalar multiplications are necessary to compute the product. Thus m[i, i] = 0 for i = 1, 2, …..,n. To compute m[i, j] when i < j, we take advantage of the structure of an optimal solution from step 1. Assume that optimal parenthesization splits the product A i , A i+1 ,….A j between A k and A k+1 , where i<=k < j.
A Recursive Solution Now m[i, j] is equal to the minimum cost for Computing the subproducts A 1..k , and Computing the subproducts A k+1..j , plus The cost of multiplying these two matrices together. For i = 1, 2, ….., n, matrix A i has dimensions P i-1 *P i Since computing the matrix product A 1..k A k+1..j takes p i-1 p k p j scalar multiplications, we obtain m[i, j] = m[i, k] + m[k+1, j] + p i-1 p k p j But we don’t know the value of k for an optimum solution. There are only j-1 possible values for k, namely k=i,i+1, …j-1.
A Recursive Solution Since, the optimal parenthesization must use one of these values for k, we need only check them all to find the best. Thus, our recursive definition for the minimum cost of parenthesizing the product A i , A i+1 , … A j becomes j i } ] , 1 [ ] , [ { min j i 0 ] , [ 1 if p p p j k m k i m if j i m j k i j k i
Matrix Chain Multiplication Problem Based on this recurrence, we can easily write the recursive algorithm to compute the minimum cost m[1..n] for multiplying A 1 , A 2 , ….,A n . Recursive-Matrix-Chain(p, i, j) { 1. if i = j 2. return 0 3. MinCost = 4. for(k = i; k <= j-1; k++) { q = Recursive-Matrix-Chain(p, i, k) + Recursive- Matrix- Chain(p, k+1, j) + p i-1 p k p j 5. if(q < MinCost) 6.

#### 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