SELECTION PROBLEM ELLYSA N KOSINAYA PREVIEW Selection problem
SELECTION PROBLEM ELLYSA N. KOSINAYA
PREVIEW • • Selection – problem & solving strategy Mergesort review – first algorithm Select 1 – second algorithm Select 2 – third algorithm
SELECTION • The problem • Given a list of n elements, find the kth-smallest one. • Special cases: when k = 1, min when k = n, max
SELECTION • The solving strategy • 1 st Algorithm – sort the elements and return the kth-smallest; • best case and worst case of O(n lg n) time. For example, use Mergesort. • 2 nd Algorithm – use Partition procedure in Quicksort repeatedly • partitions an array so all elements smaller than some pivot item come before it and all elements larger than the pivot item come after it; • best case of O(n) time and worst case of O(n 2) time • 3 rd Algorithm – Same as the 2 nd Algorithm. Use MM (median of the medians) as the pivot instead.
1 ST ALGORITHM MERGESORT REVIEW • Divide and conquer method • Given an array with n items (let n be a power of 2): 1. Divide the array into two subarrays each with n/2 items. 2. Conquer (solve) each subarray by sorting it. Unless the array is sufficiently small, use recursion to do this. 3. Combine the solutions to the subarrays by merging them into a single sorted array.
MERGESORT REVIEW Example Analysis •
2 ND ALGORITHM SELECT 1 • Idea: use partition procedure from Quicksort repeatedly until kth element is found • For each iteration: • If pivot position = k Done! • If pivot position > k recursively call SELECT 1 of the 1 st or left sub-list (with the same k value) • If pivot position < k recursively call SELECT 1 of the 2 nd or right sub-list(with k = k – pivot position)
SELECT 1 – PARTITIONING
SELECT 1 ALGORITHM Procedure Select 1 (A, n, k) // A is array, n is size of begin // array, k is key m 1; j n; loop call Partition (m, j, pivotposition); case: k = pivotposition: return A(k); k < pivotposition: j pivotposition – 1; else: m pivotposition + 1; end loop; end;
SELECT 1 EXAMPLE
SELECT 1 EXAMPLE
SELECT 1 ANALYSIS •
3 RD ALGORITHM SELECT 2 – USING MM AS PIVOT • By choosing a pivot more carefully, we can obtain a selection algorithm with a Worst-case complexity of O(n). • How: Make sure the pivot v is chosen s. t. at least some fraction of the elements will be smaller than v and at least some other fraction of elements will be greater than v. • Median – out of n distinct keys, half the elements are smaller and half are larger • Always choosing the median as a pivotitem optimal performance!
SELECT 2 – USING MM • Assume n is odd and a multiple of 5 • Divide elements into n/5 groups of elements (each containing 5 elements) • Find median of each group • Determine MM, median of the n/5 medians
SELECT 2 EXAMPLE
SELECT 2 (3 RD ALGORITHM) Use MM (median of medians) rule to choose pivot… (Note that in the previous example, we choose r=5) Procedure Select 2 (A, n, k) begin if n ≤ r, then sort A and return the kth element; divide A into n/r subset of size r each, ignore excess elements, and let M = {m 1, m 2, …, mn/r} be the set of medians of the n/r subsets;
SELECT 2 (3 RD ALGORITHM) v Select 2 (M, n/r, (n/r)/2); use “Partition” to partition A using v as the pivot; // Assume v is at pivotposition case: k = pivotposition: return (v); k < pivotposition: let S be the set of elements A(1, …, pivotposition– 1), return Select 2 (S, pivotposition– 1, k); else: let R be the set of element A (pivotposition+1, …, n), return Select 2 (R, n-pivotposition, k-pivotposition); end case; end;
SELECT 2 ANALYSIS • How many Mi’s (or ) mm? At least • How many elements (or ) mm? At least
SELECT 2 ANALYSIS • How many elements > mm (or < mm)? At most • Assume r = 5
SELECT 2 ANALYSIS •
PROOF •
PROOF •
- Slides: 22