Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.

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.
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.
Algorithm Design Techniques: Divide and Conquer. Introduction Divide and conquer – algorithm makes at least two recursive calls Subproblems should not.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Median/Order Statistics Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Prune-and-search Strategy
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.
10/15/2002CSE More on Sorting CSE Algorithms Sorting-related topics 1.Lower bound on comparison sorting 2.Beating the lower bound 3.Finding.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
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 McGraw-Hill Companies, Inc., Chapter 6 Prune-and-Search Strategy.
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
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
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.
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.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
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.
CSCI 256 Data Structures and Algorithm Analysis Lecture 10 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
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.
Randomized Algorithms
Divide and Conquer.
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.
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.
CSE 373 Data Structures and Algorithms
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.
The Selection Problem.
Richard Anderson Lecture 14 Divide and Conquer
CS200: Algorithm Analysis
Medians and Order Statistics
Presentation transcript:

Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Linear Time Selection These lecture slides are adapted from CLRS 10.3.

2 Where to Build a Road? Given (x,y) coordinates of N houses, where should you build road parallel to x-axis to minimize construction cost of building driveways?

3 Where to Build a Road? Given (x,y) coordinates of N houses, where should you build road parallel to x-axis to minimize construction cost of building driveways? n n 1 = nodes on top. n n 2 = nodes on bottom Decreases total cost by (n 2 -n 1 )   1210

4 Where to Build a Road? Given (x,y) coordinates of N houses, where should you build road parallel to x-axis to minimize construction cost of building driveways? n n 1 = nodes on top. n n 2 = nodes on bottom

5 Where to Build a Road? Given (x,y) coordinates of N houses, where should you build road parallel to x-axis to minimize construction cost of building driveways? n n 1 = nodes on top. n n 2 = nodes on bottom

6 Where to Build a Road? Given (x,y) coordinates of N houses, where should you build road parallel to x-axis to minimize construction cost of building driveways? Solution: put street at median of y coordinates

7 Order Statistics Given N linearly ordered elements, find i th smallest element. n Min: i = 1 n Max: i = N n Median: i =  (N+1) / 2  and  (N+1) / 2  n O(N) for min or max. n O(N log N) comparisons by sorting. n O(N log i) with heaps. Can we do in worst-case O(N) comparisons? n Surprisingly, yes. (Blum, Floyd, Pratt, Rivest, Tarjan, 1973) Assumption to make presentation cleaner. n All items have distinct values.

8 Select Similar to quicksort, but throw away useless "half" at each iteration. n Select i th smallest element from a 1, a 2,..., a N. x  Partition(N, a 1, a 2,..., a N ) k  rank(x) if (i == k) return x else if (i < k) b[]  all items of a[] less than x return Select(i th, k-1, b 1, b 2,..., b k-1 ) else if (i > k) c[]  all items of a[] greater than x return Select((i-k) th, N-k, c 1, c 2,..., c N-k ) Select (i th, N, a 1, a 2,..., a N ) x = partition element Want to choose x so that x is (roughly) the ith largest.

9 Partition Partition(). n Divide N elements into  N/5  groups of 5 elements each, plus extra N = 54

Partition Partition(). n Divide N elements into  N/5  groups of 5 elements each, plus extra. n Brute force sort each of the 5-element groups

11 28 Partition Partition(). n Divide N elements into  N/5  groups of 5 elements each, plus extra. n Brute force sort each of the 5-element groups. Find x = "median of medians" by Select() on  N/5  medians

12 Select if (N is small) use mergesort Divide a[] into groups of 5, and let m 1, m 2,..., m N/5 be list of medians. x  Select(N/10, m 1, m 2,..., m N/5 ) k  rank(x) if (i == k) // Case 1 return x else if (i < k) // Case 2 b[]  all items of a[] less than x return Select(i th, k-1, b 1, b 2,..., b k-1 ) else if (i > k) // Case 3 c[]  all items of a[] greater than x return Select((i-k) th, N-k, c 1, c 2,..., c N-k ) Select (i th, N, a 1, a 2,..., a N ) median of medians

median of medians Selection Analysis Crux of proof: delete roughly 30% of elements by partitioning. n At least 1/2 of 5 element medians  x – at least   N / 5  / 2  =  N / 10  medians  x

median of medians Selection Analysis Crux of proof: delete roughly 30% of elements by partitioning. n At least 1/2 of 5 element medians  x – at least   N / 5  / 2  =  N / 10  medians  x n At least 3  N / 10  elements  x.

Selection Analysis Crux of proof: delete roughly 30% of elements by partitioning. n At least 1/2 of 5 element medians  x – at least   N / 5  / 2  =  N / 10  medians  x n At least 3  N / 10  elements  x. n At least 3  N / 10  elements  x. median of medians

16 Selection Analysis Crux of proof: delete roughly 30% of elements by partitioning. n At least 1/2 of 5 element medians  x – at least   N / 5  / 2  =  N / 10  medians  x n At least 3  N / 10  elements  x. n At least 3  N / 10  elements  x.  Select() called recursively (Case 2 or 3) with at most N - 3  N / 10  elements. C(N) = # comparisons on a file of size N. Now, solve recurrence. n Apply master theorem? n Assume N is a power of 2? n Assume C(N) is monotone non-decreasing?

17 Selection Analysis Analysis of selection recurrence. n T(N) = # comparisons on a file of size  N. n T(N) is monotone, but C(N) is not! Claim: T(N)  20cN. n Base case: N < 50. n Inductive hypothesis: assume true for 1, 2,..., N-1. n Induction step: for N  50, we have: For n  50, 3  N / 10   N / 4.

18 Linear Time Selection Postmortem Practical considerations. n Constant (currently) too large to be useful. n Practical variant: choose random partition element. – O(N) expected running time ala quicksort. n Open problem: guaranteed O(N) with better constant. Quicksort. n Worst case O(N log N) if always partition on median. n Justifies practical variants: median-of-3, median-of-5.