©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Introduction to Algorithms
October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Introduction to Algorithms
October 17, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE9 Randomly built binary.
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 2 Prof. Erik Demaine.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Introduction to Algorithms
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 17 L9.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Chapter 9 continued: Quicksort
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Recurrences : 1 Chapter 3. Growth of function Chapter 4. Recurrences.
VOORBLAD.
25 seconds left…...
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Spring 2015 Lecture 5: QuickSort & Selection
Median/Order Statistics Algorithms
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Analysis of Algorithms CS 477/677
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Introduction to Algorithms Jiafen Liu Sept
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Order Statistics(Selection Problem)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1 Medians and Order Statistics CLRS Chapter 9. upper median lower median The lower median is the -th order statistic The upper median.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Introduction to Algorithms Prof. Charles E. Leiserson
Linear-Time Sorting Continued Medians and Order Statistics
Randomized Algorithms
Order Statistics(Selection Problem)
Randomized Algorithms
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
The Selection Problem.
CS200: Algorithm Analysis
Presentation transcript:

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.2 Order statistics Select the ith smallest of n elements (the element with rank i). Naive algorithm: Sort and index ith element. Worst-case running time using merge sort or heapsort (not quicksort).

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.3 Randomized divide-and-conquer algorithm

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.4 Example Select the i = 7th smallest : Partition: Select the 7 – 4 = 3rd smallest recursively.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.5 Intuition for analysis (All our analyses today assume that all elements are distinct.) Lucky: Unlucky: arithmetic series Worse than sorting!

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.6 Analysis of expected time The analysis follows that of randomized quicksort, but its a little different. Let T(n) = the random variable for the running time of RAND-SELECT on an input of size n, assuming random numbers are independent. For k = 0, 1, …, n–1, define the indicator random variable if PARTITION generates a k : n–k–1 split, otherwise.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.7 Analysis (continued) To obtain an upper bound, assume that the ith element always falls in the larger side of the partition:

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.8 Calculating expectation Take expectations of both sides.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.9 Calculating expectation Linearity of expectation.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.10 Calculating expectation Independence of from other random choices.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.11 Calculating expectation Linearity of expectation;

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.12 Upper terms appear twice. Calculating expectation

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.13 Hairy recurrence (But not quite as hairy as the quicksort one.) The constant c can be chosen large enough so that E[T(n)] cn for the base cases. Prove: E[T(n)] cn for constant c > 0.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.14 Substitution method Substitute inductive hypothesis.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.15 Substitution method Use fact.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.16 Substitution method Express as desired – residual.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.17 Substitution method if c is chosen large enough so that cn/4 dominates the

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.18 Summary of randomized order-statistic selection Works fast: linear expected time. Excellent algorithm in practice. But, the worst case is very bad: Is there an algorithm that runs in linear time in the worst case?.Yes, due to Blum, Floyd, Pratt, Rivest, and Tarjan [1973]. IDEA: Generate a good pivot recursively.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.19 Worst-case linear-time order statistics 1. Divide the n elements into groups of 5. Find the median of each 5-element group by rote. 2. Recursively S ELECT the median x of the n/5 group medians to be the pivot. 3. Partition around the pivot x. Let k = rank(x). 4. if i = k then return x elseif i < k then recursively S ELECT the ith smallest element in the lower part else recursively S ELECT the (i–k)th smallest element in the upper part

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.20 Choosing the pivot

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.21 Choosing the pivot 1. Divide the n elements into groups of 5.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L Divide the n elements into groups of 5. Find the median of each 5-element group by rote. Choosing the pivot

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.23 Choosing the pivot 1. Divide the n elements into groups of 5. Find the median of each 5-element group by rote. 2. Recursively S ELECT the median x of the n/5 group medians to be the pivot.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.24 Analysis At least half the group medians are x, which is at least n/5 /2 = n/10 group medians.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.25 Analysis (Assume all elements are distinct.) At least half the group medians are x, which is at least n/5 /2 = n/10 group medians. Therefore, at least 3 n/10 elements are x.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.26 Analysis (Assume all elements are distinct.) At least half the group medians are x, which is at least n/5 /2 = n/10 group medians. Therefore, at least 3 n/10 elements are x. Similarly, at least 3 n/10 elements are x.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.27 Minor simplification For n 50, we have 3 n/10 n/4. Therefore, for n 50 the recursive call to S ELECT in Step 4 is executed recursively on 3n/4 elements. Thus, the recurrence for running time can assume that Step 4 takes time T(3n/4) in the worst case. For n < 50, we know that the worst-case time is T(n) = Θ(1).

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.28 Developing the recurrence S ELECT (i, n) 1. Divide the n elements into groups of 5. Find the median of each 5-element group by rote. 2. Recursively S ELECT the median x of the n/5 group medians to be the pivot. 3. Partition around the pivot x. Let k = rank(x). 4. if i = k then return x elseif i < k then recursively S ELECT the ith smallest element in the lower part else recursively S ELECT the (i–k)th smallest element in the upper part

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.29 Solving the recurrence if c is chosen large enough to handle both the Θ(n) and the initial conditions.

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.30 Conclusions Since the work at each level of recursion is a constant fraction (19/20) smaller, the work per level is a geometric series dominated by the linear work at the root. In practice, this algorithm runs slowly, because the constant in front of n is large. The randomized algorithm is far more practical. Exercise: Why not divide into groups of 3?