A tour in the wonderland of math with python

Domino Shuffling Algorithm Animation on Aztec Diamonds


  1. cairocffi and matplotlib for drawing random tilings.
  2. ImageMagick for making GIF animations.



  1. To sample a random tiling of an order $n$ Aztec diamond, run
python -size imgsize -order n -prog matplotlib
  1. To make GIF animation of domino shuffling algorithm up to order $n$, , run
python -size imgsize -order n

Windows users may also need to manually add the path to your convert.exe to the environment variables.

What is domino shuffling algorithm

The following picture shows an Aztec diamond of order 10:

You can see from the top row to the bottom row there are $$2, 4, \ldots, 20, 20, \ldots, 4, 2$$ unit squares piled up layers by layers.

In general an Aztec diamond of order $n$ (denoted by $az(n)$ for short) is obtained via this way by piling up $$2, 4, \ldots, 2n, 2n, \ldots, 4, 2$$ unit squares.

Now we consider domino tilings of $az(n)$, i.e. tilings with 1x2 dominoes. The following image shows one possible tiling of the above $az(10)$:

Our first problem is:

Problem 1: How many domino tilings of $az(n)$ are there?

This problem seems quite innocent at the first glance: anyone with a high school level can understand it, but unfortunately all solutions known today (there are more than one dozen solutions there) are not elementary, they either use quite sophisticated math or requre genius insights.

The answer is $2^{n(n+1)/2}$, a short and neat expression which suggests the problem itself should also have a neat and beautiful solution, and it does. This solution is called the domino shuffling algorithm we implement here.

Before we state the algorithm, let’s step further to another problem:

Problem 2: How to choose a random tiling of $az(n)$ among all its $2^{n(n+1)/2}$ different tilings with uniform probability?

Compare this with the Wilson’s uniform spanning tree algorithm and the coupling from the past algorithm in this repo, basically all of them are devised to do the same thing: sample with uniform probability from a very very large set (in fact they can be generalized to sample under any given probability distribution). For $n=100$, $az(n)$ has $2^{5050}$ different tilings, far more larger than the number of partices in the universe!

Domino Shuffling Algorithm

Think the plane as an infinite chessboard, put $az(n)$ at the origin, the chessboard are colored in the fashion that the leftmost cell in the top row of $az(n)$ is colored white.

A 2x2 block is called “black” if its topleft cell is colored black:

Assume $\mathcal{T}$ is a domino tiling of $az(n)$, a 2x2 block is called “bad” (with respect to $\mathcal{T}$) if it’s black and it contains a pair of parellel dominoes in $\mathcal{T}$. The algorithm runs as follows:

  1. Deletion: find all bad blocks and remove all pairs of parallel dominoes in them.

  2. Sliding: for each domino that remains in $\mathcal{T}$, move it one step according to its orientation. The orientation of a domino $d$ s determined by the following rule: find the unique 2x2 black block $B$ that contains $d$, move $d$ to the opposite position in $B$.

    After sliding these dominoes are scattered in the region bounded by $az(n+1)$.

  3. Creation: it can be proven that after the moves the holes in $az(n+1)$ can be filled up by disjoint 2x2 black blocks in a unique way (this assertion is the crux of the algorithm), use either a pair of horizontal or vertical dominoes to tile each block, then one gets a domino tiling $\mathcal{T}‘$ of $az(n+1)$. Further more, if $\mathcal{T}$ is sampled from tilings of $az(n)$ with uniform probability, then $\mathcal{T}‘$ is also sampled from tilings of $az(n+1)$ with uniform probability.

So to sample a random tiling of $az(n)$ with uniform probability, one just starts from a uniformly random tiling of $az(1)$ (which is a 2x2 square, use a coin to choose one of its two tilings), and repeat the procedure deletion –> sliding –> creation –> deletion –> … until one gets a tiling of $az(n)$, then this tiling must be a perfectly random sampled one.

The proof of this algorithm is quite non-trivial and one may refer to the two papers by Elkies, etc. and Propp for more details.