Randomized Algorithms (Spring 2010)/Introduction
This is the first lecture of the Randomized Algorithms class. In this lecture, I will use two examples: randomized Quicksort and Karger's min-cut algorithm, to demonstrate what randomized algorithms look like. Two basic principles are used to analyze these algorithms: linearity of expectations, and independence of events.
These two algorithms belong respectively to two important classes of randomized algorithms: Las Vegas algorithms and Monte Carlo algorithms. I will introduce their definitions at the end of this lecture.
Contents
Randomized Quicksort
The following is the pseudocode of the famous Quicksort algorithm, whose input is a set of numbers.
- if do:
- pick an element from as the pivot;
- partition into , , and , where all elements in are smaller than and all elements in are larger than ;
- recursively sort and ;
The time complexity of this sorting algorithm is measured by the number of comparisons.
For the deterministic quicksort algorithm, the pivot element is usually the element in a fixed position (e.g. the first one) of the . This will make the worst-case time complexity , which means there exists a bad case , sorting which will cost us comparisons, every time!
It is just so unfair to have an unbeatable input for this brilliant algorithm. So we tweak the algorithm a little bit:
Algorithm: RandQSort
- if do:
- uniformly pick a random element from as the pivot;
- partition into , , and , where all elements in are smaller than and all elements in are larger than ;
- recursively sort and ;
Analysis
Our goal is to analyze the expected number of comparisons during an execution of RandQSort with an arbitrary input . We achieve this by measuring the chance that each pair of elements are compared, and summing all of them up due to Linearity of Expectation.
Let denote the th smallest element in . Let be the random variable which indicates whether and are compared during the execution of RandQSort. That is:
Elements and are compared only if one of them is chosen as pivot. After comparison they are separated (thus are never compared again). So we have the following observation:
Claim 1: Every pair of and are compared at most once.
Therefore the sum of for all pair gives the total number of comparisons. The expected number of comparisons is . Due to Linearity of Expectation, . Our next step is to analyze for each .
By the definition of expectation and ,
We are going to bound this probability.
Claim 2: and are compared if and only if one of them is chosen as pivot when they are still in the same subset.
This is easy to verify: just check the algorithm. The next one is a bit complicated.
Claim 3: If and are still in the same subset then all are in the same subset.
We can verify this by induction. Initially, itself has the property described above; and partitioning any with the property into and will preserve the property for both and . Therefore Claim 3 holds.
Combining Claim 2 and 3, we have:
Claim 4: and are compared only if one of is chosen from .
And apparently,
Claim 5: Every one of is chosen equal-probably.
This is because our RandQSort chooses the pivot uniformly at random.
Claim 4 and Claim 5 together imply:
Remark: Perhaps you feel confused about the above argument. You may ask: "The algorithm chooses pivots for many times during the execution. Why in the above argument, it looks like the pivot is chosen only once?" Good question! Let's see what really happens by looking closely.
For any pair and , initially are all in the same set (obviously!). During the execution of the algorithm, the set which containing are shrinking (due to the pivoting), until one of is chosen, and the set is partitioned into different subsets. We ask for the probability that the chosen one is among . So we really care about "the last" pivoting before is split. Formally, let be the random variable denoting the pivot element. We know that for each , with the same probability, and with an unknown probability (remember that there might be other elements in the same subset with ). The probability we are looking for is actually , which is always , provided that is uniform over . The conditional probability rules out the irrelevant events in a probabilistic argument. |
Summing all up:
is the th Harmonic number. It holds that
- .
Therefore, for an arbitrary input of numbers, the expected number of comparisons taken by RandQSort to sort is .
Minimum Cut
Let be a graph. Suppose that we want to partition the vertex set into two parts and such that the number of crossing edges, edges with one endpoint in each part, is as small as possible. This can be described as the following problem: the min-cut problem.
For a connected graph , a cut is a set of edges, removal of which causes becomes disconnected. The min-cut problem is to find the cut with minimum cardinality. A canonical deterministic algorithm for this problem is through the max-flow min-cut theorem. A global minimum cut is the minimum - min-cut, which is equal to the minimum - max-flow.
Do we have to rely on the "advanced" tools like flows? The answer is "no", with a little help of randomness.
Karger's Min-Cut Algorithm
We will introduce an extremely simple algorithm discovered by David Karger. The algorithm works on multigraphs, graphs allowing multiple edges between vertices.
We define an operation on multigraphs called contraction: For a multigraph , for any edge , let be a new multigraph constructed as follows: and in are replaced by a singe new vertex whose neighbors are all the old neighbors of and . In other words, and are merged into one vertex. The old edges between and are deleted.
Karger's min-cut algorithm is described as follows:
MinCut(multigraph )
- while do
- choose an edge uniformly at random;
- ;
- return the edges between the only two vertices in ;
A better way to understand Karger's min-cut algorithm is to describe it as randomly merging sets of vertices. Initially, each vertex corresponds to a singleton set . At each step, (1) a crossing edge (edge whose endpoints are in different sets) is chosen uniformly at random from all crossing edges; and (2) the two sets connected by the chosen crossing-edge are merged to one set. Repeat this process until there are only two sets. The crossing edges between the two sets are returned.
Analysis
For a multigraph , fixed a minimum cut (there might be more than one minimum cuts), we analyze the probability that is returned by the MinCut algorithm. is returned by MinCut if and only if no edge in is contracted during the execution of MinCut. We will bound this probability .
Lemma 1 - Let be a multigraph with vertices, if the size of the minimum cut of is , then .
Proof. - It holds that every vertex has at least neighbors, because if there exists with neighbors, then the edges adjacent to disconnect from the rest of , forming a cut of size smaller than . Therefore .
Lemma 2 - Let be a multigraph with vertices, and a minimum cut of . If , then is still a minimum cut of .
Proof. - We first show that no edge in is lost during the contraction. Due to the definition of contraction, the only edges removed from in a contraction are the parallel-edges sharing both endpoints with . Since , none of these edges can be in , or otherwise cannot be a minimum cut of . Thus every edge in remains in .
- It is then obvious to see that is a cut of . All paths in a contracted graph can be revived in the original multigraph by inserting the contracted edges into the path, thus a connected would imply a connected , which contradicts that is a cut in .
- Notice that a cut in a contracted graph must be a cut in the original graph. This can be easily verified by seeing contraction as taking the union of two sets of vertices. Therefore a contraction can never reduce the size of minimum cuts of a multigraph. A minimum cut must still be a minimum cut in the contracted graph as long as it is still a cut.
- Concluding the above arguments, we have that is a minimum cut of for any .
Let be a multigraph, and a minimum cut of .
Initially . After contractions, denote the current multigraph as . Suppose that no edge in has been chosen to be contracted yet. According to Lemma 2, must be a minimum cut of the . Then due to Lemma 1, the current edge number is . Uniformly choosing an edge to contract, the probability that the th contraction contracts an edge in is given by:
Therefore, assuming that is intact after contractions, the probability that survives the th contraction is at least . Note that , because each contraction decrease the vertex number by 1.
In each iteration, the contracted edge is independently chosen from the current graph. The probability that the minimum cut survives all contractions is at least
Therefore, we prove the following theorem,
Theorem - For any multigraph with vertices, the MinCut algorithm returns a minimum cut with probability at least .
Run MinCut independently for times and return the smallest cut returned. The probability that this the minimum cut is found is:
A constant probability!
Las Vegas and Monte Carlo
- Las Vegas（赌徒）
- A randomized algorithm is called Las Vegas if its output is always correct but its running time is a random variable. Randomized quicksort is an example of Las Vegas algorithm. Its output is always a sorted table, but the running time is random.
- Usually the analysis of a Las Vegas algorithm tries to bound the expected running time, or bound the running time with high probability.
- Monte Carlo（醉汉）
- A randomized algorithm is called Monte Carlo if its running time is fixed, but the correctness of the output is a random variable. Karger's min-cut algorithm is an example of Monte Carlo algorithm.
- The analysis of a Monte Carlo algorithm tries to bound the probability of errors, the probability that the output is incorrect.
What if both the running time and the correctness are random? Sometimes, we also call it Monte Carlo.
A Las Vegas algorithm can be transformed to a Monte Carlo algorithm with fixed time complexity by truncating: fix a time threshold and force terminating once the running time pass the threshold.
A Monte Carlo algorithm can be transformed to a Las Vegas algorithm by independent trials until the correct solution is returned. Note that this method relies on the efficiency of checking the correctness of a solution. The resulting Las Vegas algorithm is efficient only when the correctness can be efficiently verified.