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.

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.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage information during execution of algorithm Comparision-based.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
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
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Spring 2015 Lecture 5: QuickSort & Selection
Median/Order Statistics Algorithms
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
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
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
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?
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
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.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
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
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
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 David Kauchak cs302 Spring 2012.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
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.
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.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
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.
Quick Sort Divide: Partition the array into two sub-arrays
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.
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.
CS 332: Algorithms Quicksort David Luebke /9/2019.
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Chapter 9: Selection of Order Statistics
The Selection Problem.
CS200: Algorithm Analysis
Presentation transcript:

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 algorithm with O(n) expected running time –A cool algorithm of theoretical interest only with O(n) worst-case running time Naive algorithm: Sort and index i-th element T(n) = Θ(nlgn)+Θ(1) = Θ(nlgn) using merge sort or heapsort (not quicksort) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1

Selection in Expected Linear Time Randomized algorithm Divide and conquer Similar to randomized quicksort – Like quicksort: Partitions input array recursively – Unlike quicksort: – Only works on one side of the partition – Quicksort works on both sides of the partition – Expected running times: – SELECT: E[n] = Θ(n) – QUICKSORT: E[n] = Θ(nlgn) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)2

Selection in Expected Linear Time BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)3

Randomized Selection BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)4 Key idea: use partition() from quicksort –But, only need to examine one subarray –This savings shows up in running time: O(n) We will again use a slightly different partition than the book: q = RandomizedPartition(A, p, r)  A[q]  A[q] q pr

Randomized Selection RandomizedSelect(A, p, r, i) if (p == r) then return A[p]; q = RandomizedPartition(A, p, r) k = q - p + 1; if (i == k) then return A[q]; // not in book if (i < k) then return RandomizedSelect(A, p, q-1, i); else return RandomizedSelect(A, q+1, r, i-k); BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)5  A[q]  A[q] k q pr

Randomized Selection ● Analyzing RandomizedSelect() ■ Worst case: partition always 0:n-1 T(n) = T(n-1) + O(n) = O(n 2 ) (arithmetic series) ○ 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! BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)6

Randomized Selection ● Average case ■ For upper bound, assume i th element always falls in larger side of partition: ■ Let’s show that T(n) = O(n) by substitution BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)7 What happened here?

Randomized Selection Assume T(n)  cn for sufficiently large c: BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)8 The recurrence we started with Substitute T(n)  cn for T(k) “Split” the recurrence Expand arithmetic series Multiply it out

Randomized Selection BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)9 What happened here? Subtract c/2 What happened here? Assume T(n)  cn for sufficiently large c: The recurrence so far Multiply it out Rearrange the arithmetic What we set out to prove

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: ■ Generate a good partitioning element ■ Call this element x BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)10

Selection in Worst Case Linear Time SELECT(S, n, i) // return i-th element in set S with n elements if n≤5 then SORT S and return the i-th element DIVIDE S into  n/5  groups first  n/5  groups are of size 5, last group is of size n mod 5 FIND median set M={m 1, …, m  n/5  } m j = median of j-th group x ← SELECT(M,  n/5 ,  n/5  /2  +1) PARTITION set S around the pivot x into L and R if i ≤ |L| then return SELECT(L, |L|, i) else return SELECT(R, n–|L|, i–|L|) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)11

Choosing the pivot BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)12

Analysis BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)13

Analysis BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)14

Analysis BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)15

Selection in Worst Case Linear Time SELECT(S, n, i) // return i-th element in set S with n elements if n≤5 then SORT S and return the i-th element DIVIDE S into  n/5  groups first  n/5  groups are of size 5, last group is of size n mod 5 FIND median set M={m 1, …, m  n/5  } m j = median of j-th group x ← SELECT(M,  n/5 ,  n/5  /2  +1) PARTITION set S around the pivot x into L and R if i ≤ |L| then return SELECT(L, |L|, i) else return SELECT(R, n–|L|, i–|L|) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)16

Selection in Worst Case Linear Time BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)17