# Introduction

This course will study Randomized Algorithms, the algorithms that use randomness in computation.

Why do we use randomness in computation?
• Randomized algorithms can be simpler than deterministic ones.
• Randomized algorithms can be faster than the best known deterministic algorithms.
(min-cut, checking matrix multiplication, primality testing, etc.)
• Randomized algorithms can do things that deterministic algorithms cannot do.
(routing, volume estimation, communication complexity, data streams, etc.)
• Randomized algorithms may lead us to smart deterministic algorithms.
(hashing, derandomization, SL=L, Lovász Local Lemma, etc.)
• Randomness is presented in the input.
(average-case analysis, smoothed analysis, learning, etc.)
• Some deterministic problems are random in nature.
(counting, inference, etc.)
• ...
How is randomness used in computation?
• To hit a witness/certificate.
(identity testing, fingerprinting, primality testing, etc.)
• To avoid worst case or to deal with adversaries.
(randomized quick sort, perfect hashing, etc.)
• To simulate random samples.
(random walk, Markov chain Monte Carlo, approximate counting etc.)
• To enumerate/construct solutions.
(the probabilistic method, min-cut, etc.)
• To break symmetry.
(mutual exclusion, leader election, parrallel/distributed algorithms, etc.)
• ...

## Principles in probability theory

The course is organized by the advancedness of the probabilistic tools. We do this for two reasons: First, for randomized algorithms, analysis is usually more difficult and involved than the algorithm itself; and second, getting familiar with these probability principles will help you understand the true reasons for which the smart algorithms are designed.

• Basics of probability theory: probability space, events, the union bound, independence, conditional probability.
• Moments and deviations: random variables, expectation, linearity of expectation, Markov's inequality, variance, second moment method.
• The probabilistic method: averaging principle, threshold phenomena, Lovász Local Lemma.
• Concentrations: Chernoff-Hoeffding bound, martingales, Azuma's inequality, bounded difference method.
• Markov chains and random walks: Markov chians, random walks, hitting/cover time, mixing time, coupling, conductance.

# Probability Space

The axiom foundation of probability theory is laid by Kolmogorov, one of the greatest mathematician of the 20th century, who advanced various very different fields of mathematics.

 Definition (Probability Space) A probability space is a triple ${\displaystyle (\Omega ,\Sigma ,\Pr )}$. ${\displaystyle \Omega }$ is a set, called the sample space. ${\displaystyle \Sigma \subseteq 2^{\Omega }}$ is the set of all events, satisfying: (K1). ${\displaystyle \Omega \in \Sigma }$ and ${\displaystyle \emptyset \in \Sigma }$. (The certain event and the impossible event.) (K2). If ${\displaystyle A,B\in \Sigma }$, then ${\displaystyle A\cap B,A\cup B,A-B\in \Sigma }$. (Intersection, union, and diference of two events are events). A probability measure ${\displaystyle \Pr :\Sigma \rightarrow \mathbb {R} }$ is a function that maps each event to a nonnegative real number, satisfying (K3). ${\displaystyle \Pr(\Omega )=1}$. (K4). If ${\displaystyle A\cap B=\emptyset }$ (such events are call disjoint events), then ${\displaystyle \Pr(A\cup B)=\Pr(A)+\Pr(B)}$. (K5*). For a decreasing sequence of events ${\displaystyle A_{1}\supset A_{2}\supset \cdots \supset A_{n}\supset \cdots }$ of events with ${\displaystyle \bigcap _{n}A_{n}=\emptyset }$, it holds that ${\displaystyle \lim _{n\rightarrow \infty }\Pr(A_{n})=0}$.
Remark
• In general, the set ${\displaystyle \Omega }$ may be continuous, but we only consider discrete probability in this lecture, thus we assume that ${\displaystyle \Omega }$ is either finite or countably infinite.
• Sometimes it is convenient to assume ${\displaystyle \Sigma =2^{\Omega }}$, i.e. the events enumerates all subsets of ${\displaystyle \Omega }$. But in general, a probability space is well-defined by any ${\displaystyle \Sigma }$ satisfying (K1) and (K2). Such ${\displaystyle \Sigma }$ is called a ${\displaystyle \sigma }$-algebra defined on ${\displaystyle \Omega }$.
• The last axiom (K5*) is redundant if ${\displaystyle \Sigma }$ is finite, thus it is only essential when there are infinitely many events. The role of axiom (K5*) in probability theory is like Zorn's Lemma (or equivalently the Axiom of Choice) in axiomatic set theory.

Useful laws for probability can be deduced from the axioms (K1)-(K5).

 Proposition Let ${\displaystyle {\bar {A}}=\Omega \setminus A}$. It holds that ${\displaystyle \Pr({\bar {A}})=1-\Pr(A)}$. If ${\displaystyle A\subseteq B}$ then ${\displaystyle \Pr(A)\leq \Pr(B)}$.
Proof.
 The events ${\displaystyle {\bar {A}}}$ and ${\displaystyle A}$ are disjoint and ${\displaystyle {\bar {A}}\cup A=\Omega }$. Due to Axiom (K4) and (K3), ${\displaystyle \Pr({\bar {A}})+\Pr(A)=\Pr(\Omega )=1}$. The events ${\displaystyle A}$ and ${\displaystyle B\setminus A}$ are disjoint and ${\displaystyle A\cup (B\setminus A)=B}$ since ${\displaystyle A\subseteq B}$. Due to Axiom (K4), ${\displaystyle \Pr(A)+\Pr(B\setminus A)=\Pr(B)}$, thus ${\displaystyle \Pr(A)\leq \Pr(B)}$.
${\displaystyle \square }$
Notation

An event ${\displaystyle A\subseteq \Omega }$ can be represented as ${\displaystyle A=\{a\in \Omega \mid {\mathcal {E}}(a)\}}$ with a predicate ${\displaystyle {\mathcal {E}}}$.

The predicate notation of probability is

${\displaystyle \Pr[{\mathcal {E}}]=\Pr(\{a\in \Omega \mid {\mathcal {E}}(a)\})}$.

During the lecture, we mostly use the predicate notation instead of subset notation.

## Independence

 Definition (Independent events) Two events ${\displaystyle {\mathcal {E}}_{1}}$ and ${\displaystyle {\mathcal {E}}_{2}}$ are independent if and only if {\displaystyle {\begin{aligned}\Pr \left[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\right]&=\Pr[{\mathcal {E}}_{1}]\cdot \Pr[{\mathcal {E}}_{2}].\end{aligned}}}

This definition can be generalized to any number of events:

 Definition (Independent events) Events ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ are mutually independent if and only if, for any subset ${\displaystyle I\subseteq \{1,2,\ldots ,n\}}$, {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i\in I}{\mathcal {E}}_{i}\right]&=\prod _{i\in I}\Pr[{\mathcal {E}}_{i}].\end{aligned}}}

Note that in probability theory, the "mutual independence" is not equivalent with "pair-wise independence", which we will learn in the future.

# Model of Computation

Our model of computation extends the standard model (Turing machine or random-access machine) with access to uniform and independent random bits (fair coin flips). On a fixed input, the behavior of the algorithm is random. To be specific, the output or running time of the algorithm may be random.

## Monte Carlo algorithms

Monte Carlo algorithms always returns in finite steps but may output the wrong answer. For decision problems (problems with two answers "yes" and "no"), the Monte Carlo algorithms are further divided into those with one-sided errors and two-sided errors.

Monte Carlo algorithms with one-sided errors
These algorithms only make errors in one direction, which may be further divided into two cases:
False positive: If the true answer is "yes" then the algorithm returns "yes" with probability 1, and if the true answer is "no" then the algorithm returns "no" with probability at least ${\displaystyle \epsilon }$, where ${\displaystyle 0<\epsilon <1}$ is the confidence. The algorithm may return a wrong "yes" while the true answer is "no".
The one-sided error can be reduced by independent repetitions. Run the algorithm independently for ${\displaystyle t}$ times, output "yes" if all running instances return "yes", and output "no" if otherwise. If the true answer is "yes" this new algorithm returns "yes" since all running instances are guaranteed to do so, and if the true answer is "no" the new algorithm returns "yes" only if all running instances return "yes", whose probability is bounded by ${\displaystyle (1-\epsilon )^{t}}$, which can be reduced to any ${\displaystyle \delta \in (0,1)}$ by setting ${\displaystyle t=O\left({\frac {1}{\epsilon }}\log {\frac {1}{\delta }}\right)}$.
False negative: If the true answer is "yes" then the algorithm returns "yes" with probability at least ${\displaystyle \epsilon }$, and if the true answer is "no" then the algorithm returns "no" with probability 1. The algorithm may return a wrong "no" while the true answer is "yes". The error can be reduced in the same way.

Monte Carlo algorithms with two-sided errors
These algorithms make errors in both directions. If the true answer is "yes" then the algorithm returns "yes" with probability at least ${\displaystyle {\frac {1}{2}}+\epsilon }$, and if the true answer is "no" then the algorithm returns "no" with probability at least ${\displaystyle {\frac {1}{2}}+\epsilon }$, where ${\displaystyle \epsilon \in \left(0,{\frac {1}{2}}\right)}$ is a bias.
The error can be reduced by repetitions and majority vote. Run the algorithm independently for ${\displaystyle t}$ times, output "yes" if over half running instances return "yes", and output "no" if otherwise. The numbers of "yes"s and "no"s in the ${\displaystyle t}$ trials follow the Binomial distribution. For each ${\displaystyle 0\leq i\leq t}$, the probability that there are precisely ${\displaystyle i}$ correct answers in ${\displaystyle t}$ trials is given by
${\displaystyle {t \choose i}\left({\frac {1}{2}}+\epsilon \right)^{i}\left({\frac {1}{2}}-\epsilon \right)^{t-i},}$
and the new algorithm returns a wrong answer only if at most ${\displaystyle \lfloor t/2\rfloor }$ correct answers in ${\displaystyle t}$ trials, which is given by
{\displaystyle {\begin{aligned}\sum _{i=0}^{\lfloor t/2\rfloor }{t \choose i}\left({\frac {1}{2}}+\epsilon \right)^{i}\left({\frac {1}{2}}-\epsilon \right)^{t-i}&\leq \sum _{i=0}^{\lfloor t/2\rfloor }{t \choose i}\left({\frac {1}{2}}+\epsilon \right)^{t/2}\left({\frac {1}{2}}-\epsilon \right)^{t/2}\\&=\left({\frac {1}{4}}-\epsilon ^{2}\right)^{t/2}\sum _{i=0}^{\lfloor t/2\rfloor }{t \choose i}\\&\leq \left({\frac {1}{4}}-\epsilon ^{2}\right)^{t/2}2^{t}\\&=(1-4\epsilon ^{2})^{t/2},\end{aligned}}}
which can be reduced to any ${\displaystyle \delta \in (0,1)}$ by setting ${\displaystyle t=O\left({\frac {1}{\epsilon ^{2}}}\log {\frac {1}{\delta }}\right)}$.

## Las Vegas algorithms

Las Vegas algorithms always output correct answers but the running time is random. The time complexity of a Las Vegas algorithm is measure by the expected running time. The concept of Las Vegas algorithm is introduced by Babai in 1979 in his seminal work on graph isomorphsm testing.

A Las Vegas algorithm can be converted to a Monte Carlo algorithm by truncating. The error of the resulting Monte Carlo algorithm can be made one-sided and can be bounded by Markov's inequality. There is no general way known to convert a Monte Carlo algorithm to a Las Vegas algorithm.

# Checking Matrix Multiplication

The evolution of time complexity ${\displaystyle O(n^{\omega })}$ for matrix multiplication.

Let ${\displaystyle \mathbb {F} }$ be a feild (you may think of it as the filed ${\displaystyle \mathbb {Q} }$ of rational numbers, or the finite field ${\displaystyle \mathbb {Z} _{p}}$ of integers modulo prime ${\displaystyle p}$). We suppose that each field operation (addition, subtraction, multiplication, division) has unit cost. This model is called the unit-cost RAM model, which is an ideal abstraction of a computer.

Consider the following problem:

• Input: Three ${\displaystyle n\times n}$ matrices ${\displaystyle A}$, ${\displaystyle B}$, and ${\displaystyle C}$ over the field ${\displaystyle \mathbb {F} }$.
• Output: "yes" if ${\displaystyle C=AB}$ and "no" if otherwise.

A naive way to solve this is to multiply ${\displaystyle A}$ and ${\displaystyle B}$ and compare the result with ${\displaystyle C}$. The straightforward algorithm for matrix multiplication takes ${\displaystyle O(n^{3})}$ time, assuming that each arithmetic operation takes unit time. The Strassen's algorithm discovered in 1969 now implemented by many numerical libraries runs in time ${\displaystyle O(n^{\log _{2}7})\approx O(n^{2.81})}$. Strassen's algorithm starts the search for fast matrix multiplication algorithms. The Coppersmith–Winograd algorithm discovered in 1987 runs in time ${\displaystyle O(n^{2.376})}$ but is only faster than Strassens' algorithm on extremely large matrices due to the very large constant coefficient. This has been the best known for decades, until recently Stothers got an ${\displaystyle O(n^{2.3737})}$ algorithm in his PhD thesis in 2010, and independently Vassilevska Williams got an ${\displaystyle O(n^{2.3727})}$ algorithm in 2012. Both these improvements are based on generalization of Coppersmith–Winograd algorithm. It is unknown whether the matrix multiplication can be done in time ${\displaystyle O(n^{2+o(1)})}$.

## Freivalds Algorithm

The following is a very simple randomized algorithm due to Freivalds, running in ${\displaystyle O(n^{2})}$ time:

 Algorithm (Freivalds, 1979) pick a vector ${\displaystyle r\in \{0,1\}^{n}}$ uniformly at random; if ${\displaystyle A(Br)=Cr}$ then return "yes" else return "no";

The product ${\displaystyle A(Br)}$ is computed by first multiplying ${\displaystyle Br}$ and then ${\displaystyle A(Br)}$. The running time of Freivalds algorithm is ${\displaystyle O(n^{2})}$ because the algorithm computes 3 matrix-vector multiplications.

If ${\displaystyle AB=C}$ then ${\displaystyle A(Br)=Cr}$ for any ${\displaystyle r\in \{0,1\}^{n}}$, thus the algorithm will return a "yes" for any positive instance (${\displaystyle AB=C}$). But if ${\displaystyle AB\neq C}$ then the algorithm will make a mistake if it chooses such an ${\displaystyle r}$ that ${\displaystyle ABr=Cr}$. However, the following lemma states that the probability of this event is bounded.

 Lemma If ${\displaystyle AB\neq C}$ then for a uniformly random ${\displaystyle r\in \{0,1\}^{n}}$, ${\displaystyle \Pr[ABr=Cr]\leq {\frac {1}{2}}}$.
Proof.
 Let ${\displaystyle D=AB-C}$. The event ${\displaystyle ABr=Cr}$ is equivalent to that ${\displaystyle Dr=0}$. It is then sufficient to show that for a ${\displaystyle D\neq {\boldsymbol {0}}}$, it holds that ${\displaystyle \Pr[Dr={\boldsymbol {0}}]\leq {\frac {1}{2}}}$. Since ${\displaystyle D\neq {\boldsymbol {0}}}$, it must have at least one non-zero entry. Suppose that ${\displaystyle D_{ij}\neq 0}$. We assume the event that ${\displaystyle Dr={\boldsymbol {0}}}$. In particular, the ${\displaystyle i}$-th entry of ${\displaystyle Dr}$ is ${\displaystyle (Dr)_{i}=\sum _{k=1}^{n}D_{ik}r_{k}=0.}$ The ${\displaystyle r_{j}}$ can be calculated by ${\displaystyle r_{j}=-{\frac {1}{D_{ij}}}\sum _{k\neq j}^{n}D_{ik}r_{k}.}$ Once all other entries ${\displaystyle r_{k}}$ with ${\displaystyle k\neq j}$ are fixed, there is a unique solution of ${\displaystyle r_{j}}$. Therefore, the number of ${\displaystyle r\in \{0,1\}^{n}}$ satisfying ${\displaystyle Dr={\boldsymbol {0}}}$ is at most ${\displaystyle 2^{n-1}}$. The probability that ${\displaystyle ABr=Cr}$ is bounded as ${\displaystyle \Pr[ABr=Cr]=\Pr[Dr={\boldsymbol {0}}]\leq {\frac {2^{n-1}}{2^{n}}}={\frac {1}{2}}}$.
${\displaystyle \square }$

When ${\displaystyle AB=C}$, Freivalds algorithm always returns "yes"; and when ${\displaystyle AB\neq C}$, Freivalds algorithm returns "no" with probability at least 1/2.

To improve its accuracy, we can run Freivalds algorithm for ${\displaystyle k}$ times, each time with an independent ${\displaystyle r\in \{0,1\}^{n}}$, and return "yes" if and only if all running instances returns "yes".

 Freivalds' Algorithm (multi-round) pick ${\displaystyle k}$ vectors ${\displaystyle r_{1},r_{2},\ldots ,r_{k}\in \{0,1\}^{n}}$ uniformly and independently at random; if ${\displaystyle A(Br_{i})=Cr_{i}}$ for all ${\displaystyle i=1,\ldots ,k}$ then return "yes" else return "no";

If ${\displaystyle AB=C}$, then the algorithm returns a "yes" with probability 1. If ${\displaystyle AB\neq C}$, then due to the independence, the probability that all ${\displaystyle r_{i}}$ have ${\displaystyle ABr_{i}=C_{i}}$ is at most ${\displaystyle 2^{-k}}$, so the algorithm returns "no" with probability at least ${\displaystyle 1-2^{-k}}$. For any ${\displaystyle 0<\epsilon <1}$, choose ${\displaystyle k=\log _{2}{\frac {1}{\epsilon }}}$. The algorithm runs in time ${\displaystyle O(n^{2}\log _{2}{\frac {1}{\epsilon }})}$ and has a one-sided error (false positive) bounded by ${\displaystyle \epsilon }$.

# Polynomial Identity Testing (PIT)

Consider the following problem of Polynomial Identity Testing (PIT):

• Input: two polynomials ${\displaystyle P_{1},P_{2}\in \mathbb {F} [x]}$ of degree ${\displaystyle d}$.
• Output: "yes" if two polynomials are identical, i.e. ${\displaystyle P_{1}\equiv P_{2}}$, and "no" if otherwise.

The ${\displaystyle \mathbb {F} [x]}$ denote the ring of polynomials over field ${\displaystyle \mathbb {F} }$.

Alternatively, we can consider the following equivalent problem:

• Input: a polynomial ${\displaystyle P\in \mathbb {F} [x]}$ of degree ${\displaystyle d}$.
• Output: "yes" if ${\displaystyle P\equiv 0}$, and "no" if otherwise.

The probalem is trivial if ${\displaystyle P}$ is presented in its explicit form ${\displaystyle P(x)=\sum _{i=0}^{d}a_{i}x^{i}}$. But we assume that ${\displaystyle P}$ is given in product form or as black box.

A straightforward deterministic algorithm that solves PIT is to query ${\displaystyle d+1}$ points ${\displaystyle P(1),P(2),\ldots ,P(d+1)}$ and check whether thay are all zero. This can determine whether ${\displaystyle P\equiv 0}$ by interpolation.

We now introduce a simple randomized algorithm for the problem.

 Algorithm for PIT pick ${\displaystyle x\in \{1,2,\ldots ,2d\}}$ uniformly at random; if ${\displaystyle P(x)=0}$ then return “yes” else return “no”;

This algorithm requires only the evaluation of ${\displaystyle P}$ at a single point. And if ${\displaystyle P\equiv 0}$ it is always correct. And if ${\displaystyle P\not \equiv 0}$ then the probability that the algorithm wrongly returns "yes" is bounded as follows.

 Theorem Let ${\displaystyle P\in \mathbb {F} [x]}$ be a polynomial of degree ${\displaystyle d}$ over the field ${\displaystyle \mathbb {F} }$. Let ${\displaystyle S\subset \mathbb {F} }$ be an arbitrary set and ${\displaystyle x\in S}$ is chosen uniformly at random from ${\displaystyle S}$. If ${\displaystyle P\not \equiv 0}$ then ${\displaystyle \Pr[P(x)=0]\leq {\frac {d}{|S|}}.}$
Proof.
 A non-zero ${\displaystyle d}$-degree polynomial ${\displaystyle P}$ has at most ${\displaystyle d}$ distinct roots, thus at most ${\displaystyle d}$ members ${\displaystyle x}$ of ${\displaystyle S}$ satisfy that ${\displaystyle P(x)=0}$. Therefore, ${\displaystyle \Pr[P(x)=0]\leq {\frac {d}{|S|}}}$.
${\displaystyle \square }$

By the theorem, the algorithm can distinguish a non-zero polynomial from 0 with probability at least ${\displaystyle 1/2}$. This is achieved by evaluation of the polynomial at only one point and ${\displaystyle 1+\log _{2}d}$ many random bits.

## Communication Complexity of Identity

The communication complexity is introduced by Andrew Chi-Chih Yao as a model of computation which involves multiple participants, each with partial information of the input.

Assume that there are two entities, say Alice and Bob. Alice has a private input ${\displaystyle a}$ and Bob has a private input ${\displaystyle b}$. Together they want to compute a function ${\displaystyle f(a,b)}$ by communicating with each other. The communication follows a predefined communication protocol (the "algorithm" in this model) whose logics depends only on the problem ${\displaystyle f}$ but not on the inputs. The complexity of a communication protocol is measured by the number of bits communicated between Alice and Bob in the worst case.

The problem of checking identity is formally defined by the function EQ as follows: ${\displaystyle \mathrm {EQ} :\{0,1\}^{n}\times \{0,1\}^{n}\rightarrow \{0,1\}}$ and for any ${\displaystyle a,b\in \{0,1\}^{n}}$,

${\displaystyle \mathrm {EQ} (a,b)={\begin{cases}1&{\mbox{if }}a=b,\\0&{\mbox{otherwise.}}\end{cases}}}$

A trivial way to solve EQ is to let Bob send his entire input string ${\displaystyle b}$ to Alice and let Alice check whether ${\displaystyle a=b}$. This costs ${\displaystyle n}$ bits of communications.

It is known that for deterministic communication protocols, this is the best we can get for computing EQ.

 Theorem (Yao 1979) Any deterministic communication protocol computing EQ on two ${\displaystyle n}$-bit strings costs ${\displaystyle n}$ bits of communication in the worst-case.

This theorem is much more nontrivial to prove than it looks, because Alice and Bob are allowed to interact with each other in arbitrary ways. The proof of this theorem in Yao's 1979 paper initiates the field of communication complexity.

If the randomness is allowed, we can solve this problem up to a tolerable probabilistic error with significantly less communications. The inputs ${\displaystyle a,b\in \{0,1\}^{n}}$ are two strings ${\displaystyle a=a_{0}a_{1}\cdots a_{n-1},b=b_{0}b_{1}\cdots b_{n-1}}$ of ${\displaystyle n}$ bits. Let ${\displaystyle k=\lceil \log _{2}(2n)\rceil }$ and ${\displaystyle p\in [2^{k},2^{k+1}]}$ be an arbitrary prime number. (Such a prime ${\displaystyle p}$ always exists.) The input strings ${\displaystyle a,b}$ can be respectively represented as two polynomials ${\displaystyle f,g\in \mathbb {Z} _{p}[x]}$ such that ${\displaystyle f(x)=\sum _{i=0}^{n-1}a_{i}x^{i}}$ and ${\displaystyle g(x)=\sum _{i=0}^{n-1}b_{i}x^{i}}$ of degree ${\displaystyle n-1}$, where all additions and multiplications are modulo ${\displaystyle p}$. The randomized communication protocol is given as follows:

 A randomized protocol for EQ Alice does: pick ${\displaystyle x\in [p]}$ uniformly at random; send ${\displaystyle x}$ and ${\displaystyle f(x)}$ to Bob; Upon receiving ${\displaystyle x}$ and ${\displaystyle f(x)}$ Bob does: If ${\displaystyle f(x)=g(x)}$ return "yes"; else return "no".

Repeat this protocol for 100 times. The total number of bits to communicate is bounded by ${\displaystyle 200\log _{2}p=O(\log n)}$. Due to the analysis of the randomized algorithm for PIT, if ${\displaystyle a=b}$ the protocol is always correct and if ${\displaystyle a\neq b}$ the protocol fails to report a difference with probability less than ${\displaystyle 2^{-100}}$.