1 Today’s Material Medians & Order Statistics – Ch. 9.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Medians and Order Statistics
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.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
Median/Order Statistics Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Chapter 7: Sorting Algorithms
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
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.
Quicksort
Data Structures Review Session 1
Selection: Find the ith number
Study Group Randomized Algorithms Jun 7, 2003 Jun 14, 2003.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Stochastic Algorithms Some of the fastest known algorithms for certain tasks rely on chance Stochastic/Randomized Algorithms Two common variations – Monte.
Chapter 14 Randomized algorithms Introduction Las Vegas and Monte Carlo algorithms Randomized Quicksort Randomized selection Testing String Equality Pattern.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
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 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.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
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.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
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.
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
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.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
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.
Quicksort 1.
Randomized Algorithms
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
Quicksort.
Order Statistics Comp 122, Spring 2004.
Quicksort.
The Selection Problem.
CS200: Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Presentation transcript:

1 Today’s Material Medians & Order Statistics – Ch. 9

2 Selection: Problem Definition Given a sequence of numbers a1, a2, a3, …aN and integer “i”, 1 <= i <= N, compute the i th smallest element Minimum is when k = 1, maximum is when k = N Median is a special case where i = N/2 Selection looks like a very mundane problem But it is such a basic question that it arises in many places in practice –Give examples…

3 Brute Force Solution There is an obvious brute-force solution Just sort the numbers in ascending order and return the i th element of the array Takes O(nlogn)—Time to sort the numbers Can we do better? –There is a deterministic O(n) algorithm, but it is very complicated and not very practical –However, there is a simple randomized algorithm, whose expected running time is O(n) We will only look at this randomized algorithm--next

4 Randomized Algorithms – An Intro A randomized algorithm is one that incorporates a random number generator Studies in recent years because many of the practical algorithms make use of randomization There are 2 classes of randomized algorithms –Monte Carlo Algorithms May make an error in its output, but presumably the probability of this happening is very small –Las Vegas Algorithms Always produces the correct answer, but there is a small probability that the algorithm takes longer than it should –With Monte Carlo algorithms randomization affects the result, with Las Vegas it affects the running time

5 A Simple Monte-Carlo Algorithm Problem: Given a number N, is N prime? –Important for cryptography Randomized Monte-Carlo Algorithm based on a Result by Fermat: –Guess a random number A, 0 < A < N –If (A N-1 mod N) ≠ 1, then Output “N is not prime” –Otherwise, Output “N is (probably) prime” N is prime with high probability but not 100% Can repeat steps 1-3 to make error probability close to 0

6 A Las Vegas Randomized Selection As we mentioned, there is an O(n) expected- case randomized Las-Vegas algorithm for Selection –Always produces the correct answer, but with low probability it might take longer than O(n) Idea is based on a modification of QuickSort: –Assume that the array A is indexed A[1..n] –Consider the Partition() procedure in QuickSort Randomly choose a pivot x, and permute the elements of A into two nonempty sublists A[1..q] of elements = x –See page 154 of CLRS Assume Partition() returns the index “q”

7 Partition Algorithm int Partition(int A[], int N){ if (N<=1) return 0; int pivot = A[0]; // Pivot is the first element int i=1, j=N-1; while (1){ while (A[j]>pivot) j--; // Move j while (A[i]<pivot && i<j) i++; // Move i if (i>=j) break; Swap(&A[i], &A[j]); i++; j--; } //end-while Swap(&A[j], &A[0]); // Restore the pivot return j; // return the index of the pivot } //end-Partition

8 A Las Vegas Randomized Selection Observe that there are “q” elements <= pivot, and hence the rank of the pivot is q If i==q then return A[q]; If i < q then we select the i th smallest element from the left sublist, A[1..q] If i > q then we recurse on the right sublist. –Because q elements have already been eliminated, we select the (i-q) th smallest element from the right sublist

9 Randomized Selection: Pseudocode // Assumes 1<= i <=N Select(A[1..N], i){ if (N==1) return A[1]; int q = Partition(A[1..N], N); if (i == q) return A[q]; if (i < q) return Select(A[1..q], i); else return Select(A[q+1..N], i-q); } //end-Select

10 Randomized Selection: C Code // Assumes 1<= i <=N int Select(int A[], int i, int N){ if (N==1) return A[0]; int q = Partition(A, N); if (i == q+1) return A[q]; else if (i <= q) return Select(A, i, q); // We have eliminated q+1 elements else return Select(&A[q+1], i-(q+1), N-(q+1)); } //end-Select

11 Running Time - 1 Because the algorithm is randomized, we analyze its “expected” time complexity –Where the expectation is taken over all possible choices of the random pivot element Let T(n) denote the expected case running time of the algorithm on a list of size “n” –Our analysis is with respect to the worst-case in “i” –That is, since we do not know what “i” is, we make the worst case assumption that whenever we partition the list, the i th smallest element occurs on the side having greater number of elements –Partitioning procedure takes O(n) – See CLRS

12 Running Time - 2 There are “n” possible choices for the pivot Each is equally likely with probability 1/n If “x” is the k th smallest element of the list, then we create two sublists of size “k” and “n-k” If we assume that we recurse on the larger side of the two sublists, then we get –T(n) <= –Basically, the recurrence can be simplified to: –T(n) <=

13 Running Time - 3 Then an induction argument is used to show that T(n) <= c*n for some appropriately chosen constant c After working through the induction proof (see page 189 in CLRS), we arrive at the condition –c*(3n/4 – ½) + n < c*n –This is satisfied for any c >= 4 –This technique of setting up an induction with an unknown parameter, and then determining the conditions on the parameter is known as “constructive proof”

14 Deterministic Selection Once we find the median of the medians, partition the array using the medians of the medians Then run the algorithm on the partitioned array recursively Basically, we want to make partitioning deterministic by finding the median of the medians so that the array is partitioned into almost 2 equal halves

15 Deterministic Selection (1) Divide the elements into roughly n/5 groups, each of size 5 (2) Compute the median of each group (by any method you like) (3) Compute the median of these n/5 group medians How do you implement step (3)? –You call deterministic selection recursively –Since the list is of smaller size, it will eventually terminate –Why groups of 5? You need an odd number for median computation 3 does not work. The smallest odd number greater than 3 is 5. But any other bigger odd number (7, 9,..) would do too.