## DaMN book now on Softpedia

My book-in-progress Algorithmic Graph Theory, co-authored with David Joyner and Nathann Cohen, is now listed on Softpedia. These days I rather refer to the book as the DaMN book. The name is taken from the first letter of the first name of each author.

## Statistical analysis of the Fisher-Yates shuffle

The Fisher-Yates shuffle is a procedure for producing a random permutation of a sequence. This procedure is also known as the Knuth shuffle. Here I provide a statistical analysis of an implementation of the Fisher-Yates shuffle. A central idea is that any permutation of a sequence should equally likely be an output of the Fisher-Yates shuffle. That is, in a large enough number of shuffles of a fixed sequence, the observed probability of each permutation produced by a Fisher-Yates shuffle implementation should cluster around or converge to the theoretical probability for that permutation. As the number of shuffles increases, the observed probability for each possible permutation should converge to the theoretical probability. Otherwise there is something wrong with the implementation. I used my implementation of the Fisher-Yates shuffle to produce random permutations of various simple sequences of digits. The resulting output of the shuffles were used to perform frequency analyses of the behaviour of the implementation. Following are details on the particular sequences and the number of iterations for each sequence. Iteration here counts the number of times that I shuffled the given sequence. An experiment on a sequence is then the totality of all shuffles performed on it.

- Sequence: 123. Iterations: 1,000,000
- Sequence: 1234. Iterations: 1,000,000
- Sequence: 12345. Iterations: 1,000,000
- Sequence: 123456. Iterations: 10,000,000
- Sequence: 1234567. Iterations: 100,000,000
- Sequence: 12345678. Iterations: 100,000,000
- Sequence: 123456789. Iterations: 100,000,000

Each sequence was initialized as follows. Let be a vector where holds the digit . So holds the digit , holds , and so on. Two versions of the experiment was performed on each sequence. In the first version of the experiment, called version A, at the start of each iteration, I initialized to be as described above. Then I randomly permuted the vector. In version B of the experiment, I first initialized to be as above. Then I proceeded to repeatedly randomly permute . Thus if is the permutation obtained from iteration , then during iteration I applied the Fisher-Yates shuffle on to obtain . These two different versions of each experiment on a sequence were performed to see whether if they would produce qualitatively identical results. The experimental results suggest so: the two different versions of each experiment produced qualitatively similar results.

Source code of the experiments are provided here. Note that in order to compile the C files, you need to check out igraph trunk from Launchpad, apply the patch on this ticket, and then compile and install the resulting patched igraph version on your system. The C files containing the code for the experiments output the result of each shuffle to a file. For small sequences with say 3 to 4 digits, the resulting output files are a few MB in size. But for longer sequences, such as with 5 or more digits, the output files can be from tens of MB to hundreds of MB in size. The experimental data are easily generated from the above C files, so I do not provide the data. The data for each experiment were analyzed using the Python script fisherstat.py. If you intend to replicate the experiments, you need to adjust this script for each data file of each experiment. Given a data file for each experiment, the Python file is loaded from within the Sage command line interface; everything from then on is automated, from reading the experimental data to computing the frequency distribution. All experiments were run on the Sage cluster, in particular the sage.math compute node, whose purchase is supported by US National Science Foundation Grant No. DMS-0821725. Data analysis was performed using the Sage mathematics software system version 4.6.2.

Also note that the Bitbucket.org project link also points to PDF files. These files plot the normalized frequency distributions of the experimental data. The horizontal axis of each plot is for the permutation IDs. Each permutation of a fixed sequence is assigned a unique ID starting from 0. For example, for the sequence “123” here are all the possible permutations together with their corresponding IDs:

123 -> 0 132 -> 1 213 -> 2 231 -> 3 312 -> 4 321 -> 5

The vertical axis contains the corresponding normalized frequency of each permutation. Each frequency count was normalized by the number of iterations for the corresponding experiment. See the script fisherstat.py for further details. The normalized frequency for a permutation can be thought of as the empirical probability of that permutation showing up as a result of a Fisher-Yates shuffle.

And now comes the fun bit: plots of the experimental data. As I said above, both versions of each experiment produced qualitatively similar results. For this reason, below I only show some plots for version A of each experiment. To see all plots including plots for version B, refer to the Bitbucket.org project page. For kicks, each PDF file containing a plot was typeset using LaTeX and pgfplots.

As is evident from the above plots, for each sequence considered the empirical probabilities resulting from the experiments cluster around the theoretical probabilities. For a sequence of 3 or 4 digits, the empirical probabilities converge to the theoretical probability after a million or so experimental iterations. For example, the sequence “123” has six possible permutations so each permutation has a theoretical probability of of occurring as a result of the Fisher-Yates shuffle. In the above plot for the sequence “123”, it can be seen that the empirical probabilities converge to the theoretical probability after one million iterations. But as the number of digits in a sequence increases, the number of experimental iterations needs to increase as well in order to observe a convergence of the empirical probabilities to the theoretical probability for that sequence. For example, after iterations for the sequence “123456789”, observe that the empirical probabilities still cluster around the theoretical probability of . As the iteration number increases, the range of empirical probabilities should converge to the theoretical probability.

## Installing ESS (Emacs Speaks Statistics)

**Problem**

You have a non-standard directory under which you install software. You have installed Emacs under this directory. Now you want to install ESS under this directory as well so that Emacs could interact with R.

**Solution**

Suppose your non-standard directory is

/scratch/username/usr

where username is your actual username. I assume that you have installed both Emacs and R under this directory. Download ESS and extract the downloaded tarball to

/scratch/username/usr/share/emacs/site-lisp

Edit your /home/username/.emacs by inserting the following lines:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ESS: Emacs Speaks Statistics, mainly for R ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (load "/scratch/username/usr/share/emacs/site-lisp/ess-x.y.z/lisp/ess-site") (setq inferior-R-program-name "/scratch/username/usr/bin/R")

Remember to replace “x.y.z” with the actual version number of ESS that you use. Now load Emacs and to use R from Emacs, enter the command

M-x R

This should bring up an R session from within Emacs. Here’s a screenshot for your viewing pleasure:

## Typeset trees using TikZ/PGF

Some combinatorial trees typeset using TikZ/PGF. The Linux filesystem hierarchy:

\begin{tikzpicture} [-,thick] \footnotesize \node {\texttt{/}} [edge from parent fork down] child {node {\texttt{bin}}} child {node {\texttt{etc}}} child {node {\texttt{home}} child {node {\texttt{anne}}} child {node {\texttt{sam}}} child {node {$\dots$}} } child {node {\texttt{lib}}} child {node {\texttt{opt}}} child {node {\texttt{proc}}} child {node {\texttt{tmp}}} child {node {\texttt{usr}} child {node {\texttt{bin}} child {node {\texttt{acyclic}}} child {node {\texttt{diff}}} child {node {\texttt{dot}}} child {node {\texttt{gc}}} child {node {\texttt{neato}}} child {node {$\dots$}} } child {node {\texttt{include}}} child {node {\texttt{local}}} child {node {\texttt{share}}} child {node {\texttt{src}}} child {node {$\dots$}} } child {node {$\dots$}}; \end{tikzpicture}

Classification tree of organisms:

\begin{tikzpicture} [sibling distance=6cm,-,thick] \footnotesize \node {organism} child {node {plant} [sibling distance=2cm] child {node {tree} child {node {deciduous}} child {node {evergreen}} } child {node {flower}} } child {node {animal} [sibling distance=2.5cm] child {node {invertebrate}} child {node {vetebrate} [sibling distance=4.7cm] child {node {bird} [sibling distance=1.5cm] child {node {finch}} child {node {rosella}} child {node {sparrow}} } child {node {mammal} [sibling distance=1.5cm] child {node {dolphin}} child {node {human}} child {node {whale}} } } }; \end{tikzpicture}

The Bernoulli family tree of mathematicians:

\begin{tikzpicture} [-,thick,% every node/.style={shape=rectangle,inner sep=3pt,draw,thick}] \footnotesize \node {Nikolaus senior} [edge from parent fork down] [sibling distance=4cm] child {node {Jacob}} child {node {Nicolaus} child {node {Nicolaus I}} } child {node {Johann} [sibling distance=2cm] child {node {Nicolaus II}} child {node {Daniel}} child {node {Johann II} child {node {Johann III}} child {node {Daniel II}} child {node {Jakob II}} } }; \end{tikzpicture}

An expression tree:

\begin{tikzpicture} [-,thick] \node {$+$} [sibling distance=2.5cm] child {node {$\times$} [sibling distance=1cm] child {node {$a$}} child {node {$a$}} } child {node {$\times$} [sibling distance=1cm] child {node {$2$}} child {node {$a$}} child {node {$b$}} } child {node {$\times$} [sibling distance=1cm] child {node {$b$}} child {node {$b$}} }; \end{tikzpicture}

## Simple graphs, bridges of Konigsberg and directed graphs

Some combinatorial graphs drawn using TikZ/PGF. The seven bridges of Konigsberg:

\begin{tikzpicture} [lineDecorate/.style={-,thick},% nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y/\direction/\navigate in { a/0/0/left/west, b/0/2/left/west, c/0/4/left/west, d/4/2/right/east} { \node (\nodename) at (\x,\y) [nodeDecorate] {}; \node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$}; } %% edges or lines \path \foreach \startnode/\endnode in {a/d, b/d, c/d} { (\startnode) edge[lineDecorate] node {} (\endnode) } \foreach \startnode/\endnode in {a/b, b/c, c/b, b/a} { (\startnode) edge[lineDecorate,bend left] node {} (\endnode) }; \end{tikzpicture}

A house graph:

\begin{tikzpicture} [lineDecorate/.style={-,thick},% nodeDecorate/.style={shape=circle,inner sep=2pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y/\direction/\navigate in { a/0/5/above/north, b/2/3/right/east, e/-2/3/left/west, c/2/0/right/east, d/-2/0/left/west} { \node (\nodename) at (\x,\y) [nodeDecorate] {}; \node [\direction] at (\nodename.\navigate) {\footnotesize$\nodename$}; } %% edges or lines \path \foreach \startnode/\endnode in {a/b, b/c, b/e, c/d, d/e, e/a} { (\startnode) edge[lineDecorate] node {} (\endnode) }; \end{tikzpicture}

\begin{tikzpicture} [nodeDecorate/.style={shape=circle,inner sep=1pt,draw,thick}] %% nodes or vertices \foreach \nodename/\x/\y in {v_1/4/0, v_2/0/0, v_3/0/3, v_4/4/3, v_5/7/1.5} { \node (\nodename) at (\x,\y) [nodeDecorate] {\scriptsize$\nodename$}; } %% edges or lines \tikzstyle{EdgeStyle}=[->,>=stealth,thick] \tikzstyle{LabelStyle}=[fill=white] \foreach \startnode/\endnode/\bend/\weight in { v_1/v_2/bend left/1, v_1/v_3/bend left/3, v_2/v_3/bend left/1, v_2/v_4/bend left/3, v_3/v_1/bend left/1, v_3/v_2/bend left=0/2, v_3/v_4/bend left/1, v_4/v_1/bend left=0/3, v_4/v_5/bend left/2, v_5/v_1/bend left=0/3, v_5/v_1/bend left/6, v_5/v_4/bend left/1} { \scriptsize \Edge[label=$\weight$,style=\bend](\startnode)(\endnode) } \end{tikzpicture}

## UTF-8 and byte order mark

**Problem**

You have a text file with non-ASCII characters that you want to view within a web browser. But the browser jumbles up all those character accents, etc. How do you fix this?

**Solution**

With Vim, you can use bomb. But I just use Bash to prepend a text file with the relevant hex characters:

$ echo -n -e "\0357\0273\0277" | cat - myfile.txt > /tmp/myfile.tmp && mv /tmp/myfile.tmp myfile.txt

I wish it could be as easy and dramatic as letting the Bomberman take care of the above problem ðŸ™‚

## QED symbol for end of proof

The amssymb package defines a default Halmos symbol that automatically appears when you use the proof environment. That default Halmos symbol is . But if you want that to be filled with black, you could redefine the Halmos symbol. Put the following in your preamble:

\renewcommand{\qed}{\hfill \mbox{\raggedright \rule{0.1in}{0.1in}}}

Better still, you could customize what symbol you want to use as your QED symbol. Just replace the macro \rule{0.1in}{0.1in} with the the symbol of your choice.