Finding the Median Algorithm : Design & Analysis [11]

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

MS 101: Algorithms Instructor Neelima Gupta
MS 101: Algorithms Instructor Neelima Gupta
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.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Lecture 3: Randomized Algorithm and Divide and Conquer II: Shang-Hua Teng.
Lecture 12: Lower bounds By "lower bounds" here we mean a lower bound on the complexity of a problem, not an algorithm. Basically we need to prove that.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Medians and Order Statistics
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers.
CS4413 Divide-and-Conquer
Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
Insertion Sort Algorithm : Design & Analysis [5].
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Lecture 13: Adversary Arguments continues … This lecture, we continue with the adversary argument by giving more examples.
Median/Order Statistics Algorithms
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”,
Divide and Conquer Reading Material: Chapter 6 (except Section 6.9).
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Prune-and-search Strategy
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
© The McGraw-Hill Companies, Inc., Chapter 6 Prune-and-Search Strategy.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
TECH Computer Science Problem: Selection Design and Analysis: Adversary Arguments The selection problem >  Finding max and min Designing against an adversary.
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.
Sorting CS 110: Data Structures and Algorithms First Semester,
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1 CSC 421: Algorithm Design & Analysis Spring 2014 Complexity & lower bounds  brute force  decision trees  adversary arguments  problem reduction.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
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.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
The Selection Algorithm : Design & Analysis [10].
Insertion Sort Algorithm : Design & Analysis [4].
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).
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Median Finding Algorithm
Randomized Algorithms
Randomized Algorithms
“Human Sorting” It’s a “Problem Solving” game:
Topic: Divide and Conquer
Sorting … and Insertion Sort.
Order Statistics Comp 550, Spring 2015.
ICS 353: Design and Analysis of Algorithms
Topic: Divide and Conquer
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
ICS 353: Design and Analysis of Algorithms
“Human Sorting” It’s a “Problem Solving” game:
Presentation transcript:

Finding the Median Algorithm : Design & Analysis [11]

In the last class … The Selection Problems Finding max and min Finding the second largest key Adversary argument Adversary argument and lower bound

Finding the Median Design against an Adversary Selection Problem – Median A Linear Time Selection Algorithm Analysis of Selection Algorithm A Lower Bound for Finding the Median

Design Against an Adversary For comparison-based algorithm: The algorithm gives the “ question ”, which is a test by comparison in searching for as more information from the input as possible. The adversary specifies the “ answer ”, which is the output for the comparison as unfavorable as possible for the algorithm. So, the algorithm prefers to give the “ question ”, for which, both answers gives the same amount of information, as far as possible.

An Example: Median in 5 Entries The problem: Finding the median of five distinct keys with only six comparisons in the worst case After 1st comparison After 2nd comparison

An Example: Continuing After 3rd comparison After 4th comparison Case 1Case 2

An Example: Continuing After 5th comparison Case 1-1 Case 1-2/ 2-1/2-2 After 6th comparison Done!

Finding the Median: the Strategy Obervation: If we can partition the problem set of keys into 2 subsets: S1, S2, such that any key in S1 is smaller that that of S2, then the median must located in the set with more elements. Divide-and-Conquer: only one subset is needed to be processed recursively. The rank of the median (of the original set) in the subset considered can be evaluated easily.

Partitioning: Larger and Smaller Dividing the array to be considered into two subsets: “ small ” and “ large ”, the one with more elements will be processed recursively. for any element in this segment, the key is less than pivot. for any element in this segment, the key is not less than pivot. A “bad” pivot will give a very uneven partition! [splitPoint]:pivot small large To beprocessedrecursively

Partition Improved: the Strategy All the elements are put in groups of 5 Increasing Medians Increasing by medians

Selection: the Algorithm Input: S, a set of n keys; and k, an integer such that 1  k  n. Output: The kth smallest key in S. Note: Median selection is only a special case of the algorithm, with k=  n/2 . Procedure Element select(SetOfElements S, int k) if (|S|  5) return direct solution; else Constructing the subsets S 1 and S 2; Divide and conquer;

Constructing the Partition Find the m*, the median of medians of all the groups of 5, as illustrated previously. Compare each key in sections A and D to m*, and Let S 1 =C  {x|x  A  D and x<m*} Let S 2 =B  {x|x  A  D and x>m*} (m* is the pivot for the partition)

Divide and Conquer if (k=|S 1 |+1) return m*; else if (k  |S 1 |) return select(S 1,k); //recursion else return select(S 2,k-|S 1 |-1); //recursion

Counting the Number of Comparisons For simplicity: Assuming n=5(2r+1) for all calls of select. Note: n is about n/10, and 0.7n+2 is about 0.7n, so Finding the median in every group of 5 Finding the median of the medians Comparing all the elements in A  D with m* The extreme case: all the elements in A  D in one subset.

Worst Case Complexity of Select Note: Row sums is a decreasing geometric series, so W(n)  (n) W(.2 3 n)W(.2 2 (.7)n) W(.2(.7) 2 n)W(.2 2 (.7)n)W(.2(.7) 2 n) W(.2 3 n) W(.04n) 1.6(.04n)W(.14n) 1.6(.14n) W(.49n) 1.6(.49n) W(.2n) 1.6(.2n)W(.7n) 1.6(.7n) W(n) 1.6n 1.6n 1.6(. 9)n 1.6(. 81)n 1.6(. 9) 3 n

Crucial Comparisons for Selection Observation: Any algorithm of selection must know the relation of every element to the median. A crucial comparison establishes the relation of some x to the median. y y Median Wrong

Adversary for Lower Bound Adversary rule: Comparands Adversary’s action N,N one L, the another S L,N or N,L change N to L S,N or N,S change N to S (In all other cases, just keep consistency) All actions explicitly specified above make the comparisons uncrucial. At least, (n-1)/2 L or S can be assigned freely. So, an adversary can force the algorithm to do (n-1)/2 uncrucial comprisons at least. Compared to the expected median

Lower Bound for Selection Problem Theorem: Any algorithm to find the median of n keys(for odd n) by comparison of keys must do at least 3n/2-3/2 comparisons in the worst case. Argument: There must be done n-1 crucial comparisons at least. An adversary can force the algorithm to perform as many as (n-1)/2 uncrucial comparisons. (Note: the algorithm can always start out by doing (n-1)/2 comparisons involving 2 N-keys, so, only (n-1)/2 L or S left for the adversary to assign freely as the adversary rule.

Home Assignment