270 likes | 392 Views
CSE 780: Design and Analysis of Algorithms. Lecture 7: Prob. Analysis Randomized Alg. Expected Complexity. Probabilistic method: Given a distribution for all possible inputs Derive expected time based on distribution Randomized algorithm: Add randomness in the algorithm
E N D
CSE 780: Design and Analysis of Algorithms Lecture 7: Prob. Analysis Randomized Alg. CSE 2331/5331
Expected Complexity • Probabilistic method: • Given a distribution for all possible inputs • Derive expected time based on distribution • Randomized algorithm: • Add randomness in the algorithm • Analyze the expected behavior of the algorithm CSE 2331/5331
First Example • What is worst case time complexity? • What is expected / average time complexity? CSE 2331/5331
Expected Running Time • Expected / average running time • = probability of event I • = running time given event I • To analyze, need to assume a probabilistic distribution for all inputs CSE 2331/5331
SeqSearchAlg • Expected running time = • If we assume • = 0 • All permutations are equally likely • implies • Then expected running time = CSE 2331/5331
Remarks • For probabilistic analysis • An input probabilistic distribution input model has be assumed! • For a fixed input, the running time is fixed. • The average / expected time complexity is for if we consider running it for a range of inputs, what the average behavior is. • Randomized algorithm • No assumption in input distribution! • Randomness is added in the algorithm • For a fixed input, the running time is NOT fixed. • The expected time is what we can expect when we run the algorithm on ANY SINGLE input. CSE 2331/5331
Randomized Algorithms CSE 2331/5331
Expectation • X is a random variable • The expectation of X is • E.g, coin flip • Linearity of expectation: • Conditional expectation: CSE 2331/5331
Linearity of Expectation • = expected running time for func2 • What is ? + cn CSE 2331/5331
Conditional Expectation • = expected running time of func2 • = expected running time of func3 • What is the expected running time of func1? CSE 2331/5331
A Randomized Example Worst case complexity? Expected case? CSE 2331/5331
Running Time Analysis • Worst Case: • Expected running time: CSE 2331/5331
Time Analysis • Worst case: • Expected running time: CSE 2331/5331
A more complicated variation. CSE 2331/5331
Analysis • Worst case: • Expected case: CSE 2331/5331
An Example with Recursion CSE 2331/5331
Worst case: • Expected running time • Solving this we have CSE 2331/5331
Another Example with Recursion CSE 2331/5331
Analysis • Worst case: • ! • Expected running time: • ! CSE 2331/5331
Another Example CSE 2331/5331
Analysis • Worst case: • Expected running time: CSE 2331/5331
Back To QuickSort • Randomized-QuickSort( A, 1, n ) • If ( )return; • s = Randomized-Partition ( A, 1, n ); • A1 = Randomized-QuickSort( A, 1, s-1 ); • A2 = Randomized-QuickSort( A, s+1, n ); CSE 2331/5331
= expected running time of Randomized-QuickSort • Assume input array has no duplicates One can use substitution method to show that ET(n) = O(n lg n) CSE 2331/5331
Another Way of Analysis CSE 2331/5331
Summary • Difference between probabilistic analysis and randomized algorithm analysis • Analyze a simple randomized algorithm • Linearity of expectation and conditional expectation • Randomized quicksort • expected running time CSE 2331/5331