Randomness is a fleeting commodity if you believe in the “Big Freeze” theory of our universe’s fate [3]. Should we survive as flesh or metal to explore the universe, a lack of randomness could make our algorithms and programs inefficient (if P≠BPPP \neq BPP), or at least it would likely be more complicated to write (if P=BPPP = BPP) [1]. It is currently unknown which case we exist in, although most likely the former case, but neither are ideal situations. Randomness is particularly vital to a field I find great interest in, that is probabilistic proof systems [2] [5]. Although it may make winning the big lottery jackpot harder, we should be cherishing randomness while we have it, because in this blog post I will show examples where access to randomness can give us improvements in communication cost, running time, and space cost.

Communication Complexity Improvements

Imagine the following scenario. Alice and Bob are on opposite ends of the earth. Bob wants to check if a document that Alice has on her computer is the same as the one Bob has on his. How might Bob be convinced of this? Well, the first idea that probably comes to mind is for Alice to just send her file to Bob. If Alice is not using any coding theory techniques to detect if any errors occur during the transmission, which would be unlikely in practice, Alice would have to send the entire file to Bob. This may be doable if our file is small, but what if the file was all of Wikipedia? Or what if it were the Ethereum blockchain? That would start to become an issue. Unfortunately if we had to be 100% confident that our files were the same, there is no shortcut. However, if we were willing to allow some probability of one-sided error, that is Bob being convinced the files are the same when they are not, then we can utilize randomness to help us with this task. I will walk through an example of a protocol for this, then describe the protocol in technical detail, then prove the correctness of the protocol.

First, let’s assume that they already established that their file is the same size, because if not, they quickly know the contents aren’t the same. Suppose Alice has a file containing “hello”, as does Bob. Supposing we know the file contains only ASCII characters, then each character can be mapped to a number between 0 and 127, inclusive. So, “hello” gets mapped to “104 101 108 108 111”. We then interpret this encoded message as the polynomial

pAlice(x)=104+101x+108x2+108x3+111x4,p_{Alice}(x) = 104 + 101x + 108x^2 + 108x^3 + 111x^4,

over the field F131\mathbb{F}_{131}. If you don’t know what a field is, don’t worry. I won’t go over it here, since there are many introductions to fields available online, including Dummit and Foote notes, Wikipedia, and Socratica. All you need to know for the purpose of this article, for the fields we will be working with, Fp\mathbb{F}_p is a number system that works exactly like clock arithmetic, but on a clock with pp hours. You can do all your familiar operations you know and love, just mod by pp in the end. Division is a little trickier, since you’ll have to use the Extended Euclidean Algorithm, or you can trust my math here.

Now that Alice encoded her message as a polynomial, she will pick an element uniformly random from the field, notated r←RF131r \xleftarrow{R} \mathbb{F}_{131}, and evaluate it on the polynomial. For this example, say r=71r = 71. So, Alice calculates

pAlice(71)=104+101(71)+108(712)+108(713)+111(714)=2859902682≡24(mod131).\begin{aligned} p_{Alice}(71) &= 104 + 101(71) + 108(71^2) + 108(71^3) + 111(71^4) \\ &= 2859902682 \\ &\equiv 24 \pmod{131}. \end{aligned}

Alice then sends Bob the message (pAlice(r),r)=(24,71)(p_{Alice}(r), r) = (24, 71). Now, Bob encodes his file contents as the polynomial pBobp_{Bob} in the same way that Alice did hers, and he also evaluates his polynomial on rr. So, he checks pBob(71)≡?24(mod131)p_{Bob}(71) \stackrel{?}{\equiv} 24 \pmod{131}. If so, he is convinced that with high probability (at least 96.9%) that they share the same file, otherwise he knows the file contents are different. We prove this property of the protocol shortly below. This confidence can also be made arbitrarily high!

Pause and Reflect

Let’s take a second to really appreciate the power of what we just did. Instead of Alice sending the entire file contents, which happen to be five elements from the field in this example, she instead only sent two field elements to Bob, 24 and 71. You might be thinking, we saved our communication by halfish, but we did FAR better than that! If you look closely, it doesn’t matter how big the file is. We always only need to send two field elements to Bob to convince him of his question!

File Equality Check Protocol

  • INPUT: Alice has file contents mA=(mA,1,mA,2,…,mA,n)m_A = (m_{A,1}, m_{A,2}, \dots, m_{A,n}) and Bob has file contents mB=(mB,1,mB,2,…,mB,n)m_B = (m_{B,1}, m_{B,2}, \dots, m_{B,n}), whose characters come from an alphabet of size mm. Let p>max⁡{m,n2}p > \max\{m, n^2\} be prime.
  • GOAL: Bob wants to determine if mA=?mBm_A \stackrel{?}{=} m_B. If so, he outputs EQUAL\texttt{EQUAL}, otherwise he outputs NOT-EQUAL\texttt{NOT-EQUAL}.
  1. Alice selects r←RFpr \xleftarrow{R} \mathbb{F}_p.
  2. Alice sends the message (pA(r),r)(p_A(r), r) to Bob, where pA(x)=∑i=1nmA,i⋅xi−1(modp)p_A(x) = \sum_{i = 1}^{n} m_{A,i} \cdot x^{i - 1} \pmod{p}.
  3. Bob checks pA(r)=?pB(r)p_A(r) \stackrel{?}{=} p_B(r), where pB(x)=∑i=1nmB,i⋅xi−1(modp)p_B(x) = \sum_{i = 1}^n m_{B,i} \cdot x^{i - 1} \pmod{p}. If true, output EQUAL\texttt{EQUAL}, otherwise output NOT-EQUAL\texttt{NOT-EQUAL}.

Now, below, I will prove that if the file contents are the same and Alice is honest, meaning Alice may not deviate from the protocol to convince Bob otherwise, then Bob is convinced that the files are the same with probability 1. I will also prove that if the file contents are not the same and Alice is honest, then Bob will be falsely convinced the files are the same with probability <1/n< 1/n. In other words, the probability that Bob is convinced of the contrary decreases linearly with the size of the file contents. Both properties have technical terms in probabilistic proof systems, where the first property is called completeness and the second is called soundness. Completeness is usually trivial, while soundness is not.

Proof.

Completeness: Suppose that mA=mBm_A = m_B and Alice is honest. Since mA,i=mB,im_{A,i} = m_{B,i} for all ii, then Bob outputs EQUAL\texttt{EQUAL} for all choices of rr.

Soundness: Suppose mA≠mBm_A \neq m_B. Then the polynomials pA(x)p_A(x) and pB(x)p_B(x) are distinct, since mA,i≠mB,im_{A,i} \neq m_{B,i} for at least some ii. Since the degree of both polynomials is at most n−1n - 1 and they are distinct, then pA(x)=pB(x)p_A(x) = p_B(x) for at most n−1n - 1 values of x∈Fpx \in \mathbb{F}_p. Now, since p>max⁡{m,n2}p > \max\{m, n^2\}, the probability that Bob outputs EQUAL\texttt{EQUAL} is ≤n−1p<n−1n2<1n\leq \frac{n-1}{p} < \frac{n-1}{n^2} < \frac{1}{n}.

Running Time Complexity Improvements

Now, I’ll describe how randomness can improve the running time performance of algorithms. Imagine this new scenario. Some salesman comes to your door and is selling you a black box which will spit out the result of two n×nn \times n matrices multiplied together in O(1)O(1) time! Now, the naive time complexity of multiplying two n×nn \times n matrices is O(n3)O(n^3) with your normal matrix multiplication you would learn in a linear algebra class. A popular improvement used today is called Strassen’s algorithm, which runs in O(nlog⁡27+o(1))≈O(n2.8074)O(n^{\log_2 7 + o(1)}) \approx O(n^{2.8074}) time. The fastest known algorithm for multiplying two n×nn \times n matrices currently runs in time O(n2.371339)O(n^{2.371339}), however it isn’t very useful unless the matrices are incredibly large.

This would be an incredibly useful tool to have to quickly satisfy all your square matrix multiplication needs! But, you don’t really trust what the salesman says and what he claims the product to do. You also don’t want to spend a lot of time checking if it did the calculation right, which would require running one of those algorithms above, and at that point there would be no point in buying the box. However, luckily we can use randomness to be highly convinced of the correctness of the answer in O(n2)O(n^2) time!

Now, from a bird’s eye view, how does this algorithm work? Imagine you have two matrices A,B∈Fpn×nA,B \in \mathbb{F}_p^{n \times n} and you throw them into this machine, which returns the supposed result C∈Fpn×nC \in \mathbb{F}_p^{n \times n}. Now, we don’t want to check if C=?A⋅BC \stackrel{?}{=} A \cdot B since, again, we would need to use one of the algorithms above, which is too slow. Instead, what if we choose r←RFpr \xleftarrow{R} \mathbb{F}_p and multiply both sides of the equation by the column vector x=(1,r,r2,…,rn−1)x = (1, r, r^2, \dots, r^{n-1}) so that we now check if C⋅x=?(A⋅B)⋅xC \cdot x \stackrel{?}{=} (A \cdot B) \cdot x? Well, now we still have a matrix multiplication, which is slow, but we are definitely getting somewhere because matrix multiplication is associative! So, we can check if C⋅x=?A⋅(B⋅x)C \cdot x \stackrel{?}{=} A \cdot (B \cdot x) without the slow matrix multiplication.

Freivalds' Algorithm (Variant)

  • INPUT: A,B,C∈Fpn×nA,B,C \in \mathbb{F}_p^{n \times n}
  • GOAL: Determine C=?A⋅BC \stackrel{?}{=} A \cdot B. Output EQUAL\texttt{EQUAL} if true, and output NOT-EQUAL\texttt{NOT-EQUAL} otherwise.
  1. Choose r←RFpr \xleftarrow{R} \mathbb{F}_p and let x=(1,r,r2,…,rn−1)x = (1, r, r^2, \dots, r^{n-1}).
  2. Check C⋅x=?A⋅(B⋅x)C \cdot x \stackrel{?}{=} A \cdot (B \cdot x). If true, output EQUAL\texttt{EQUAL}, otherwise output NOT-EQUAL\texttt{NOT-EQUAL}.

This algorithm is a variation of Freivalds’ algorithm, runs in time O(n2)O(n^2), and can verify the matrix product with a probability of failure ≤(n−1)/p\leq (n-1)/p. This is a variation on the original because the original algorithm chooses x←R{0,1}nx \xleftarrow{R} \{0,1\}^n, which still runs in time O(n2)O(n^2), but has an error probability of ≤1/2\leq 1/2. You can see that we get a tradeoff where choosing pp much larger than nn gives us better confidence in our verification, at the cost of using randomness from a larger field. I chose to show the variation of Freivalds’ algorithm because its analysis is actually easier, especially given the proof we already established in the section above.

Proof.

Completeness: Suppose that C=A⋅BC = A \cdot B. Let D=A⋅BD = A \cdot B. Then C=DC = D because Ci,j=Di,jC_{i,j} = D_{i,j} for all i,j∈[n]i,j \in [n]. Thus, C⋅x=D⋅xC \cdot x = D \cdot x for all xx. Thus, we output EQUAL\texttt{EQUAL} with probability 1.

Soundness: Suppose C≠A⋅BC \neq A \cdot B. Let D=A⋅BD = A \cdot B. Then Ci,j≠Di,jC_{i,j} \neq D_{i,j} for some i,j∈[n]i,j \in [n]. Without loss of generality, suppose it is such that i=1i = 1. Then we are testing the equality of two distinct polynomials at a point rr. Namely, the polynomial on the LHS is pC(r)=∑j=1nC1,j⋅rj−1(modp)p_C(r) = \sum_{j = 1}^n C_{1,j} \cdot r^{j-1} \pmod{p} and the RHS polynomial is pD(r)=∑j=1nD1,j⋅rj−1(modp)p_D(r) = \sum_{j = 1}^n D_{1,j} \cdot r^{j - 1} \pmod{p}. Since they are distinct, they agree on at most n−1n - 1 points. Therefore, we will output EQUAL\texttt{EQUAL} with probability ≤(n−1)/p\leq (n-1)/p.

Space Complexity Improvements

Lastly, we are going to look at an example where access to randomness can save us space. The problem we are going to look at is called the undirected ss-tt connectivity (USTCON) problem. Given an undirected graph GG and two vertices ss and tt in GG, we must decide whether ss and tt are in the same component, that is that there is a path going from one to the other. This may seem like an esoteric example, but there are actually many real-world examples where this is useful. We could imagine that Google Maps stores all of the roads as a graph where the vertices are intersections and the roads going in and out of intersections are edges. So, we could see Google Maps being able to answer the question “Can you get from intersection xx to intersection yy”?

How might we solve this problem if we wanted to be 100% sure of its connectivity? Well, the easiest way might be to do a depth-first search starting from ss. The space complexity of this solution is O(n)O(n) and the time complexity is O(n+m)O(n + m), where nn and mm are the number of vertices and edges in the graph, respectively, since it has to keep track of where it has been and possibly traverse the whole graph. As with our previous examples, if we use randomness and we allow ourselves a probability of outputting the incorrect solution, we can find improvements in space complexity.

Now, what if we just walk around the graph randomly? This could greatly reduce our space because all we need to do is keep track of the current vertex we are on, which only takes O(log⁡2n)O(\log_2 n) space with a numbering scheme of the vertices. But, we don’t want to be walking forever because it may be the case that ss and tt are not connected. So, how long might we want to be randomly walking for in order to be highly confident in outputting NOT-CONNECTED\texttt{NOT-CONNECTED} if we have not seen tt? Luckily for us the study of Markov Chains gives us some terminology and prior results to solve this question.

The hitting time between vertices uu and vv, denoted huvh_{uv}, is the expected number of steps in a random walk that starts at uu and ends upon first reaching vv. The commute time between vertices uu and vv, denoted CuvC_{uv}, is the expected number of steps in a random walk that starts at uu, reaches vv, and returns to uu. Clearly, huv<Cuvh_{uv} < C_{uv} when u≠vu \neq v. I will prove that if we walk for 2n32n^3 steps and ss and tt are connected, we will output CONNECTED\texttt{CONNECTED} with probability ≥1/2\geq 1/2. The following corollary is useful to us, but will not be proved, as it is out of the scope of the blog post. See Chapter 6 of the Motwani and Raghavan book for details [4].

Corollary 1 Commute Time Bound

For any connected undirected graph G=(V,E)G = (V,E) with nn vertices, and any distinct vertices u,v∈Vu,v \in V, the hitting and commute times satisfy huv<Cuv<n3=O(n3)h_{uv} < C_{uv} < n^3 = O(n^3).

Theorem 1

For a graph G=(V,E)G = (V,E) with nn vertices and vertices s,t∈Vs,t \in V, if we perform a random walk of length 2n32n^3 starting at ss and output CONNECTED\texttt{CONNECTED} if we reach tt, then if ss and tt are connected, we will output CONNECTED\texttt{CONNECTED} with probability ≥1/2\geq 1/2, and if they are not connected, we will output NOT-CONNECTED\texttt{NOT-CONNECTED} with probability 1.

Proof.

Let G=(V,E)G = (V,E). If s,t∈Vs,t \in V are not connected, then our random walk of length 2n32n^3 starting at ss will never reach tt. Thus, we output NOT-CONNECTED\texttt{NOT-CONNECTED} with probability 1.

If ss and tt are connected and s=ts = t, then we output CONNECTED\texttt{CONNECTED} immediately. If ss and tt are connected and distinct, then the corollary above gives hst<n3h_{st} < n^3. By Markov’s inequality, the probability that the random walk has not reached tt after 2n32n^3 steps is less than 1/21/2. Therefore, when we walk for 2n32n^3 steps, we output CONNECTED\texttt{CONNECTED} with probability ≥1/2\geq 1/2.

So, notice that we have given ourselves a trade-off. The depth-first search solution was able to output an always correct answer in time O(n+m)O(n + m), which is O(n2)O(n^2) in the densest case, while the randomized walk algorithm runs in time O(n3)O(n^3), with a probability of outputting a correct answer. However, as noted previously, we do not need to store all previously visited vertices in the randomized algorithm, just the current vertex and a step counter, which gives us a greatly improved space complexity of O(log⁡2n)O(\log_2 n).

There is one important modern caveat here. This random-walk algorithm is a simple and historically important way to see how randomness can reduce space, but it is not the final word on USTCON. Omer Reingold later proved that undirected ss-tt connectivity can be solved deterministically in logarithmic space. So, randomness is not necessary for the best-known space bound here, but it remains the most approachable route to the idea.

References

[1] Adleman, L. (1979). Time, space and randomness. MIT/LCS/TM-131.

[2] Goldreich, O. (2011). Randomness and Computation. In: Goldreich, O. (eds) Studies in Complexity and Cryptography. Miscellanea on the Interplay between Randomness and Computation. Lecture Notes in Computer Science, vol 6650. Springer, Berlin, Heidelberg.

[3] Hayes, B. (2001). Computing Science: Randomness as a Resource. American Scientist, 89(4), 300-304.

[4] Motwani, R., & Raghavan, P. (1995). Randomized Algorithms. Cambridge, England: Cambridge University Press.

[5] Thaler, J. (2023). Proofs, Arguments, and Zero-Knowledge (Draft).