# Randomized Algorithms (Spring 2010)/Markov chains and random walks

## Markov Chains

### The Markov property

A stochastic processes ${\displaystyle \{X_{t}\mid t\in T\}}$ is a collection of random variables. The index ${\displaystyle t}$ is often called time, as the process represents the value of a random variable changing over time. Let ${\displaystyle {\mathcal {S}}}$ be the set of values assumed by the random variables ${\displaystyle X_{t}}$. We call each element of ${\displaystyle {\mathcal {S}}}$ a state, as ${\displaystyle X_{t}}$ represents the state of the process at time ${\displaystyle t}$.

The model of stochastic processes can be very general. In this class, we only consider the stochastic processes with the following properties:

discrete time
The index set ${\displaystyle T}$ is countable. Specifically, we assume that ${\displaystyle T=\{0,1,2,\ldots \}}$ and the process is ${\displaystyle X_{0},X_{1},X_{2},\ldots }$
discrete space
The state space ${\displaystyle {\mathcal {S}}}$ is countable. We are especially interested in the case that ${\displaystyle {\mathcal {S}}}$ is finite, in which case the process is called a finite process.

The next property is about the dependency structure among random variables. The simplest dependency structure for ${\displaystyle X_{0},X_{1},\ldots }$ is no dependency at all, that is, independence. We consider the next simplest dependency structure called the Markov property.

 Definition (the Markov property) A process ${\displaystyle X_{0},X_{1},\ldots }$ satisfies the Markov property if ${\displaystyle \Pr[X_{n+1}=x_{n+1}\mid X_{0}=x_{0},X_{1}=x_{1},\ldots ,X_{n}=x_{n}]=\Pr[X_{n+1}=x_{n+1}\mid X_{n}=x_{n}]}$ for all ${\displaystyle n}$ and all ${\displaystyle x_{0},\ldots ,x_{n+1}\in {\mathcal {S}}}$.

Informally, the Markov property means: "conditioning on the present, the future does not depend on the past." Hence, the Markov property is also called the memoryless property.

A stochastic process ${\displaystyle X_{0},X_{1},\ldots }$ of discrete time and discrete space is a Markov chain if it has the Markov property.

#### Transition matrix

Let ${\displaystyle P_{i,j}^{(t+1)}=\Pr[X_{t+1}=j\mid X_{t}=i]}$. For a Markov chain with a finite state space ${\displaystyle {\mathcal {S}}=[N]}$. This gives us a transition matrix ${\displaystyle P^{(t+1)}}$ at time ${\displaystyle t}$. The transition matrix is an ${\displaystyle N\times N}$ matrix of nonnegative entries such that the sum over each row of ${\displaystyle P^{(t)}}$ is 1, since

{\displaystyle {\begin{aligned}\sum _{j}P_{i,j}^{(t+1)}=\sum _{j}\Pr[X_{t+1}=j\mid X_{t}=i]=1\end{aligned}}}.

In linear algebra, matrices of this type are called stochastic matrices.

Let ${\displaystyle \pi ^{(t)}}$ be the distribution of the chain at time ${\displaystyle t}$, that is, {\displaystyle {\begin{aligned}\pi _{i}^{(t)}=\Pr[X_{t}=i]\end{aligned}}}. For a finite chain, ${\displaystyle \pi ^{(t)}}$ is a vector of ${\displaystyle N}$ nonnegative entries such that {\displaystyle {\begin{aligned}\sum _{i}\pi _{i}^{(t)}=1\end{aligned}}}. In linear algebra, vectors of this type are called stochastic vectors. Then, it holds that

{\displaystyle {\begin{aligned}\pi ^{(t+1)}=\pi ^{(t)}P^{(t+1)}\end{aligned}}}.

To see this, we apply the law of total probability,

{\displaystyle {\begin{aligned}\pi _{j}^{(t+1)}&=\Pr[X_{t+1}=j]\\&=\sum _{i}\Pr[X_{t+1}=j\mid X_{t}=i]\Pr[X_{t}=i]\\&=\sum _{i}\pi _{i}^{(t)}P_{i,j}^{(t+1)}\\&=(\pi ^{(t)}P^{(t+1)})_{j}.\end{aligned}}}

Therefore, a finite Markov chain ${\displaystyle X_{0},X_{1},\ldots }$ is specified by an initial distribution ${\displaystyle \pi ^{(0)}}$ and a sequence of transition matrices ${\displaystyle P^{(1)},P^{(2)},\ldots }$. And the transitions of chain can be described by a series of matrix products:

${\displaystyle \pi ^{(0)}{\stackrel {P^{(1)}}{\longrightarrow }}\pi ^{(1)}{\stackrel {P^{(2)}}{\longrightarrow }}\pi ^{(2)}{\stackrel {P^{(3)}}{\longrightarrow }}\cdots \cdots \pi ^{(t)}{\stackrel {P^{(t+1)}}{\longrightarrow }}\pi ^{(t+1)}\cdots }$

A Markov chain is said to be homogenous if the transitions depend only on the current states but not on the time, that is

${\displaystyle P_{i,j}^{(t)}=P_{i,j}}$ for all ${\displaystyle t}$.

The transitions of a homogenous Markov chain is given by a single matrix ${\displaystyle P}$. Suppose that ${\displaystyle \pi ^{(0)}}$ is the initial distribution. At each time ${\displaystyle t}$,

{\displaystyle {\begin{aligned}\pi ^{(t+1)}=\pi ^{(t)}P\end{aligned}}}.

Expanding this recursion, we have

{\displaystyle {\begin{aligned}\pi ^{(n)}=\pi ^{(0)}P^{n}\end{aligned}}}.

From now on, we restrict ourselves to the homogenous Markov chains, and the term "Markov chain" means "homogenous Markov chian" unless stated otherwise.

 Definition (finite Markov chain) Let ${\displaystyle P}$ be an ${\displaystyle N\times N}$ stochastic matrix. A process ${\displaystyle X_{0},X_{1},\ldots }$ with finite space ${\displaystyle {\mathcal {S}}=[N]}$ is said to be a (homogenous) Markov chain with transition matrix ${\displaystyle P}$, if for all ${\displaystyle n\geq 0,}$ all ${\displaystyle i,j\in [N]}$ and all ${\displaystyle i_{0},\ldots ,i_{n-1}\in [N]}$ we have {\displaystyle {\begin{aligned}\Pr[X_{n+1}=j\mid X_{0}=i_{0},\ldots ,X_{n-1}=i_{n-1},X_{n}=i]&=Pr[X_{n+1}=j\mid X_{n}=i]\\&=P_{i,j}.\end{aligned}}}

To describe a Markov chain, we only need to specify:

• initial distribution ${\displaystyle \pi ^{(0)}}$;
• transition matrix ${\displaystyle P}$.

Then the transitions can be simulated by matrix products:

${\displaystyle \pi ^{(0)}{\stackrel {P}{\longrightarrow }}\pi ^{(1)}{\stackrel {P}{\longrightarrow }}\pi ^{(2)}{\stackrel {P}{\longrightarrow }}\cdots \cdots \pi ^{(t)}{\stackrel {P}{\longrightarrow }}\pi ^{(t+1)}{\stackrel {P}{\longrightarrow }}\cdots }$

The distribution of the chain at time ${\displaystyle n}$ can be computed by ${\displaystyle \pi ^{(n)}=\pi ^{(0)}P^{n}}$.

#### Transition graph

Another way to picture a Markov chain is by its transition graph. A weighted directed graph ${\displaystyle G(V,E,w)}$ is said to be a transition graph of a finite Markov chain with transition matrix ${\displaystyle P}$ if:

• ${\displaystyle V={\mathcal {S}}}$, i.e. each node of the transition graph corresponds to a state of the Markov chain;
• for any ${\displaystyle i,j\in V}$, ${\displaystyle (i,j)\in E}$ if and only if ${\displaystyle P_{i,j}>0}$, and the weight ${\displaystyle w(i,j)=P_{i,j}}$.

A transition graph defines a natural random walk: at each time step, at the current node, the walk moves through an adjacent edge with the probability of the weight of the edge. It is easy to see that this is a well-defined random walk, since {\displaystyle {\begin{aligned}\sum _{j}P_{i,j}=1\end{aligned}}} for every ${\displaystyle i}$. Therefore, a Markov chain is equivalent to a random walk, so these two terms are often used interchangeably.

### Stationary distributions

Suppose ${\displaystyle \pi }$ is a distribution over the state space ${\displaystyle {\mathcal {S}}}$ such that, if the Markov chain starts with initial distribution ${\displaystyle \pi ^{(0)}=\pi }$, then after a transition, the distribution of the chain is still ${\displaystyle \pi ^{(1)}=\pi }$. Then the chain will stay in the distribution ${\displaystyle \pi }$ forever:

${\displaystyle \pi {\stackrel {P}{\longrightarrow }}\pi {\stackrel {P}{\longrightarrow }}\pi {\stackrel {P}{\longrightarrow }}\cdots \cdots }$

Such ${\displaystyle \pi }$ is called a stationary distribution.

 Definition (stationary distribution) A stationary distribution of a finite Markov chain with transition matrix ${\displaystyle P}$ is a probability distribution ${\displaystyle \pi }$ such that {\displaystyle {\begin{aligned}\pi P=\pi \end{aligned}}}.
Example
An ${\displaystyle N\times N}$ matrix is called double stochastic if every row sums to 1 and every column sums to 1. If the transition matrix ${\displaystyle P}$ of the chain is double stochastic, the uniform distribution ${\displaystyle \pi _{i}=1/N}$ for all ${\displaystyle i}$, is a stationary distribution. (Check by yourself.)
If the transition matrix ${\displaystyle P}$ is symmetric, the uniform distribution is a stationary distribution. This is because a symmetric stochastic matrix is double stochastic. (Check by yourself.)

Every finite Markov chain has a stationary distribution. This is a consequence of Perron's theorem in linear algebra.

For some Markov chains, no matter what the initial distribution is, after running the chain for a while, the distribution of the chain approaches the stationary distribution. For example, consider the transition matrix:

${\displaystyle P={\begin{bmatrix}0&1&0\\{\frac {1}{3}}&0&{\frac {2}{3}}\\{\frac {1}{3}}&{\frac {1}{3}}&{\frac {1}{3}}\end{bmatrix}}.}$

Run the chain for a while, we have:

${\displaystyle P^{5}\approx {\begin{bmatrix}0.2469&0.4074&0.3457\\0.2510&0.3621&0.3868\\0.2510&0.3663&0.3827\end{bmatrix}},P^{10}\approx {\begin{bmatrix}0.2500&0.3747&0.3752\\0.2500&0.3751&0.3749\\0.2500&0.3751&0.3749\end{bmatrix}},P^{20}\approx {\begin{bmatrix}0.2500&0.3750&0.3750\\0.2500&0.3750&0.3750\\0.2500&0.3750&0.3750\end{bmatrix}}.}$

Therefore, no matter what the initial distribution ${\displaystyle \pi ^{(0)}}$ is, after 20 steps, ${\displaystyle \pi ^{(0)}P^{20}}$ is very close to the distribution ${\displaystyle (0.25,0.375,0.375)}$, which is a stationary distribution for ${\displaystyle P}$. So the Markov chain converges to the same stationary distribution no matter what the initial distribution is.

However, this is not always true. For example, for the Markov chain with the following transition matrix:

${\displaystyle P={\begin{bmatrix}{\frac {1}{2}}&{\frac {1}{2}}&0&0\\{\frac {1}{3}}&{\frac {2}{3}}&0&0\\0&0&{\frac {3}{4}}&{\frac {1}{4}}\\0&0&{\frac {1}{4}}&{\frac {3}{4}}\end{bmatrix}}.}$

And

${\displaystyle P^{20}\approx {\begin{bmatrix}0.4&0.6&0&0\\0.4&0.6&0&0\\0&0&0.5&0.5\\0&0&0.5&0.5\end{bmatrix}}.}$

So the chain will converge, but not to the same stationary distribution. Depending on the initial distribution, the chain could converge to any distribution which is a linear combination of ${\displaystyle (0.4,0.6,0,0)}$ and ${\displaystyle (0,0,0.5,0.5)}$. We observe that this is because the original chain ${\displaystyle P}$ can be broken into two disjoint Markov chains, which have their own stationary distributions. We say that the chain is reducible.

Another example is as follows:

${\displaystyle P={\begin{bmatrix}0&1\\1&0\end{bmatrix}}.}$

The chain oscillates between the two states. Then

${\displaystyle P^{t}={\begin{bmatrix}0&1\\1&0\end{bmatrix}}}$ for any odd ${\displaystyle t}$, and
${\displaystyle P^{t}={\begin{bmatrix}1&0\\0&1\end{bmatrix}}}$ for any even ${\displaystyle t}$.

So the chain does not converge. We say that the chain is periodic.

We will see that for finite Markov chains, being reducible and being periodic are the only two possible cases that a Markov chain does not converge to a unique stationary distribution.

#### Irreducibility and aperiodicity

 Definition (irreducibility) State ${\displaystyle j}$ is accessible from state ${\displaystyle i}$ if it is possible for the chain to visit state ${\displaystyle j}$ if the chain starts in state ${\displaystyle i}$, or, in other words, {\displaystyle {\begin{aligned}P^{n}(i,j)>0\end{aligned}}} for some integer ${\displaystyle n\geq 0}$. State ${\displaystyle i}$ communicates with state ${\displaystyle j}$ if ${\displaystyle j}$ is accessible from ${\displaystyle i}$ and ${\displaystyle i}$ is accessible from ${\displaystyle j}$. We say that the Markov chain is irreducible if all pairs of states communicate.

It is more clear to interprete these concepts in terms of transition graphs:

• ${\displaystyle j}$ is accessible from ${\displaystyle i}$ means that ${\displaystyle j}$ is connected from ${\displaystyle i}$ in the transition graph, i.e. there is a directed path from ${\displaystyle i}$ to ${\displaystyle j}$.
• ${\displaystyle i}$ communicates with ${\displaystyle j}$ means that ${\displaystyle i}$ and ${\displaystyle j}$ are strongly connected in the transition graph.
• A finite Markov chain is irreducible if and only if its transition graph is strongly connected.

It is easy to see that communicating is an equivalence relation. That is, it is reflexive, symmetric, and transitive. Thus, the communicating relation partition the state space into disjoint equivalence classes, called communicating classes. For a finite Markov chain, communicating classes correspond to the strongly connected components in the transition graph. It is possible for the chain to move from one communicating class to another, but in that case it is impossible to return to the original class.

 Definition (aperiodicity) The period of a state ${\displaystyle i}$ is the greatest common divisor (gcd) {\displaystyle {\begin{aligned}d_{i}=\gcd\{n\mid (P^{n})_{i,i}>0\}\end{aligned}}}. A state is aperiodic if its period is 1. A Markov chain is aperiodic if all its states are aperiodic.

For example, suppose that the period of state ${\displaystyle i}$ is ${\displaystyle d_{i}=3}$. Then, starting from state ${\displaystyle i}$,

${\displaystyle i,\bigcirc ,\bigcirc ,\square ,\bigcirc ,\bigcirc ,\square ,\bigcirc ,\bigcirc ,\square ,\bigcirc ,\bigcirc ,\square ,\cdots \cdots }$

only the squares are possible to be ${\displaystyle i}$.

In the transition graph of a finite Markov chain, ${\displaystyle (P^{n})_{i,i}>0}$ is equivalent to that ${\displaystyle i}$ is on a cycle of length ${\displaystyle n}$. Period of a state ${\displaystyle i}$ is the greatest common devisor of the lengths of cycles passing ${\displaystyle i}$.

The next theorem shows that period is in fact a class property.

 Theorem If the states ${\displaystyle i}$ and ${\displaystyle j}$ communicate, then ${\displaystyle d_{i}=d_{j}}$.
Proof.
 For communicating ${\displaystyle i}$ and ${\displaystyle j}$, there is a path ${\displaystyle P_{1}}$ from ${\displaystyle i}$ to ${\displaystyle j}$ of length ${\displaystyle n_{1}}$, and there is a path ${\displaystyle P_{2}}$ from ${\displaystyle j}$ to ${\displaystyle i}$ of length ${\displaystyle n_{2}}$. Then ${\displaystyle P_{1}P_{2}}$ gives a cycle starting at ${\displaystyle i}$ of length ${\displaystyle n_{1}+n+2}$, and for any cycle ${\displaystyle C}$ starting at ${\displaystyle j}$ of length ${\displaystyle n}$, ${\displaystyle P_{1}CP_{2}}$ gives a cycle starting at ${\displaystyle i}$ of length ${\displaystyle n_{1}+n_{2}+n}$. Since the period of ${\displaystyle i}$ is ${\displaystyle d_{i}}$, then both ${\displaystyle (n_{1}+n_{2})}$ and ${\displaystyle (n_{1}+n_{2}+n)}$ are devisable by ${\displaystyle d_{i}}$. Subtracting the two, ${\displaystyle n}$ is devisable by ${\displaystyle d_{i}}$. Note that this holds for arbitrary cycle ${\displaystyle C}$ starting at ${\displaystyle j}$, then ${\displaystyle d_{i}}$ is the common divisor of all such ${\displaystyle n}$ that ${\displaystyle P_{j,j}^{n}>0}$. Since ${\displaystyle d_{j}}$ is defined to be the greatest common divisor of the same set of ${\displaystyle n}$, it holds that ${\displaystyle d_{j}\geq d_{i}}$. Interchanging the role of ${\displaystyle i}$ and ${\displaystyle j}$, we can show that ${\displaystyle d_{i}\geq d_{j}}$. Therefore ${\displaystyle d_{i}=d_{j}}$.
${\displaystyle \square }$

Due to the above theorem, an irreducible Markov chain is aperiodic if one of the states is aperiodic.

#### The Markov chain convergence theorem

 Theorem (Markov chain convergence theorem) Let ${\displaystyle X_{0},X_{1},\ldots ,}$ be an irreducible aperiodic Markov chain with finite state space ${\displaystyle {\mathcal {S}}=[N]}$, transition matrix ${\displaystyle P}$, and arbitrary initial distribution ${\displaystyle \pi ^{(0)}}$. Then, there exists a stationary distribution ${\displaystyle \pi }$ such that ${\displaystyle \pi P=\pi }$, and ${\displaystyle \lim _{n\rightarrow \infty }\left(\pi ^{(0)}P^{n}\right)_{i}=\pi _{i}}$ for all states ${\displaystyle i\in {\mathcal {S}}}$.

The theorem says that if we run an irreducible aperiodic finite Markov chain for a sufficient long time ${\displaystyle n}$, then, regardless of what the initial distribution was, the distribution at time ${\displaystyle n}$ will be close to the stationary distribution ${\displaystyle \pi }$.

Three pieces of information are delivered by the theorem regarding the stationary distribution:

• Existence: there exists a stationary distribution.
• Uniqueness: the stationary distribution is unique.
• Convergence: starting from any initial distribution, the chain converges to the stationary distribution.

First, for the existence of stationary distribution, neither irreducibility nor aperiodicity is necessary for the existence of a stationary distribution. In fact, any finite Markov chain has a stationary distribution. Irreducibility and aperiodicity guarantee the uniqueness and convergence behavior of the stationary distribution.

• For a reducible chain, there could be more than one stationary distributions. We have seen such examples. Note that there do exist reducible Markov chains with just one stationary distribution. For example, the chain
${\displaystyle P={\begin{bmatrix}1/2&1/2\\0&1\\\end{bmatrix}}}$
is reducible, but only has one stationary distribution ${\displaystyle (0,1)}$, because the transition graph is still weakly connected.
• For a periodic chain, the stationary probability ${\displaystyle \pi _{i}}$ of state ${\displaystyle i}$ is not the limiting probability of being in state ${\displaystyle i}$ but instead just the long-term frequency of visiting state ${\displaystyle i}$.

Second, the theorem itself only guarantees the existence and the convergence. The uniqueness is a consequence of these two. To see this, suppose that there exists a ${\displaystyle \pi '}$ such that ${\displaystyle \pi 'P=\pi '}$. Starting from the distribution ${\displaystyle \pi '}$, due to the theorem, ${\displaystyle \lim _{n\rightarrow \infty }\pi 'P^{n}=\pi }$. Since ${\displaystyle \pi 'P^{n}=\pi '}$ for any ${\displaystyle n}$, it holds that ${\displaystyle \lim _{n\rightarrow \infty }\pi '=\pi }$. But ${\displaystyle \pi '}$ does not depend on ${\displaystyle n}$, hence ${\displaystyle \pi '=\pi }$.

#### Coupling

The convergence theorem is proved by coupling, which is an important idea in probabilistic argument, and is a powerful tool for the analysis of Markov chains.

To illustrate the idea of coupling, we consider an example, which has nothing to do with Markov chain:

Example: connectivity of random graphs
Consider two distributions of random graphs, ${\displaystyle G(n,{\frac {1}{2}})}$ and ${\displaystyle G(n,{\frac {1}{3}})}$. We have seen this notation before: a ${\displaystyle G(n,p)}$ means that with probability ${\displaystyle p}$, an edge is drawn independently between any pair of the ${\displaystyle n}$ vertices. We want to show that
${\displaystyle \Pr \left[G(n,{\frac {1}{2}}){\mbox{ is connected }}\right]\geq \Pr \left[G(n,{\frac {1}{3}}){\mbox{ is connected }}\right]}$.
It seems obvious to us: expectedly ${\displaystyle G(n,{\frac {1}{2}})}$ will have more edges than ${\displaystyle G(n,{\frac {1}{3}})}$, so it should have more chance to be connected. However, formally proving this is not so easy. If we can compute the probability that a ${\displaystyle G(n,p)}$ is connected, then we can compare the probabilities, but computing the probability that a random graph is connected is a very non-trivial task.
We then show that with coupling, we can compare two distributions without actually computing the probabilities.
Suppose that ${\displaystyle G(n,p)}$ is generated as follows, for the ${\displaystyle i}$th pair of vertices, where ${\displaystyle i=1,\ldots ,{n \choose 2}}$, let ${\displaystyle U_{i}}$ be a uniform and independent random variable ranging over ${\displaystyle [0,1]}$, and an edge is drawn between the pair of vertices if ${\displaystyle U_{i}\leq p}$. Note that this is exactly the same as the definition of ${\displaystyle G(n,p)}$. So a random graph ${\displaystyle G(n,p)}$ is generated from a sequence of random sources ${\displaystyle U_{1},U_{2},\ldots ,U_{n \choose 2}}$.
Our trick is to use the same sequence of ${\displaystyle U_{1},U_{2},\ldots ,U_{n \choose 2}}$ to generate both ${\displaystyle G(n,{\frac {1}{2}})}$ and ${\displaystyle G(n,{\frac {1}{3}})}$. Note that both ${\displaystyle G(n,{\frac {1}{2}})}$ and ${\displaystyle G(n,{\frac {1}{3}})}$ are distributed the same as before, but now ${\displaystyle e\in G(n,{\frac {1}{3}})}$ implies ${\displaystyle e\in G(n,{\frac {1}{2}})}$ since ${\displaystyle U_{e}\leq {\frac {1}{3}}\Rightarrow U_{e}\leq {\frac {1}{2}}}$. Therefore if ${\displaystyle G(n,{\frac {1}{3}})}$ is connected then ${\displaystyle G(n,{\frac {1}{2}})}$ must be also connected. Then,
${\displaystyle \Pr \left[G(n,{\frac {1}{2}}){\mbox{ is connected }}\right]\geq \Pr \left[G(n,{\frac {1}{3}}){\mbox{ is connected }}\right]}$.

In the last example, we see that coupling means forcing the two distributions use the same source of randomness. Now we see how this idea can help analyze the convergence of Markov chain.

A Markov chain is a sequence of random variables

{\displaystyle {\begin{aligned}X_{0},X_{1},X_{2}\ldots \end{aligned}}}

where the distribution of ${\displaystyle X_{0}}$ is given by an initial distribution ${\displaystyle \pi ^{(0)}}$; and for each ${\displaystyle t=1,2,\ldots }$, assuming that ${\displaystyle X_{t-1}=i}$, the distribution of ${\displaystyle X_{t}}$ is given by the ${\displaystyle i}$th row of the transition matrix ${\displaystyle P_{i}}$.

So we can generate the chain by a sequence of uniform and independent random variables ${\displaystyle U_{0},U_{1},\ldots }$ ranging over ${\displaystyle [0,1]}$. Initially

${\displaystyle X_{0}=j}$ if ${\displaystyle \sum _{k;

and for each ${\displaystyle t=1,2,\ldots }$, assuming ${\displaystyle X_{t-1}=i}$,

${\displaystyle X_{0}=j}$ if ${\displaystyle \sum _{k.

The Markov chain generated in this way is distributed exactly the same as having initial distribution ${\displaystyle \pi ^{(0)}}$ and transition matrix ${\displaystyle P}$.

Let ${\displaystyle X_{0},X_{1},\ldots }$ be a finite Markov chain with initial distribution ${\displaystyle \pi ^{(0)}}$ and transition matrix ${\displaystyle P}$, and generated by the uniform and independent random variables ${\displaystyle U_{0},U_{1},\ldots }$. Suppose that the Markov chain has a stationary distribution ${\displaystyle \pi }$, such that ${\displaystyle \pi P=\pi }$. We run another chain ${\displaystyle X_{0}',X_{1}',\ldots }$ with the initial distribution ${\displaystyle \pi }$, transition matrix ${\displaystyle P}$, and independent random sources ${\displaystyle U_{0}',U_{1}',\ldots }$. So we have two independent sequences:

{\displaystyle {\begin{aligned}X_{0},X_{1},X_{2}\ldots \end{aligned}}} and {\displaystyle {\begin{aligned}X_{0}',X_{1}',X_{2}'\ldots \end{aligned}}}.

We define another chain, which starts as {\displaystyle {\begin{aligned}X_{0},X_{1},X_{2}\ldots \end{aligned}}} and for the first time that ${\displaystyle X_{n}=X_{n}'}$, the chain switches to ${\displaystyle X_{n}',X_{n+1}',X_{n+2},\ldots }$. The transitions are illustrated by the following figure.

${\displaystyle {\begin{matrix}\pi ^{(0)}:&X_{0}&\rightarrow &X_{1}&\rightarrow &\cdots &\rightarrow &X_{n}\\&\nparallel &&\nparallel &&\nparallel &&\parallel &\searrow \\\pi ^{}:&X_{0}'&\rightarrow &X_{1}'&\rightarrow &\cdots &\rightarrow &X_{n}'&\rightarrow &X_{n+1}'\rightarrow X_{n+2}'\rightarrow \cdots \end{matrix}}}$

It is not hard to see that the distribution of the chain {\displaystyle {\begin{aligned}X_{0},X_{1},\ldots ,X_{n},X_{n+1}',X_{n+2}',\ldots \end{aligned}}} is identically distributed as the original chain {\displaystyle {\begin{aligned}X_{0},X_{1},\ldots \end{aligned}}}, since we do nothing except switching the source of randomness from ${\displaystyle U_{0},U_{1},\ldots ,}$ to the sequence ${\displaystyle U_{n}',U_{n+1}',\ldots }$, which does not affect the distribution of the chain.

On the other hand, since the chain {\displaystyle {\begin{aligned}X_{0}',X_{1}',\ldots \end{aligned}}} starts from a stationary distribution ${\displaystyle \pi }$, by the definition of stationary distribution, it will stay in that distribution forever. Thus, the distribution of every one of {\displaystyle {\begin{aligned}X_{n+1}',X_{n+2}',\ldots \end{aligned}}} is ${\displaystyle \pi }$. Therefore, once ${\displaystyle X_{n}=X_{n}'}$ for a finite ${\displaystyle n}$, the chain {\displaystyle {\begin{aligned}X_{0},X_{1},\ldots \end{aligned}}} converges to the stationary distribution ${\displaystyle \pi }$.

For irreducible aperiodic Markov chain with finite state space, starting from an arbitrary initial distribution (including the stationary distribution), after running the chain for a finite time ${\displaystyle n}$, ${\displaystyle X_{n}}$ is possible to be any state, that is ${\displaystyle \Pr[X_{n}=i]>0}$ for any ${\displaystyle i}$ (we will not formally prove this, but it is not very hard to imagine why this is true). This implies that ${\displaystyle X_{n}=X_{n}'}$ must happen for a finite ${\displaystyle n}$, which supports the convergence of the Markov chain.

#### Hitting time and the stationary distribution

We will see that the stationary distribution of a Markov chain is related to its hitting times. For a Markov chain starting from state ${\displaystyle i}$, let

${\displaystyle T_{i,j}=\min\{n>0\mid X_{n}=j\}}$,

which is the first time that a chain starting from state ${\displaystyle i}$ visits state ${\displaystyle j}$, with the convention that ${\displaystyle T_{i,j}=\infty }$ if the chain never visit ${\displaystyle j}$. We define the hitting time

${\displaystyle \tau _{i,j}=\mathbf {E} [T_{i,j}]}$.

The special case ${\displaystyle \tau _{i,i}}$ gives the expected time a chain starting from state ${\displaystyle i}$ returns to state ${\displaystyle i}$.

 Theorem Any irreducible aperiodic Markov chain with finite state space ${\displaystyle {\mathcal {S}}}$, and transition matrix ${\displaystyle P}$ has a stationary distribution ${\displaystyle \pi }$ such that ${\displaystyle \pi _{i}=\lim _{n\rightarrow \infty }(P^{n})_{j,i}={\frac {1}{\tau _{i,i}}}}$ for any ${\displaystyle i\in {\mathcal {S}}}$.

Note that in the above theorem, the limit ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{j,i}}$ does not depend on the ${\displaystyle j}$, which means that ${\displaystyle P^{n}}$ in the limit has identical rows.

We will not prove the lemma, but only give an informal justification: the expected time between visits to state ${\displaystyle i}$ is ${\displaystyle \tau _{i,i}}$, and therefore state ${\displaystyle i}$ is visited ${\displaystyle {\frac {1}{\tau _{i,i}}}}$ of the time. Not that ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{j,i}}$ represents the probability a state chosen far in the future (at time ${\displaystyle n\rightarrow \infty }$) is at state ${\displaystyle i}$ when the chain starts at state ${\displaystyle j}$, but if the future is far, who ${\displaystyle j}$ is does not really matter, and ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{j,i}}$ is the frequency that ${\displaystyle i}$ is visited, which is ${\displaystyle {\frac {1}{\tau _{i,i}}}}$.

### PageRank

PageRank is the algorithm reportedly used by Google to assign a numerical rank to every web page. The rank of a page measures the "importance" of the page. A page has higher rank if it is pointed to by more high-rank pages. Low-rank pages have less influence on the rank of a page. If one page points to many others, it will have less influence on their ranking than if it just points to a few.

This intuitive idea can be formalized as follows. The world-wide-web is treated as a directed graph ${\displaystyle G(V,E)}$, with web pages as vertices and hyperlinks as directed edges. The rank of vertex ${\displaystyle v}$ is denoted ${\displaystyle r(v)}$, and is supposed to satisfy:

${\displaystyle r(v)=\sum _{u:(u,v)\in E}{\frac {r(u)}{d_{+}(u)}},\qquad (*)}$

where ${\displaystyle d_{+}(u)}$ is the number of edges going out of ${\displaystyle u}$. Note that the sum is over edges going in to ${\displaystyle v}$.

This formula nicely models both the intuitions that a page gets higher rank if it is pointed by more high-rank pages, and that the influence of a page is penalized by the number of pages it points to. Let ${\displaystyle P}$ be a matrix with rows and columns corresponded to vertices, and ${\displaystyle \forall u,v\in V}$,

${\displaystyle P(u,v)={\begin{cases}{\frac {1}{d_{+}(u)}}&{\mbox{if }}(u,v)\in E,\\0&{\mbox{otherwise}}.\end{cases}}}$

Then the formular {\displaystyle {\begin{aligned}(*)\end{aligned}}} can be expressed as

{\displaystyle {\begin{aligned}rP=r.\end{aligned}}}

It is also easy to verify that ${\displaystyle P}$ is stochastic, that is, {\displaystyle {\begin{aligned}\sum _{v}P(u,v)=1\end{aligned}}} for all ${\displaystyle u\in V}$. Then the ranks of a pages is actually a stationary distribution of the Markov chain with transition matrix ${\displaystyle P}$. This is not entirely a coincidence. ${\displaystyle P}$ is the transition matrix for the random walk over the web pages, defined as that at each time, pick a uniform page pointed by the current page and walk to it. This can be imagined as a "tireless random surfer" who starts from an arbitrary page, randomly follows the hyperlinks, and given infinitely long time, will eventually approaches the stationary distribution. The importance of a web page is reflected by the frequency that the random surfer visits the page, which is the stationary probability.

We assume the world-wide-web is strongly connected, thus the Markov chain is irreducible. And given the huge number of webpages over the Internet, it is almost impossible that the lengths of all cycles have a common divisor greater than 1, thus the Markov chain is aperiodic. Therefore, the random surfer model indeed converges.

In practice, PageRank also consider a damping factor, since a typical surfer cannot browse the web forever. The damping factor effectively gives an upper bound on the number of hyperlinks the surfer would follow before he/she has a random start over.

## Random Walks on Undirected Graphs

A walk on a graph ${\displaystyle G=(V,E)}$ is a sequence of vertices ${\displaystyle v_{1},v_{2},\ldots \in V}$ such that ${\displaystyle v_{i+1}}$ is a neighbor of ${\displaystyle v_{i}}$ for every index ${\displaystyle i}$. When ${\displaystyle v_{i+1}}$ is selected uniformly at random from among ${\displaystyle v_{i}}$’s neighbors, independently for every ${\displaystyle i}$, this is called a random walk on ${\displaystyle G}$.

We consider the special case that ${\displaystyle G(V,E)}$ is an undirected graph, and denote that ${\displaystyle n=|V|}$ and ${\displaystyle m=|E|}$.

A Markov chain is defined by this random walk, with the vertex set ${\displaystyle V}$ as the state space, and the transition matrix ${\displaystyle P}$, which is defined as follows:

${\displaystyle P(u,v)={\begin{cases}{\frac {1}{d(u)}}&{\mbox{if }}(u,v)\in E,\\0&{\mbox{otherwise }},\end{cases}}}$

where ${\displaystyle d(u)}$ denotes the degree of vertex ${\displaystyle u}$.

Note that unlike the PageRank example, now the probability depends on ${\displaystyle d(u)}$ instead of ${\displaystyle d_{+}(u)}$. This is because the graph is undirected.

 Proposition Let ${\displaystyle M_{G}}$ be the Markov chain defined as above. ${\displaystyle M_{G}}$ is irreducible if and only if ${\displaystyle G}$ is connected. ${\displaystyle M_{G}}$ is aperiodic if and only if ${\displaystyle G}$ is non-bipartite.

We leave the proof as an exercise.

We can just assume ${\displaystyle G}$ is connected, so we do not have to worry about the reducibility any more.

The periodicity of a random walk on a undirected bipartite graph is usually dealt with by the following trick of "lazy" random walk.

Lazy random walk
Given an undirected graph ${\displaystyle G(V,E)}$, a random walk is defined by the transition matrix
${\displaystyle P'(u,v)={\begin{cases}{\frac {1}{2}}&{\mbox{if }}u=v,\\{\frac {1}{2d(u)}}&{\mbox{if }}(u,v)\in E,\\0&{\mbox{otherwise }}.\end{cases}}}$
For this random walk, at each step, we first flip a fair coin to decide whether to move or to stay, and if the coin told us to move, we pick a uniform edge and move to the adjacent vertex. It is easy to see that the resulting Markov chain is aperiodic for any ${\displaystyle G}$.

We consider the non-lazy version of random walk. We observe that the random walk has the following stationary distribution.

 Theorem The random walk on ${\displaystyle G(V,E)}$ with ${\displaystyle |E|=m}$ has a stationary distribution ${\displaystyle \pi }$, where ${\displaystyle \forall v\in V}$, {\displaystyle {\begin{aligned}\pi _{v}={\frac {d(v)}{2m}}\end{aligned}}} for ${\displaystyle v\in V}$.
Proof.
 First, since ${\displaystyle \sum _{v\in V}d(v)=2m}$, it follows that ${\displaystyle \sum _{v\in V}\pi _{v}=\sum _{v\in V}{\frac {d(v)}{2m}}=1,}$ thus ${\displaystyle \pi }$ is a well-defined distribution. And let ${\displaystyle N(v)}$ denote the set of neighbors of ${\displaystyle v}$. Then for any ${\displaystyle v\in V}$, ${\displaystyle (\pi P)_{v}=\sum _{u\in V}\pi _{u}P(u,v)=\sum _{u\in N(v)}{\frac {d(u)}{2m}}{\frac {1}{d(u)}}={\frac {1}{2m}}\sum _{u\in N(v)}1={\frac {d(v)}{2m}}=\pi _{v}.}$ Thus ${\displaystyle \pi P=\pi }$, and ${\displaystyle \pi }$ is stationary.
${\displaystyle \square }$

For connected and non-bipartite ${\displaystyle G}$, the random walk converges to this stationary distribution. Note that the stationary distribution is proportional to the degrees of the vertices, therefore, if ${\displaystyle G}$ is a regular graph, that is, ${\displaystyle d(v)}$ is the same for all ${\displaystyle v\in V}$, the stationary distribution is the uniform distribution.

The following parameters of random walks are closely related to the performances of randomized algorithms based on random walks:

• Hitting time: It takes how long for a random walk to visit some specific vertex.
• Cover time: It takes how long for a random walk to visit all vertices.
• Mixing time: It takes how long for a random walk to be close enough to the stationary distribution.

### Hitting and covering

For any ${\displaystyle u,v\in V}$, the hitting time ${\displaystyle \tau _{u,v}}$ is the expected number of steps before vertex ${\displaystyle v}$ is visited, starting from vertex ${\displaystyle u}$.

Recall that any irreducible aperiodic Markov chain with finite state space converges to the unique stationary distribution {\displaystyle {\begin{aligned}\pi _{v}={\frac {1}{\tau _{v,v}}}.\end{aligned}}} Combining with what we know about the stationary distribution ${\displaystyle \pi }$ of a random walk on an undirected graph ${\displaystyle G(V,E)}$ where ${\displaystyle |E|=m}$, we have that for any vertex ${\displaystyle v\in V}$,

${\displaystyle \tau _{v,v}={\frac {1}{\pi _{v}}}={\frac {2m}{d(v)}}.}$

This fact can be used to estimate the hitting time between two adjacent vertices.

 Lemma For a random walk on an undirected graph ${\displaystyle G(V,E)}$ where${\displaystyle |E|=m}$, for any ${\displaystyle u,v\in V}$ that ${\displaystyle (u,v)\in E}$, the mean hitting time {\displaystyle {\begin{aligned}\tau _{u,v}<2m\end{aligned}}}.
Proof.
 The proof is by double counting. We know that ${\displaystyle \tau _{v,v}={\frac {1}{\pi _{v}}}={\frac {2m}{d(v)}}.}$ Let ${\displaystyle N(v)}$ be the set of neighbors of vertex ${\displaystyle v}$. We run the random walk from ${\displaystyle v}$ for one step, and by the law of total expectation, ${\displaystyle \tau _{v,v}=\sum _{w\in N(v)}{\frac {1}{d(v)}}(1+\tau _{w,v}).}$ Combining the above two equations, we have ${\displaystyle 2m=\sum _{w\in N(v)}(1+\tau _{w,v}),}$ which implies that ${\displaystyle \tau _{u,v}<2m}$.
${\displaystyle \square }$

Note that the lemma holds only for the adjacent ${\displaystyle u}$ and ${\displaystyle v}$. With this lemma, we can prove an upper bound on the cover time.

• Let ${\displaystyle C_{u}}$ be the expected number of steps taken by a random walk which starts at ${\displaystyle u}$ to visit every vertex in ${\displaystyle G}$ at least once. The cover time of ${\displaystyle G}$, denoted ${\displaystyle C(G)}$ is defined as ${\displaystyle C(G)=\max _{u\in V}C_{u}}$.
 Theorem (cover time) For any connected undirected graph ${\displaystyle G(V,E)}$ with ${\displaystyle |V|=n}$ and ${\displaystyle |E|=m}$, the cover time ${\displaystyle C(G)\leq 4nm}$
Proof.
 Let ${\displaystyle T}$ be an arbitrary spanning tree of ${\displaystyle G}$. There exists an Eulerian tour of ${\displaystyle T}$ in which each edge is traversed once in each direction. Let ${\displaystyle v_{1}\rightarrow v_{2}\rightarrow \cdots \rightarrow v_{2(n-1)}\rightarrow v_{2n-1}=v_{1}}$ be such a tour. Clearly the expected time to go through the vertices in the tour is an upper bound on the cover time. Hence, ${\displaystyle C(G)\leq \sum _{i=1}^{2(n-1)}\tau _{v_{i},v_{i+1}}<2(n-1)\cdot 2m<4nm.}$
${\displaystyle \square }$

A tighter bound (with a smaller constant factor) can be proved with a more careful analysis. Please read the textbook [MR].

### USTCON

USTCON stands for undirected ${\displaystyle s}$-${\displaystyle t}$ connectivity. It is the problem which asks whether there is a path from vertex ${\displaystyle s}$ to vertex ${\displaystyle t}$ in a given undirected graph ${\displaystyle G(V,E)}$. This problem is an abstraction of various search problems in graphs, and has theoretical significance in complexity theory.

The problem can be solved deterministically by traversing the graph ${\displaystyle G(V,E)}$, which takes ${\displaystyle \Omega (n)}$ extra space to keep track of which vertices have been visited, where ${\displaystyle n=|V|}$. And the following theorem is implied by the upper bound on the cover time.

 Theorem (Aleliunas-Karp-Lipton-Lovász-Rackoff 1979) USTCON can be solved by a polynomial time Monte Carlo randomized algorithm with bounded one-sided error, which uses ${\displaystyle O(\log n)}$ extra space.

The algorithm is a random walk starting at ${\displaystyle s}$. If the walk reaches ${\displaystyle t}$ in ${\displaystyle 4n^{3}}$ steps, then return "yes", otherwise return "no".

It is obvious that if ${\displaystyle s}$ and ${\displaystyle t}$ are disconnected, the random walk from ${\displaystyle s}$ can never reach ${\displaystyle t}$, thus the algorithm always returns "no".

We know that for an undirected ${\displaystyle G}$, the cover time is ${\displaystyle <4nm<2n^{2}}$. So if ${\displaystyle s}$ and ${\displaystyle t}$ are connected, the expected time to reach ${\displaystyle t}$ from ${\displaystyle s}$ is ${\displaystyle <2n^{3}}$. By Markov's inequality, the probability that it takes longer than ${\displaystyle 4n^{3}}$ steps to reach ${\displaystyle t}$ from ${\displaystyle s}$ is ${\displaystyle <1/2}$.

The random walk use ${\displaystyle O(\log n)}$ bits to store the current position, and another ${\displaystyle O(\log n)}$ bits to count the number of steps. So the total space used by the algorithm inaddition to the input is ${\displaystyle O(\log n)}$.

This shows that USTCON is in the complexity class RL (randomized log-space).

Story in complexity theory

If the randomness if forbidden, it is known that USTCON can be solved nondeterministically in logarithmic space, thus USTCON is in NL. In fact USTCON is complete for the symmetric version of nondeterministic log-space. That is, every problem in the class of SL can be solved by USTCON via log-space reductions. Therefore, USTCON${\displaystyle \in }$RL implies that SL${\displaystyle \subseteq }$RL.

In 2004, Reingold shows that USTCON can be solved deterministically in log-space, which proves SL=L. The deterministic algorithm for USTCON is by the derandomization of random walk.

It is conjectured that RL=L, but this is still open.

## References

• David Aldous and Jim Fill. Reversible Markov Chains and Random Walks on Graphs.
• László Lovász. Random Walks on Graphs: A Survey. 1993.

Both are available online.