Light Puzzle
This puzzle is taken from a lab assignment given to UCSD undergraduates in their linear algebra course. (It can be found here.)
In a kingdom far, far away, the King decided that the time had come to find a husband for his princess daughter. The King wanted to find a worthy lad for his princess, so he promised to give his daughter away to the first young (or old) man who would solve the puzzle that had stumped the best of his court mathematicians for years. The puzzle was very simple: in a palace, there are 25 rooms arranged in a square---5 rows of rooms with 5 rooms in each row. In every room there is a light switch which not only switches on/off the light in that room, but also switches the lights in the adjacent rooms---the room to the right, to the left, the room above, and the room below. Initially, all of the lights are turned off. The goal is to turn the lights on in every room of the palace.
You can try out the puzzle below. The "Clear" button will allow you to clear all the lights and start over.
| R1 | R2 | R3 | R4 | R5 |
| R6 | R7 | R8 | R9 | R10 |
| R11 | R12 | R13 | R14 | R15 |
| R16 | R17 | R18 | R19 | R20 |
| R21 | R22 | R23 | R24 | R25 |
Encode each switch as a vector of zeros and ones. A one in the th position indicates that the switch will toggle the light in the th room. For example, Switch 1 toggles the lights in Rooms 1, 2, and 6; therefore, the switch in Room 1 can be encoded as the following vector:
The switch in Room 2 is
What happens if we flip both switches in Room 1 and Room 2? This is accomplished by adding and . The trick, though, is to do all calculations mod 2. To see why this makes sense, consider Room 1: the vector has a one in the first slot, so when we flip the switch in Room 1, the light in Room 1 turns on. Now we flip the switch in Room 2. The vector also has a one in the first slot, so if we add , we get a two in the first slot. But the actual effect of the latter switch is to turn the light in Room 1 off again. So this two should be a zero.
Let's try this in Sage.
Check that the above answer makes sense by going to the game board above and verifying that flipping the switches in Room 1 and Room 2 will result in having the lights on in Rooms 3, 6, and 7.
To solve the problem, we need to turn on a set of switches. Note that the order in which we flip the switches is irrelevant and we never need to flip any switch more than once. In other words, each switch will either be flipped or not.
Let be the initial state of the lights. (This is just the zero vector in our scenario.)
Now let if we leave the switch in Room alone and if we flip the switch in Room . Then the final state of the lights will be
Let be the final state we want to achieve. (If the goal is to turn on all the lights, then will be a vector of all ones.)
Therefore, solving our puzzle is equivalent to solving the equation for the coefficients . Or put another way, if we let , then we need to solve
Recall that the above expression of a linear combination of vectors is equivalent to a system of linear equations, encoded in a matrix-vector equation as where is the matrix with columns , and is the vector of coefficients .
Let's set up the augmented matrix for this system. If you write out the 25 x 25 matrix , you'll see that it takes the block form where is the 5x5 block is the 5x5 identity matrix, and is the 5x5 zero matrix.
Let's define in Sage.
Now we can form the augmented matrix with as the last column. Remember that is a vector of all ones (all the lights need to be turned on) and is the zero vector (all the lights are off at the beginning).
Now we simply row reduce. The row reduction will automatically be done mod 2 because all the objects that form were defined over the integers mod 2.
Looking at the bottom of the above matrix, we can see two rows of zeros. That means that we have two free variables. One easy solution is to set the free variables equal to zero, and then the particular solution is just the last column of the augmented matrix above. (The 26th column is actually indexed as 25 because the first column is indexed—as everything is in Python—by 0.)
To make this easier to read, let's arrange the vector as a 5 x 5 array so you can tell which rooms correspond to the ones.
Check that this is a solution by clearing the game board and flipping the switches according to the solution above (i.e., Rooms 2, 3, 5, 7, 8, 9, etc.) It might be useful to use the "Split view" button in the toolbar to be able to see the solution and the game board at the same time. (Hit it twice to get a vertical split instead of a horizontal split.)
The other solutions are obtained by adding the particular solution to all vectors in the null space. These are typically obtained by taking the negative of the 24th and 25th columns of the reduced matrix, and including an extra 1 corresponding to the free variable (in the 24th or 25th slot). However, in arithmetic mod 2, -1 is the same as 1, so we don't need to worry about the negative signs.
Of course, since we're only working with numbers mod 2, the only possible multiples are zero or one. If and are a basis for the null space, then all possible solutions are given by where and can only be zero or one.
We've already seen (the result of setting and equal to zero). Here are the other possible solutions:
What do you notice about these four solutions? Do these really represent four unique solutions to the puzzle?
The quick way
We can get to the answer quicker by using some handy linear algebra functionality built into Sage. Rather than row reducing and then extracting vectors from a matrix, let's allow Sage to simply solve the matrix-vector equation. (To make this work, though, we need to define as a vector, and not a 25 x 1 matrix.)