lecture_05

lecture_05 - Introduction to Algorithms 6.046J/18.401J...

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

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J Lecture 5 Prof. Piotr Indyk
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 September 22, 2004 L5.2 © Piotr Indyk and Charles Leiserson Today Order statistics (e.g., finding median) Two O(n) time algorithms: – Randomized: similar to Quicksort – Deterministic: quite tricky Both are examples of divide and conquer
Background image of page 2
Introduction to Algorithms September 22, 2004 L5.3 © Piotr Indyk and Charles 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 . How fast can we solve the problem ? Min/max: O(n) General i : O(n log n) by sorting We will see how to do it in O(n) time
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 September 22, 2004 L5.4 © Piotr Indyk and Charles Leiserson Randomized Algorithm for Finding the i th element Divide and Conquer Approach Main idea: P ARTITION x x x x pq k x If i<k , recurse on the left If i>k , recurse on the right Otherwise, output x
Background image of page 4
Introduction to Algorithms September 22, 2004 L5.5 © Piotr Indyk and Charles Leiserson Randomized Divide-and- Conquer R AND -S ELECT ( A , p, r, i ) if p = r then return A [ p ] q R AND -P ARTITION ( A , p, r ) k q p + 1 k =rank( A [ q ]) if i = k then return A [ q ] if i < k then return R AND -S ELECT ( A , p, q – 1 , i ) else return R AND -S ELECT ( A , q + 1 , r, i – k ) A [ q ] A [ q ] A [ q ] A [ q ] q pr k
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 September 22, 2004 L5.6 © Piotr Indyk and Charles 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 6
Introduction to Algorithms September 22, 2004 L5.7 © Piotr Indyk and Charles Leiserson Analysis Lucky: 1 0 1 log 9 / 10 = = n n C ASE 3 T ( n )= T (9 n /10) + Θ ( n ) = Θ ( n ) What is the worst-case running time ?
Background image of page 7

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

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

Page1 / 24

lecture_05 - Introduction to Algorithms 6.046J/18.401J...

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

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