lecture06 - Introduction to Algorithms...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine
Image of page 1

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 9 L6.2 © 2001 by Charles E. Leiserson Order statistics Select the i th smallest of n elements (the element with rank i ). i = 1 : minimum ; i = n : maximum ; i = ( n +1)/2 or ( n +1)/2 : median . Naive algorithm : Sort and index i th element. Worst-case running time = Θ ( n lg n ) + Θ (1) = Θ ( n lg n ) , using merge sort or heapsort ( not quicksort).
Image of page 2
Introduction to Algorithms Day 9 L6.3 © 2001 by Charles E. Leiserson Randomized divide-and- conquer algorithm R AND -S ELECT ( A , p, q, i ) i th smallest of A [ p . . q ] if p = q then return A [ p ] r R AND -P ARTITION ( A , p, q ) k r p + 1 k = rank( A [ r ]) if i = k then return A [ r ] if i < k then return R AND -S ELECT ( A , p, r – 1 , i ) else return R AND -S ELECT ( A , r + 1 , q, i – k ) A [ r ] A [ r ] r p q k
Image of page 3

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 9 L6.4 © 2001 by Charles E. Leiserson Example pivot i = 7 6 10 13 5 8 3 2 11 k = 4 Select the 7 – 4 = 3 rd smallest recursively. Select the i = 7 th smallest: 2 5 3 6 8 13 10 11 Partition:
Image of page 4
Introduction to Algorithms Day 9 L6.5 © 2001 by Charles E. Leiserson Intuition for analysis Lucky: 1 0 1 log 9 / 10 = = n n C ASE 3 T ( n ) = T (9 n /10) + Θ ( n ) = Θ ( n ) Unlucky: T ( n ) = T ( n – 1) + Θ ( n ) = Θ ( n 2 ) arithmetic series Worse than sorting! (All our analyses today assume that all elements are distinct.)
Image of page 5

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 9 L6.6 © 2001 by Charles E. Leiserson Analysis of expected time Let T ( n ) = the random variable for the running time of R AND -S ELECT on an input of size n , assuming random numbers are independent. For k = 0, 1, …, n –1 , define the indicator random variable X k = 1 if P ARTITION generates a k : n k –1 split, 0 otherwise. The analysis follows that of randomized quicksort, but it’s a little different.
Image of page 6
Introduction to Algorithms Day 9 L6.7 © 2001 by Charles E. Leiserson Analysis (continued) T ( n ) = T (max{0, n –1}) + Θ ( n ) if 0 : n –1 split, T (max{1, n –2}) + Θ ( n ) if 1 : n –2 split, M T (max{ n –1, 0}) + Θ ( n ) if n –1 : 0 split, ( ) = Θ + = 1 0 ) ( }) 1 , (max{ n k k n k n k T X . To obtain an upper bound, assume that the i th element always falls in the larger side of the partition:
Image of page 7

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 9 L6.8 © 2001 by Charles E. Leiserson Calculating expectation ( ) Θ + = = 1 0 ) ( }) 1 , (max{ )] ( [ n k k n k n k T X E n T E Take expectations of both sides.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the 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