HW6_sol - number of comparisons is six. Figure 1: Finding...

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

View Full Document Right Arrow Icon
Fall Semester 2008 CS300 Algorithms Solution #6 1. (a) Function one-of-k-smallest(A: array, n, k: integer):integer; min :=A[1] for i=2 to n-k+1 do if A[i] < min then min := A[i] end if end for return min This algorithm finds the smallest among n - k + 1 elements and needs n - k com- parisons. (b) We can prove that the lower bound for this problem needs also n - k comparisons. Suppose there is an algorithm that can find one of the k smallest keys, say v , with less than n - k comparisons. However, this algorithm can produce at most n - k - 1 winners (i.e., keys found to be larger with at least one comparison). Among the other k + 1 non-winners, we can assign values smaller than v to at least k keys. Therefore v is not one of the k smallest keys, which is a contradiction. 2. In case of 5 numbers as below figure, you will find three numbers which can not be the median after five comparisons. These are ones that have more than three numbers larger or less than themselves. So, the number of median candidates are two (red circles in the figure), and we would determine the median by comparing these two. Therefore, the total
Background image of page 1

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

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

Unformatted text preview: number of comparisons is six. Figure 1: Finding the median of five numbers 1 3. The recurrence is defined as following: T ( n ) = ( O (1) if n < = 9 T ‡ 2 n 3 · + T ‡ n 3 · + cn otherwise where c is a positive constant. We solve this recursion by induction. Assume that T ( n ) ≥ dn lg n , when n ≤ k and d is a positive constant. As a base case, when n = 9, we take a hidden constant for O (1) as C such that C ≥ 9 d lg 9. Then, for n = k + 1, T ( k + 1) ≥ T ‡ 2( k + 1) 3 · + T ‡ ( k + 1) 3 · + c ( k + 1) ≥ d ‡ 2( k + 1) 3 · lg ‡ 2( k + 1) 3 · + d ‡ ( k + 1) 3 · lg ‡ ( k + 1) 3 · + c ( k + 1) = d ‡ 2( k + 1) 3 · (lg( k + 1)-lg 3 2 ) + d ‡ ( k + 1) 3 · (lg( k + 1)-lg 3) + c ( k + 1) = d ( k + 1)lg( k + 1)-d ‡ 2( k + 1) 3 lg 3 2 + k + 1 3 lg 3 · + c ( k + 1) = d ( k + 1)lg( k + 1)-d ( k + 1)(lg 3-2 3 ) + c ( k + 1) ≥ d ( k + 1)lg( k + 1) . Therefore, this recurrence belongs Ω( n lg n ) as long as d ≤ c log 3-2 / 3 . 2...
View Full Document

This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

Page1 / 2

HW6_sol - number of comparisons is six. Figure 1: Finding...

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