Presentation is loading. Please wait.

Presentation is loading. Please wait.

Finding the Median Algorithm : Design & Analysis [11]

Similar presentations


Presentation on theme: "Finding the Median Algorithm : Design & Analysis [11]"— Presentation transcript:

1 Finding the Median Algorithm : Design & Analysis [11]

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

3 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

4 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.

5 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

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

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

8 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.

9 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

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

11 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;

12 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)

13 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

14 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.

15 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

16 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

17 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

18 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.

19 Home Assignment 5.8 5.12 5.13


Download ppt "Finding the Median Algorithm : Design & Analysis [11]"

Similar presentations


Ads by Google