1 Medians and Order Statistics CLRS Chapter 9. upper median 341314232741 lower median 546575 The lower median is the -th order statistic The upper median.

Slides:



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

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.
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.
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.
Medians and Order Statistics
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.
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
1 Today’s Material Medians & Order Statistics – Ch. 9.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
SELECTION CS16: Introduction to Data Structures & Algorithms Tuesday, March 3,
Median Finding, Order Statistics & Quick Sort
Spring 2015 Lecture 5: QuickSort & Selection
Median/Order Statistics Algorithms
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Selection: Find the ith number
Analysis of Algorithms CS 477/677
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
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.
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.
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.
Order Statistics(Selection Problem)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Lecture 10. Paradigm #8: Randomized Algorithms Back to the “majority problem” (finding the majority element in an array A). FIND-MAJORITY(A, n) while (true)
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.
1 Sorting an almost sorted input Suppose we know that the input is “almost” sorted Let I be the number of “inversions” in the input: The number of pairs.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
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.
Randomized Quicksort (8.4.2/7.4.2) Randomized Quicksort –i = Random(p, r) –swap A[p]  A[i] –partition A(p, r) Average analysis = Expected runtime –solving.
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
Sorting We have actually seen already two efficient ways to sort:
Order Statistics(Selection Problem)
Dr. Yingwu Zhu Chapter 9, p Linear Time Selection Dr. Yingwu Zhu Chapter 9, p
Randomized Algorithms
Medians and Order Statistics
Topic: Divide and Conquer
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
CS 3343: Analysis of Algorithms
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.
Chapter 9: Medians and Order Statistics
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
The Selection Problem.
CS200: Algorithm Analysis
Presentation transcript:

1 Medians and Order Statistics CLRS Chapter 9

upper median lower median The lower median is the -th order statistic The upper median is the -th order statistic If n is odd, lower and upper median are the same What Are Order Statistics? 8th order statistic The k-th order statistic is the k-th smallest element of an array.

3 What are Order Statistics? Selecting i th -ranked item from a collection. –First:i = 1 –Last:i = n –Median(s):i =

4 Order Statistics Overview Assume collection is unordered, otherwise trivial. find ith order stat = A[i] Can sort first –  (n lg n), but can do better –  (n). I can find max and min in  (n) time (obvious) Can we find any order statistic in linear time? (not obvious!)

5 Order Statistics Overview How can we modify Quicksort to obtain expected-case  (n)? ? ? Pivot, partition, but recur only on one set of data. No join.

6 Using the Pivot Idea Randomized-Select(A[p..r],i) looking for ith o.s. if p = r return A[p] q <- Randomized-Partition(A,p,r) k <- q-p+1 the size of the left partition if i=kthen the pivot value is the answer return A[q] else if i < kthen the answer is in the front return Randomized-Select(A,p,q-1,i) elsethen the answer is in the back half return Randomized-Select(A,q+1,r,i-k)

7 Randomized Selection Analyzing RandomizedSelect() –Worst case: partition always 0:n-1 T(n) = T(n-1) + O(n) = O(n 2 ) No better than sorting! –“Best” case: suppose a 9:1 partition T(n) = T(9n/10) + O(n) = O(n)(Master Theorem, case 3) Better than sorting! –Average case: O(n) remember from quicksort

8 Worst-Case Linear-Time Selection Randomized algorithm works well in practice What follows is a worst-case linear time algorithm, really of theoretical interest only Basic idea: –Guarantee a good partitioning element –Guarantee worst-case linear time selection Warning: Non-obvious & unintuitive algorithm ahead! Blum, Floyd, Pratt, Rivest, Tarjan (1973)

9 Worst-Case Linear-Time Selection The algorithm in words: 1.Divide n elements into groups of 5 2.Find median of each group (How? How long?) 3.Use Select() recursively to find median x of the  n/5  medians 4.Partition the n elements around x. Let k = rank(x) 5.if (i == k) then return x if (i k) use Select() recursively to find (i-k)th smallest element in last partition

10 Order Statistics: Algorithm Select(A,n,i): Divide input into groups of size 5. /* Partition on median-of-medians */ medians = array of each group’s median. pivot = Select(medians,, ) Left Array L and Right Array G = partition(A, pivot) /* Find i th element in L, pivot, or G */ k = |L| + 1 If i=k, return pivot If i<k, return Select(L, k-1, i) If i>k, return Select(G, n-k, i-k) O(n) T(n) O(n) T( ) O(n) O(1) T(k) T(n-k) Only one done. All this to find a good split.

11 Order Statistics: Analysis #less#greater How to simplify?

12 Order Statistics: Analysis One group of 5 elements. Median Greater Elements Lesser Elements

13 Order Statistics: Analysis All groups of 5 elements. (And at most one smaller group.) Median of Medians Greater Medians Lesser Medians

14 Order Statistics: Analysis Definitely Lesser Elements Definitely Greater Elements

15 Order Statistics: Analysis 1 Must recur on all elements outside one of these boxes. How many?

16 Order Statistics: Analysis 1 At most full groups of 5 partial groups of 2 Count elements outside smaller box.

17 Order Statistics: Analysis A very unusual recurrence. How to solve? ? ?

18 Order Statistics: Analysis Substitution: Prove. when choose c,d such that Overestimate ceiling Algebra

19 Order Statistics Why groups of 5? Sum of two recurrence sizes must be < 1. Grouping by 5 is smallest size that works. ? ?