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.

Slides:



Advertisements
Similar presentations
Linear-time Median Def: Median of elements A=a 1, a 2, …, a n is the (n/2)-th smallest element in A. How to find median? sort the elements, output the.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Rank Rank of an element is its position in ascending key order. [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7.
Median Finding Algorithm
Prune-and-Search Method
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
The Divide-and-Conquer Strategy
Median/Order Statistics Algorithms
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Divide and Conquer Reading Material: Chapter 6 (except Section 6.9).
Administrative Sep. 20 (today) – HW1 due Sep. 21 8am – problem session Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Prune-and-search Strategy
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 CSC401 – Analysis of Algorithms Lecture Notes 9 Radix Sort and Selection Objectives  Introduce no-comparison-based sorting algorithms: Bucket-sort and.
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.
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.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
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.
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.
LECTURE 40: SELECTION CSC 212 – Data Structures.  Sequence of Comparable elements available  Only care implementation has O(1) access time  Elements.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
Divide and Conquer Strategy
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.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Experimental Study on the Five Sort Algorithms You Yang, Ping Yu, Yan Gan School of Computer and Information Science Chongqing Normal University Chongqing,
Problem Definition Given a set of "n" unordered numbers we want to find the "k th " smallest number. (k is an integer between 1 and n).
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Sort Algorithm.
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Order Statistics Comp 122, Spring 2004.
Rank Rank of an element is its position in ascending key order.
Median Finding Algorithm
Randomized Algorithms
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Algorithm Design Methods
Rank Rank of an element is its position in ascending key order.
Divide and Conquer Mergesort Quicksort Binary Search Selection
Randomized Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Medians and Order Statistics
Topic: Divide and Conquer
Linked List and Selection Sort
ICS 353: Design and Analysis of Algorithms
Divide and Conquer Algorithms Part I
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.
Divide-and-Conquer 7 2  9 4   2   4   7
Every number has its place!
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
演算法概論 Introduction to Algorithms
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
The Selection Problem.
ICS 353: Design and Analysis of Algorithms
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
CS200: Algorithm Analysis
Medians and Order Statistics
Presentation transcript:

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 element. The straightforward algorithm: –step 1: Sort the n elements –step 2: Locate the k-th element in the sorted list. Time complexity: O(nlogn)

The selection problem Prune-and-search S={a 1, a 2, …, a n } Let p  S, use p to partition S into 3 subsets S 1, S 2, S 3 : –S 1 ={ a i | a i < p, 1  i  n} –S 2 ={ a i | a i = p, 1  i  n} –S 3 ={ a i | a i > p, 1  i  n} If |S 1 | > k, then the k-th smallest element of S is in S 1, prune away S 2 and S 3. Else, if |S 1 | + |S 2 | > k, then p is the k-th smallest element of S. Else, the k-th smallest element of S is the (k - |S 1 | - |S 2 |)-th smallest element in S 3, prune away S 1 and S 2.

The selection problem How to select P? The n elements are divided into subsets. (Each subset has 5 elements.) M P At least 1/4 of S known to be less than or equal to P. Each 5-element subset issorted in non-decreasingsequence. At least 1/4 of S known to be greater than or equal to P.

Prune-and-Search approach Input: A set S of n elements. Output: The k-th smallest element of S. Step 1. Divide S into  n/5  subsets. Each subset contains five elements. Add some dummy  elements to the last subset if n is not a net multiple of S. Step 2. Sort each subset of elements. Step 3. Find the element p which is the median of the medians of the  n/5  subsets..

Prune-and-Search approach Step 4. Partition S into S 1, S 2 and S 3, which contain the elements less than, equal to, and greater than p, respectively Step 5. If |S 1 |  k, then discard S 2 and S 3 and solve the problem that selects the k-th smallest element from S 1 during the next iteration; else if |S 1 | + |S 2 |  k then p is the k-th smallest element of S; otherwise, let k = k - |S 1 | - |S 2 |, solve the problem that selects the k ’ -th smallest element from S 3 during the next iteration.

Prune-and-Search approach At least n/4 elements are pruned away during each iteration. The problem remaining in step 5 contains at most 3n/4 elements. Time complexity: T(n) = O(n) –step 1: O(n) –step 2: O(n) –step 3: T(n/5) –step 4: O(n) –step 5: T(3n/4) –T(n) = T(3n/4) + T(n/5) + O(n)

Prune-and-Search approach Let T(n) = a 0 + a 1 n + a 2 n 2 + …, a 1  0 T(3n/4) = a 0 + (3/4)a 1 n + (9/16)a 2 n 2 + … T(n/5) = a 0 + (1/5)a 1 n + (1/25)a 2 n 2 + … T(3n/4 + n/5) = T(19n/20) = a 0 + (19/20)a 1 n + (361/400)a 2 n 2 + … T(3n/4) + T(n/5)  a 0 + T(19n/20)  T(n)  cn + T(19n/20)  cn + (19/20)cn +T((19/20) 2 n)   cn + (19/20)cn + (19/20) 2 cn + … +(19/20) p cn + T((19/20) p+1 n), (19/20) p+1 n  1  (19/20) p n =  20 cn +b = O(n)