Lecture 9 Randomized Algorithms Quicksort Goal Sort a

  • Slides: 8
Download presentation
Lecture 9 Randomized Algorithms

Lecture 9 Randomized Algorithms

Quicksort • Goal: Sort a list of numbers a[] = {4, 2, 8, 6,

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.

Recursion • Left Part Split cost Right Part

Recursion • Left Part Split cost Right Part

Quick. Selection • Goal: Given an array of numbers Find the k-th smallest number.

Quick. Selection • 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

Recursion • Right Part Left Part Split cost

Recursion • Right Part Left Part Split cost

Rejection Sampling •

Rejection Sampling •

Example: Biased Coin • Suppose we only have a biased coin (the coin lands

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?

Monte Carlo Algorithm • Problem: Compute the area of a circle. • Monte-Carlo algorithm

Monte Carlo Algorithm • Problem: Compute the area of a circle. • Monte-Carlo algorithm Count = 0 FOR i = 1 to n Generate x, y from [-1, 1] IF x 2+y 2 <= 1 THEN Count = Count + 1 RETURN 4. 0*Count/n