Download presentation
Presentation is loading. Please wait.
1
Lecture 9 Randomized Algorithms
2
Quicksort Goal: Sort a list of numbers a[] = {4, 2, 8, 6, 3, 1, 7, 5}
Algorithm: Pick a random pivot number (say 3) Partition the array into numbers smaller and larger than the pivot ({2, 1}, {4, 8, 6, 7, 5}) Recursively sort the two parts.
3
Recursion Consider the possible choices for the first pivot
Let Xn be a random variable that represents the running time of QuickSort on n numbers. ๐ผ ๐๐ = ๐=1 ๐ Pr ๐๐๐ฃ๐๐ก=๐ ๐ผ[๐๐|๐๐๐ฃ๐๐ก=๐] = 1 ๐ ๐=1 ๐ ๐ผ ๐ ๐โ1 +๐ด๐+๐ผ[ ๐ ๐โ๐ ] Left Part Split cost Right Part
4
QuickSelection Goal: Given an array of numbers Find the k-th smallest number. Example: a[] = {4, 2, 8, 6, 3, 1, 7, 5} k = 3 Output = 3
5
Recursion Consider the possible choices for the first pivot
Let Xn be a random variable that represents the running time of QuickSelect on n numbers. ๐ผ ๐๐ = ๐=1 ๐ Pr ๐๐๐ฃ๐๐ก=๐ ๐ผ[๐๐|๐๐๐ฃ๐๐ก=๐] = 1 ๐ ๐=1 ๐โ1 ๐ผ ๐ ๐โ๐ ๐ ๐=๐+1 ๐ ๐ผ ๐ ๐โ1 +๐ด๐ Right Part Left Part Split cost
6
Rejection Sampling Problem: Given a random variable X, Pr[X=i] = pi want to generate a random variable Y, such that Pr[Y=i] = qi Rejection sampling: Sample X, then with probability ๐๐ ๐ถ๐๐ , keep the sample otherwise throw the sample away.
7
Example: Biased Coin Suppose we only have a biased coin (the coin lands on heads with probability p > ยฝ). How do we simulate a fair coin? [Von Neumann] Toss the coin twice, if the result is HT, claim Heads; if the result is TH, claim Tails; if the result is HH or TT, retry. Question: How many coin tosses do we need to do until we get the result?
8
Monte Carlo Algorithm Problem: Compute the area of a circle.
Count = 0 FOR i = 1 to n Generate x, y from [-1,1] IF x2+y2 <= 1 THEN Count = Count + 1 RETURN 4.0*Count/n
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.