CS 573: Algorithms, Fall 2013 Randomized Algorithms Lecture 9 September 24, 2013 Sariel (UIUC) CS573 1 Fall 2013 1 / 32
Part I . Randomized Algorithms . Sariel (UIUC) CS573 2 Fall 2013 2 / 32
Probability - quick review Definitions . Definition (Informal) . Random variable : a function from probability space to I R . Associates value ∀ atomic events in probability space. . . Definition . The conditional probability of X given Y is [ ] Pr ( X = x ) ∩ ( Y = y ) [ � ] Pr X = x � Y = y = . � [ ] Pr Y = y Equivalent to [ ] [ � ] [ ] Pr ( X = x ) ∩ ( Y = y ) = Pr X = x � Y = y ∗ Pr Y = y . � . Sariel (UIUC) CS573 3 Fall 2013 3 / 32
Probability - quick review Definitions . Definition (Informal) . Random variable : a function from probability space to I R . Associates value ∀ atomic events in probability space. . . Definition . The conditional probability of X given Y is [ ] Pr ( X = x ) ∩ ( Y = y ) [ � ] Pr X = x � Y = y = . � [ ] Pr Y = y Equivalent to [ ] [ � ] [ ] Pr ( X = x ) ∩ ( Y = y ) = Pr X = x � Y = y ∗ Pr Y = y . � . Sariel (UIUC) CS573 3 Fall 2013 3 / 32
Probability - quick review Definitions . Definition (Informal) . Random variable : a function from probability space to I R . Associates value ∀ atomic events in probability space. . . Definition . The conditional probability of X given Y is [ ] Pr ( X = x ) ∩ ( Y = y ) [ � ] Pr X = x � Y = y = . � [ ] Pr Y = y Equivalent to [ ] [ � ] [ ] Pr ( X = x ) ∩ ( Y = y ) = Pr X = x � Y = y ∗ Pr Y = y . � . Sariel (UIUC) CS573 3 Fall 2013 3 / 32
Probability - quick review Even more definitions . Definition . The events X = x and Y = y are independent , if Pr[ X = x ∩ Y = y ] = Pr[ X = x ] · Pr[ Y = y ] . ≡ . . Definition . The expectation of a random variable X its average value: [ ] ∑ = x · Pr[ X = x ] , E X x . Sariel (UIUC) CS573 4 Fall 2013 4 / 32
Probability - quick review Even more definitions . Definition . The events X = x and Y = y are independent , if Pr[ X = x ∩ Y = y ] = Pr[ X = x ] · Pr[ Y = y ] . � [ ] ≡ Pr X = x � Y = y = Pr[ X = x ] . � . . Definition . The expectation of a random variable X its average value: [ ] ∑ = x · Pr[ X = x ] , E X x . Sariel (UIUC) CS573 4 Fall 2013 4 / 32
Linearity of expectations . Lemma (Linearity of expectation.) . [ ] [ ] [ ] ∀ random variables X and Y : E X + Y = E X + E Y . . . Proof. . Use definitions, do the math. See notes for details. . Sariel (UIUC) CS573 5 Fall 2013 5 / 32
Probability - quick review Conditional Expectation . Definition . X , Y : random variables. The conditional expectation of X given Y (i.e., you know Y = y ): [ � ] [ � ] [ � ] ∑ X � Y = E X � Y = y = x ∗ Pr X = x � Y = y . E � � � x . E[ X ] is a number. [ � ] f ( y ) = E � Y = y is a function. X � Sariel (UIUC) CS573 6 Fall 2013 6 / 32
Conditional Expectation . Lemma . [ [ � ]] ∀ X , Y (not necessarily independent): E[ X ] = E . E X � Y � . [ [ � ]] [ [ � ]] = E y � Y = y E E X � Y E X � � . Proof. . Use definitions, and do the math. See class notes. . Sariel (UIUC) CS573 7 Fall 2013 7 / 32
Conditional Expectation . Lemma . [ [ � ]] ∀ X , Y (not necessarily independent): E[ X ] = E . E X � Y � . [ [ � ]] [ [ � ]] = E y � Y = y E E X � Y E X � � . Proof. . Use definitions, and do the math. See class notes. . Sariel (UIUC) CS573 7 Fall 2013 7 / 32
Conditional Expectation . Lemma . [ [ � ]] ∀ X , Y (not necessarily independent): E[ X ] = E . E X � Y � . [ [ � ]] [ [ � ]] = E y � Y = y E E X � Y E X � � . Proof. . Use definitions, and do the math. See class notes. . Sariel (UIUC) CS573 7 Fall 2013 7 / 32
Sorting Nuts & Bolts . Problem ( Sorting Nuts and Bolts ) . . Input: Set n nuts + n bolts. 1 . . Every nut have a matching 2 bolt. . . All diff sizes. 3 . . Task: Match nuts to bolts. 4 (In sorted order). . . Restriction: You can only 5 compare a nut to a bolt. . Q: How to match the n nuts 6 to the n bolts quickly? . Sariel (UIUC) CS573 8 Fall 2013 8 / 32
Sorting Nuts & Bolts . Problem ( Sorting Nuts and Bolts ) . . Input: Set n nuts + n bolts. 1 . . Every nut have a matching 2 bolt. . . All diff sizes. 3 . . Task: Match nuts to bolts. 4 (In sorted order). . Restriction: You can only 5 compare a nut to a bolt. . Q: How to match the n nuts 6 to the n bolts quickly? . Sariel (UIUC) CS573 8 Fall 2013 8 / 32
Sorting Nuts & Bolts . Problem ( Sorting Nuts and Bolts ) . . Input: Set n nuts + n bolts. 1 . . Every nut have a matching 2 bolt. . . All diff sizes. 3 . . Task: Match nuts to bolts. 4 (In sorted order). . . Restriction: You can only 5 compare a nut to a bolt. . . Q: How to match the n nuts 6 to the n bolts quickly? . Sariel (UIUC) CS573 8 Fall 2013 8 / 32
Sorting Nuts & Bolts . Problem ( Sorting Nuts and Bolts ) . . Input: Set n nuts + n bolts. 1 . . Every nut have a matching 2 bolt. . . All diff sizes. 3 . . Task: Match nuts to bolts. 4 (In sorted order). . . Restriction: You can only 5 compare a nut to a bolt. . Q: How to match the n nuts 6 to the n bolts quickly? . Sariel (UIUC) CS573 8 Fall 2013 8 / 32
Sorting Nuts & Bolts . Problem ( Sorting Nuts and Bolts ) . . Input: Set n nuts + n bolts. 1 . . Every nut have a matching 2 bolt. . . All diff sizes. 3 . . Task: Match nuts to bolts. 4 (In sorted order). . . Restriction: You can only 5 compare a nut to a bolt. . Q: How to match the n nuts 6 to the n bolts quickly? . Sariel (UIUC) CS573 8 Fall 2013 8 / 32
Sorting nuts & bolts... Algorithm . . Naive algorithm... 1 . . ...better algorithm? 2 Sariel (UIUC) CS573 9 Fall 2013 9 / 32
Sorting nuts & bolts... Algorithm . . Naive algorithm... 1 . . ...better algorithm? 2 Sariel (UIUC) CS573 9 Fall 2013 9 / 32
Sorting nuts & bolts... MatchNutsAndBolts ( N : nuts, B : bolts) Pick a random nut n pivot from N Find its matching bolt b pivot in B B L ← All bolts in B smaller than n pivot N L ← All nuts in N smaller than b pivot B R ← All bolts in B larger than n pivot N R ← All nuts in N larger than b pivot MatchNutsAndBolts ( N R , B R ) MatchNutsAndBolts ( N L , B L ) QuickSort style... Sariel (UIUC) CS573 10 Fall 2013 10 / 32
Sorting nuts & bolts... MatchNutsAndBolts ( N : nuts, B : bolts) Pick a random nut n pivot from N Find its matching bolt b pivot in B B L ← All bolts in B smaller than n pivot N L ← All nuts in N smaller than b pivot B R ← All bolts in B larger than n pivot N R ← All nuts in N larger than b pivot MatchNutsAndBolts ( N R , B R ) MatchNutsAndBolts ( N L , B L ) QuickSort style... Sariel (UIUC) CS573 10 Fall 2013 10 / 32
What is running time for randomized algorithms? Definitions . Definition . RT ( U ) : random variable – running time of the algorithm on input U . . . Definition . Expected running time E[ RT ( U )] for input U . . . Definition . expected running-time of algorithm for input size n : [ ] T ( n ) = max RT ( U ) . U is an input of size n E . Sariel (UIUC) CS573 11 Fall 2013 11 / 32
What is running time for randomized algorithms? Definitions . Definition . RT ( U ) : random variable – running time of the algorithm on input U . . . Definition . Expected running time E[ RT ( U )] for input U . . . Definition . expected running-time of algorithm for input size n : [ ] T ( n ) = max RT ( U ) . U is an input of size n E . Sariel (UIUC) CS573 11 Fall 2013 11 / 32
What is running time for randomized algorithms? Definitions . Definition . RT ( U ) : random variable – running time of the algorithm on input U . . . Definition . Expected running time E[ RT ( U )] for input U . . . Definition . expected running-time of algorithm for input size n : [ ] T ( n ) = max RT ( U ) . U is an input of size n E . Sariel (UIUC) CS573 11 Fall 2013 11 / 32
What is running time for randomized algorithms? More definitions . Definition . rank( x ) : rank of element x ∈ S = number of elements in S smaller or equal to x . . Sariel (UIUC) CS573 12 Fall 2013 12 / 32
Nuts and bolts running time . Theorem . Expected running time MatchNutsAndBolts ( QuickSort ) is T ( n ) = O ( n log n ) . Worst case is O ( n 2 ) . . . Proof. . Pr[rank( n pivot ) = k ] = 1 n . Thus, [ ] T ( n ) = O ( n ) + T ( k − 1) + T ( n − k ) E k =rank( n pivot ) . Sariel (UIUC) CS573 13 Fall 2013 13 / 32
Nuts and bolts running time . Theorem . Expected running time MatchNutsAndBolts ( QuickSort ) is T ( n ) = O ( n log n ) . Worst case is O ( n 2 ) . . . Proof. . Pr[rank( n pivot ) = k ] = 1 n . Thus, [ ] T ( n ) = O ( n ) + T ( k − 1) + T ( n − k ) E k =rank( n pivot ) = O ( n ) + E k [ T ( k − 1) + T ( n − k )] . Sariel (UIUC) CS573 13 Fall 2013 13 / 32
Recommend
More recommend