×   Home   Blog   Newsletter   Privacy   Contact Us   About

Self Avoiding Walks

This week, I’m going to talk about space filling self-avoiding walks.

Imagine you had a chess board, and you started on one square. If you were allowed to only move one square at a time, horizontally or vertically (but not diagonally) to a previously un-visited square, is it possible to find a tour that visits all the squares on the board?

Is it always possible? Does it depend on where you start? How does this change with the size of the grid?

Here is an example for an 8x8 grid (starting from where a bishop would start a game).

Try it out

Here’s a little application for you to try it out yourself. Using the buttons on the right you can define the grid size. Each time the board is redrawn the start seed moves to a random position (You can force this by clicking the Reset button). Click on the grid to extend the path, and use Undo to, recursively, remove the last step if you back yourself into a corner.
Did you find any patterns?
These kind of paths are called Hamiltonian Paths by mathematicians. They are routes that visit every vertex (in our case squares) just once. If the start and end can be connected, it’s called a Hamiltonian circuit.

Hamiltonian Paths

You might see analogies to these graphs to playing the game of snake, which we’ve talked about before. They also raised their heads in ways to solve De Bruijn Sequences, and the, related, classic Pardoner’s Puzzle from The Canterbury Puzzles collection (although this puzzle imposes additional restrictions).
Hamiltonian Circuits can be used to generate perfect mazes.


Returning to our initial problem, let’s start by looking at a 1x1 grid. This is trivial. There’s only one path!


This is also trivial. There are two solutions (no matter where we start). Our first move can be either horizontal, or vertical (the only two choices); after that, the other moves are automatic.


This is the first, non-trivial, problem. If we start at one of the corners, there are just eight possible solutions. (It does not matter which corner, as the solutions are symmetrical both in rotation and reflection).
Tours that start at a corner are called Greek Key Tours (presumably in reference to the popular tiling pattern?).
If we start in the center, there are also eight solutions.
However, if we start at one of the edge pieces, there is no solution. Why is this? Well, the checkerboard pattern gives us a strong clue. If we shade the board alternating black and white we can see that a move from a square changes the parity of the color. If we were on a black square, a move will change us to a white square, and vice-versa. Because of the odd sized grid, there are an odd number of squares in total (in the example here, there is one more white square than black squares).
The only way to toggle, alternately, between colors is to start on a white square, and finish on a white square. There can be no solution if we start on a black square.


There are an equal number of black and white squares on an even sized square board, so there are no parity restrictions. There are solutions from every starting position. However, there are the most solutions when the route starts in a corner (The Greek Key Tour). Here is a count of all the possible solutions based on their starting condition. It’s should be no surprise at the symmetry.
Here are the 52 solutions starting at a corner space:
Here are the 25 solutions starting at one of the edge locations:
Here are the 36 solutions possible from one of the four center locations:


For 5x5 we’re back to having parity restrictions on every other space (as we have for all odd-sized boards). Again, the Greek Key Tour offers the highest number of possible solutions at 824.
Here is a count of all the possible solutions based on their starting condition. There are no solutions starting from any green square.
Here is one example solution:


The number of solutions is starting to increase rapidly now. Here are the enumerations of possible solutions based on starting position. The number of Greek Key solutions is 22,144. As this is an even grid, there are solutions from every possible starting space.
Here is one example solution:

7x7 and beyond …

I’m not sure if a formula has been derived for determining the number of Greek Key solutions based on arbitrary grid sizes, but here are the number of solutions for the first fifteen square grids:
GridNumber of Greek Key Tour Solutions
The is the sequence: OEIS A145157

Space Filing Curves

The above paths show all possible solutions. There is a special class of these solutions that follow a defined structure. We've talked about these before. One example is the Hilbert Curve. These are self-similar, convolutions that can described with the a simple formula.
Related to these are self-similar curves are like the Koch Snowflakes

Generating Hamiltonian Paths

Instead of enumerating all possible permutations, another alternative is to randomly generate them. One popular way to do this is to use an algorithm called the backbite algorithm.
We start with a non-random Hamiltonian Path, such as a basic, easy to make, zig-zag pattern. Then, random changes are made to morph this into a different path. Using the procedures described below it ensures that the next generated graph is also a Hamiltonian path, and the constraints maintained.
  • Select either of the two end points on the current graph.
  • Select, at random, one of the neighbor vertices of this end point that is not the one connected to the current point. (If the chosen vertex happens to be the opposite endpoint, add an edge to it and remove the edge that it was connected to originally).
  • This creates a loop, and the selected node now has a valency of three. (One link is the edge just created. One will lead to the opposite end of the graph, and the third loops back to itself).
  • The one that loops back to itself should be removed. This is the backbite.
The resulting graph remains Hamiltonian. The randomization can be repeated as often as desired.


Here's a walk-through example.
Considering the following path, which is already a Hamiltonian Path.
One of the end nodes is selected at random. This is shown in yellow.
A new link is to be added. Connections to the nearest neighbors are considering (other than the the link that is already present at this node). The possible new links are shown in dotted orange.

One of these candidates is selected at random and added to to the graph. This is shown in red. The addition of this new edge creates a loop in the graph.

Adding this link also creates a node with a valency of three. The newly created link remains, and we need to determine which of the other two links to break. This is done by propagating along each of the other two links. One of the these paths will take us to the other end-node of the graph, and the other will loop back on itself.

One path goes to the other end, the other loops back.

The link that forms the loop is deleted.

This is the backbite.

And the final result is a new Hamiltonian Path!