Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Randomized Algorithms

Similar presentations


Presentation on theme: "Lecture 9 Randomized Algorithms"โ€” Presentation transcript:

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


Download ppt "Lecture 9 Randomized Algorithms"

Similar presentations


Ads by Google