1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 More Sorting; Searching Dan Barrish-Flood. 2 Bucket Sort Put keys into n buckets, then sort each bucket, then concatenate. If keys are uniformly distributed.
CSE 3101: Introduction to the Design and Analysis of Algorithms
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.
Prune-and-Search Method
Medians and Order Statistics
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Median/Order Statistics Algorithms
The Selection Problem. The selection problem Input: A set S of n elements Output: The k-th smallest element of S The median problem: to find the -th smallest.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Selection: Find the ith number
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Median and Order Statistics Jeff Chastine. Medians and Order Statistics The i th order statistic of a set of n elements is the i th smallest element The.
10/15/2002CSE More on Sorting CSE Algorithms Sorting-related topics 1.Lower bound on comparison sorting 2.Beating the lower bound 3.Finding.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
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?
1 Summary of lectures 1.Introduction to Algorithm Analysis and Design (Chapter 1-3). Lecture SlidesLecture Slides 2.Recurrence and Master Theorem (Chapter.
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.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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.
Analysis of Algorithms CS 477/677
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.
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Order Statistics(Selection Problem)
Deterministic and Randomized Quicksort Andreas Klappenecker.
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 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
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.
Linear-Time Sorting Continued Medians and Order Statistics
Randomized Algorithms
Order Statistics(Selection Problem)
Randomized Algorithms
Medians and Order Statistics
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
Lecture 15, Winter 2019 Closest Pair, Multiplication
Chapter 9: Selection of Order Statistics
The Selection Problem.
Quicksort and Randomized Algs
CS200: Algorithm Analysis
Medians and Order Statistics
Presentation transcript:

1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection problem Minimum and maximum Median, lower median, upper median Selection in expected/average linear time Selection in worst-case linear time

2 O(nlg n) Algorithm Suppose n elements are sorted by an O(nlg n) algorithm, e.g., MERGE-SORT –Minimum: the first element –Maximum: the last element –The ith order statistic: the ith element. –Median: If n is odd, then ((n+1)/2)th element. If n is even, –then (  (n+1)/2  )th element, lower median –then (  (n+1)/2  )th element, upper median All selections can be done in O(1), so total: O(nlg n). Can we do better?

3 Selection in Expected Linear Time O(n) Select ith element A divide-and-conquer algorithm RANDOMIZED- SELECT Similar to quicksort, partition the input array recursively Unlike quicksort, which works on both sides of the partition, just work on one side of the partition. –Called prune-and-search, prune one side, just search the other side). (Please review or read quicksort in chapter 7.)

4 RANDOMIZED-SELECT(A,p,r,i) 1.if p=r then return A[p] 2.q  RANDOMIZED-PARTITION(A,p,r) 3.//the q holds for A[p,q-1]  A[q]  A[q+1,r] 4.k  q-p+1 5.if i=k then return A[q] 6. else if i<k 7. then return RANDOMIZED-SELECT(A,p,q-1,i) 8. else return RANDOMIZED-SELECT(A,q+1,r,i-k)

5 Analysis of RANDOMIZED-SELECT Worst-case running time  (n 2 ), why??? it may be unlucky and always partition into A[q], an empty side and a side with remaining elements. So every partitioning of m elements will take  (m) time, and m=n,n-1,…,2. Thus total is  (n)+  (n-1)+…+  (2)=  (n(n+1)/2-1) =  (n 2 ). Moreover, no particular input elicits the worst-case behavior, Because of “randomness”. But in average, it is good. By using probabilistic analysis/random variable, it can be proven that the expected running time is O(n). (ref. to page 187). Can we do better, such that O(n) in worst case??

6 Selection in worst case linear time O(n) Select the ith smallest element of S={a 1, a 2,…, a n } Use so called prune-and-search technique: –Let x  S, and partition S into three subsets –S 1 ={a j | a j x} –If | S 1 |>i, search ith smallest element in S 1 recursively, (prune S 2 and S 3 away) –Else If | S 1 |+| S 2 |>i, then return x (the ith smallest element) –Else search (i-(| S 1 |+| S 2 |))th in S 3 recursively, (prune S 1 and S 2 away) The question is how to select x such that S 1 and S 3 are nearly equal. ?

7 The Way to Select x Divide elements into  n/5  groups of 5 elements each. Find the median of each group Find the median of the medians At least (3n/10)-6 elements >x At least (3n/10)-6 elements <x Because each of 1/2  n/5  -2 groups contributes 3 elements which are  x

8 SELECT ith Element in n Elements) 1.Divide n elements into  n/5  groups of 5 elements. 2.Find the median of each group. 3.Use SELECT recursively to find the median x of the above  n/5  medians. 4.Partition n elements around x into S 1, S 2, and S 3. 5.If |S 1 |>i, search ith smallest element in S 1 recursively, Else If |S 1 |+|S 2 |>i, then return x (the ith smallest element) Else search (i-(|S 1 |+|S 2 |))th in S 3 recursively,

9 Analysis of SELECT (cont.) Steps 1,2,4 take O(n), Step 3 takes T(  n/5  ). Let us see step 5: –At least half of medians in step 2 are  x, thus at least 1/2  n/5  -2 groups contribute 3 elements which are  x. i.e, 3(  1/2  n/5   -2)  (3n/10)-6. –Similarly, the number of elements  x is also at least (3n/10)-6. –Thus, |S 1 | is at most (7n/10)+6, similarly for |S 3 |. –Thus SELECT in step 5 is called recursively on at most (7n/10)+6 elements. Recurrence is: –T(n)= O(1) if n< some value (i.e. 140) – T(  n/5  )+T(7n/10+6)+O(n) if n  the value (i.e, 140)

10 Solve recurrence by substitution Suppose T(n)  cn, for some c. T(n)  c  n/5  + c(7n/10+6) + an –  cn/5+ c + 7/10cn+6c + an – = 9/10cn+an+7c – =cn+(-cn/10+an+7c) –Which is at most cn if -cn/10+an+7c<0. –i.e., c  10a(n/(n-70)) when n>70. –So select n=140, and then c  20a. –Note: n may not be 140, any integer >70 is OK.

11 Summary Bucket sort, counting sort, radix sort: –Their running times, –Modifications The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements: –Minimum and maximum. –Median, lower median, upper median Selection in expected/average linear time –Worst case running time –Prune-and-search Selection in worst-case linear time: –Why group size 5?