# Markov Chain

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 \Omega }$ be the set of values assumed by the random variables ${\displaystyle X_{t}}$. We call each element of ${\displaystyle \Omega }$ 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 \Omega }$ is countable. We are especially interested in the case that ${\displaystyle \Omega }$ 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 \Omega }$.

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_{x,y}^{(t+1)}=\Pr[X_{t+1}=y\mid X_{t}=x]}$. For a Markov chain with a finite state space ${\displaystyle \Omega =[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 _{y}P_{x,y}^{(t+1)}=\sum _{y}\Pr[X_{t+1}=y\mid X_{t}=x]=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 _{x}^{(t)}=\Pr[X_{t}=x]\end{aligned}}}. For a finite chain, ${\displaystyle \pi ^{(t)}}$ is a vector of ${\displaystyle N}$ nonnegative entries such that {\displaystyle {\begin{aligned}\sum _{x}\pi _{x}^{(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 _{y}^{(t+1)}&=\Pr[X_{t+1}=y]\\&=\sum _{x}\Pr[X_{t+1}=y\mid X_{t}=x]\Pr[X_{t}=x]\\&=\sum _{x}\pi _{x}^{(t)}P_{x,y}^{(t+1)}\\&=(\pi ^{(t)}P^{(t+1)})_{y}.\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_{x,y}^{(t)}=P_{x,y}}$ 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 \Omega =[N]}$ is said to be a (homogenous) Markov chain with transition matrix ${\displaystyle P}$, if for all ${\displaystyle n\geq 0,}$ all ${\displaystyle x,y\in [N]}$ and all ${\displaystyle x_{0},\ldots ,x_{n-1}\in [N]}$ we have {\displaystyle {\begin{aligned}\Pr[X_{n+1}=y\mid X_{0}=x_{0},\ldots ,X_{n-1}=x_{n-1},X_{n}=x]&=Pr[X_{n+1}=y\mid X_{n}=x]\\&=P_{x,y}.\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=\Omega }$, i.e. each node of the transition graph corresponds to a state of the Markov chain;
• for any ${\displaystyle x,y\in V}$, ${\displaystyle (x,y)\in E}$ if and only if ${\displaystyle P_{x,y}>0}$, and the weight ${\displaystyle w(x,y)=P_{x,y}}$.

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 _{y}P_{x,y}=1\end{aligned}}} for every ${\displaystyle x}$. 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 \Omega }$ 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 _{x}=1/N}$ for all ${\displaystyle x}$, 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 y}$ is accessible from state ${\displaystyle x}$ if it is possible for the chain to visit state ${\displaystyle y}$ if the chain starts in state ${\displaystyle x}$, or, in other words, {\displaystyle {\begin{aligned}P^{n}(x,y)>0\end{aligned}}} for some integer ${\displaystyle n\geq 0}$. State ${\displaystyle x}$ communicates with state ${\displaystyle y}$ if ${\displaystyle y}$ is accessible from ${\displaystyle x}$ and ${\displaystyle x}$ is accessible from ${\displaystyle y}$. 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 y}$ is accessible from ${\displaystyle x}$ means that ${\displaystyle y}$ is connected from ${\displaystyle x}$ in the transition graph, i.e. there is a directed path from ${\displaystyle x}$ to ${\displaystyle y}$.
• ${\displaystyle x}$ communicates with ${\displaystyle y}$ means that ${\displaystyle x}$ and ${\displaystyle y}$ 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 x}$ is the greatest common divisor (gcd) {\displaystyle {\begin{aligned}d_{x}=\gcd\{n\mid (P^{n})_{x,x}>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 x}$ is ${\displaystyle d_{x}=3}$. Then, starting from state ${\displaystyle x}$,

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

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

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

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

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

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

## Convergence of Markov chain

 Fundamental Theorem of Markov Chain Let ${\displaystyle X_{0},X_{1},\ldots ,}$ be an irreducible aperiodic Markov chain with finite state space ${\displaystyle \Omega }$, transition matrix ${\displaystyle P}$, and arbitrary initial distribution ${\displaystyle \pi ^{(0)}}$. Then, there exists a unique stationary distribution ${\displaystyle \pi }$ such that ${\displaystyle \pi P=\pi }$, and ${\displaystyle \lim _{t\rightarrow \infty }\pi ^{(0)}P^{t}=\pi .}$

The theorem says that if we run an irreducible aperiodic finite Markov chain for a sufficient long time ${\displaystyle t}$, then, regardless of what the initial distribution was, the distribution at time ${\displaystyle t}$ 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.

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 _{x}}$ of state ${\displaystyle x}$ is not the limiting probability of being in state ${\displaystyle x}$ but instead just the long-term frequency of visiting state ${\displaystyle x}$.

### Coupling

The above 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.

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}=x}$, the distribution of ${\displaystyle X_{t}}$ is given by the ${\displaystyle x}$th row of the transition matrix ${\displaystyle P}$, denoted as ${\displaystyle P_{x}}$.

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]}$. Suppose that the states in ${\displaystyle \Omega }$ assume an arbitrary total order ${\displaystyle <}$. Initially

${\displaystyle X_{0}=y}$ if ${\displaystyle \sum _{z;

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

${\displaystyle X_{t}=y}$ if ${\displaystyle \sum _{z.

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 }$.

Now we need to show that ${\displaystyle X_{n}=X_{n}'}$ for some ${\displaystyle n}$ will eventually happen. To this end, let ${\displaystyle M}$ be the minimum integer such that ${\displaystyle (P^{M})_{x,y}>0}$ for all ${\displaystyle x,y\in \Omega }$, we have

{\displaystyle {\begin{aligned}\Pr[X_{M}=X'_{M}]&\geq \Pr(X_{M}=x\land X_{M}'=x)\\&=\Pr[X_{M}=x]\cdot \Pr[X_{M}'=x]\\&\geq c^{2}\end{aligned}}}

where ${\displaystyle c=\min\{(P^{M})_{x,y}\mid x,y\in \Omega \}}$ and hence it is a positive constant.

Similarly, we have

{\displaystyle {\begin{aligned}\Pr[X_{2M}\neq X'_{2M}]&=\Pr[X_{M}\neq X'_{M}]\cdot \Pr[X_{2M}\neq X'_{2M}\mid X_{M}\neq X'_{M}]\\&\leq (1-c^{2})^{2}\end{aligned}}}

Repeat the above argument, we have for every integer ${\displaystyle \ell \geq 0}$

${\displaystyle \Pr[X_{\ell M}\neq X'_{\ell M}]\leq (1-c^{2})^{\ell }}$

This implies ${\displaystyle \Pr[X_{n}=X'_{n}]\to 1}$ as ${\displaystyle n\to \infty }$.

## 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 x}$, let

${\displaystyle T_{x,y}=\min\{n>0\mid X_{n}=y\}}$,

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

${\displaystyle \tau _{x,y}=\mathbf {E} [T_{x,y}]}$.

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

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

Note that in the above theorem, the limit ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{y,x}}$ does not depend on the ${\displaystyle y}$, 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 x}$ is ${\displaystyle \tau _{x,x}}$, and therefore state ${\displaystyle x}$ is visited ${\displaystyle {\frac {1}{\tau _{x,x}}}}$ of the time. Not that ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{y,x}}$ represents the probability a state chosen far in the future (at time ${\displaystyle n\rightarrow \infty }$) is at state ${\displaystyle x}$ when the chain starts at state ${\displaystyle y}$, but if the future is far, who ${\displaystyle y}$ is does not really matter, and ${\displaystyle \lim _{n\rightarrow \infty }(P^{n})_{y,x}}$ is the frequency that ${\displaystyle x}$ is visited, which is ${\displaystyle {\frac {1}{\tau _{x,x}}}}$.

## 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 Walk on 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].

## Electric Networks

An undirected graph ${\displaystyle G=(V,E)}$ is called an electrical network, if

1. Every edge ${\displaystyle e=\{u,v\}}$ is associated with resistance ${\displaystyle R_{uv}}$.
2. The flow of current and the voltage at each vertex in the network follows Kirchhoff's Law and Ohm's Law.

In the following, we use ${\displaystyle C_{u\rightarrow v}}$ to denote the current flow from ${\displaystyle u}$ to ${\displaystyle v}$, ${\displaystyle \phi _{u}}$ to denote the potential at vertex ${\displaystyle u}$, ${\displaystyle \phi _{u,v}}$ to denote the potential difference between ${\displaystyle u}$ and ${\displaystyle v}$, i.e. ${\displaystyle \phi _{u,v}=\phi _{u}-\phi _{v}}$.

• Kirchhoff's Law: For every vertex ${\displaystyle v}$, the sum of currents entering ${\displaystyle v}$ is equal to the the sum of currents leaving ${\displaystyle v}$.
• Ohm's Law: For every edge ${\displaystyle e=\{u,v\}}$ in the graph, the currents across ${\displaystyle e}$ is equal to the difference of potential between ${\displaystyle u}$ and ${\displaystyle v}$ over the resistance of ${\displaystyle e}$, i.e. ${\displaystyle C_{u\rightarrow v}={\frac {\phi _{u,v}}{R_{uv}}}}$.
 Definition (Effective Resistance) Let ${\displaystyle u,v\in V}$, the effective resistance ${\displaystyle R(u,v)}$ between ${\displaystyle u}$ and ${\displaystyle v}$ is defined to be the potential difference between ${\displaystyle u}$ and ${\displaystyle v}$ while one unit of current is sent from ${\displaystyle u}$ to ${\displaystyle v}$.

Now we use the idea of electrical network to analyze the cover time of random walk. Consider an undirected graph ${\displaystyle G=(V,E)}$, we construct an electrical network whose underlying graph is ${\displaystyle G}$ and ${\displaystyle R_{e}=1}$ for every ${\displaystyle e\in E}$. If we inject ${\displaystyle d(w)}$ units of currents into each vertex ${\displaystyle w\in V}$, where ${\displaystyle d(w)}$ is the degree of ${\displaystyle w}$, and remove all the ${\displaystyle 2|E|=2m}$ units of currents from ${\displaystyle v}$, then

 Lemma For every ${\displaystyle u,v\in V}$ ${\displaystyle \phi _{u,v}=\tau _{u,v}}$
Proof.
 We first represent ${\displaystyle \tau _{u,v}}$ by a linear system. Consider one step of random walk, we have {\displaystyle {\begin{aligned}\tau _{u,v}&=\sum _{xu\in E}{\frac {1}{d(u)}}(1+\tau _{x,v})\\&=1+{\frac {1}{d(u)}}\sum _{xu\in E}\tau _{x,v}.\qquad (*)\end{aligned}}} On the other hand, for the corresponding electric network constructed as above, {\displaystyle {\begin{aligned}d(u)&=\sum _{ux\in E}C_{u\rightarrow x}&&{\mbox{(Kirchhoff)}}\\&=\sum _{ux\in E}\phi _{u,x}&&{\mbox{(Ohm)}}\\&=\sum _{ux\in E}(\phi _{u,v}-\phi _{x,v})\\&=d(u)\phi _{u,v}-\sum _{ux\in E}\phi _{x,v}.\end{aligned}}} Thus ${\displaystyle \phi _{u,v}=1+{\frac {1}{d(u)}}\sum _{ux\in E}\phi _{x,v}\qquad (**)}$ Note that (*) and (**) are both linear systems with unique solution, thus ${\displaystyle \tau _{u,v}=\phi _{u,v}}$.
${\displaystyle \square }$
 Theorem (Chandra-Raghavan-Ruzzo-Smolensky-Tiwari 1989) Let ${\displaystyle u,v\in V}$, then ${\displaystyle \tau _{u,v}+\tau _{v,u}=2|E|\cdot R(u,v)}$
Proof.
 We now construct four electrical networks ${\displaystyle A,B,C,D}$ with underlying graph ${\displaystyle G}$. First, we inject ${\displaystyle d(w)}$ units of currents into each vertex ${\displaystyle w}$ and remove ${\displaystyle 2|E|}$ units of currents from ${\displaystyle v}$. Let ${\displaystyle A}$ denote this electrical network, then by the above lemma, we have ${\displaystyle \tau _{u,v}=\phi _{u,v}^{A}}$. Similarly, if we inject ${\displaystyle d(w)}$ units of currents into every vertex ${\displaystyle w}$, remove ${\displaystyle 2|E|}$ units of currents from ${\displaystyle u}$ and denote this electrical network by ${\displaystyle B}$, then ${\displaystyle \tau _{v,u}=\phi _{v,u}^{B}}$. Now we change the direction of all the currents in ${\displaystyle B}$, that is, ${\displaystyle 2|E|}$ units of currents are injected into ${\displaystyle u}$ and ${\displaystyle d(w)}$ units of currents are removed from every vertex ${\displaystyle w}$. In this electrical network ${\displaystyle C}$, we have ${\displaystyle \phi _{u,v}^{C}=-\phi _{u,v}^{B}=\phi _{v,u}^{B}}$, which gives us ${\displaystyle \tau _{v,u}=\phi _{v,u}^{B}=\phi _{u,v}^{C}}$ as a consequence. Since electrical networks are linear systems, we can super-pose ${\displaystyle A}$ and ${\displaystyle C}$ (summing up the voltage and currents on corresponding vertices and edges) and let the resulting electrical network be ${\displaystyle D}$. By the linearity, we have ${\displaystyle \phi _{u,v}^{D}=\phi _{u,v}^{A}+\phi _{u,v}^{C}=\tau _{u,v}+\tau _{v,u}}$. Note that the resulting electric network ${\displaystyle D}$ is an electrical network such that ${\displaystyle 2|E|}$ units of currents are injected into ${\displaystyle u}$ and ${\displaystyle 2|E|}$ units of currents are removed form ${\displaystyle v}$, thus ${\displaystyle \phi _{u,v}^{D}=2|E|\cdot R(u,v)}$ by definition of effective resistance. Altogether we have ${\displaystyle \tau _{u,v}+\tau _{v,u}=\phi _{u,v}^{A}+\phi _{u,v}^{C}=\phi _{u,v}^{D}=2|E|\cdot R(u,v).}$
${\displaystyle \square }$

Armed with this theorem, we can now give a better estimation of the cover time of random walk. Let ${\displaystyle T}$ be a spanning tree of ${\displaystyle G}$, and ${\displaystyle x=v_{1}\to v_{2}\to \ldots \to v_{2n}=x}$ be an Euler tour of ${\displaystyle T}$ for any particular vertex ${\displaystyle x\in V}$, then

${\displaystyle C_{x}\leq \sum _{i=1}^{2n-1}H_{v_{i}v_{i+1}}=\sum _{uv\in T}(\tau _{u,v}+\tau _{v,u})=2m\sum _{uv\in T}R(u,v)<2mn.}$

Thus the cover time for any connected graph ${\displaystyle G}$ of ${\displaystyle n}$ vertices and ${\displaystyle m}$ edges is bounded as

${\displaystyle C(G)=\max _{x\in V}C_{x}\leq 2mn}$.

## Graph Connectivity

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^{3}}$. 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 is 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.