Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf ·...

24
Chapter Chapter 9. Medians and Medians and Order Statistics Order Statistics

Transcript of Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf ·...

Page 1: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Chapter Chapter 99..

Medians and Medians and

Order StatisticsOrder Statistics

Page 2: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Medians and Order Medians and Order StatisticsStatistics

The ith order statistic of a set of n elements is the

ith smallest element. Example: minimum and

maximum

Median: lower median and upper medianMedian: lower median and upper median

Selection problem: selecting the ith order statistic

from a set of n distinct numbers

Selection problem can be solved in O(nlgn)—

faster algorithms?

Average Case- O(n)

Worst case -O(n)

Page 3: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Selection ProblemSelection Problem

Page 4: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Maximum and MinimumMaximum and Minimum

Page 5: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Simultaneously Maximum Simultaneously Maximum and Minimumand Minimum

Page 6: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Selection in Expected Selection in Expected Linear TimeLinear Time

Once position of pivot in A is fixed, it will not changed anymore

Page 7: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

AnalysisAnalysis

Worst-case running time: ΘΘ(n2)– We could be extremely unlucky and always recurse on a

sub-array that is only 1 element smaller than the

previous sub-arrayprevious sub-array

Expected running time: O(n)– Randomized-select works well on average

– It is randomized, no particular input bring out the worst-case behavior

The running time of Randomized-select is a random variable T(n), we will obtain an upper bound on E[T(n)]

Page 8: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Expected Running Time(1)Expected Running Time(1)

Including pivot A[q]

With equal probability for all possible k

Page 9: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Expected Running Time(2)Expected Running Time(2)

1

1 / , 1n

k k

i

EX n X

=

= =∑

Page 10: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Expected Running Time(3)Expected Running Time(3)

Page 11: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the
Page 12: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Expected Running Time(4)Expected Running Time(4)

Page 13: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

hypothesis

>0?

Page 14: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Linear Expected Running timeLinear Expected Running time

Page 15: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Selection in worstSelection in worst--case linear timecase linear time

Select algorithm: its running time is O(n) in Select algorithm: its running time is O(n) in

the the worst caseworst case

It finds the desired element by recursively It finds the desired element by recursively

partitioning the input arraypartitioning the input arraypartitioning the input arraypartitioning the input array

The The main ideamain idea behind the algorithm, behind the algorithm,

however, is to however, is to guaranteeguarantee a a good splitgood split

Select use deterministic PARTITION Select use deterministic PARTITION

algorithm with algorithm with pivotpivot as inputas input

Page 16: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

SELECT (1)SELECT (1)

Median of median

Page 17: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

SELECT (2)SELECT (2)

Page 18: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Analysis (1)Analysis (1)

Page 19: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Analysis (2): #nodes greater Analysis (2): #nodes greater than xthan x

Both elements greater

than and less than x are

all at least 3n/10-6

Page 20: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Analysis (3)Analysis (3)

No matter recursively call on which sides of x, at most 7n/10+6 elements

Page 21: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

RecurrenceRecurrence

Page 22: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Constants 140?Constants 140?

Page 23: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

ConclusionConclusion

Page 24: Chapter 99.. Medians and Order Statisticsdatamining-iip.fudan.edu.cn/ppts/algo/Lecture09.pdf · Medians and Order Statistics The ith order statistic of a set of n elements is the

Class ExerciseClass Exercise

Exercise 9.1Exercise 9.1--2, 9.22, 9.2--2, 9.32, 9.3--55