## A Markov Chain in TiKz

This entry was posted by on Monday, 17 May, 2010 at

When you want to draw a Markov Chain (or any other graph-theoretical structure) you can toil around with various drawing programs, or you can simply turn to PGF/TiKz, the LaTeX drawing environment. Since true scientists publish their work in LaTeX, this immediately eases the work required for your next paper.

### A simple example (thanks to Daniel)

The following code shows a simple example.

\documentclass[12pt]{article}
\usepackage{amsfonts,graphicx,amsmath,amssymb,hyperref,color}
\usepackage[english]{babel}
\makeindex
\frenchspacing
\sloppy
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\begin{document}
\begin{center}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
semithick]
\tikzstyle{every state}=[fill=white,draw=black,thick,text=black,scale=1]
\node[state,initial]         (A)              {$s_0$};
\node[state]         (B) [right of=A] {$s_1$};
\path (B) edge  [bend right] node[above] {999} (A);
\path (A) edge  [bend right] node[below] {1} (B);
\tikzstyle{every state}=[fill=blue,draw=black,thick,text=white,scale=1]
\node[state]         (C) [below of=A] {$s_2$};
\path (A) edge  [bend right] node[left] {999} (C);
\path (C) edge  [bend right] node[right] {1} (A);
\tikzstyle{every state}=[fill=red,draw=black,thick,text=white,scale=1]
\node[state]         (D) [below of=B] {$s_3$};
\path (B) edge  [loop above] node {1} (B);
\path (B) edge  [bend right] node[left] {1} (D);
\path (D) edge  [bend right] node[right] {1} (B);
\node (E) [right=3cm,below=2cm] at (B) {};
\path (B) edge  [] node[right] {} (E);
\end{tikzpicture}
\end{center}
\end{document}


The resulting chain:

We can add some annotations as nodes:

\node[rectangle,draw,fill=blue!10] [below=5cm, left=2cm] at (s11) {initialisation};
\node[rectangle,draw,fill=green!30,dashed] [below=5cm, right=2cm] at (s11) {shutting down};
\node[rectangle,draw,thick,dotted,text width=4cm, text height=2cm] (idle2) [below=7cm, left=1cm] at (s11) {box with height=2cm};
\node[rectangle,draw,thick,text width=4cm] (idle) [below=6cm] at (s11) {A textbox containing multi-line text (height is auto)};


The result is shown below. Note the ‘semi-thick’, ‘thick’ and ‘thin’ show marginal difference, best visible with a dotted box. This example also shows how to get multiple lines i a box in TiKz, and of course you can also make empty boxes which may help grouping other nodes.

You can change the intensity of the color (e.g. ‘blue’, ‘green’ in the example) with an exclamation mark followed by a value between 0 and 100, like ‘!10’ for the blue box in the example.

### A more involved Chain

This example shows a model of a simplified CSMA/CA backoff instance (not exactly as a Markov chain, but this example gives some ideas about what is possible:

Below’s the code, note the following:

• the first [ ] in the \path sets the color of the arrows
• the shade=ball just gives a fancy look
• notice the combination of relative placing on the grid (CCA, BO, TX) and by means of values (the other states)
\documentclass[12pt]{article}
\usepackage{amsfonts,graphicx,amsmath,amssymb,hyperref,color}
\usepackage[english]{babel}
\makeindex
\frenchspacing
\sloppy
\newcommand{\BibTeX}{{\sc Bib}\TeX}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\begin{document}
\begin{center}
\node[state]	(CCA)              {CCA};
\node[state]         	(TX) [below of=CCA] {TX};
\node[state]		(BO) [right of=CCA] {[0,CW-1]};
\path [black] (CCA) edge node {idle} (TX);
\path [black] (CCA) edge node {busy} (BO);
\node (start) [left of=CCA] {};
\path [black] (start) edge node {start} (CCA);
\node[state]		(BC15) [right=2.7cm, above=1.8cm] at (BO) {15};
\path [black] (BO) edge [bend right] node[left] {} (BC15);
\path [black] (BC15) edge [loop right] node {} (BC15);
\node[state]		(BC14) [right=3cm, above=1.2cm] at (BO) {14};
\path [black] (BO) edge [bend right] node[left] {} (BC14);
\path [black] (BC14) edge [loop right] node {} (BC14);
\node[state]		(BC13) [right=3.3cm, above=0.4cm] at (BO) {13};
\path [black] (BO) edge [bend right] node[left] {} (BC13);
\path [black] (BC13) edge [loop right] node {} (BC13);
\node[state]		(BC12) [right=3.3cm, above=-0.4cm] at (BO) {12};
\path [black] (BO) edge [bend right] node[left] {} (BC12);
\path [black] (BC12) edge [loop right] node {} (BC12);
\node[state]		(BC11) [right=3.1cm, below=0.3cm] at (BO) {11};
\path [black] (BO) edge [bend right] node[left] {} (BC11);
\path [black] (BC11) edge [loop right] node {} (BC11);
\node[state]		(BC4) [right=2.5cm, below=1.3cm] at (BO) {...};
\path [black] (BO) edge [bend right] node[left] {} (BC4);
\path [black] (BC4) edge [loop right] node {} (BC4);
\path [black] (BC11) edge [bend left] node {} (BC4);
\node[state]		(BC3) [right=1.3cm, below=2cm] at (BO) {3};
\path [black] (BO) edge [bend right] node[left] {} (BC3);
\path [black] (BC3) edge [loop below] node {} (BC3);
\path [black] (BC4) edge [bend left] node {} (BC3);
\node[state]		(BC2) [left=0.1cm, below=2.4cm] at (BO) {2};
\path [black] (BO) edge [bend right] node[left] {} (BC2);
\path [black] (BC2) edge [loop below] node {} (BC2);
\path [black] (BC3) edge [bend left] node {} (BC2);
\node[state]		(BC1) [left=1.5cm, below=2.5cm] at (BO) {1};
\path [black] (BO) edge [bend right] node[left] {} (BC1);
\path [black] (BC1) edge [loop below] node {} (BC1);
\path [black] (BC2) edge [bend left] node {} (BC1);
\path [black] (BC1) edge [bend left] node {} (TX);
\node (finish) [left of=TX] {};
\path [black] (TX) edge node {} (finish);
\end{tikzpicture}
\end{center}
\end{document}