11. Randomized Algorithms
Types of Random Algorithms
Las Vegas: An algorithm that will always produce a correct result, but whose runtime will vary.
Monte Carlo: An algorithm that may or may not produce a correct result, but whose runtime is constant.
Primality Testing
Fermat Test
A naive solution would be to simply factor a number into its prime factors to determine if it's prime. However, the fastest algorithm for this, the General Number Field Sieve, runs in , i.e. exponential time. Can we do better?
Recall Fermat's Little Theorem:
If is prime, then , .
We can thus develop a very simple test known as the Fermat Test for testing if a number is prime.
- Pick uniformly at random
- If , output prime. Otherwise, output composite.
However, primality testing is not as simple as just choosing an and checking if . If is composite, this congruence could also be true, for certain values of . In essence, the Fermat Test guarantees that, if is prime, it will output prime, but if is composite, it may output prime or composite.
Thus, the Fermat Test will not always be correct. In fact, you may notice that this seems to fit the definition of a Monte Carlo randomized algorithm! Let's examine this further, though. In particular, we will determine an upper bound on the probability of some "passing" the Fermat Test (i.e. outputting prime) when is composite, for all .
Any that fails the Fermat Test when is composite, i.e. outputs the correct result / , is known as a Fermat witness. If is coprime to , we further describe is as a nontrivial Fermat witness, since if is not coprime to , it is trivially a Fermat witness. (See the proof below for an explanation).
Note that there do exist composite numbers that pass the Fermat Test for all coprime , i.e. for all nontrivial Fermat witnesses. For simplicity, we will ignore these.
Claim:
Let be composite, and . Suppose there exists at least one such that is a nontrivial Fermat witness (i.e. is not a Carmichael number). Let be the set of Fermat witnesses. Then .
Proof:
We partition into four disjoint subsets:
Consider . As aforementioned, any number that is not coprime to is trivially a Fermat witness. Why? Let . If , then such that . However, and , which implies , which is contradictory. Thus, .
Meanwhile, represents the nontrivial Fermat witnesses, represents the trivial Fermat witnesses, and represents the non Fermat witnesses. Note that . Also note that since , is nonempty as given by the claim, and is nonempty since is composite.
Assume for contradiction that . Then . Let , where . Choose an arbitrary , and consider , i.e.
has two useful properties that we will prove.
- .
- The elements of are distinct, i.e. .
. Moreover, since and . Note that is also clearly in , due to modular arithmetic. Therefore, .
Suppose for contradiction that for . This implies that , i.e. . Since , we must have . However, implies that , i.e. . This yields a contradiction, and thus the elements of must be distinct.
The above two properties thus imply that, if , then there are at least elements in , where . This produces a contradiction, however, and therefore it must be true that .
Therefore, given an integer with unknown primality, if we run the Fermat Test for rounds and no Fermat witness is identified, then either is one of the (very rare) Carmichael numbers or is prime with probability . Thus, we have a nice, randomized Monte Carlo test for primality! (Effective for all but the Carmichael numbers).
Miller-Rabin Test
The Miller-Rabin test is a more robust test than the Fermat Test, in the sense that it works on all numbers, including Carmichael numbers. It is a Monte Carlo algorithm, like the Fermat test, and always outputs "prime" if is prime and outputs "composite" if is composite with probability . Thus, it returns the correct answer with probability in rounds.
The Miller-Rabin test relies on a different witness of compositeness, based on the following theorem.
If is prime, then all integer roots of satisfy or .
Proof:
Since is prime, either , , or both. This means , , or both. Thus, the only possible roots are . Finally, we note that these are not extraneous, i.e. is in fact true.
Corollary: Let such that . If , then must be composite.
Now, we can introduce some similar notation as for the Fermat Test. For a composite number , we denote as a root witness of 's compositeness if and . By definition, a root witness is a nontrivial root.
We'll subsequently derive the intuition and logic behind the Miller-Rabin test.
In essence, the Miller-Rabin test determines that is composite by using both Fermat and root witnesses. Let us assume and is odd (otherwise it is immediately composite), and let be chosen uniformly at random from .
Since is odd, is even. Let , where . We can substitute this into the Fermat Test's equation to produce
Suppose that , i.e. is not a Fermat witness. Consider rewriting the expression as
Then, if
We have successfully identified a root witness of compositeness, and thus is correctly identified as composite. However, there's still more to this!
If , then we can do nothing further. However, if , we can form yet another equation of the form . In particular,
Thus, as long as we get and , we can continue trying to find a root witness.
Therefore, we can now define the steps of the Miller-Rabin algorithm. For simplicity, we consider only a single round. (Note: any output immediately returns from the function).
- Express for some odd
- Choose uniformly at random
- If , output "composite (Fermat)"
- For to ,
- If , output "composite (Root)"
- If , output "probably prime"
- output "probably prime"
Actually, though, this algorithm is a little more computationally expensive than necessary, since it must compute . We can instead start from and square this quantity times in the for loop. Thus, a more optimized version looks like this.
- Express for some odd
- Choose uniformly at random
- Let
- If , output "probably prime"
Why? All future squares will be , so there are no root witnesses. - If , output "probably prime"
Why? Same reason as 3.1.
- If , output "probably prime"
- For to
- If , output "composite (Root)"
- If , output "probably prime"
Why? Same reason as 3.1.
- Output "composite"
Suppose we continued to . Then it must have been the case that . Now, consider . If it is , we have a root witness. Otherwise, if it is , we have a Fermat witness (recall ).
Finally, it can be shown that the single-round Miller-Rabin primality test will output composite for a composite for a randomly chosen with probability . The proof of this is nontrivial, and thus is excluded from these notes. In conclusion, though, after running the Miller-Rabin test for rounds on a composite , it will find a witness of compositeness with probability .
Agrawal-Kayal-Saxena (AKS)
This is a deterministic algorithm for primality test in (actually, it's been shortened to now). This is effectively a derandomized version of the Miller-Rabin algorithm. For brevity, this algorithm's details are left out.
Minimum Cut
We previously saw an algorithm in Section 7 that solved for the maximum flow / minimum cut for a pair of vertices deterministically in , or for dense graphs. However, we now consider a related, but slightly different problem—given an unweighted, undirected graph , we seek the minimum cut across all possible pairs , i.e. the cut with the globally minimum number of crossing edges. There actually exists a Monte Carlo algorithm for this problem that runs in : Karger's Algorithm.
Karger's Algorithm proceeds simply as follows.
- For
- Pick a uniformly random edge
- Contract , i.e. merge its vertices into a "supervertex"
At the end of the loop, we are left with two supervertices, and we return the value of the cut between these two supervertices, which effectively represent the two sets of vertices we've divided the graph into.
...what? How does this work??
The critical idea behind this seemingly nonsensically-simple algorithm is that contracting is much less likely to contract the edges crossing the minimum cut. Why? Because the minimum cut, by definition, has the least number of crossing edges of any cut. Thus, it is by far more likely that an edge that does not cross the minimum cut is chosen, considering that there are many edges in the graph and the minimum cut edges form only a minuscule subset.
In fact, that algorithm is better!
Let's not stop here though—we'd like to formalize this intuition, and derive the probability that Karger's algorithm does, in fact, output the minimum cut. In particular, we will prove the following theorem.
Theorem: Let be a minimum cut of size . .
Proof:
First, let's outline some definitions. Let be the graph at the beginning of the th iteration of the algorithm, with base case , and let be a "happy" (^w^) event, i.e. the event in which the th contracted edge doesn't cross the cut . Let denote the event that .
Then,
The inequality follows from the following
Where the number of edges in follows from the following sequence of facts
- (since any cut in corresponds precisely to some cut in )
- Number of vertices in , , is , since vertices have been removed
- Degree of each vertex in (otherwise, if , minimum cut is less than with )
- Number of edges in is .
Thus, the probability of success, , is . In fact, this scales linearly with the number of valid minimum cuts, i.e. the number of cuts with the minimum number of crossing edges. So the probability of returning a minimum cut with minimum cuts in the graph is . In general, though this is not known a priori—thus, the lower bound on the probability of success is still . Therefore, it suffices to run this algorithm times, and take the minimum cut seen across all runs. For instance, repeating times ensures that the probability a minimum cut is not seen in any run is [Source]. So, the total runtime for Karger's Algorithm (to derive a minimum cut with high probability) is effectively .