• Tidak ada hasil yang ditemukan

Algorithms

5.3 Randomized algorithms

5.4.3 Streaks

Suppose you flip a fair coin n times. What is the longest streak of consecutive heads that you expect to see? The answer is ‚.lgn/, as the following analysis shows.

We first prove that the expected length of the longest streak of heads isO.lgn/.

The probability that each coin flip is a head is 1=2. LetAi k be the event that a streak of heads of length at leastkbegins with theith coin flip or, more precisely, the event that thekconsecutive coin flipsi; iC1; : : : ; i Ck1yield only heads, where1knand1i nkC1. Since coin flips are mutually independent, for any given eventAi k, the probability that allkflips are heads is

PrfAi kg D1=2k : (5.8)

Fork D2dlgne,

PrfAi;2dlgneg D 1=22dlgne 1=22lgn D 1=n2;

and thus the probability that a streak of heads of length at least2dlgnebegins in positioni is quite small. There are at mostn2dlgne C1positions where such a streak can begin. The probability that a streak of heads of length at least2dlgne begins anywhere is therefore

Pr

(n2dlgneC1 [

iD1

Ai;2dlgne

)

n2dlgXneC1

iD1

1=n2

<

Xn iD1

1=n2

D 1=n ; (5.9)

since by Boole’s inequality (C.19), the probability of a union of events is at most the sum of the probabilities of the individual events. (Note that Boole’s inequality holds even for events such as these that are not independent.)

We now use inequality (5.9) to bound the length of the longest streak. For j D0; 1; 2; : : : ; n, letLj be the event that the longest streak of heads has length ex- actlyj, and letLbe the length of the longest streak. By the definition of expected value, we have

EŒLD Xn jD0

jPrfLjg : (5.10)

We could try to evaluate this sum using upper bounds on each PrfLjgsimilar to those computed in inequality (5.9). Unfortunately, this method would yield weak bounds. We can use some intuition gained by the above analysis to obtain a good bound, however. Informally, we observe that for no individual term in the sum- mation in equation (5.10) are both the factors j and PrfLjglarge. Why? When j 2dlgne, then PrfLjgis very small, and whenj < 2dlgne, then j is fairly small. More formally, we note that the eventsLj forj D0; 1; : : : ; nare disjoint, and so the probability that a streak of heads of length at least2dlgnebegins any- where isPn

jD2dlgnePrfLjg. By inequality (5.9), we havePn

jD2dlgnePrfLjg< 1=n.

Also, noting that Pn

jD0PrfLjg D 1, we have thatP2dlgne1

jD0 PrfLjg 1. Thus, we obtain

EŒL D Xn jD0

jPrfLjg

D

2dlgne1X

jD0

jPrfLjg C Xn jD2dlgne

jPrfLjg

<

2dlgne1X

jD0

.2dlgne/PrfLjg C Xn

jD2dlgne

nPrfLjg

D 2dlgne

2dlgXne1 jD0

PrfLjg Cn Xn

jD2dlgne

PrfLjg

< 2dlgne 1Cn.1=n/

D O.lgn/ :

The probability that a streak of heads exceedsrdlgneflips diminishes quickly withr. Forr 1, the probability that a streak of at leastrdlgne heads starts in positioni is

PrfAi;rdlgneg D 1=2rdlgne 1=nr :

Thus, the probability is at most n=nr D 1=nr1 that the longest streak is at leastrdlgne, or equivalently, the probability is at least11=nr1that the longest streak has length less thanrdlgne.

As an example, fornD1000coin flips, the probability of having a streak of at least2dlgne D20heads is at most1=nD1=1000. The chance of having a streak longer than3dlgne D30heads is at most1=n2D1=1,000,000.

We now prove a complementary lower bound: the expected length of the longest streak of heads inncoin flips is.lgn/. To prove this bound, we look for streaks

of length s by partitioning the n flips into approximately n=s groups of s flips each. If we chooses D b.lgn/=2c, we can show that it is likely that at least one of these groups comes up all heads, and hence it is likely that the longest streak has length at leastsD.lgn/. We then show that the longest streak has expected length.lgn/.

We partition the ncoin flips into at leastbn=b.lgn/=2ccgroups of b.lgn/=2c consecutive flips, and we bound the probability that no group comes up all heads.

By equation (5.8), the probability that the group starting in positionicomes up all heads is

PrfAi;b.lgn/=2cg D 1=2b.lgn/=2c 1=p

n :

The probability that a streak of heads of length at leastb.lgn/=2cdoes not begin in positioni is therefore at most11=p

n. Since thebn=b.lgn/=2ccgroups are formed from mutually exclusive, independent coin flips, the probability that every one of these groupsfailsto be a streak of lengthb.lgn/=2cis at most

11=p

nbn=b.lgn/=2cc

11=p

nn=b.lgn/=2c1

11=p

n2n=lgn1

e.2n=lgn1/=

pn

D O.elgn/ D O.1=n/ :

For this argument, we used inequality (3.12),1Cxex, and the fact, which you might want to verify, that.2n=lgn1/=p

nlgnfor sufficiently largen.

Thus, the probability that the longest streak exceedsb.lgn/=2cis Xn

jDb.lgn/=2cC1

PrfLjg 1O.1=n/ : (5.11)

We can now calculate a lower bound on the expected length of the longest streak, beginning with equation (5.10) and proceeding in a manner similar to our analysis of the upper bound:

EŒL D Xn jD0

jPrfLjg

D

b.lgXn/=2c

jD0

jPrfLjg C

Xn

jDb.lgn/=2cC1

jPrfLjg

b.lgXn/=2c jD0

0PrfLjg C

Xn

jDb.lgn/=2cC1

b.lgn/=2cPrfLjg

D 0

b.lgXn/=2c jD0

PrfLjg C b.lgn/=2c

Xn

jDb.lgn/=2cC1

PrfLjg 0C b.lgn/=2c.1O.1=n// (by inequality (5.11)) D .lgn/ :

As with the birthday paradox, we can obtain a simpler but approximate analysis using indicator random variables. We letXi k D IfAi kgbe the indicator random variable associated with a streak of heads of length at least kbeginning with the ith coin flip. To count the total number of such streaks, we define

X D

nkC1X

iD1

Xi k :

Taking expectations and using linearity of expectation, we have EŒX D E

"nkC1 X

iD1

Xi k

#

D

nkC1X

iD1

EŒXi k

D

nkC1X

iD1

PrfAi kg

D

nkC1X

iD1

1=2k

D nkC1 2k :

By plugging in various values for k, we can calculate the expected number of streaks of lengthk. If this number is large (much greater than1), then we expect many streaks of lengthk to occur and the probability that one occurs is high. If

this number is small (much less than1), then we expect few streaks of lengthkto occur and the probability that one occurs is low. Ifk D clgn, for some positive constantc, we obtain

EŒX D nclgnC1 2clgn D nclgnC1

nc D 1

nc1 .clgn1/=n nc1 D ‚.1=nc1/ :

Ifc is large, the expected number of streaks of lengthclgnis small, and we con- clude that they are unlikely to occur. On the other hand, ifc D1=2, then we obtain EŒX D ‚.1=n1=21/ D ‚.n1=2/, and we expect that there are a large number of streaks of length.1=2/lgn. Therefore, one streak of such a length is likely to occur. From these rough estimates alone, we can conclude that the expected length of the longest streak is‚.lgn/.

Dalam dokumen Introduction to Algorithms, Third Edition (Halaman 156-160)