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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern