This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ECE608, Homework #8 Solution (1) CLR 15.22 MATRIX CHAIN MULTIPLY ( A,s,i,j ) 1. if i = j 2. then return A i 3. else X = MATRIX CHAIN MULTIPLY ( A,s,i,s [ i,j ]) 4. Y = MATRIX CHAIN MULTIPLY ( A,s,s [ i,j ] + 1 ,j ) 6. return MATRIXMULTIPLY ( X,Y ) (2) CLR 15.25 Each time the lloop executes, the iloop executes n l +1 times. Each time the iloop executes, the kloop executes j i = l 1 times, each time referencing m twice to create an entry. Thus the total number of times m is referenced is n X l =2 ( n l + 1)( l 1)2. n X i =1 n X j = i R ( i,j ) = n X l =2 ( n l + 1)( l 1)2 let k = l 1 = 2 n 1 X k =1 ( n k ) k = 2 n 1 X k =1 nk 2 n 1 X k =1 k 2 = 2 n ( n 1) n 2 2 ( n 1) n (2 n 1) 6 = n 3 n 2 2 n 3 3 n 2 + n 3 = n 3 n 3 (3) CLR 15.26 Given that parentheses group two matrices at a time, there must be exactly n 1 pairs of parentheses in a full parenthesization of an nelement expression. To see this, consider arbitrarily parenthesizing two matrices in a chain. This effectively replaces the two grouped matrices with one multiplied matrix. This process of grouping two elements can occur n 1 times before we are left with a single item. 1 (4) CLR 15.31 Running RecursiveMatrixChain is asymptotically more efficient than enumer ating all the ways of parenthesizing the product and computing the number of mul tiplications for each. Consider the treatment of subproblems by each approach: (i) For each possible place to split the matrix chain, the enumeration approach finds all ways to parenthesize the left half, finds all ways to parenthesize the right half, and looks at all possible combinations of the left half with the right half. The amount of work to look at each combination of left and right half subproblem results is thus the product of the number of ways to parenthesize the left half and the number of ways to parenthesize the right half. (ii) For each possible place to split the matrix chain, RecursiveMatrixChain finds the best way to parenthesize the left half, finds the best way to parenthesize the right half, and combines just those two results. Thus the amount of work to combine the left and right half subproblem results is O (1). Section 15.2 showed that the running time for enumeration is ( 4 n n 3 / 2 ). We will show that the running time for RecursiveMatrixChain is O ( n 3 n 1 ). To obtain an upper bound on the running time of RecursiveMatrixChain , we shall use a similar approach as was used in Section 15.3 to obtain a lower bound. First, we derive an upper bound recurrence and then use substitution to prove the O bound. For the lowerbound recurrence, the book assumed that the execution of lines 12 and 67 take at least unit time each. For the upperbound recurrence, well assume those pairs of lines each take at most constant time c . This yields the recurrence T (1) c and T ( n ) c + n 1 k =1 ( T ( k ) + T ( n k ) + c ), for n &gt; 1....
View Full
Document
 Fall '11
 PAI

Click to edit the document details