Download presentation
Presentation is loading. Please wait.
1
Topic 3: Prob. Analysis Randomized Alg.
CSE 2331/5331 Topic 3: Prob. Analysis Randomized Alg. CSE 2331/5331
2
Expected Complexity Probabilistic method: Randomized algorithm:
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
3
First Example What is worst case time complexity?
What is expected / average time complexity? CSE 2331/5331
4
Expected Running Time Expected / average running time
πΈπ π = πΌ Pr πΌ π‘ πΌ Pr πΌ = probability of event I π‘ πΌ = running time given event I To analyze, need to assume a probabilistic distribution for all inputs CSE 2331/5331
5
SeqSearch Alg Expected running time =
Pr K βA t KβA + π=1 π Pr π΄ π =πΎ π‘ π΄ π =πΎ If we assume Pr K βA = 0 All permutations are equally likely implies Pr π΄ π =πΎ = 1 π Then expected running time = π 1 π βππ =Ξ π CSE 2331/5331
6
Remarks For probabilistic analysis Randomized algorithm
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
7
Randomized Algorithms
CSE 2331/5331
8
Expectation X is a random variable The expectation of X is
πΈ π = πΌ Pr π=πΌ πΌ E.g, coin flip Linearity of expectation: πΈ π 1 + π 2 =πΈ π 1 +πΈ π 2 Conditional expectation: πΈ π =πΈ π π) Pr π +πΈ(π πππ‘ π 1 β Pr π CSE 2331/5331
9
Linearity of Expectation
πΈ π 1 π =π πΈ π 2 π + cn πΈ π 2 π = expected running time for func2 What is πΈ π 1 π ? CSE 2331/5331
10
Conditional Expectation
πΈ π 1 π = Pr βπππ πΈ π 2 π +(1 β Pr βπππ ) πΈ π 3 π + π πΈ π 2 π = expected running time of func2 πΈ π 3 π = expected running time of func3 What is the expected running time of func1? CSE 2331/5331
11
A Randomized Example Worst case complexity? Expected case?
Worst case \Theta(n^2) Expected case: T(n) = \sum(1/n) ck^2 = \Theta(n^2) too CSE 2331/5331
12
Running Time Analysis Worst Case: Expected running time: π π =Ξ( π 2 )
πΈπ π = π=1 π Pr π=π β
(π π 2 ) = π=1 π 1 π β
(π π 2 ) = π π π=1 π π 2 =Ξ π 2 CSE 2331/5331
13
Another Example CSE 2331/5331
14
Running time analysis Worst case: π π = π=1 π π π 3 2 =Ξ π 2.5 .
Expected running time: Lines 4β8 takes time: π π 3 2 Let π π denote the running time for the i-th iteration of outer for-loop (i.e, from line 3 β 8) πΈ π π = π=1 π ππππ π=π ππ π‘βπ ππ‘β ππ‘ππππ‘πππ ππππ πππππ 3β8 π=π = π=1 π 1 π β
π π = π π π=1 π π =Ξ( π 1.5 ) Hence we can assume that πΈ π π = π β² π 1.5 πΈπ π =πΈ π=1 π π π = π=1 π πΈ π π = π=1 π π β² π 1.5 =Ξ π 2.5 CSE 2331/5331
15
CSE 2331/5331
16
Time Analysis Worst case: Expected running time: π π =Ξ π 2
π π =Ξ π 2 Expected running time: πΈπ π = Pr π< π β
πΈπ₯πππππ(π< π )+ Pr πβ₯ π β
πΈπ₯πππππ πβ₯ π = π π β
π π β π π β
( π β² π lg π) βπΈπ(π)=Ξ( π 1.5 ) CSE 2331/5331
17
A more complicated variation. CSE 2331/5331
18
Analysis Worst case: Expected case β analysis (1) π π =Ξ(π lg π )
πΈπ π = Pr π< π β
πΈπ πππππ 4β7 π< π + Pr πβ₯ π β
πΈπ πππππ 11β17β£ πβ₯ π = π π π=1 π Pr π=πβ£π< π β
πΈπ πππππ 4β7β£π=π + = 1 π π=1 π π β
π π β π π β
( π β² π lg π) =Ξ( π ) +Ξ π lg π =Ξ(π lg π) CSE 2331/5331
19
Analysis Expected case β analysis (2)
πΈπ π = π=1 π Pr π=π β
πΈπ πππππ 4β7β£π=π + Pr πβ₯ π β
πΈπ πππππ 11β17β£πβ₯ π = π=1 π 1 π β
π π β π π β
( π β² π lg π) =Ξ( π ) +Ξ π lg π =Ξ(π lg π) CSE 2331/5331
20
An Example with Recursion
CSE 2331/5331
21
Expected running time πΈπ π
Worst case: T n =T nβ1 +cn= Ξ π 2 Expected running time πΈπ π πΈπ π =ππ πΈπ πβ1 Solving this we have πΈπ π = 1 2 ET nβ1 +cnβ€ 1 2 πΈπ π +πn β 1 2 πΈπ π β€ππβπΈπ π β€2ππβπΈπ π =π π βπΈπ π = Ξ π CSE 2331/5331
22
Another Example with Recursion
CSE 2331/5331
23
Analysis Worst case: Expected running time: π π =π+π πβ1 +π πβ2
π π =Ξ© 2 π/2 ! Expected running time: πΈπ π =π πΈπ πβ1 +πΈπ πβ2 βπΈπ π β€π+ 1 2 ET n β 1 2 ET n β€π βπΈπ π β€2πβπΈπ π =O(1) πΈπ π =Ξ 1 ! CSE 2331/5331
24
Another Example CSE 2331/5331
25
Analysis Worst case: Expected running time: π π =2π π 2 +ππ=Ξ π lg π
πΈπ π = 1 4 πΈπ π 2 +πΈπ π 2 +ππ= 1 2 πΈπ π 2 +ππ βπΈπ π β€ 1 2 πΈπ π + ππβ 1 2 πΈπ π β€ππ βπΈπ π =π π βπΈπ π =Ξ π CSE 2331/5331
26
Example 5 CSE 2331/5331
27
πΈπ(π) = expected running time of πΉπ’ππ5 on π values
CSE 2331/5331
28
Bounding πΈπ(π) ππ π< π 4 = 1 4 ; ππ π 4 β€πβ€ 3π 4 = 1 2 ;
ππ π< π 4 = 1 4 ; ππ π 4 β€πβ€ 3π 4 = 1 2 ; ππ π> 3π 4 = 1 4 ; CSE 2331/5331
29
Upper Bound CSE 2331/5331
30
Upper Bound cont. πΈπ π β€ππ+ 1 2 πΈπ π + 1 2 (πΈπ π 4 +πΈπ 3π 4 )
πΈπ π β€ππ+ 1 2 πΈπ π (πΈπ π 4 +πΈπ 3π 4 ) CSE 2331/5331
31
Lower Bound In the best case, π= π 2 Hence
Putting upper and lower bound together, we have: πΈπ π =Ξ π ππ π CSE 2331/5331
32
Example 6 CSE 2331/5331
33
Recurrence for Func6 πΈπ(π) = expected running time of πΉπ’ππ6 on π values CSE 2331/5331
34
Bounding ET(n) CSE 2331/5331
35
Upper Bound CSE 2331/5331
36
Lower Bound Whatever π is, πΈπ(π) is at least ππ. Hence πΈπ(π) =Ξ©(π)
Putting upper and lower bounds together, we have πΈπ(π) =Ξ(π) CSE 2331/5331
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.