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

Finding the Median Algorithm : Design & Analysis [11]

Embed Size (px)

Citation preview

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

Finding the Median

Algorithm : Design & Analysis

[11]

Page 2: 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

Page 3: Finding the Median Algorithm : Design & Analysis [11]

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

Page 4: Finding the Median Algorithm : Design & Analysis [11]

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.

Page 5: Finding the Median Algorithm : Design & Analysis [11]

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

Page 6: Finding the Median Algorithm : Design & Analysis [11]

An Example: Continuing

After 3rd comparison

After 4th comparison

Case 1 Case 2

Page 7: Finding the Median Algorithm : Design & Analysis [11]

An Example: Continuing

After 5th comparison

Case 1-1 Case 1-2/2-1/2-2 After 6th comparison

Done!

Page 8: Finding the Median Algorithm : Design & Analysis [11]

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.

Page 9: Finding the Median Algorithm : Design & Analysis [11]

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!

A “bad” pivot will give a very uneven partition!

[splitPoint]: pivot

small large

To be processed recursively

Page 10: Finding the Median Algorithm : Design & Analysis [11]

Partition Improved: the Strategy

A B

C

D

m* ... ...

<m*

>m*

All the elements are put in groups of 5

Increasing

Medians

Increasing by medians

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

Selection: the Algorithm

Input: S, a set of n keys; and k, an integer such that 1kn.

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 S1 and S2;

Divide and conquer;

Page 12: Finding the Median Algorithm : Design & Analysis [11]

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 S1=C{x|xAD and x<m*}

Let S2=B{x|xAD and x>m*}

(m* is the pivot for the partition)

Page 13: Finding the Median Algorithm : Design & Analysis [11]

Divide and Conquer

if (k=|S1|+1)

return m*;

else if (k|S1|)

return select(S1,k); //recursion

else

return select(S2,k-|S1|-1); //recursion

Page 14: Finding the Median Algorithm : Design & Analysis [11]

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

)27(455

6)(

rWr

nW

nnW

Finding the median in every group of 5

Finding the median in every group of 5

Finding the median of the medians

Finding the median of the medians

Comparing all the elements in AD with m*

Comparing all the elements in AD with m*

The extreme case: all the elements in AD in one subset.

The extreme case: all the elements in AD in one subset.

)7.0()2.0(6.1)( nWnWnnW

Page 15: Finding the Median Algorithm : Design & Analysis [11]

Worst Case Complexity of Select

Note: Row sums is a decreasing geometric series, so W(n)(n)

W(.23n)

W(.22(.7)n) W(.22(.7)n)

W(.2(.7)2n)

W(.22(.7)n)

W(.2(.7)2n)

W(.2(.7)2n)

W(.23n)

W(.04n) 1.6(.04n) W(.14n) 1.6(.14n) 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)3n

Page 16: Finding the Median Algorithm : Design & Analysis [11]

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

WrongWrong

Page 17: Finding the Median Algorithm : Design & Analysis [11]

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

Compared to the expected median

Page 18: Finding the Median Algorithm : Design & Analysis [11]

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.

Page 19: Finding the Median Algorithm : Design & Analysis [11]

Home Assignment

5.8 5.12 5.13