



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Probabilistic Analysis, Randomized Quicksort Date: 9/7/
As we discussed the first lecture, one issue with trying to do an “average-case” analysis of an algorithm is figuring out what the average case actually is. Different applications might result in vastly different empirical input distributions, and when we design basic, low-level algorithms and data structures we want our guarantees to hold in every possible application.
On the other hand, as we’ll see today, sometimes it turns out that adding randomness can help, even in the worst-case! This is the paradigm of randomized algorithms: given a (worst-case) input, we use randomness in our algorithm and provide guarantees either in expectation or with high- probability. Note how different this is from trying to do an average-case analysis: instead of using randomness to model the input, we still allow arbitrary inputs but use randomness internally in our algorithm.
The first approach, of modeling the input using a distribution, is commonly seen in machine learn- ing, computational biology, and other applications. There is much interesting work there, but if we model the input as a distribution we are fundamentally tied to that distribution, and lose general- ity. Adding randomness can be counterintuitive to some people, but allows us to design algorithms that do not lose generality but instead have guarantees that hold (for any input) in expectation or with high probability.
As a side note, there is a class offered many fall semesters by Vladimir Braverman on randomized algorithms. I highly recommend this class. This lecture (and possibly others) is my attempt to add some randomized algorithms to this class, since in the past 20 years randomized algorithms have moved from a specialized topic to the mainstream.
So today we’re going to discuss a particularly nice use of randomization: randomized quicksort. First, recall the Quicksort algorithm. For now, let’s assume that we are given an array of length n in which all elements are distinct – it’s easy to see that allowing equal elements only helps us. Given an array of length n, quicksort does the following:
Note that this algorithm is not totally well-specified, since we have not defined how to pick the pivot p. The traditional thing to do is come up with some arbitrary but deterministic choice. For example, always choosing the first (not necessarily the smallest) element as the pivot. Next lecture we will see a better way of deterministically choosing a pivot, but the original (and probably most common) version of quicksort just picks the first element.
What is the running time of this basic version of quicksort? Everyone should know that the worst- case running time is Θ(n^2 ), but let’s prove it again.
For an upper bound, note that by definition if we pick a pivot p in step 2 then after step 3 p is in the right place, and thus step 2 can be executed at most n times. Each time we execute step 2 we also have to execute step 3, but this takes at most O(n) time. Thus the total running time is O(n^2 ).
For a lower bound, suppose that the array is already sorted. Then when we choose p to be the first element, all other elements are compared to it and put in G. The pivot then stays where it is, and we recurse on all remaining elements. So the running time in this case is T (n) = T (n − 1) + cn, which we now know is Ω(n^2 ).
Thus the worst-case running time of basic quicksort is Θ(n^2 ).
Randomized quicksort is the simple variation that picks a pivot p uniformly at random from the elements. We will show that in expectation the running time of randomized quicksort is O(n log n). Moreover, since the randomness is from the algorithm itself rather than over the distribution of the inputs, this bound holds for every possible input. So on any fixed input the running time is a random variable, but we will show that this random variable always has expectation at most O(n log n) (and clearly has maximum at most O(n^2 )). This is better than an average-case bound because we don’t have to assume anything about the input – it could be that in some application the arrays tend to be already sorted or in some other order which makes deterministic quicksort slow, but since our bound is worst-case (although in expectation over the randomness used by the algorithm) it still holds.
Before we prove this, though, we will need to cover some basic probability.
I’m not going to get too formal, but I highly, highly recommend learning basic probability theory
The example we will use is rolling a pair of dice and looking at the sum. The sample space (traditionally denoted by Ω) is the set of possible outcomes, i.e., the 36 possible pairs of rolls (or more formally, Ω = { 1 , 2 , 3 , 4 , 5 , 6 } × { 1 , 2 , 3 , 4 , 5 , 6 }). An event is a subset of the sample space. So you could talk about “the event that the first die is 3” (the subset of Ω in which the first coordinate is 3) or “the event that the dice add up to 7 or 11” (the subset {(x, y) ∈ Ω : (x + y =
analyze it using equation (3.3.2), but that would require figuring out the probability that the sum equaled 2, the probability in equaled 3, etc. Neither of these are impossible (or even particularly difficult), but they require some combinatorics.
On the other hand, we can define two other random variables: let X 1 be the outcome of the first die, and let X 2 be the outcome of the second. Then X = X 1 + X 2 (or more formally, X(e) = X 1 (e) + X 2 (e) for all e ∈ Ω), so by linearity of expectations E[X] = E[X 1 + X 2 ] = E[X 1 ] + E[X 2 ]. But now by definition we have that
y=
y =
and thus E[X] = 7.
Theorem 3.3.2 (Linearity of Expectations) For any two random variables X and Y , and any constants α and β, E[αX + βY ] = αE[X] + βE[Y ].
Proof: We will simply use equation (3.3.1):
E[αX + βY ] =
e∈Ω
Pr[x](αX(e) + βY (e)) = α
e∈Ω
Pr[e]X(e) + β
e∈Ω
Pr[e]Y (e)
= αE[X] + βE[Y ],
as claimed.
There are a few ways to analyze Randomized Quicksort – I think that the following analysis is really slick, and demonstrates the power of linearity of expectations. Recall that we’re assuming all elements are distinct, and note that the running time is essentially the total number of comparisons.
Theorem 3.4.1 The expected number of comparisons in Randomized Quicksort is at most O(n log n).
Proof: Let X be the total number of comparisons made (so X is a random variable). Let ei be the ith smallest value in the given array, for i ∈ { 1 ,... , n}. We define another random variable Xij for each i, j ∈ { 1 ,... , n} with i < j. We set Xij = 1 if the algorithm at some point compares ei and ej , and otherwise we set Xij = 0.
It is easy to see that Randomized Quicksort will never compare the same two elements more than once, since if we compare ei and ej then we must have picked either ei or ej as the pivot, so after that iteration whichever one is the pivot will not be compared to any other elements. This means that X =
∑n− 1 i=
∑n j=i+1 Xij^ , so by linearity of expectations we know that
n∑− 1
i=
∑^ n
j=i+
E[Xij ]. (3.4.3)
So now we just need to understand E[Xij ]. Let’s start by considering two extremes. First, suppose that j = i + 1. Then Xij = 1, since we will inevitably compare ei and ei+1 (they will stay in the
same subarray throughout the recursion until eventually one of them is chosen as the pivot, when they will be compared).
On the other hand, suppose that i = 1 and j = n. Then when we choose the first pivot, if we choose neither e 1 nor en then e 1 ∈ L and en ∈ G, so the recursive calls separate them and we will never compare them. So the probability that we compare e 1 and en is 2/n, and thus E[X 1 n] = 2/n.
Now consider general i and j. Let ep be the pivot that we pick. If p = i or p = j then we will compare ei and ej. If i < p < j then ei ∈ L while ej ∈ G, so we will not compare them.
The more subtle cases are if p < i or p > j. Suppose p < i. Then ei and ej are both in G, so are in the same subarray in the recursive call. So they may be compared, but they may not. Similarly, is p > j then ei and ej are both in L, so may be compared later on but may not.
But let’s look more closely. What if we condition on i ≤ p ≤ j? Then the probability that Xij = 1 is exactly (^) j−^2 i+1. This is independent of n, and since if we pick p < i or p > j we simply retry (albeit with a smaller value of n), this means that eventually i ≤ p ≤ j and so Xij = 1 with probability 2 j−i+1. Thus^ E[Xij^ ] =^
2 j−i+. To see this more formally, let Yk be the event that the k’th pivot picked is between ei and ej (inclusive) and all of the previous pivots picked are either less than ei or greater than ej. Note that Yk occurs for exactly one value of k, so the events are disjoint and partition the sample space. Then we argued above that E[Xij |Yk] = (^) j−^2 i+1. So we get that
E[Xij ] =
∑^ n
k=
E[Xij |Yk]Pr[Yk] =
j − i + 1
∑^ n
k=
Pr[Yk] =
j − i + 1
as claimed.
Another way of saying this, which is possibly simpler, is to convince yourself of the following: ei and ej are compared if and only if either ei or ej is chosen as the pivot before any ek with i < k < j. This isn’t too hard to see. For the “if” direction: If ei or ej is chosen as a pivot before any such ek, then clearly ei and ej are still in the same subarray when one of them becomes the pivot, and hence they will be compared. On the other hand, for the “only if” direction, suppose that ei and ej are compared. Then they must be in the same subarray when one of them is chosen as the pivot, which means that no ek with i < k < j has been chosen as the pivot yet. Now that we have this iff condition, it immediately implies that E[Xij ] = (^) j−^2 i+1 , since the ordering of pivots is chosen uniformly at random.
Putting this together with equation (3.4.3), we get that
E[X] =
n∑− 1
i=
∑^ n
j=i+
j − i + 1
n∑− 1
i=
n − i + 1
≤ 2 nHn = O(n log n),
where Hn = 1 + 12 + 13 + 14 + · · · + (^1) n is the nth harmonic number, which is O(log n) (you can prove this by considering the integral of 1/x).
A possibly easier way to see the bound on E[Xij ] is through a “dart game”^1. When we pick a pivot p, we are essentially throwing a dart uniformly at random at the (sorted) array. If p < i then since (^1) I first saw this analysis from Avrim Blum in the undergraduate algorithms class at CMU.