MIT 6.1220 — Lecture 5
Randomized Algorithms
Definition. There are two types of randomized algorithms.
Monte-Carlo. Necessarily a good runtime, but not necessarily a good answer.
Las Vegas. Not necessarily a good runtime, but necessarily a good answer.
Problem. (Matrix Product) Given matrices and , compute .
You could just compute dot products, each of which takes time, yielding runtime.
Remark. If you're smarter about it, you might discover Strassen's divide-and-conquer algorithm, with recursion , yielding a runtime of .
The best algorithm we've achieved so far is .
Problem. (Matrix Product Verification) Given matrices , verify whether .
Solution. (Randomized Solution #1) Pick a random nonzero . Check whether in time.
This is a guaranteed success if , and performs worse the lower the nonzero rank of is. Notably, if , the algorithm succeeds with probability at least .
Thus, we could run this algorithm multiple times to decrease the false positive rate.
Problem. (Rank Finding) Let be an unsorted array of numbers, and let be a rank.
Output the smallest number (i.e. the unique number with rank ).
Recall the following solution outline, altered slightly to integrate randomness:
Solution. (Quick Select) Pick a random pivot , with and . Then recurse either on or , depending on whether .
We can sacrifice a little time complexity to make the analysis simpler, too.
Solution. (Paranoid Select) Randomly try pivots until is -balanced, that is, .
In expectation, Paranoid Select requires two tries to pick a good , yielding runtime in expectation.
Remark. Though the analysis of Paranoid Select is simpler, it turns out Quick Select is just better in practice.
Problem. (Sorting) Just sort an array .
Solution. (Paranoid Quick Sort) Pick a random pivot , and compute and . Then write . Pick by randomly trying pivots until a -balance is achieved.
This yields the following recurrence:
which solves to . (We skipped over the step of showing is worst-case.)
To finish, we'll analyze the non-paranoid version of quick sort: what happens if we allow randomly-chosen unbalanced pivots?
Chernoff Bound. For a binomial random variable and :
For example, when , this upper-bounds the probability
that a binomial variable exceeds its expected value by .
Claim. With high probability (e.g., at least ), Quick Sort runs in time.
Proof: Call a choice of pivot good if it is -balanced, and bad otherwise.
Suppose a run of Quick Sort goes to depth . We'd like to show that for any , if we follow down steps of the recursion tree, then with high probability encounters sufficiently many good pivots.
To be precise, let's define for every index a set of indicator variables .
We want to encounter good pivots, so we want to (probably) be small. Well, , so:
We must declare a logarithmic depth for which the following hold:
If for all , then Quick Sort has succeeded (i.e. no is still in a subset with size).
The probability that for all is high.
We claim works. For part 1, the condition (i.e. at least good pivots) tells us:
For part 2, we can use the union bound:
So with probability at least , every is “done with quick sort” by depth . Since the depth is logarithmic in , this means Quick Sort has run in time, as desired.
Remark. We could have set to show that Quick Sort runs in time with probability at least . This sounds even better, but the cost of this improvement is hidden in the big-.
In the very likely case that Quick Sort finishes with depth at most , the coefficient of the bound on the time complexity is very large. So it'd be a pretty bad .
Thus, we care about achieving “high probability”, but not how high that “high probability” is.