# Solution to "The Lights Out Puzzle"

Problem 1: Here is a solution for the 5 x 5 case. Press the indicated buttons once, in any order you like, and all the lights will be out when you are done!

```        +---+---+---+---+---+
|   |   |   | D | E |
+---+---+---+---+---+
| F | G |   | I | J |
+---+---+---+---+---+
| K | L | M |   |   |
+---+---+---+---+---+
|   | Q | R | S |   |
+---+---+---+---+---+
| U |   | W | X |   |
+---+---+---+---+---+
```
Obviously, there are three more solutions that can be obtained by rotating this figure 90, 180, or 270 degrees. We'll see why in a minute.

One interesting insight, from Anderson and Feil, is the following. Suppose we know which buttons to push in row 1. Then we can figure out the rest of the solution as follows. If any light is on in row 1, then the corresponding button in row 2 must be pushed to turn it off, and no other buttons in row 2 may be pushed. Then, we can move to row 3 and reason the same way. In fact, this is how I found the above solution. I started by guessing that the buttons to push in the first row were "----E" and worked out the rest of the pattern, only to find that the last row still had lights on. Then I tried "---D-", and then "---DE", which worked.

Shen claims that the problem can always be solved, if it is required to start with all lights on and transform it to all lights off. Anderson and Feil show that, for the 5 x 5 grid, if we start with an arbitrary pattern of lights on, then 75% of the time we will be unable to reach lights out. They use a linear algebra technique, and arithmetic mod 2, to show how any problem can be analyzed to determine if there is a solution, and what that solution would be.

By the way, one reason that not all puzzles are solvable is that there is a two dimensional null space associated with the problem. Essentially, this means that there are two distinct patterns of button pressing that result in no lights being on. The representative patterns chosen by Anderson and Feil are the "cross":

```        +---+---+---+---+---+
|   | B | C | D |   |
+---+---+---+---+---+
| F |   | H |   | J |
+---+---+---+---+---+
| K | L |   | N | O |
+---+---+---+---+---+
| P |   | R |   | T |
+---+---+---+---+---+
|   | V | W | X |   |
+---+---+---+---+---+
```
(the "X" is missing in the original paper), and the "comb":
```        +---+---+---+---+---+
| A |   | C |   | E |
+---+---+---+---+---+
| F |   | H |   | J |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
| P |   | R |   | T |
+---+---+---+---+---+
| U |   | W |   | Y |
+---+---+---+---+---+
```
One way to verify that these patterns result in no lights on is to note that, for each pattern, every cell has an even number of "pushed" neighbors (considering itself, north, south, east and west).

The two patterns above form a basis for the null space. We can "add" either pattern to any pattern without changing the resulting light configuration, although we need to do this modulo 2, which accounts for the fact that pushing a button twice is the same as not pushing it at all. However, if we simply add our two basic null space patterns, we get a very revealing pattern:

```        +---+---+---+---+---+
| A | B |   | D | E |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
| K | L |   | N | O |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
| U | V |   | X | Y |
+---+---+---+---+---+
```
which is simply the "transpose" of the comb solution. In keeping with the symmetry of our problem, it seems more satisfactory to use the comb and the transposed comb as the basis for our null space. The cross can be regarded as the sum of the two combs.

Now let us look at the solution to the 5 x 5 puzzle that we gave above. We said there were obviously 3 more solutions, since we can rotate our given solution. This in fact can be represented by writing the set of solutions as

{ s, s+comb, s+comb', s+comb+comb' }
where the apostrophe is used to designate the transpose. Keeping in mind that we are working modulo 2, we are essentially using the mathematical technique of representing all solutions to a problem as the sum of one particular solution plus any combination of solutions of the homogeneous problem.

The null space solutions tell you many interesting things about the problem. In particular, for a particular grid size, if there are no null space solutions (or, as a mathematician would say, there is only the "trivial" null space solution), then every configuration is solvable. Secondly, the dimension of the null space tells you the chances that a randomly configured problem is solvable. And third, a basis for the null space allows you to display all variations of the solution.

Anderson and Feil worked out the dimension of the null space for problems on a square grid:
Dimension of the null space for an N by N grid.
N Dimension
1 0
2 0
3 0
4 4
5 2
6 0
7 0
8 0
9 8
10 0
11 6
12 0
13 0
14 4
15 0
16 8
17 2
18 0
19 16
20 0
21 0

It's an interesting task to work out the null solutions. It's much more pleasant if you copy the Visual Basic implementation mentioned below, and it's not too hard if you simply use the hint of Anderson and Feil, and proceed by guessing the pattern of lights for the first row, and then simply proceeding to determine what the pattern must be in the following rows. For instance, for the 4 x 4 case, if you guess that the first row has the form "1 0 0 0", then you are quickly led to the null solution:

```        +---+---+---+---+
| A |   |   |   |
+---+---+---+---+
| E | F |   |   |
+---+---+---+---+
| I |   | K |   |
+---+---+---+---+
|   | N | O | P |
+---+---+---+---+
```
from which rotation gives you the other three null solutions.

While we're at it, here are solutions for square boards up to 8x8. Note that the 4x4 and 5x5 solutions are not unique. If a solution on a square board is unique, then it has to be invariant under reflection and 90 degree rotation.

```        +---+
| * |
+---+
```
+---+---+ | * | * | +---+---+ | * | * | +---+---+
```        +---+---+---+
| * |   | * |
+---+---+---+
|   | * |   |
+---+---+---+
| * |   | * |
+---+---+---+
```
```        +---+---+---+---+
|   | * |   |   |
+---+---+---+---+
|   |   |   | * |
+---+---+---+---+
| * |   |   |   |
+---+---+---+---+
|   |   | * |   |
+---+---+---+---+
```
```        +---+---+---+---+---+
|   |   |   | * | * |
+---+---+---+---+---+
| * | * |   | * | * |
+---+---+---+---+---+
| * | * | * |   |   |
+---+---+---+---+---+
|   | * | * | * |   |
+---+---+---+---+---+
| * |   | * | * |   |
+---+---+---+---+---+
```
```        +---+---+---+---+---+---+
| * |   | * | * |   | * |
+---+---+---+---+---+---+
|   | * | * | * | * |   |
+---+---+---+---+---+---+
| * | * | * | * | * | * |
+---+---+---+---+---+---+
| * | * | * | * | * | * |
+---+---+---+---+---+---+
|   | * | * | * | * |   |
+---+---+---+---+---+---+
| * |   | * | * |   | * |
+---+---+---+---+---+---+
```
```        +---+---+---+---+---+---+---+
| * | * |   | * |   | * | * |
+---+---+---+---+---+---+---+
| * | * | * |   | * | * | * |
+---+---+---+---+---+---+---+
|   | * | * |   | * | * |   |
+---+---+---+---+---+---+---+
| * |   |   | * |   |   | * |
+---+---+---+---+---+---+---+
|   | * | * |   | * | * |   |
+---+---+---+---+---+---+---+
| * | * | * |   | * | * | * |
+---+---+---+---+---+---+---+
| * | * |   | * |   | * | * |
+---+---+---+---+---+---+---+
```
```        +---+---+---+---+---+---+---+---+
| * | * |   |   |   |   | * | * |
+---+---+---+---+---+---+---+---+
| * | * |   | * | * |   | * | * |
+---+---+---+---+---+---+---+---+
|   |   | * | * | * | * |   |   |
+---+---+---+---+---+---+---+---+
|   | * | * | * | * | * | * |   |
+---+---+---+---+---+---+---+---+
|   | * | * | * | * | * | * |   |
+---+---+---+---+---+---+---+---+
|   |   | * | * | * | * |   |   |
+---+---+---+---+---+---+---+---+
| * | * |   | * | * |   | * | * |
+---+---+---+---+---+---+---+---+
| * | * |   |   |   |   | * | * |
+---+---+---+---+---+---+---+---+
```

Problem 2: for this problem, we are giving up the detailed information about how the lights are connected. We are left knowing only that the connections are reflexive and symmetric, and that our task is to go from all lights on to all lights off. Lossers gives a short explanation of how to see that this is so. First, we define the obvious matrix A with 1's indicating which switches control which lights. The matrix is symmetric and has 1's on its diagonal.

Now it's equivalent to show that there is some strategy x of button pushing that can turn all the lights on from an off position. But that's just asking if there is a vector x such that

A * x = d,
where d is the vector of all 1's. That is equivalent to asking if d is in the column space of A. And that is equivalent to asking if the perpendicular space of A is contained in the perpendicular space of d.

So let x be any vector in the perpendicular space of A. Then

Sum (1 <= i <= N) xi * Ai,j = 0
for all j. This implies
Sum ( 1 <= j <= N ) Sum (1 <= i <= N) xi * Ai,j * xj = 0,
and by symmetry of A,
Sum (1 <= i <= N) xi2 * Ai,i = 0
but, because Ai,i = di and xi2=xi, we have
Sum (1 <= i <= N) xi * di = 0
and hence x is in the perpendicular space of d. This means d is in the column space of A, so a button strategy exists that turns a set of off lights all on, and the same strategy will turn a set of on lights all off.

References:

Alexander Shen,
Mathematical Entertainments: Lights Out,
Mathematical Intelligencer,
Volume 22, Number 13, Summer 2000, pages 20-21.
(a cryptic and hurried discussion)
Marlow Anderson and Todd Feil,
Turning Lights Out with Linear Algebra,
Mathematics Magazine,
Volume 71, Number 4, October 1998, pages 300-303.
(discusses general initial configuration, larger boards, and problems on a torus)
Oscar Martin-Sanches and Cristobal Pareja-Flores have a web site containing a very nice Visual Basic implementation of the puzzle.
(Although they supply a standalone executable, I could not get it to work until I went out and bought my own copy of Visual Basic. Happy Birthday, Bill!)
Uri Peled,
Problem 10197,
American Mathematical Monthly,
Volume 99, Number 2, February 1992, page 162.
O P Lossers,
An All-Ones Problem
A Solution to Problem 10197,
American Mathematical Monthly,
Volume 100, Number 8, October 1993, pages 806-807.
F Galvin,
Solution to Problem 88-8,
Mathematical Intelligencer,
Volume 11, Number 2, 1989, pages 31-32.
K Sutner,
Linear Cellular Automata and the Garden-of-Eden,
Mathematical Intelligencer,
Volume 11, Number 2, 1989, pages 49-53.

Back to The Lights Out Puzzle.

Last revised on 05 October 2000.