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.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
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.
Median Finding Algorithm
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer The most-well known algorithm design strategy:
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Spring 2015 Lecture 5: QuickSort & Selection
Median/Order Statistics Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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.
Advanced Topics in Algorithms and Data Structures Page 1 Parallel merging through partitioning The partitioning strategy consists of: Breaking up the given.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Parallel Merging Advanced Algorithms & Data Structures Lecture Theme 15 Prof. Dr. Th. Ottmann Summer Semester 2006.
Quicksort.
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.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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.
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.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Order Statistics(Selection Problem)
Deterministic and Randomized Quicksort Andreas Klappenecker.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
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.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
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).
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.
Divide and Conquer Sorting
Chapter 2. Divide-and-conquer Algorithms
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Chapter 4 Divide-and-Conquer
Median Finding Algorithm
Randomized Algorithms
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Order Statistics(Selection Problem)
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Randomized Algorithms
Linear-Time Selection
Medians and Order Statistics
Topic: Divide and Conquer
CS 3343: Analysis of Algorithms
CSE 2010: Algorithms and Data Structures
Divide-and-Conquer The most-well known algorithm design strategy:
EE 312 Software Design and Implementation 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.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Lecture 15, Winter 2019 Closest Pair, Multiplication
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Divide and Conquer Merge sort and quick sort Binary search
Chapter 11 Sets, and Selection
Presentation transcript:

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 elem. at (n/2)-th position - running time ?

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 elem. at (n/2)-th position - running time:  (n log n) we will see a faster algorithm - will solve a more general problem: SELECT ( A, k ): returns the k-th smallest element in A

Linear-time Median Idea: Suppose A = 22,5,10,11,23,15,9,8,2,0,4,20,25,1,29,24,3,12,28,14,27,19,17,21,18,6,7,13,16,26

Linear-time Median SELECT (A, k) 1. split A into n/5 groups of five elements 2. let b i be the median of the i-th group 3. let B = [b 1, b 2, …, b n/5 ] 4. medianB = SELECT (B, B.length/2) 5. rearrange A so that all elements smaller than medianB come before medianB, all elements larger than medianB come after medianB, and elements equal to medianB are next to medianB 6. j1,j2 = the first and the last position of medianB in rearranged A 7. if (k < j1) return SELECT ( A[1…j1-1], k ) 8. if (k ¸ j and k · j2) return medianB 9. if (k > j2) return SELECT ( A[j2+1…n], k-j2 )

Linear-time Median Running the algorithm:

Linear-time Median Rearrange columns so that medianB in the “middle.” Recurrence: Running the algorithm:

Linear-time Median Recurrence:T(n) < T(n/5) + T(3n/4) + cn T(n) < c if n > 5 if n < 6 Claim: There exists a constant d such that T(n) < dn.

Randomized Linear-time Median Idea: Instead of finding medianB, take a random element from A. SELECT-RAND (A, k) 1. x = a i where i = a random number from {1,…,n} 2. rearrange A so that all elements smaller than x come before x, all elements larger than x come after x, and elements equal to x are next to x 3. j = position of x in rearranged A (if more x’s, then take the closest position to n/2) 4. if (k < j) return SELECT-RAND ( A[1…j-1], k ) 5. if (k = j) return medianB 6. if (k > j) return SELECT-RAND ( A[j+1…n], k-j)

Randomized Linear-time Median Worst case running time: O(n 2 ). SELECT-RAND (A, k) 1. x = a i where i = a random number from {1,…,n} 2. rearrange A so that all elements smaller than x come before x, all elements larger than x come after x, and elements equal to x are next to x 3. j1,j2 = the leftmost and the rightmost position of x in rearranged A 4. if (k<j1) return SELECT-RAND(A[1…(j1-1)],k) 5. if (j1 · k · j2) return x 6. if (k>j2) return SELECT-RAND(A[(j2+1)…n],k-j)

Randomized Linear-time Median Worst case running time: O(n 2 ). Claim: Expected running time is O(n).

Master Theorem Let a ¸ 1 and b>1 be constants, f(n) be a function and for positive integers we have a recurrence for T of the form T(n) = a T(n/b) + f(n), where n/b is rounded either way. Then, If f(n) = O(n log a/log b - e ) for some constant e  > 0, then T(n) =  (n log a/log b ). If f(n) =  (n log a/log b ), then T(n) =  (n log a/log b log n). If f(n) =  (n log a/log b + e ) for some constant e > 0, and if af(n/b) · cf(n) for some constant c < 1 (and all sufficiently large n), then T(n) =  (f(n)).