Art_of_Programming_Contest_Part7

Art_of_Programming_Contest_Part7 - CHAPTER 11 DYNAMIC...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 11 DYNAMIC PROGRAMMING 123 Matrix Chain Multiplication (MCM) Let's start by analyzing the cost of multiplying 2 matrices: Matrix-Multiply(A,B): if columns[A] != columns[B] then error "incompatible dimensions" else for i = 1 to rows[A] do for j = 1 to columns[B] do C[i,j]=0 for k = 1 to columns[A] do C[i,j] = C[i,j] + A[i,k] * B[k,j] return C Time complexity = O(pqr) where |A|=p x q and |B| = q x r |A| = 2 * 3, |B| = 3 * 1, therefore to multiply these 2 matrices, we need O(2*3*1)=O(6) scalar multiplication . The result is matrix C with |C| = 2 * 1 TEST YOUR MATRIX MULTIPLICATION KNOWLEDGE Solve UVa problems related with Matrix Multiplication: 442 - Matrix Chain Multiplication - Straightforward problem Matrix Chain Multiplication Problem Input : Matrices A 1 ,A 2 ,...A n , each A i of size P i-1 x P i Output : Fully parenthesized product A 1 A 2 ...A n that minimizes the number of scalar multiplications A product of matrices is fully parenthesized if it is either 1. a single matrix 2. the product of 2 fully parenthesized matrix products surrounded by parentheses Example of MCM problem: We have 3 matrices and the size of each matrix: A 1 (10 x 100), A 2 (100 x 5), A 3 (5 x 50)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CHAPTER 11 DYNAMIC PROGRAMMING 124 We can fully parenthesized them in two ways: 1. (A 1 (A 2 A 3 )) = 100 x 5 x 50 + 10 * 100 * 50 = 75000 2. ((A 1 A 2 ) A 3 ) = 10 x 100 x 5 + 10 x 5 x 50 = 7500 (10 times better) See how the cost of multiplying these 3 matrices differ significantly. The cost truly depend on the choice of the fully parenthesization of the matrices. However, exhaustively checking all possible parenthesizations take exponential time. Now let's see how MCM problem can be solved using DP. Step 1: characterize the optimal sub-structure of this problem. Let A i..j (i < j) denote the result of multiplying A i A i+1 ..A j . A i..j can be obtained by splitting it into A i..k and A k+1. .j and then multiplying the sub- products. There are j-i possible splits (i.e. k=i,. ..,j-1) Within the optimal parenthesization of A i..j : (a) the parenthesization of A i..k must be optimal (b) the parenthesization of A k+1. .j must be optimal Because if they are not optimal, then there exist other split which is better, and we should choose that split and not this split. Step 2: Recursive formulation Need to find A 1..n Let m[i,j] = minimum number of scalar multiplications needed to compute A i..j Since A i..j can be obtained by breaking it into A i..k A k+1. .j , we have m[i,j] = 0, if i=j = min i<=k<j { m[i,k]+m[k+1,j]+p i-1 p k p j }, if i<j let s[i,j] be the value k where the optimal split occurs. Step 3 Computing the Optimal Costs Matric-Chain-Order(p) n = length[p]-1
Background image of page 2
CHAPTER 11 DYNAMIC PROGRAMMING 125 for i = 1 to n do m[i,i] = 0 for l = 2 to n do for i = 1 to n-l+1 do j = i+l-1 m[i,j] = infinity for k = i to j-1 do q = m[i,k] + m[k+1,j] + pi-1*pk*pj if q < m[i,j] then m[i,j] = q s[i,j] = k return m and s Step 4: Constructing an Optimal Solution Print-MCM(s,i,j) if i=j then print Ai
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Art_of_Programming_Contest_Part7 - CHAPTER 11 DYNAMIC...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online