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: COT 5405 Analysis of Algorithms, Spring 2010. Homework 3 Due Friday, April 2 2010, 12:00pm. Notes You may hand the homework to TAs during office hours. You may also hand the homework to Prof. Ranka in class. No late submissions. To force you to write succinctly, we have enforced page limits. These are noted in front of each question. Answer each question on a fresh page. If the problem necessitates writing an algorithm, you must first informally describe the algorithm, in brief, in a paragraph. You can choose to follow this up with pseudocode that formally describes the algorithm. We will peruse your pseudocode only if your English description is not clear. Write your name on the top right hand corner of your homework. Be sure to write your last name as the last word in your name. If you are designing an algorithm, you must write a formal proof of correctness. Please write legibly. 1 1. [1 page] [15pts] You are given n matrices M 1 ,M 2 ,...,M n of sizes s s 1 ,s 1 s 2 ,...,s n- 1 s n re- spectively. Suppose you want to compute their product M 1 M 2 ... M n . Matrix multipli- cation is not commutative (i.e. M 1 M 2 6 = M 2 M 1 ), but we do know it is associative (i.e. M 1 ( M 2 M 3 ) = ( M 1 M 2 ) M 3 ). It turns out that the order in which you compute the product is important because for some such orders, the number of operations (i.e. number of multiplications) required is less than that for other orders. For example, if you have four matrices M 1 ,M 2 ,M 3 ,M 4 of sizes 40 25 , 25 1 , 1 11 , 11 101 respectively, then ( M 1 M 2 ) ( M 3 M 4 ) is the best ordering as it takes only (40 25 1) + (1 11 101) + (40 1 101) = 6151 multiplications. The ordering ( M 1 (( M 2 M 3 ) M 4 )) is the worst. Remember that multiplying a matrix of size a b with another of size b c takes O ( abc ) multiplications using the naive method (we arent going to consider any faster algorithms like Strassens method). It turns out that for this problem, the greedy approach of choosing the available matrix pair whose product is the cheapest to compute, is suboptimal. Give an example to illustrate this. Next, prepare an efficient dynamic programming strategy to find the best possible ordering to compute the product of the n given matrices. Write a recurrence relation, explain the algorithm and then write a non-recursive pseudocode. Analyze the complexity of your algorithm. (It turns out that there is an O ( n 3 ) algorithm for this problem). Solution: Counterexample for greedy strategy: In the example given in the problem, the pair M 23 = M 2 M 3 is the cheapest, needing only 25 11 multiplications. This is followed by M 123 = M 1 M 23 takes 40 25 11 multiplications and lastly M 123 M 4 takes another 40 11 101 multiplications. The total is 55715 (suboptimal)....
View Full Document
- Fall '08