introduction to algorithms - massachusetts institute of technology · 2003-02-27 · © charles...

31
Introduction to Algorithms 6.046J/18.401J Lecture 6 Prof. Piotr Indyk

Upload: others

Post on 26-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to Algorithms6.046J/18.401J

Lecture 6Prof. Piotr Indyk

Introduction to Algorithms February 25, 2003 L6.2© Charles Leiserson and Piotr Indyk

Order statistics

Select the i th smallest of n elements (theelement with rank i).• i = 1: minimum;• i = n: maximum;• i = Î(n+1)/2˚ or È(n+1)/2˘: median.

Naive algorithm: Sort and index i th element.Worst-case running time = Q(n lg n) + Q(1)

= Q(n lg n),using merge sort.

Introduction to Algorithms February 25, 2003 L6.3© Charles Leiserson and Piotr Indyk

Randomized divide-and-conquer

RAND-SELECT(A, p, q, i) _ i th smallest of A[ p…q]if p = q then return A[ p]r ¨ RAND-PARTITION(A, p, q)k ¨ r – p + 1 _ k = rank(A[r])if i = k then return A[ r]if i < k

then return RAND-SELECT( A, p, r – 1, i )else return RAND-SELECT( A, r + 1, q, i – k )

£ A[r]£ A[r] ≥ A[r]≥ A[r]rp q

k

Introduction to Algorithms February 25, 2003 L6.4© Charles Leiserson and Piotr Indyk

Example

pivoti = 766 1010 1313 55 88 33 22 1111

k = 4

Select the 7 – 4 = 3rd smallest recursively.

Select the i = 7th smallest:

22 55 33 66 88 1313 1010 1111

Partition:

Introduction to Algorithms February 25, 2003 L6.5© Charles Leiserson and Piotr Indyk

Intuition for analysis

Lucky:101log 9/10 == nn

CASE 3T(n) = T(9n/10) + Q(n)

= Q(n)Unlucky:

T(n) = T(n – 1) + Q(n)= Q(n2)

arithmetic series

Worse than sorting!

(All our analyses today assume that all elementsare distinct.)

Introduction to Algorithms February 25, 2003 L6.6© Charles Leiserson and Piotr Indyk

Analysis

• The probability that a random pivot induces luckypartition is at least 8/10 (Lecture 4)

• Let ti be the number of partitions performedbetween the (i-1)-th and the i-th lucky partition

• The total time is at most:t1 n + t2 (9/10) n + t3 (9/10)2 n + …

• The total expected time is at most:10/8 n + 10/8 (9/10) n + 10/8 (9/10)2 n + …

= O(n)

Introduction to Algorithms February 25, 2003 L6.7© Charles Leiserson and Piotr Indyk

Alternative analysis of expectedtime

Let T(n) = the random variable for the runningtime of RAND-SELECT on an input of size n,assuming random numbers are independent.

For k = 0, 1, …, n–1, define the indicatorrandom variable

Xk = 1 if PARTITION generates a k : n–k–1 split,0 otherwise.

The analysis follows that of randomizedquicksort, but it’s a little different.

Introduction to Algorithms February 25, 2003 L6.8© Charles Leiserson and Piotr Indyk

Analysis (continued)

T(n) =

T(max{0, n–1}) + Q(n) if 0 : n–1 split,T(max{1, n–2}) + Q(n) if 1 : n–2 split,

M

T(max{n–1, 0}) + Q(n) if n–1 : 0 split,

( )Â-

=

Q+--=1

0

)(})1,(max{n

kk nknkTX .

To obtain an upper bound, assume that the i thelement always falls in the larger side of thepartition:

Introduction to Algorithms February 25, 2003 L6.9© Charles Leiserson and Piotr Indyk

Calculating expectation

( )̇˚

˘ÍÎ

ÈQ+--= Â

-

=

1

0)(})1,(max{)]([

n

kk nknkTXEnTE

Take expectations of both sides.

Introduction to Algorithms February 25, 2003 L6.10© Charles Leiserson and Piotr Indyk

Calculating expectation

( )

( )[ ]Â

Â-

=

-

=

Q+--=

˙˚

˘ÍÎ

ÈQ+--=

1

0

1

0

)(})1,(max{

)(})1,(max{)]([

n

kk

n

kk

nknkTXE

nknkTXEnTE

Linearity of expectation.

Introduction to Algorithms February 25, 2003 L6.11© Charles Leiserson and Piotr Indyk

Calculating expectation

( )

( )[ ]

[ ] [ ]Â

Â

Â

-

=

-

=

-

=

Q+--⋅=

Q+--=

˙˚

˘ÍÎ

ÈQ+--=

1

0

1

0

1

0

)(})1,(max{

)(})1,(max{

)(})1,(max{)]([

n

kk

n

kk

n

kk

nknkTEXE

nknkTXE

nknkTXEnTE

Independence of Xk from other randomchoices.

Introduction to Algorithms February 25, 2003 L6.12© Charles Leiserson and Piotr Indyk

Calculating expectation

( )

( )[ ]

[ ] [ ]

[ ] ÂÂ

Â

Â

Â

-

=

-

=

-

=

-

=

-

=

Q+--=

Q+--⋅=

Q+--=

˙˚

˘ÍÎ

ÈQ+--=

1

0

1

0

1

0

1

0

1

0

)(1})1,(max{1

)(})1,(max{

)(})1,(max{

)(})1,(max{)]([

n

k

n

k

n

kk

n

kk

n

kk

nn

knkTEn

nknkTEXE

nknkTXE

nknkTXEnTE

Linearity of expectation; E[Xk] = 1/n .

Introduction to Algorithms February 25, 2003 L6.13© Charles Leiserson and Piotr Indyk

Calculating expectation

( )

( )[ ]

[ ] [ ]

[ ]

[ ]Î ˚

)()(2

)(1})1,(max{1

)(})1,(max{

)(})1,(max{

)(})1,(max{)]([

1

2/

1

0

1

0

1

0

1

0

1

0

nkTEn

nn

knkTEn

nknkTEXE

nknkTXE

nknkTXEnTE

n

nk

n

k

n

k

n

kk

n

kk

n

kk

Q+£

Q+--=

Q+--⋅=

Q+--=

˙˚

˘ÍÎ

ÈQ+--=

Â

ÂÂ

Â

Â

Â

-

=

-

=

-

=

-

=

-

=

-

=

Upper termsappear twice.

Introduction to Algorithms February 25, 2003 L6.14© Charles Leiserson and Piotr Indyk

Hairy recurrence

[ ]Î ˚

)()(2)]([1

2/nkTE

nnTE

n

nkQ+= Â

-

=

Prove: E[T(n)] £ c n for constant c > 0 .

Use fact: Î ˚

21

2/83nk

n

nkÂ-

=£ (exercise).

• The constant c can be chosen large enoughso that E[T(n)] £ c n for the base cases.

(But not quite as hairy as the quicksort one.)

Introduction to Algorithms February 25, 2003 L6.15© Charles Leiserson and Piotr Indyk

Substitution method

[ ]Î ˚

)(2)(1

2/nck

nnTE

n

nkQ+£ Â

-

=

Substitute inductive hypothesis.

Introduction to Algorithms February 25, 2003 L6.16© Charles Leiserson and Piotr Indyk

Substitution method

[ ]Î ˚

)(832

)(2)(

2

1

2/

nnnc

nckn

nTEn

nk

Q+˜¯ˆÁ

ËÊ£

Q+£ Â-

=

Use fact.

Introduction to Algorithms February 25, 2003 L6.17© Charles Leiserson and Piotr Indyk

Substitution method

Express as desired – residual.

[ ]Î ˚

˜¯ˆÁ

ËÊ Q--=

Q+˜¯ˆÁ

ËÊ£

Q+£ Â-

=

)(4

)(832

)(2)(

2

1

2/

ncncn

nnnc

nckn

nTEn

nk

Introduction to Algorithms February 25, 2003 L6.18© Charles Leiserson and Piotr Indyk

Substitution method

[ ]Î ˚

cn

ncncn

nnnc

nckn

nTEn

nk

£

˜¯ˆÁ

ËÊ Q--=

Q+˜¯ˆÁ

ËÊ£

Q+£ Â-

=

)(4

)(832

)(2)(

2

1

2/

if c is chosen large enough sothat cn/4 dominates the Q(n).

,

Introduction to Algorithms February 25, 2003 L6.19© Charles Leiserson and Piotr Indyk

Summary of randomizedorder-statistic selection

• Works fast: linear expected time.• Excellent algorithm in practice.• But, the worst case is very bad: Q(n2).

Q. Is there an algorithm that runs in lineartime in the worst case?

IDEA: Generate a good pivot recursively.

A. Yes, due to Blum, Floyd, Pratt, Rivest,and Tarjan [1973].

Introduction to Algorithms February 25, 2003 L6.20© Charles Leiserson and Piotr Indyk

Worst-case linear-time orderstatistics

if i = k then return xelseif i < k

then recursively SELECT the i th smallest element in the lower part

else recursively SELECT the (i–k)thsmallest element in the upper part

SELECT(i, n)1. Divide the n elements into groups of 5. Find

the median of each 5-element group by rote.2. Recursively SELECT the median x of the În/5˚

group medians to be the pivot.3. Partition around the pivot x. Let k = rank(x).4.

Same asRAND-SELECT

Introduction to Algorithms February 25, 2003 L6.21© Charles Leiserson and Piotr Indyk

Choosing the pivot

Introduction to Algorithms February 25, 2003 L6.22© Charles Leiserson and Piotr Indyk

Choosing the pivot

1. Divide the n elements into groups of 5.

Introduction to Algorithms February 25, 2003 L6.23© Charles Leiserson and Piotr Indyk

Choosing the pivot

lesser

greater

1. Divide the n elements into groups of 5. Findthe median of each 5-element group by rote.

Introduction to Algorithms February 25, 2003 L6.24© Charles Leiserson and Piotr Indyk

Choosing the pivot

lesser

greater

1. Divide the n elements into groups of 5. Findthe median of each 5-element group by rote.

2. Recursively SELECT the median x of the Î n/5˚group medians to be the pivot.

x

Introduction to Algorithms February 25, 2003 L6.25© Charles Leiserson and Piotr Indyk

Analysis

lesser

greater

x

At least half the group medians are £ x, whichis at least Î Î n/5˚ /2˚ = Î n/10˚ group medians.

Introduction to Algorithms February 25, 2003 L6.26© Charles Leiserson and Piotr Indyk

Analysis

lesser

greater

x

At least half the group medians are £ x, whichis at least Î Î n/5˚ /2˚ = Î n/10˚ group medians.• Therefore, at least 3 Î n/10˚ elements are £ x.

(Assume all elements are distinct.)

Introduction to Algorithms February 25, 2003 L6.27© Charles Leiserson and Piotr Indyk

Analysis

lesser

greater

x

At least half the group medians are £ x, whichis at least Î Î n/5˚ /2˚ = Î n/10˚ group medians.• Therefore, at least 3 Î n/10˚ elements are £ x.• Similarly, at least 3 Î n/10˚ elements are ≥ x.

(Assume all elements are distinct.)

Introduction to Algorithms February 25, 2003 L6.28© Charles Leiserson and Piotr Indyk

Minor simplification• For n ≥ 50, we have 3 Î n/10˚ ≥ n/4.

• Therefore, for n ≥ 50 the recursive call toSELECT in Step 4 is executed recursivelyon £ 3n/4 elements.

• Thus, the recurrence for running timecan assume that Step 4 takes time T(3n/4)in the worst case.

• For n < 50, we know that the worst-casetime is T(n) = Q(1).

Introduction to Algorithms February 25, 2003 L6.29© Charles Leiserson and Piotr Indyk

Developing the recurrence

if i = k then return xelseif i < k

then recursively SELECT the i th smallest element in the lower part

else recursively SELECT the (i–k)thsmallest element in the upper part

SELECT(i, n)1. Divide the n elements into groups of 5. Find

the median of each 5-element group by rote.2. Recursively SELECT the median x of the În/5˚

group medians to be the pivot.3. Partition around the pivot x. Let k = rank(x).4.

T(n)

Q(n)

T(n/5)Q(n)

T(3n/4)

Introduction to Algorithms February 25, 2003 L6.30© Charles Leiserson and Piotr Indyk

Solving the recurrence

)(43

51)( nnTnTnT Q+˜

¯ˆÁ

ËÊ+˜

¯ˆÁ

ËÊ=

if c is chosen large enough to handle both theQ(n) and the initial conditions.

cn

ncncn

ncn

ncncnnT

£

˜¯ˆÁ

ËÊ Q--=

Q+=

Q++£

)(201

)(2019

)(43

51)(

,

Substitution:T(n) £ cn

Introduction to Algorithms February 25, 2003 L6.31© Charles Leiserson and Piotr Indyk

Conclusions

• Since the work at each level of recursionis a constant fraction (19/20) smaller, thework per level is a geometric seriesdominated by the linear work at the root.

• In practice, this algorithm runs slowly,because the constant in front of n is large.

• The randomized algorithm is far morepractical.

Exercise: Why not divide into groups of 3?