Almost 150 years ago the game 15-puzzle was invented and quickly gain lots of popularity. The rules are quite simple – you start with a board with 15 numbered pieces as in the image below, where the pieces are ordered according to their numbers, except the 14 and 15 pieces which switch positions. Given this board we are allowed to slide into the missing place any one of the pieces next to it, how many times that we want, and we win if we manage to get all the pieces in the right order.
This game became very popular during the 1880 and there was even a cash reward for anyone who can solve it. However, no matter how many people tried, no one could solve it, which is not surprising since it is unsolvable (what is surprising, it that it was proved to be unsolvable in 1879). Indeed – this game has a game invariant which doesn’t change when using legal moves and the initial state (with 14, 15 switched) and the ordered state have different invariants, so it cannot be solved.
In this post we will see, as mathematicians, how to even start thinking about this problem, and eventually get to this game invariant.
While this post is self contained, there is a more basic introduction to general game invariants in this and this post. For the Hebrew speaking readers, I made a video about this game here.
Finally, you can play the game yourselves here.
How to approach a problem
While we are going to give a full proof for the insolvability of the 15-puzzle, the main goal of this post is how to approach a problem, with this interesting problem as our example. I suggest that before you continue reading, you should try to play the game for some time, and see that you cannot actually solve it, and try reach some insights of your own and see eventually if they match the solution.
As for me, whenever I encounter a new problem, the first thing that I do to get some intuition is:
Find the simplest nontrivial example
Many times, we see a new problem and don’t even know where to start at. Which tools should we use, from which area? In this case, if we were just handed the game without any more information, we wouldn’t even know if we need to prove that it is solvable or not!
When there are too many variables and not enough information to know where to start, I usually try to simplify the problem as much as possible, without making it too trivial. Hopefully, we could then get some intuition which we can take with us to the original, more complicated problem.
In this case, to simplify the problem, we can take smaller boards instead of the board. For a general board with places, we can add numbered pieces in the right order and then switch the last 2 pieces.
The simplest board is the board. However, this board will only have one empty place without any actual pieces, so it is also trivial. A board with 2 places will only have 1 numbered piece, so we didn’t even get to switch the last 2 pieces. To get to anything interesting, we need to be at least .
If the board is just one line, then it is still too trivial, since we can only move the piece back and forward, and this reason will not hold in the case.
With these trivial cases behind us, the first really nontrivial case is:
Our first move will be to either move the “2” or the “3” piece. Suppose that we move “2”. The second move is to either move “2” or “1”. If we move “2”, then we are just going back to the initial state, so we will move “1”. In a sense, in this very simple case we have a “forward” and “backward” directions, which we can think of as moving the piece counter clockwise or clockwise. If we continue moving forward, namely moving “3” and then “2”, we will get to this state:
Note that again the missing piece is at the bottom right location. Another such rotation will produce
After one more such rotation, we will get back to our original state. It is now not hard to show that we have seen all the states that we can reach with legal moves. In particular, we didn’t reach the state with the pieces ordered, so this proves the insolvability for the board.
The question now, is can we say something that we might take with us to the case? We have seen all the states that we can reach, meaning that we also know what are all the states that we can’t reach:
We can now observe a very interesting property of this game – exactly half of the states are reachable and half are not. Something like this usually means that there is some background structure to the problem that causes this half\half result. This structure might be true just for the or might be true in general. Before we try to prove it, let’s see if it holds in other small example.
What does our new conjecture tells us in the case? We should try to figure out how many orderings our board may have, and hope to show that exactly half of them are reachable. However, in the case we had only pieces, and therefore only orderings. In this case we have pieces and therefore orderings! This is much more that we can handle, and even a computer program will take some time running over all of these options. Luckily for us, we can also try our luck in smaller, non square boards.
In the board, we have only pieces, so there are a total of orderings, which is not that large. However, unlike our case, we no longer have the “forward” and “backward” directions. There are many ways to move the pieces to get to states where the missing piece is in the bottom right location.
This extra complication is problematic, but unlike having a whole new problem, it is just one more complication which we can deal on its own. The main problem was that there are a lot of ways for the “empty” piece to travel until it gets back to the bottom right location. However, if we don’t require it to return to its place, and only look at a single step, then it can only choose to move in at most four directions – up, down, left and right. This is a much simpler setting which we can track. However, simplifying this movement, we need to track more states of the board, since now the missing place can be everywhere. In other words, it is treated similar to the other pieces, so lets just add another piece labeled with “6” in the missing place, so that a legal move is to switch it with one of its neighbors:
The number of orderings is now which is still not that large. Considering these orderings as vertices, and the legal moves as edges, we get a graph where the connected component of our initial state contains all the reachable states. Using any graph transversal algorithm will work here, and you can check that exactly states are reachable, and from those with the missing place at the bottom right corner exactly are reachable.
We now have an algorithm which can check our conjecture for us! You can even run it for the case, though it might take a while ( is quite large). But for the case, the graph has size of , so I would advise against running the algorithm on it. However, now we have a conjecture and a starting place to understand our probelm:
Conjecture: Exactly half of the states are reachable.
With this conjecture, lets continue to the next step of trying to understand a problem.
Breaking the game
The easiest way to solve the game is to just take out all of the pieces, reorder them, and putting them back in. Or in other words – cheating. However, we just allowed ourselves to change the size of the board, so why not allow this also?
The idea of this step, is to allow ourselves to break the game to a degree and try to move from rules which allow us to solve the game to rules which don’t allow us to solve, and hope that we find where these good and bad rules meet and somehow get some intuition about the game along the way.
Note that in a sense this step is very similar to the previous step. We try to move a long a spectrum of parameters (in the previous step it was the board size, and here they are the rules), where at one endpoint we know exactly what happens, and we try to reach the second endpoint with some conjectures or results along the way.
In our game, one endpoint is the standard rules of “moving” the missing piece, while at the other endpoint we allow to change the order completely, which of course makes the game solvable. However, even if “break” it a bit less, and we only allow to switch between any two pieces, it is enough to solve the game, since we only need to switch 14 and 15 (or in general the last two numbered pieces). These are much simpler rules than being able to do anything, so it is much easier to work with them. However, you should try to show that these simpler rules are actually enough to get to any ordering. This result is not that important here, but it gives a better view of our problem, and is connected a bit to the final step of the proof.
In the standard rules, we can also switch between two pieces, but have extra conditions. The first is that one of the pieces is the “missing” piece, or the “16” piece. The second is that we can only move up, down, left or right. If we delete the second restriction – namely the rules are that we can switch “16” with any other piece, then it is quite easy to see that the game is solvable. This means that there is something important in the up\down\left\right movement that makes the game unsolvable.
Thinking about it for a few minutes, one observation comes to mind. Since “16” starts and end in the bottom right corner, for every time it moves up it needs to move also down eventually, and similarly with the left\right movement. In particular, this means that we move an even amount of steps. We can also see it by coloring our board by red and blue alternatively as follows:
The “16” piece starts at red, and ends in red, and since at each step it changes colors, at must make even amount of steps.
This observation connects to our previous observation. If we must have even amount of steps, then it sounds reasonable that half of the state will be reachable (for even amount of steps) and half not reachable (odd amount). Moreover, to check the importance of this observation, we can try to look at the rules where we are allowed even amount to switching between any two pieces (not necessarily with “16”). If you try it with the smaller boards, you will see that you get exactly the same reachable states as with the standard rules – so this parity condition must be really important.
At this point, we have two interesting, probably connected observations (still not proved for a general board):
- Legal games have even amount of switching between any two pieces.
- Exactly half of the states are reachable.
If you already studied some group theory, then a couple of lightbulbs should have light up around you. This sounds very similar to the parity of a permutation, and indeed, this will be the final step.
The symmetric group and the even permutations
Up until now we talked about ordering of our pieces, with or without the missing piece. The structure used to study this sorts of object is called the symmetric group. The symmetric group is the group of all permutation on elements. In this post we will assume familiarity with the symmetric group (though let me know if you want a separate post explaining it), and we start by recalling the basic results needed for us.
Recall first that if is any permutation, then we can write it as a product of disjoint cycles. For example, the permutation
can be written as . Most of the times, we don’t write the 1-cycles and simply write instead.
A single 2-cycle, namely where is called a transposition. One of the first result that we learn about the symmetric group is that
Theorem: Every permutation can be written as a product of transpositions. In other words, the transpositions generate the symmetric group.
This is not a hard theorem to prove. Simply think of getting a mixed deck of cards that you want to order. To get the ace of hearts to the top of the deck, you only need to switch with the card at the top of the deck. You can then switch the 2 of hearts card with the card just below the top of the deck and so on.
In our puzzle world, we have a set of 16 pieces (including the missing one) and an initial state which can represented as the permutation . We then ask if we can get it using transpositions, each including 16 and one of its neighbors. However, we later saw that we can allow any transpositions, as long as it is an even amount. In other words, we asked if we can write
The goal is to show that when writing a permutation as a product of transpositions, the parity of the number of transpositions is well defined. This means that in the equation above the left hand side is an odd permutation, while the right hand side is an even permutation, and therefore there is no solution.
What are even permutations
We already know that each permutation can be written as a product of transpositions. We now want to extend this result and show that while the number of transpositions in this presentation is not uniquely determined the parity of this number is.
Start with examples
As before, when trying to understand a new object, we start with the simplest nontrivial example that we can find, which in this case is where the permutations are of elements . In this case there is exactly one permutation which can be written with 0 transpositions, namely the identity, and exactly one permutation which can be written with 1 transposition, namely the transposition . This already covers all the permutations, so we can write
and try to show that these are really the even and odd permutations. The proof here is not too difficult, because we only have one transposition and is either the identity if is even or if it is odd.
For permutations on , it becomes a little bit trickier to prove, since we have 3 transpositions. First, to find our candidates for the even and odd permutations, you can check that the identity is the only product of 0 transpositions, the 3 transpositions and are the only products of 1 transpositions, and finally the identity and the two 3-cycles and are the products of 2 transpositions. We already saw all of the permutations, so we would like to show that
are the even and odd permutations respectively.
Since we have 3 transpositions, it is not as straight forward as with the , but it is still not difficult. We just need to show that any element in when multiplied by a transposition is going to be in and similarly the other way around. Since and there are transpositions, there are computations that we need to do, and an extra computations for the other direction. As an exercise, show that because each transposition is its own inverse, you can get the second batch of computations for free.
At least for when multiplying by the transposition from the right you get the following diagram:
As another exercise, show that it is enough to consider the right multiplication.
You can do the same for and get that contains the (1) identity, (2) 3-cycles (there are 8 such permutation) and (3) product of two disjoint transpositions (3 permutations), while are the (1) single transpositions (6 permutations) and (2) 4-cycles (6 permutations). Or written generically
Now comes the hard part. We need to somehow characterize the sets and for general . Once they are properly defined, we need to show that multiplying by transpositions moves us from one set to the other.
Next we generalize
One property that can be seen in our simple examples, is that the parity of a permutation doesn’t change when we relabel the elements, or more precisely two permutations with the same cycle structure have the same parity. For example, if one 3-cycle is an even permutation, then all 3-cycles are even permutations. Another example, which you can check in is that is an odd permutation, and by our conjecture all the products of disjoint 3-cycle with 2-cycle are odd (for example ). You should take it as an exercise to show that if the parity is well defined, then it must have this property. For now, lets try to use this conjecture and maybe get some intuition on how to prove the general claim.
With this maybe a clue, lets ask what happens to the cycle structure when multiplying by a transposition. In our example above we only had very simple structure, since was very small, so at this point you should start with some random permutation for some large with a lot of disjoint cycles and see what happens there when multiplying by the different transpositions. If you run a few examples like this, you will see something interesting:
Conjecture: When multiplying a permutation given as product of disjoint cycles with a transposition – either one cycle splits into two cycles, or two cycles are combined into one.
Once we have this conjecture from our experiments, it is an easy exercise to actually show it, and I will leave it as such.
Assuming this claim, let’s show how to complete our proof. While we don’t know if the parity of the number of transpositions is well defined, the number of disjoint cycles appearing in a permutation is well defined, and therefore its parity is well defined. For example, in our case above we had the identity the 3-cycles (e.g. ) and the product of disjoint 2-cycles (e.g. ) and they all have even number of cycles (4 or 2). Similarly all the permutations in have odd number of cycles (3 or 1). The same happens in and , but notice that there the permutations in have odd number of disjoint cycles while have even amount.
More generally, we can re(well)define these sets for general as follows. If is the number of disjoint cycles in , then
Other than the fact that these are well defined, what other properties do these sets have? Since every permutation has either even or odd amount of disjoint cycles, then , and in particular since the identity has cycles (of size 1) in . Moreover, by the exercise above, if is a transposition then and . Actually, because these are equalities (and in particular ).
Finally, a simple induction shows that a product of even (resp. odd) amount of transpositions is in (resp ), which is exactly what we wanted to show.
Even permutation are reachable
At this point we know that legal moves can only create even permutations (when the missing piece returns to the bottom right corner). However, is the opposite true as well? Given any even permutation, can we generate it using legal moves? In this section we will give the main ideas which can be used to show that for boards with at least two rows and two columns this is true.
We have already used the fact that every permutation can be written as a product of transpositions. A bit less known, but still not that hard, is the fact that all even permutation can be written as a product of 3 cycles.
Claim: Every even permutation is a product of 3-cycles.
Proof: Any even permutation can be written as a product of even number of transpositions. Thus, it is enough to prove this claim for a product of two transposition, and this is left as an exercise.
Going back to our puzzle, with , we want to show that any 3-cycle can be generated using legal moves.
There are certain 3-cycles which are really easy to generate on our board. These are the cycles which contain a piece, its right neighbor and its bottom neighbor. For example, the pieces in the board can be cycled as follows:
These already generate a lot of 3-cycles – denote the set of all these cycles by . We claim that these already generate all of the even permutations. The main idea is to use conjugation since – a conjugation of a 3-cycle is another 3-cycle.
Lets see as example how to show that the 3-cycle is in the group generated by .
- Using the , it is enough to show that is in the group generated by . Note that this cycle no longer change any element in the top line.
- Next, using conjugation by , we can look at .
- Finally, is in so we are done.
I will leave the general induction process for any 3-cycle and any board as an exercise.
In conclusion – what did we see?
We started with a new problem without knowing too much about it. Looking for ways to investigate it, we tried to look for simpler versions of the problem where we can solve it. Once we had such a version, we added back the complexity a bit by bit, each time trying to understand only this new complexity and looking for observations which might help to understand the original problem. In this case we saw that not only the game is not solvable, but half of the initial states are not solvable.
We did a similar process by changing the rules of the problem – also moving from a simple version to the complicated one. Looking for all sorts of rules is much more difficult, since there can be so many of them. However, since we already had this half\half observation, it was easier to focus on a certain rule about the parity of the number of steps.
By that time we had in our hands an almost mathematical conjecture, which led to a well known result about the symmetric group. Even when trying to understand this result, we were able to use similar approaches to solve it.
Hopefully by now you see how powerful these simple steps can be. They can be applied to new and complicated problems, as well as homework problems. Once we have a solution and a lot of intuition about a given problem, we can start to look for more elegant and hopefully shorter solutions.