Lecture 14
In-class notes: CS 505 Spring 2025 Lecture 14
Randomized Computations
So far, we have only examined deterministic and non-deterministic Turing machines. Crucially, neither of these models for Turing machines utilize randomness. This is obvious for deterministic Turing machines, but it is very important to understand that non-determinism is not randomness, it is an idealized computation model that is not realistic.
Today, we’ll consider probabilistic Turing machines. These Turing machines will be allowed to sample uniformly and independently random bits and use these bits to help make decisions. Here, uniformly random means that , and independent means that the probability is or does not depend on any previous bits sampled or decision made by the algorithm.
Definition. A probabilistic Turing machine (PTM) is a Turing machine with two transition functions . For all , a PTM on input does the following.
- For each step of the computation, samples .
- Execute .
We let denote the random variable corresponding to PTM ’s output on input given the random choices it makes during its execution. We say that runs in time if for all , halts in steps for any set of random choices.
Deciding Languages: PTMs vs NTMs
As stated above, PTMs and NTMs are not the same. Recall that for NP, we say that an NTM accepts a string if there exists an execution of such that . Similarly, for coNP, the NTM accepts string if all executions of satisfy . Above, these definitions are quantified with respect to any possible non-deterministic choices the NTM make during its execution.
Now, for a PTM, say , we can similarly define it with respect to the number of accepting paths. That is, we can say that accepts a string if some fraction of all executions satisfy . For example, we can specify that if for at least half of all computation paths (that is, looking at every possible set of computation paths could take using randomness), then we say accepts . This gives us a natural definition for deciding a language with a PTM.
Definition. Let be a language and be a function. We say that a PTM decides in time if halts in time for any and where the probability is taken over the random choices made by on input , and if and if .
In the above definition, decidability with respect to a PTM has two-sided error, meaning that outputs correctly with probability at least for both and . This leads us to our first probabilistic complexity class: BPP.
Definition. For a function , we say that a language if there exists a PTM which decides in time . The complexity class BPP is defined as
Note that BPP is still a worst-case class since we require deciding languages on a PTM in strict polynomial time.
BPP vs. Other Classes
Intuitively, like how NP was the non-deterministic analogue of P, BPP is the randomized analogue of P. In fact, we know that . To see the first inequality, notice that every deterministic algorithm is a randomized algorithm that uses no randomness (e.g., you just pick , where is the transition function of the DTM). For the second inequality, suppose that is a PTM running in time . Then has possible computation paths (each step picks 0/1 with equal probability). So we can construct a machine running in time which enumerates all possible computation paths of and outputs if and only if at least 2/3 of the paths are accepting.
Now, it is unknown whether or if . For the second statement, most complexity theorists actually believe that , but this is a topic we will not cover in this course.
This is not the last time we will see how BPP relates to other classes; we will return to this topic in later lectures.
Alternate Definition of BPP
Taking inspiration from the above outline of , we can give an alternative definition of BPP similar to the certificate definition of NP.
Definition. A language is in the class BPP if there exists a polynomial-time deterministic Turing machine and a polynomial such that for all ,
3 Examples of BPP Algorithms
We’ll now give 3 examples of randomized algorithms.
Finding the Median
Given a set of numbers , the median of is the number such that for elements and for elements . Stated as a decision problem, you’d be given and would need to decide if is the median of .
There is an easy deterministic algorithm that requires time to check.
- Sort and obtain , where for all .
- If is even, set (more generally, any number in the range ). If is odd, set .
This takes time since we sort .
Now there is a time deterministic algorithm for finding the median and, more generally, the th smallest element. However, the algorithm is highly non-trivial.
To contrast, we will give an time simple randomized algorithm to find the th smallest element, where an element is the th smallest if satisfy and satisfy . In particular, setting gives us the median problem. Let .
- FindthElement:
- Pick . Set .
- Scan and let denote the number of elements such that .
- If , output .
- If :
- Create set .
- Run FindthElement.
- If :
- Let .
- Run FindthElement.
We now give the intuition on why this algorithm runs in time for initial list size .
- The deterministic parts of the algorithm run in linear time.
- The expected sizes of and can be shown to be at most . That is, with good/high probability over the random choice of , and (where the “” is updated every recursive call to mean ).
- Together, this implies that if is the runtime of the algorithm, then , which can be shown to imply that .
Polynomial Identity Testing
Polynomial identity testing is very common in many areas of theoretical computer science and cryptography. Consider an variate polynomial with integer coefficients We define the degree of to be , where is the largest degree of in . For example, if , then .
The most natural identity to test is whether is the identically zero polynomial. That is, for all . Note that this isn’t even checkable in polynomial time. One can give a with a small description but, once expanded, contain terms! An example of one such polynomial is for some constants .
However, there is a probabilistic algorithm which can efficiently check if is identically zero, assuming that evaluating at any single point is efficient (i.e., polynomial-time).
:
- Sample .
- Sample uniformly random .
- Check if . Output if this check passes, and otherwise.
Observations. Let .
- If then for any .
- Suppose that . We want to analyze the probability that . This is equal to the probability that divides . Suppose that has prime factors . We can upper bound the probability that divides by the probability that is any one of these prime numbers. By the Prime Number Theorem, the number of primes in is at least . Now, one can show that . This implies that the number of elements of not equal to is at least . So, with probability at least , the randomly chosen will not be equal to any of . This gives us
- Now suppose that but is not the identically zero polynomial. This means that is a root of the polynomial . By the Schwartz-Zippel lemma, this implies that
All together, these observations imply that
Verifying Matrix Multiplications
Let be a prime number and let be the set of integers modulo . Fix three matrices . We want to decide if .
The fastest known matrix multiplication algorithm runs in time (and, I believe, is not possible to be run on a real computer because the hidden constant is enormous). There is also the trivial algorithm which takes time; here, we’re actually counting the number of operations, so this algorithm takes roughly multiplications and additions over .
We can use randomness to get an -time algorithm to verify if . The algorithm operates as follows.
- Sample .
- Create vector .
- Check if . Output if and only if this check passes.
Notice that computing takes time. Similarly, computing takes time, and also takes time, so the algorithm runs in time.
Now, if , then for any choice of , and thus with probability if then . What if ? This basically reduces to polynomial equality checks (or, equivalently, zero-checks). Let be the th row of . Then, if we set , we have that . Recall that , so this inner product looks like , which looks exactly like evaluating a degree polynomial, say , at a random point .
Now, if we set , suppose that in row , we have . Then, our random check is actually testing the following: In particular, the above is true if and differ on a single entry! So this shows that
One-Sided Error
Interestingly, the above algorithms all have stronger guarantees than just BPP gives. Finding the median always outputs the correct answer, but runs in expected linear time. Both polynomial identity testing and verifying matrix multiplications will answer “YES” with probability when the polynomial given as input is identically zero and when for the given matrices, but both of these algorithms will output “YES” with small probability (not equal to ) when the input does not have the correct form.
Building on the latter two algorithms, this lets define two “stronger” classes than BPP which have one-sided error.
Definition. The class is the set of all languages that are decidable by a PTM running in time such that for all We define the class RP as
Naturally, we have the co-class of RP, called coRP defined as . Equivalently, it is also a one-sided error class with the opposite guarantees as RP.
Definition. The class is the set of all languages that are decidable by a PTM running in time such that for all We define the class coRP as