probabilistic analysis of algorithms: what's it good...

143
Probabilistic analysis of algorithms: What’s it good for? Conrado Martínez Univ. Politècnica de Catalunya, Spain University of Cape Town February 2008

Upload: others

Post on 26-Jan-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Probabilistic analysis of algorithms:What’s it good for?

Conrado Martínez

Univ. Politècnica de Catalunya, Spain

University of Cape TownFebruary 2008

Page 2: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

1 Introduction

2 Example #1: Generating random derangements

3 Example #2: Updating K­d trees

4 Example #3: Partial sorting

5 Concluding remarks

Page 3: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

Probabilistic analysis of algorithms is the right toolwhen

We want to analyze “typical” behavior ofalgorithmsWe want to compare algorithms withasymptotically equivalent performancesWe want to analyze randomized algorithms(essential!)We want to have some mathematical fun :)

Page 4: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

Probabilistic analysis of algorithms is the right toolwhen

We want to analyze “typical” behavior ofalgorithmsWe want to compare algorithms withasymptotically equivalent performancesWe want to analyze randomized algorithms(essential!)We want to have some mathematical fun :)

Page 5: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

Probabilistic analysis of algorithms is the right toolwhen

We want to analyze “typical” behavior ofalgorithmsWe want to compare algorithms withasymptotically equivalent performancesWe want to analyze randomized algorithms(essential!)We want to have some mathematical fun :)

Page 6: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

Probabilistic analysis of algorithms is the right toolwhen

We want to analyze “typical” behavior ofalgorithmsWe want to compare algorithms withasymptotically equivalent performancesWe want to analyze randomized algorithms(essential!)We want to have some mathematical fun :)

Page 7: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 8: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 9: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 10: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 11: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 12: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

A few well known examples:QuicksortFind, a.k.a. QuickselectHashingSimplexRandomized data structuresand many more . . .

Page 13: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

It was indeed very difficult for me to make a choice ofexamples . . .

Page 14: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Introduction

. . . even if I restricted myself to those few that I’veworked out myself!

Randomized binary search treesOptimal sampling for quicksort and quickselectAdaptive sampling for quickselectUpdates and associative queries in relaxed K­dtreesExhaustive and random generation ofcombinatorial objectsPartial sortingProbabilistic analysis of binary search trees, skiplists, . . .

Page 15: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

1 Introduction

2 Example #1: Generating random derangements

3 Example #2: Updating K­d trees

4 Example #3: Partial sorting

5 Concluding remarks

Page 16: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #1: Generating random derangements

Le Problème des Derangements:“A number of gentlemen, say n, surrender their tophats in the cloakroom and proceed to the evening’senjoyment. After wining and dining (and wining somemore), they stumble back to the cloakroom andconfusedly take the first top­hat they see. What is theprobability that no gentleman gets his own hat?”

Page 17: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Derangements

A derangement is a permutation without fixedpoints: �(i) 6= i for any i, 1 � i � nThe number Dn of derangements of size n isDn = n! � � 10! � 11! + 12! � 13! + � � �+ (�1)nn! � = �n! + 1e � :As n!1, Dn=n! � 1=e � 0:36788. In fact, e�1 is aextremely good approximation to the probabilitythat a random permutation is a derangement forn � 10.

Page 18: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Derangements

A derangement is a permutation without fixedpoints: �(i) 6= i for any i, 1 � i � nThe number Dn of derangements of size n isDn = n! � � 10! � 11! + 12! � 13! + � � �+ (�1)nn! � = �n! + 1e � :As n!1, Dn=n! � 1=e � 0:36788. In fact, e�1 is aextremely good approximation to the probabilitythat a random permutation is a derangement forn � 10.

Page 19: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Derangements

A derangement is a permutation without fixedpoints: �(i) 6= i for any i, 1 � i � nThe number Dn of derangements of size n isDn = n! � � 10! � 11! + 12! � 13! + � � �+ (�1)nn! � = �n! + 1e � :As n!1, Dn=n! � 1=e � 0:36788. In fact, e�1 is aextremely good approximation to the probabilitythat a random permutation is a derangement forn � 10.

Page 20: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: Fisher­Yates’ shuffle

procedure RandomPermutation(n)for i 1 to n do A[i℄ ifor i n downto 1 doj Uniform(1; i)A[i℄$ A[j℄return A

Page 21: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: Sattolo’s algorithm

procedure RandomCyclicPermutation(n)for i 1 to n do A[i℄ ifor i n downto 1 doj Uniform(1; i� 1)A[i℄$ A[j℄return A

Page 22: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: The rejection method

Require: n 6= 1procedure RandomDerangement(n)

repeatA RandomPermutation(n)until Is­Derangement(A) return A

P[A is a derangement℄ � 1eE [# of calls to Random℄ = e � n+O(1)

Page 23: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: The rejection method

Require: n 6= 1procedure RandomDerangement(n)

repeatA RandomPermutation(n)until Is­Derangement(A) return A

P[A is a derangement℄ � 1eE [# of calls to Random℄ = e � n+O(1)

Page 24: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A recurrence for the number ofderangements

D0 = 1;D1 = 0Dn = (n� 1)Dn�1 + (n� 1)Dn�2

Page 25: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A recurrence for the number ofderangements

D0 = 1;D1 = 0Dn = (n� 1)Dn�1 + (n� 1)Dn�2Choice #1: n belongs to a cycle of length > 2.The derangement of size n is built by constructing aderangement of size n� 1 and then n is inserted intoany of the cycles (of length � 2); there are (n� 1)possible ways to do that

Page 26: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A recurrence for the number ofderangements

D0 = 1;D1 = 0Dn = (n� 1)Dn�1 + (n� 1)Dn�2Choice #2: n belongs to a cycle of length 2.The derangement of size n is built by constructing acycle of size 2 with n and some j, 1 � j � n� 1; then webuild a derangement of size n� 2 with the remainingelements

Page 27: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The recursive methodC f1; 2; : : : ; ngRandomDerangement­Rec(n;C)

Require: n 6= 1procedure RandomDerangement­Rec(n, C)

if n � 1 then returnj a random element from Cp Uniform(0; 1)if p < (n� 1)Dn�2=Dn then

RandomDerangement­Rec(n� 2; C n fj; ng)�(n) j;�(j) nelse

RandomDerangement­Rec(n� 1; C n fng)�(n) �(j);�(j) n

Page 28: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Our algorithm

Require: n 6= 1procedure RandomDerangement(n)

for i 1 to n do A[i℄ i;mark[i℄ falsei n;u nwhile u � 2 do

if :mark[i℄ thenj a random unmarked element in A[1::i� 1℄A[i℄$ A[j℄if j has to close a cycle thenmark[j℄ true; u u� 1u u� 1i i� 1

return A

Page 29: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Our algorithm

Require: n 6= 1procedure RandomDerangement(n)

for i 1 to n do A[i℄ i;mark[i℄ falsei n;u nwhile u � 2 do

if :mark[i℄ thenrepeat j Random(1; i � 1)until :mark[j℄A[i℄$ A[j℄p Uniform(0; 1)if p < (u� 1)Du�2=Dumark[j℄ true;u u� 1u u� 1i i� 1

return A

Page 30: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

# of marked elements = # of cycles (Cn)# of iterations = # of calls to Uniform = n�CnG = # of calls to RandomGi = # of calls to Random at iteration iE [cost℄ = n� E [Cn℄ + E [G℄= n� E [Cn℄ + X1<i�n E [Gi℄

Page 31: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

# of marked elements = # of cycles (Cn)# of iterations = # of calls to Uniform = n�CnG = # of calls to RandomGi = # of calls to Random at iteration iE [cost℄ = n� E [Cn℄ + E [G℄= n� E [Cn℄ + X1<i�n E [Gi℄

Page 32: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

# of marked elements = # of cycles (Cn)# of iterations = # of calls to Uniform = n�CnG = # of calls to RandomGi = # of calls to Random at iteration iE [cost℄ = n� E [Cn℄ + E [G℄= n� E [Cn℄ + X1<i�n E [Gi℄

Page 33: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

# of marked elements = # of cycles (Cn)# of iterations = # of calls to Uniform = n�CnG = # of calls to RandomGi = # of calls to Random at iteration iE [cost℄ = n� E [Cn℄ + E [G℄= n� E [Cn℄ + X1<i�n E [Gi℄

Page 34: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

# of marked elements = # of cycles (Cn)# of iterations = # of calls to Uniform = n�CnG = # of calls to RandomGi = # of calls to Random at iteration iE [cost℄ = n� E [Cn℄ + E [G℄= n� E [Cn℄ + X1<i�n E [Gi℄

Page 35: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The computation of E [Cn℄ can be done via standardgenerating function techniques:C(z; v) = XA2D zjAjjAj!v# cycles(A)= exp�v �log 11� z � z�� = e�vz 1(1� z)vE hvCni = n!Dn [zn℄C(z; v) = e1�v(v � 1)!nv�1(1 +O(n�1+�))E [Cn℄ = log n+O(1); V[Cn℄ = log n+O(1)Cn � log nplog n ! N (0; 1)

Page 36: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The computation of E [Cn℄ can be done via standardgenerating function techniques:C(z; v) = XA2D zjAjjAj!v# cycles(A)= exp�v �log 11� z � z�� = e�vz 1(1� z)vE hvCni = n!Dn [zn℄C(z; v) = e1�v(v � 1)!nv�1(1 +O(n�1+�))E [Cn℄ = log n+O(1); V[Cn℄ = log n+O(1)Cn � log nplog n ! N (0; 1)

Page 37: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The computation of E [Cn℄ can be done via standardgenerating function techniques:C(z; v) = XA2D zjAjjAj!v# cycles(A)= exp�v �log 11� z � z�� = e�vz 1(1� z)vE hvCni = n!Dn [zn℄C(z; v) = e1�v(v � 1)!nv�1(1 +O(n�1+�))E [Cn℄ = log n+O(1); V[Cn℄ = log n+O(1)Cn � log nplog n ! N (0; 1)

Page 38: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The computation of E [Cn℄ can be done via standardgenerating function techniques:C(z; v) = XA2D zjAjjAj!v# cycles(A)= exp�v �log 11� z � z�� = e�vz 1(1� z)vE hvCni = n!Dn [zn℄C(z; v) = e1�v(v � 1)!nv�1(1 +O(n�1+�))E [Cn℄ = log n+O(1); V[Cn℄ = log n+O(1)Cn � log nplog n ! N (0; 1)

Page 39: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The computation of E [Cn℄ can be done via standardgenerating function techniques:C(z; v) = XA2D zjAjjAj!v# cycles(A)= exp�v �log 11� z � z�� = e�vz 1(1� z)vE hvCni = n!Dn [zn℄C(z; v) = e1�v(v � 1)!nv�1(1 +O(n�1+�))E [Cn℄ = log n+O(1); V[Cn℄ = log n+O(1)Cn � log nplog n ! N (0; 1)

Page 40: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Mi indicator variable for the event “A[i℄ getsmarked”Mi = 1 =) Gi = 0E [G℄ = X1<i�n E [Gi jMi = 0℄ � P[Mi = 0℄

Page 41: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Mi indicator variable for the event “A[i℄ getsmarked”Mi = 1 =) Gi = 0E [G℄ = X1<i�n E [Gi jMi = 0℄ � P[Mi = 0℄

Page 42: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Mi indicator variable for the event “A[i℄ getsmarked”Mi = 1 =) Gi = 0E [G℄ = X1<i�n E [Gi jMi = 0℄ � P[Mi = 0℄

Page 43: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysisUi = # of unmarked elements in A[1::i℄;Un = nBi+1 = # of marked elements in A[1::i℄;Bn+1 = 0Ui +Bi+1 = iIf A[i℄ is not marked then Gi is geometricallydistributed with probability of success(Ui � 1)=(i � 1) = (i� 1�Bi+1)=(i� 1); henceE [Gi jMi = 0℄ = E � i� 1i� 1�Bi+1 jMi = 0�

Page 44: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysisUi = # of unmarked elements in A[1::i℄;Un = nBi+1 = # of marked elements in A[1::i℄;Bn+1 = 0Ui +Bi+1 = iIf A[i℄ is not marked then Gi is geometricallydistributed with probability of success(Ui � 1)=(i � 1) = (i� 1�Bi+1)=(i� 1); henceE [Gi jMi = 0℄ = E � i� 1i� 1�Bi+1 jMi = 0�

Page 45: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysisUi = # of unmarked elements in A[1::i℄;Un = nBi+1 = # of marked elements in A[1::i℄;Bn+1 = 0Ui +Bi+1 = iIf A[i℄ is not marked then Gi is geometricallydistributed with probability of success(Ui � 1)=(i � 1) = (i� 1�Bi+1)=(i� 1); henceE [Gi jMi = 0℄ = E � i� 1i� 1�Bi+1 jMi = 0�

Page 46: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysisUi = # of unmarked elements in A[1::i℄;Un = nBi+1 = # of marked elements in A[1::i℄;Bn+1 = 0Ui +Bi+1 = iIf A[i℄ is not marked then Gi is geometricallydistributed with probability of success(Ui � 1)=(i � 1) = (i� 1�Bi+1)=(i� 1); henceE [Gi jMi = 0℄ = E � i� 1i� 1�Bi+1 jMi = 0�

Page 47: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Bi+1 � Cn0 � Bi+1 � iUi 6= 1 and Bi+1 6= i� 1 for all 1 � i � nIf Mi = 0 then Bi+1 < i� 1

Page 48: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Bi+1 � Cn0 � Bi+1 � iUi 6= 1 and Bi+1 6= i� 1 for all 1 � i � nIf Mi = 0 then Bi+1 < i� 1

Page 49: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Bi+1 � Cn0 � Bi+1 � iUi 6= 1 and Bi+1 6= i� 1 for all 1 � i � nIf Mi = 0 then Bi+1 < i� 1

Page 50: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Bi+1 � Cn0 � Bi+1 � iUi 6= 1 and Bi+1 6= i� 1 for all 1 � i � nIf Mi = 0 then Bi+1 < i� 1

Page 51: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

E [G℄ = X1<i�n E � i� 1i� 1�Bi+1 jMi = 0� � P[Mi = 0℄� X1<i�n E �min�i� 1; i� 1i� 1� Cn��� X1�k�bn=2 P[Cn = k℄0�k+1Xi=1(i� 1) + bn=2 Xi=k+2 i� 1i� 1� k1A= n� 1� E [Cn℄ + 12 E hCn2i+O(E [Cn log(n� Cn)℄)= n+O(E hCn2i) +O(log n � E [Cn℄) = n+O(log2 n)

Page 52: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

E [G℄ = X1<i�n E � i� 1i� 1�Bi+1 jMi = 0� � P[Mi = 0℄� X1<i�n E �min�i� 1; i� 1i� 1� Cn��� X1�k�bn=2 P[Cn = k℄0�k+1Xi=1(i� 1) + bn=2 Xi=k+2 i� 1i� 1� k1A= n� 1� E [Cn℄ + 12 E hCn2i+O(E [Cn log(n� Cn)℄)= n+O(E hCn2i) +O(log n � E [Cn℄) = n+O(log2 n)

Page 53: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

E [G℄ = X1<i�n E � i� 1i� 1�Bi+1 jMi = 0� � P[Mi = 0℄� X1<i�n E �min�i� 1; i� 1i� 1� Cn��� X1�k�bn=2 P[Cn = k℄0�k+1Xi=1(i� 1) + bn=2 Xi=k+2 i� 1i� 1� k1A= n� 1� E [Cn℄ + 12 E hCn2i+O(E [Cn log(n� Cn)℄)= n+O(E hCn2i) +O(log n � E [Cn℄) = n+O(log2 n)

Page 54: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

E [G℄ = X1<i�n E � i� 1i� 1�Bi+1 jMi = 0� � P[Mi = 0℄� X1<i�n E �min�i� 1; i� 1i� 1� Cn��� X1�k�bn=2 P[Cn = k℄0�k+1Xi=1(i� 1) + bn=2 Xi=k+2 i� 1i� 1� k1A= n� 1� E [Cn℄ + 12 E hCn2i+O(E [Cn log(n� Cn)℄)= n+O(E hCn2i) +O(log n � E [Cn℄) = n+O(log2 n)

Page 55: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

E [G℄ = X1<i�n E � i� 1i� 1�Bi+1 jMi = 0� � P[Mi = 0℄� X1<i�n E �min�i� 1; i� 1i� 1� Cn��� X1�k�bn=2 P[Cn = k℄0�k+1Xi=1(i� 1) + bn=2 Xi=k+2 i� 1i� 1� k1A= n� 1� E [Cn℄ + 12 E hCn2i+O(E [Cn log(n� Cn)℄)= n+O(E hCn2i) +O(log n � E [Cn℄) = n+O(log2 n)

Page 56: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Since we also have E [G℄ � n� E [Cn℄, we have finallyE [cost℄ = n� E [Cn℄ + E [G℄ = 2n+O(log2 n)

Page 57: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

1 Introduction

2 Example #1: Generating random derangements

3 Example #2: Updating K­d trees

4 Example #3: Partial sorting

5 Concluding remarks

Page 58: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

Page 59: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

1

1

Page 60: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

1

2

1

2

Page 61: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

1

2

1

2

33

Page 62: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

1

2

1

2

33

4

4

Page 63: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #2: Updating K­d trees

1

2

1

2

33

4

4

5 5

Page 64: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Insertion in relaxed K­d treesrkdt insert (rkdt t, onst Elem& x) {int n = size(t);int u = random (0,n);if (u == n)return insert_at_root(t, x);else { // t annot be emptyint i = t -> dis r;if (x[i℄ < t -> key [i℄)t -> left = insert (t -> left , x);elset -> right = insert (t -> right , x);return t;}}

Page 65: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Deletion in relaxed K­d treesrkdt delete (rkdt t, onst Elem& x) {if (t == NULL) return NULL;if (t -> key == x)return join(t -> left , t -> right );int i = t -> dis r;if (x -> key[i℄ < t -> key[i℄)t -> left = delete (t -> left , x);elset -> right = delete (t -> right , x);return t;}

Page 66: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #1

t r

Page 67: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #1

t r

Page 68: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #1

t r

Page 69: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #2

t

r

Page 70: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #2

t

r

Page 71: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #2

t

r

Page 72: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Split: Case #2

t

r

Page 73: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/joinsn = avg. number of visited nodes in a splitmn = avg. number of visited nodes in a joinsn = 1 + 2nK X0�j<n j + 1n+ 1sj + 2(K � 1)nK X0�j<n sj+ K � 1K X0�j<n�n;jmj ;where �n;j is probability of joining two trees withtotal size j .

Page 74: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/joinsn = avg. number of visited nodes in a splitmn = avg. number of visited nodes in a joinsn = 1 + 2nK X0�j<n j + 1n+ 1sj + 2(K � 1)nK X0�j<n sj+ K � 1K X0�j<n�n;jmj ;where �n;j is probability of joining two trees withtotal size j .

Page 75: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/joinsn = avg. number of visited nodes in a splitmn = avg. number of visited nodes in a joinsn = 1 + 2nK X0�j<n j + 1n+ 1sj + 2(K � 1)nK X0�j<n sj+ K � 1K X0�j<n�n;jmj ;where �n;j is probability of joining two trees withtotal size j .

Page 76: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

The recurrence for sn issn = 1 + 2nK X0�j<n j + 1n+ 1sj + 2(K � 1)nK X0�j<n sj+ 2(K � 1)nK X0�j<n n� jn+ 1mj ;with s0 = 0.The recurrence for mn has exactly the same shapewith the rôles of sn and mn interchanged; it easilyfollows that sn = mn.

Page 77: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

Define S(z) = Xn�0 snznThe recurrence for sn translates toz d2Sdz2 + 21� 2z1� z dSdz� 2�3K � 2K � z� S(z)(1� z)2 = 2(1� z)3 ;with initial conditions S(0) = 0 and S0(0) = 1.

Page 78: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

The homogeneous second order linear ODE is ofhypergeometric type.An easy particular solution of the ODE is�12 � KK � 1� 11� z

Page 79: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

TheoremThe generating function S(z) of the expected cost ofsplit is, for any K � 2,S(z) = 12 11� 1K �(1� z)�� � 2F1 � 1� �; 2 � �2 ���� z�� 11� z � ;where � = �(K) = 12 �1 +q17� 16K �.

Page 80: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

TheoremThe expected cost sn of splitting a relaxed K­d tree ofsize n is sn = �(K)n�(K) + o(n);with � = 12 11� 1K �(2�� 1)��3(�) ;� = �� 1 = 12 0�s17� 16K � 11A :

Page 81: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of split/join

7060

1.1

K

100908010

1.2

403020

1.5

1.4

50

1.3

1.0

Plot of �(K)

Page 82: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The cost of insertions and deletions

The recurrence for the expected cost of aninsertion isIn = Inn+ 1 + �1� 1n+ 1�0�1 + 2n X0�j<n j + 1n+ 1Ij1A= Inn+ 1 + 1 +O� 1n�+ 2n+ 1 X0�j<n j + 1n+ 1Ij :with In the average cost of an insertion at rootThe expected cost of deletions Dn satisfies asimilar recurrence; it is asymptotically equivalent tothe average cost of insertionsWe substitute In by the costs obtained previously(sn)

Page 83: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The cost of insertions and deletions

TheoremLet In and Dn denote the average cost of arandomized insertion and randomized deletion in arandom relaxed K­d tree of size n using split and join.Then1 if K = 2 then In � Dn = 4 lnn+O(1).2 if K > 2 thenIn � Dn = ��� 1�+ 1n��1 +O(log n);

where In = � n� +O(1).

Page 84: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The cost of insertions and deletions

TheoremLet In and Dn denote the average cost of arandomized insertion and randomized deletion in arandom relaxed K­d tree of size n using split and join.Then1 if K = 2 then In � Dn = 4 lnn+O(1).2 if K > 2 thenIn � Dn = ��� 1�+ 1n��1 +O(log n);

where In = � n� +O(1).Note that for K > 2, �(K) > 1!

Page 85: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Copy­based insertions

2

1

3

4

5

x

x

1

2

3 4

5

Page 86: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Copy­based insertions

5 2

3 4

x

1

2

1

3

4

5

x

Page 87: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Copy­based insertions

x

1

2

3 4

5

2

1

3

4

5

x

Page 88: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Copy­based insertions

32

1

3

4

5

x

5

x

1

2

4

Page 89: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Copy­based insertions

3

5

x

1

2

4

2

1

3

4

5

x

Page 90: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: Partial match

Given a query q = (q0; : : : ; qK1) where each qi 2 [0; 1℄ orqi = �, find all elements x in the K­d tree such thatxi = qi whenever qi 6= �.Partial matchvoid partial_mat h(rkdt t, query q) {if (t == NULL) return ;if (mat hes (t -> key , q))report (t-> key );int i = t -> dis r ;if (q[i℄ == '*') {partial_mat h(t -> left , q);partial_mat h(t -> right , q);} else if (q[i℄ < t -> key) {partial_mat h(t -> left , q);} else {partial_mat h(t -> left , q);}}

Page 91: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

The cost of building T using copy­based insertion of akey x:C(T ) = P (T ) + 1K jLj+ 1jT j+ 1C(L) + 1K jRj+ 1jT j+ 1C(R)+ K � 1K (C(L) + C(R)) ;where P (T ) denotes the number of nodes visited by apartial match in T � fxg with queryq = (x0; : : : ; xi�1; �; xi+1; : : : ; xK�1)

Page 92: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

The cost of making an insertion at root into a treeof size n:Cn = Pn + 2nK X0�k<n k + 1n+ 1Ck + 2(K � 1)nK X0�k<nCk:with Pn the expected cost of a partial match in arandom relaxed K­d tree of size n with only onespecified coordinate out of K coordinates

Page 93: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

Theorem (Duch et al. 1998, Martínez et al. 2001)The expected cost Pn (measured as the number of keycomparisons) of a partial match query with s out of Kattributes specified, 0 < s < K , in a randomly builtrelaxed K­d tree of size n isPn = �(s=K) � n�(s=K) +O(1);where � = �(x) = �p9� 8x� 1� =2;�(x) = �(2� + 1)(1� x)(� + 1)�3 (�+ 1) ;and �(x) is Euler’s Gamma function.

Page 94: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

We will use Roura’s Continuous Master Theorem tosolve recurrences of the form:Fn = tn + X0�j<nwn;jFj ; n � n0;where tn is the so­called toll function and thequantities wn;j � 0 are called weights

Page 95: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Excursion: Roura’s Continuous MasterTheorem

Theorem (Roura 2001)Let tn � Cna logb n for some constants C , a � 0 andb > �1, and let !(z) be a real function over [0; 1℄ suchthat X0�j<n �����wn;j � Z (j+1)=nj=n !(z) dz����� = O(n�d)for some constant d > 0. Let �(x) = R 10 zx !(z) dz, anddefine H = 1� �(a). Then1 If H > 0 then Fn � tn =H.2 If H = 0 then Fn � tn lnn=H0, whereH0 = �(b+ 1) R 10 za ln z !(z) dz .3 If H < 0 then Fn = �(n�), where � is the unique real

solution of �(x) = 1.

Page 96: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

Applying the CMT to our recurrence we have!(z) = 2zK + 2(K�1)Ktn = Pn =) a = % = �(1=K) = (p9� 8=K � 1)=2Thus H = 0

Page 97: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

Applying the CMT to our recurrence we have!(z) = 2zK + 2(K�1)Ktn = Pn =) a = % = �(1=K) = (p9� 8=K � 1)=2Thus H = 0We have to compute H0 with b = 0H0 = �(b+ 1) Z 10 za!(z) ln z dzand get H0 = 2K%2 + (4K � 2)% + 4K � 3K(%+ 2)2(%+ 1)2 :

Page 98: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Analysis of copy­based updates

TheoremThe average cost Cn of copy­based insertion at rootof a random relaxed K­d tree isCn = � n% lnn+ o(n lnn);where% = %(K) = �(1=K) = �q9� 8=K � 1� =2; = �(1=K)H0 = �(2% + 1)K(%+ 2)2(%+ 1)2(1 � 1K )�3(%+ 1)(K%2 + (4K � 2)% + (4K � 3)) :The average cost C 0n of copy­based deletion of theroot of a random relaxed K­d tree of size n+ 1 is Cn .

Page 99: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The cost of insertions and deletions (2)

TheoremFor any fixed dimension K � 2, the average cost of arandomized insertion or deletion in random relaxedK­d tree of size n using copy­based updates isIn � Dn = 2 lnn+�(1):

Page 100: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The cost of insertions and deletions (2)

TheoremFor any fixed dimension K � 2, the average cost of arandomized insertion or deletion in random relaxedK­d tree of size n using copy­based updates isIn � Dn = 2 lnn+�(1):The “reconstruction” phase has constant cost on theaverage!

Page 101: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

1 Introduction

2 Example #1: Generating random derangements

3 Example #2: Updating K­d trees

4 Example #3: Partial sorting

5 Concluding remarks

Page 102: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #3: Partial sorting

Partial sorting: Given an array A of n elementsand a value 1 � m � n, rearrange A so that its firstm positions contain the m smallest elements inascending orderFor m = �(n) it might be OK to sort the array;otherwise, we are doing too much work

Page 103: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Example #3: Partial sorting

Partial sorting: Given an array A of n elementsand a value 1 � m � n, rearrange A so that its firstm positions contain the m smallest elements inascending orderFor m = �(n) it might be OK to sort the array;otherwise, we are doing too much work

Page 104: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #1: Partial heapsortBuild a heap with the n elements and perform mextractions of the heap’s minimumThe worst­case cost is �(n+m logn)This the “traditonal” implementation of C++ STL’spartial_sort

Page 105: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #1: Partial heapsortBuild a heap with the n elements and perform mextractions of the heap’s minimumThe worst­case cost is �(n+m logn)This the “traditonal” implementation of C++ STL’spartial_sort

Page 106: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #1: Partial heapsortBuild a heap with the n elements and perform mextractions of the heap’s minimumThe worst­case cost is �(n+m logn)This the “traditonal” implementation of C++ STL’spartial_sort

Page 107: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #2: On­line selectionBuild a heap with the m first elements; then scanthe remaining n�m elements and update the heapas needed; finally extract the m elements from theheapThe worst­case cost is �(n logm)Not very attractive unless m is very small or if usedin on­line settings

Page 108: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #2: On­line selectionBuild a heap with the m first elements; then scanthe remaining n�m elements and update the heapas needed; finally extract the m elements from theheapThe worst­case cost is �(n logm)Not very attractive unless m is very small or if usedin on­line settings

Page 109: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #2: On­line selectionBuild a heap with the m first elements; then scanthe remaining n�m elements and update the heapas needed; finally extract the m elements from theheapThe worst­case cost is �(n logm)Not very attractive unless m is very small or if usedin on­line settings

Page 110: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #3: “Quickselsort”Find the mth smallest element with quickselect,then quicksort the preceding m� 1 elementsThe average cost is �(n+m logm)Uses two basic algorithms widely available (and highlytuned for performance in standard libraries)

Page 111: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #3: “Quickselsort”Find the mth smallest element with quickselect,then quicksort the preceding m� 1 elementsThe average cost is �(n+m logm)Uses two basic algorithms widely available (and highlytuned for performance in standard libraries)

Page 112: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

A few common solutions

Idea #3: “Quickselsort”Find the mth smallest element with quickselect,then quicksort the preceding m� 1 elementsThe average cost is �(n+m logm)Uses two basic algorithms widely available (and highlytuned for performance in standard libraries)

Page 113: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Partial quicksort

void partial_qui ksort(ve tor <Elem >& A,int i, int j, int m) {if (i < j) {int p = get_pivot (A, i, j);swap(A[p℄, A[l℄);int k;partition (A, i, j, k);partial_qui ksort(A, i, k - 1, m);if (k < m - 1)partial_qui ksort(A, k + 1, j, m);} }

Page 114: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Probability that the selected pivot is the k­th of nelements: �n;kAverage number of comparisons Pn;m to sort them smallest elements out of n:Pn;m = n� 1 + nXk=m+1�n;k � Pk�1;m+ mXk=1�n;k � (Pk�1;k�1 + Pn�k;m�k)

Page 115: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

Probability that the selected pivot is the k­th of nelements: �n;kAverage number of comparisons Pn;m to sort them smallest elements out of n:Pn;m = n� 1 + nXk=m+1�n;k � Pk�1;m+ mXk=1�n;k � (Pk�1;k�1 + Pn�k;m�k)

Page 116: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

For m = n, partial quicksort � quicksort; let qndenote the average number of comparisons usedby quicksortHence,Pn;m = n� 1 + X0�k<m�n;k+1 � qk+ nXk=m+1�n;k � Pk�1;m + mXk=1 �n;k � Pn�k;m�k (1)

Page 117: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

For m = n, partial quicksort � quicksort; let qndenote the average number of comparisons usedby quicksortHence,Pn;m = n� 1 + X0�k<m�n;k+1 � qk+ nXk=m+1�n;k � Pk�1;m + mXk=1 �n;k � Pn�k;m�k (1)

Page 118: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The recurrence for Pn;m is the same as forquickselect but the toll function istn;m = n� 1 + X0�k<m�n;k+1 � qkUp to now, everything holds no matter which pivotselection scheme do we use; for the standardvariant we must take �n;k = 1=n, for all 1 � k � n

Page 119: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis

The recurrence for Pn;m is the same as forquickselect but the toll function istn;m = n� 1 + X0�k<m�n;k+1 � qkUp to now, everything holds no matter which pivotselection scheme do we use; for the standardvariant we must take �n;k = 1=n, for all 1 � k � n

Page 120: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Define the two BGFsP (z; u) = Xn�0 X1�m�nPn;mznumT (z; u) = Xn�0 X1�m�n tn;mznumThen the recurrence (1) translates to�P�z = P (z; u)1� z + uP (z; u)1� uz + �T�z (2)

Page 121: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Define the two BGFsP (z; u) = Xn�0 X1�m�nPn;mznumT (z; u) = Xn�0 X1�m�n tn;mznumThen the recurrence (1) translates to�P�z = P (z; u)1� z + uP (z; u)1� uz + �T�z (2)

Page 122: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Let P (z; u) = F (z; u) + S(z; u), where F (z; u)corresponds to the selection part of the tollfunction (n� 1) and S(z; u) to the sorting part(Pk qk=n)Let TF (z; u) = Xn�0 X1�m�n(n� 1)znumTS(z; u) = Xn�0 X1�m�n 1n 0� X0�k<m qk1A znum

Page 123: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Let P (z; u) = F (z; u) + S(z; u), where F (z; u)corresponds to the selection part of the tollfunction (n� 1) and S(z; u) to the sorting part(Pk qk=n)Let TF (z; u) = Xn�0 X1�m�n(n� 1)znumTS(z; u) = Xn�0 X1�m�n 1n 0� X0�k<m qk1A znum

Page 124: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Then, each of F (z; u) and S(z; u) satisfies adifferential equation like (2) andF (z; u) = 1(1� z)(1 � zu)� �Z (1� z)(1 � zu)�TF�z dz +KF�S(z; u) = 1(1� z)(1 � zu)� �Z (1� z)(1 � zu)�TS�z dz +KS�

Page 125: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

F (z; u) satisfies exactly the same differentialequation as standard quickselect; it is well known(Knuth, 1971) that for 1 � m � n,Fn;m = [znum℄F (z; u) = 2�n+ 3 + (n+ 1)Hn� (m+ 2)Hm � (n+ 3�m)Hn+1�m�

Page 126: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

To compute S(z; u), we need first to determineTS(z; u) �TS�z = u1� z Q(uz)1� uzwhere Q(z) =Pn�0 qnzn .With the toll function n� 1, we solve therecurrence for quicksort to getQ(z) = 2(1� z)2 �ln 11� z � z�

Page 127: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

To compute S(z; u), we need first to determineTS(z; u) �TS�z = u1� z Q(uz)1� uzwhere Q(z) =Pn�0 qnzn .With the toll function n� 1, we solve therecurrence for quicksort to getQ(z) = 2(1� z)2 �ln 11� z � z�

Page 128: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Hence,S(z; u) = 1(1� z)(1� uz) �Z uQ(uz) dz +KS�= 2(1� uz)2(1� z) ln 11� uz+ 2(1� z)(1 � uz) ln 11� uz� 4 uz(1� uz)2(1� z)

Page 129: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Extracting coefficients Sn;m = [znum℄S(z; u)Sn;m = 2(m+ 1)Hm � 6m+ 2HmAnd finallyPn;m = 2n+ 2(n + 1)Hn � 2(n+ 3�m)Hn+1�m� 6m+ 6

Page 130: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

The analysis: Generating functions

Extracting coefficients Sn;m = [znum℄S(z; u)Sn;m = 2(m+ 1)Hm � 6m+ 2HmAnd finallyPn;m = 2n+ 2(n + 1)Hn � 2(n+ 3�m)Hn+1�m� 6m+ 6

Page 131: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Partial quicksort vs. quickselsort

The average number of comparisons made byquickselsort is Qn;m = Fn;m + qm�1Using partial quicksort we saveQn;m � Pn;m = 2m� 4Hm + 2comparisons on the average

Page 132: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Partial quicksort vs. quickselsort

The average number of comparisons made byquickselsort is Qn;m = Fn;m + qm�1Using partial quicksort we saveQn;m � Pn;m = 2m� 4Hm + 2comparisons on the average

Page 133: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Final remarks on partial quicksort

Partial quicksort avoids some of the redundantcomparisons, exchanges, . . . made by quickselsortIt is easily implementedIt benefits from standard optimization techniques:sampling, recursion removal, recursion cutoff onsmall subfiles, improved partitioning schems, etc.The same idea can be applied to similar algorithmslike radix sorting and quicksort for strings

Page 134: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Final remarks on partial quicksort

Partial quicksort avoids some of the redundantcomparisons, exchanges, . . . made by quickselsortIt is easily implementedIt benefits from standard optimization techniques:sampling, recursion removal, recursion cutoff onsmall subfiles, improved partitioning schems, etc.The same idea can be applied to similar algorithmslike radix sorting and quicksort for strings

Page 135: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Final remarks on partial quicksort

Partial quicksort avoids some of the redundantcomparisons, exchanges, . . . made by quickselsortIt is easily implementedIt benefits from standard optimization techniques:sampling, recursion removal, recursion cutoff onsmall subfiles, improved partitioning schems, etc.The same idea can be applied to similar algorithmslike radix sorting and quicksort for strings

Page 136: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Final remarks on partial quicksort

Partial quicksort avoids some of the redundantcomparisons, exchanges, . . . made by quickselsortIt is easily implementedIt benefits from standard optimization techniques:sampling, recursion removal, recursion cutoff onsmall subfiles, improved partitioning schems, etc.The same idea can be applied to similar algorithmslike radix sorting and quicksort for strings

Page 137: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

1 Introduction

2 Example #1: Generating random derangements

3 Example #2: Updating K­d trees

4 Example #3: Partial sorting

5 Concluding remarks

Page 138: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Concluding remarks

I hope I have convinced you about the usefulness ofprobabilistic analysis

Provides useful information about typical behaviorNecessary when analyzing randomized algorithmsAllows meaningful comparisons betweencompetitors of equivalent performanceA source of beautiful and challenging mathematicalproblems!

Page 139: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Concluding remarks

I hope I have convinced you about the usefulness ofprobabilistic analysis

Provides useful information about typical behaviorNecessary when analyzing randomized algorithmsAllows meaningful comparisons betweencompetitors of equivalent performanceA source of beautiful and challenging mathematicalproblems!

Page 140: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Concluding remarks

I hope I have convinced you about the usefulness ofprobabilistic analysis

Provides useful information about typical behaviorNecessary when analyzing randomized algorithmsAllows meaningful comparisons betweencompetitors of equivalent performanceA source of beautiful and challenging mathematicalproblems!

Page 141: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Concluding remarks

I hope I have convinced you about the usefulness ofprobabilistic analysis

Provides useful information about typical behaviorNecessary when analyzing randomized algorithmsAllows meaningful comparisons betweencompetitors of equivalent performanceA source of beautiful and challenging mathematicalproblems!

Page 142: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

Credits

Alois Panholzer and Helmut Prodinger:Generating random derangementsAmalia Duch: Updating K­d trees

Page 143: Probabilistic analysis of algorithms: What's it good for?conrado/research/talks/survey-UCT.pdfProbabilistic analysis of algorithms is the right tool when We want to analyze “typical”

THANKS!!