Introduction to Algorithms

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.
Order Statistics Sorted
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 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.
Median Finding, Order Statistics & Quick Sort
Spring 2015 Lecture 5: QuickSort & Selection
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Median/Order Statistics Algorithms
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
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.
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
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
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.
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
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 David Kauchak cs302 Spring 2012.
Order Statistics(Selection Problem)
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.
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
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 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
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.
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.
Analysis of Algorithms CS 477/677
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Randomized Algorithms
Order Statistics(Selection Problem)
Randomized Algorithms
Medians and Order Statistics
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.
Chapter 9: Medians and Order Statistics
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
CS 583 Analysis of Algorithms
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
CS200: Algorithm Analysis
Medians and Order Statistics
Presentation transcript:

Introduction to Algorithms Chapter 9: Median and Order Statistics.

Selection General Selection Problem: select the i-th smallest element form a set of n distinct numbers that element is larger than exactly i - 1 other elements The selection problem can be solved in O(nlgn) time Sort the numbers using an O(nlgn)-time algorithm, such as merge sort Then return the i-th element in the sorted array

Medians and Order Statistics Def.: The i-th order statistic of a set of n elements is the i-th smallest element. The minimum of a set of elements: The first order statistic i = 1 The maximum of a set of elements: The n-th order statistic i = n The median is the “halfway point” of the set i = (n+1)/2, is unique when n is odd i = (n+1)/2 = n/2 (lower median) and (n+1)/2 = n/2+1 (upper median), when n is even

Finding Minimum or Maximum Alg.: MINIMUM(A, n) min ← A[1] for i ← 2 to n do if min > A[i] then min ← A[i] return min How many comparisons are needed? n – 1: each element, except the minimum, must be compared to a smaller element at least once The same number of comparisons are needed to find the maximum The algorithm is optimal with respect to the number of comparisons performed

Simultaneous Min, Max Find min and max independently Use n – 1 comparisons for each  total of 2n – 2 At most 3n/2 comparisons are needed Process elements in pairs Maintain the minimum and maximum of elements seen so far Don’t compare each element to the minimum and maximum separately Compare the elements of a pair to each other Compare the larger element to the maximum so far, and compare the smaller element to the minimum so far This leads to only 3 comparisons for every 2 elements

Analysis of Simultaneous Min, Max Setting up initial values: n is odd: n is even: Total number of comparisons: n is odd: we do 3(n-1)/2 comparisons n is even: we do 1 initial comparison + 3(n-2)/2 more comparisons = 3n/2 - 2 comparisons set both min and max to the first element compare the first two elements, assign the smallest one to min and the largest one to max

Example: Simultaneous Min, Max n = 5 (odd), array A = {2, 7, 1, 3, 4} Set min = max = 2 Compare elements in pairs: 1 < 7  compare 1 with min and 7 with max  min = 1, max = 7 3 < 4  compare 3 with min and 4 with max  min = 1, max = 7 3 comparisons 3 comparisons We performed: 3(n-1)/2 = 6 comparisons

Example: Simultaneous Min, Max n = 6 (even), array A = {2, 5, 3, 7, 1, 4} Compare 2 with 5: 2 < 5 Set min = 2, max = 5 Compare elements in pairs: 3 < 7  compare 3 with min and 7 with max  min = 2, max = 7 1 < 4  compare 1 with min and 4 with max  min = 1, max = 7 1 comparison 3 comparisons 3 comparisons We performed: 3n/2 - 2 = 7 comparisons

General Selection Problem Select the i-th order statistic (i-th smallest element) form a set of n distinct numbers Idea: Partition the input array similarly with the approach used for Quicksort (use RANDOMIZED-PARTITION) Recurse on one side of the partition to look for the i-th element depending on where i is with respect to the pivot Selection of the i-th smallest element of the array A can be done in (n) time p q r A i < k  search in this partition i > k  search

Randomized Select Try: A={1,4,2,6,8,5} p q-1 q q+1 r Alg.: RANDOMIZED-SELECT(A, p, r, i ) if p = r then return A[p] q ←RANDOMIZED-PARTITION(A, p, r) k ← q - p + 1 if i = k pivot value is the answer then return A[q] elseif i < k then return RANDOMIZED-SELECT(A, p, q-1, i ) else return RANDOMIZED-SELECT(A, q + 1, r, i-k) Try: A={1,4,2,6,8,5} i < k  search in this partition i > k  search in this partition pivot

Analysis of Running Time Worst case running time: If we always partition around the largest/smallest remaining element Partition takes (n) time T(n) = O(1) (choose the pivot) + (n) (partition) + T(n-1) = 1 + n + T(n-1) = (n2) (n2) q p r n-1 elements

A Better Selection Algorithm Can perform Selection in O(n) Worst Case Idea: guarantee a good split on partitioning Running time is influenced by how “balanced” are the resulting partitions Use a modified version of PARTITION Takes as input the element around which to partition

Selection in O(n) Worst Case x1 x2 x3 xn/5 x x k – 1 elements n - k elements Divide the n elements into groups of 5  n/5 groups Find the median of each of the n/5 groups Use insertion sort, then pick the median Use SELECT recursively to find the median x of the n/5 medians Partition the input array around x, using the modified version of PARTITION There are k-1 elements on the low side of the partition and n-k on the high side If i = k then return x. Otherwise, use SELECT recursively: Find the i-th smallest element on the low side if i < k Find the (i-k)-th smallest element on the high side if i > k

Example Find the –11th smallest element in array: A = {12, 34, 0, 3, 22, 4, 17, 32, 3, 28, 43, 82, 25, 27, 34, 2 ,19 ,12 ,5 ,18 ,20 ,33, 16, 33, 21, 30, 3, 47} Divide the array into groups of 5 elements 4 17 32 3 28 12 34 22 43 82 25 27 2 19 5 18 20 33 16 21 30 47

Example (cont.) Sort the groups and find their medians Find the median of the medians 12, 12, 17, 21, 34, 30 4 3 17 32 28 12 34 22 25 27 43 82 2 5 19 18 20 16 21 33 30 47

Example (cont.) Partition the array around the median of medians (17) First partition: {12, 0, 3, 4, 3, 2, 12, 5, 16, 3} Pivot: 17 (position of the pivot is q = 11) Second partition: {34, 22, 32, 28, 43, 82, 25, 27, 34, 19, 18, 20, 33, 33, 21, 30, 47} To find the 6-th smallest element we would have to recurse our search in the first partition.