lecture06 - Introduction to Algorithms...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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).
Background 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 ] A [ r ] A [ r ] r pq k
Background image of page 3

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

View Full DocumentRight Arrow Icon
Introduction to Algorithms Day 9 L6.4 © 2001 by Charles E. Leiserson Example pivot i = 7 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 k = 4 Select the 7 – 4 = 3 rd smallest recursively. Select the i = 7 th smallest: 2 2 5 5 3 3 6 6 8 8 13 13 10 10 11 11 Partition:
Background 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.)
Background image of page 5

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

View Full DocumentRight 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.
Background 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:
Background image of page 7

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

View Full DocumentRight Arrow Icon