hw9sol - CS 473 Homework 9 Solutions Spring 2010 1. Let k...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: CS 473 Homework 9 Solutions Spring 2010 1. Let k be an arbitrary fixed integer. We say that an array A [ 1.. n ] is k-sorted if it can be divided into k blocks, each of size n / k , such that the elements in each block are larger than the elements in earlier blocks, and smaller than elements in later blocks. The elements within each block need not be sorted. You can assume that n / k is an integer. (a) Describe an algorithm that k-sorts an arbitrary array in time O ( n log k ) . Solution: Without loss of generality, we can assume that k is a power of 2; otherwise, we can add new entries to the end of the input array, each with value . We use the following recursive algorithm. S ELECT ( A , r ) returns the index of the r th smallest element of the array A in O ( n ) time. P ARTITION ( A , p ) is the partition subroutine from quicksort, which also runs in O ( n ) time. B LOCK S ORT ( A [ 1.. n ] , k ) : if k = 1 return A r n / 2 P ARTITION ( A ,S ELECT ( A , r )) B LOCK S ORT ( A [ 1.. r ] , k / 2 ) B LOCK S ORT ( A [ r + 1, .. n ] , k / 2 ) Let T ( k , ) be the time required to partition an array of size k into k blocks of length . This function satisfies the recurrence T ( k , ) ( if k = 1 2 T ( k / 2, ) + O ( k ) otherwise The solution to this recurrence is T ( k , ) = O ( k log k ) . The running time of our algorithm is T ( k , n / k ) = O ( n log k ) . Rubric: 2 points max = 1 for the algorithm + 1 for analysis (b) Prove that any comparison-based k-sorting algorithm requires ( n log k ) comparisons in the worst case. Solution (leaf counting): Consider any decision tree that represents a k-sorting algorithm. Each leaf is labeled with a vector ( b 1 , b 2 ,..., b n ) , where each b i is an integer between 1 and k and indicates which output block contains A [ i ] . The number of possible output vectors is n n / k n / k ... = n ! ( n / k ) ! k ( n / e ) n (( n / ke ) n / k ) k = k n so any decision tree must have at least that many leaves. Thus, the depth of any decision tree must be at least log 2 k n = ( n log k ) . Solution (reduction): Suppose there were a comparison-based k-sorting algorithm that ran in o ( n log k ) time. Combining this algorithm with the solution to part (c) gives us a comparison-based sorting algorithm that runs in O ( n log ( n / k )) + o ( n log k ) = o ( n log n ) time, which is impossible. Rubric: 3 points max = 1 for identifying the decision tree output + 1 for counting possible outputs + 1 for remaining details. Max 1 point for a solution that only works when k = n or k = 1 . The reduction is also worth full credit....
View Full Document

Page1 / 6

hw9sol - CS 473 Homework 9 Solutions Spring 2010 1. Let k...

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

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