12.07.2015 Views

Divide and Conquer Algorithms Homework 5

Divide and Conquer Algorithms Homework 5

Divide and Conquer Algorithms Homework 5

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Divide</strong> <strong>and</strong> <strong>Conquer</strong> <strong>Algorithms</strong> <strong>Homework</strong> 5February 16, 20051. Write an algorithm that searches a sorted list of n items by dividing itinto three subsets of almost n 3items. This algorithm finds the sublist thatmight contains the given item <strong>and</strong> divides it into three smaller sublists ofalmost equal size. The algorithm repeats this process until it finds the itemor concludes that the item is not in the list. Analyze your algorithm <strong>and</strong>give the results using order notation. How does its performance compareto binary search?This algorithm is similar to binary search <strong>and</strong> has complexity in Θ(log 3 n).1Sincelg 3 lg n ≡ log 3 n, this algorithm is also in Θ(lg n). It does notimprove on the basic complexity of binary search, <strong>and</strong> it may actually runslower than binary search since it requires an extra comparison to figureout which partition to recurse on.2. Use the divide-<strong>and</strong>-conquer approach to write an algorithm that finds thelargest item in a list of n items. Analyze your algorithm, <strong>and</strong> show theresults in order notation.If you sort the list first, then you get Θ(n lg n), which is worst than sequentialsearch. A possible algorithm is to subdivide the list in 2 at each level.When the list is size two or one, then return the maximum value. Recombinethe values that come up through the call tree until you have the actualmaximum value at the top. The recurrence relation is T (n) = 2T ( n 2 ) + 1since we always have 2 branches that are half as big, <strong>and</strong> the comparisonis a constant cost. This recurrence give Θ(n) by the master theorem onpage 224 in the book (l = 2, b = 2, <strong>and</strong> k = 0).3. Use quicksort to sort the following list: 123 34 189 56 150 12 9 240. Givethe tree of recursive calls like we did in class showing the list at each stepof the recursion.4. Use Strassen’s algorithm to compute c 11 in the multiplication of these 2matrices: ⎛⎞ ⎛⎞1 2 3 48 9 1 2A = ⎜ 5 6 7 8⎟⎝ 9 1 2 3 ⎠ B = ⎜ 3 4 5 6⎟⎝ 7 8 9 1 ⎠4 5 6 72 3 4 51


The termination condition on the recursion is when A <strong>and</strong> B are both1 × 1 matrices.( ) ( )1 2 8 9m 2 = ×5 6 3 4( ) ( )3 4 7 8m 3 = ×7 8 2 3We need to now repeat this process for m 2 <strong>and</strong> m 3 since these matricesare not yet at your base case. This time, however, we need to generate allseven expressions so we can compute the 4 × 4 matrix multiplication. Form 2m 1 = (5 + 6 − 1)(4 − 9 + 8)= 30m 2 = 8m 3 = 6.After we get the results of the two 4 × 4 matrix multiplication for m 2 <strong>and</strong>m 3 we combine them results for c 11 :c 11 = m 2 + m( 3) ( )14 17 27 33=+55 78 63 77( ) 41 50=118 1555. How many multiplications would be performed in find the product of two64 × 64 matrices using the st<strong>and</strong>ard algorithm?C i j =64∑k=1= 64A ik B kjWe need to compute 64 2 entries. So total number is 64 3 .6. How many multiplications are required in finding the product of two 64×64matrices using the Strassen’s algorithm with 2 × 2 matrices as the basecase solved in the traditional way?This is best solved using the recurrence (we set g(n) to 0 since we are onlycounting multiplications):T (n) = 7 ∗ T ( n 2 ) + g(n)2


T (64) = 7 ∗ T (32) = 134456T (32) = 7 ∗ T (16) = 19208T (16) = 7 ∗ T (8) = 2744T (8) = 7 ∗ T (4) = 392T (4) = 7 ∗ T (2) = 56T (2) = 8;3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!