← MAIN
07 // LINEAR ALGEBRA · GAUSSIAN ELIMINATION MOD 2

LIGHTS OUT

Press lights until every light is dark. The solver finds the fewest presses by Gaussian elimination mod 2.
STATUS LOADING PRESSES 0 LIT — MINIMUM —

HOW TO PLAY

Lights Out is a grid of lights, and the goal is to make every light dark. A press toggles the pressed light and the lights directly above, below, left and right of it.

Click a light to press it. NEW BOARD deals a fresh board of the chosen SIZE, and NEW BOARDS ARE chooses whether every board is solvable or any board may be dealt.

HINT lights the next press of the minimum solution, and SOLVE shows all of it. STEP and AUTO play the shown solution one press at a time, and the panels below the board show the elimination that finds it.

Click a light to press it. Goal: every light dark.
THE SOLUTION
Press SOLVE or HINT to see the minimum solution.
GAUSSIAN ELIMINATION MOD 2
Each row is one light's equation. Pink is the lit state.
RANK —
Move log
BOARD
SIZE
NEW BOARDS ARE

HOW IT WORKS

This page uses no search and no learning. Pressing a light is addition mod 2, meaning only the remainder after division by 2 is kept, so 1 + 1 = 0. The rules therefore form a linear system with one equation per light, and Gaussian elimination solves that system exactly. The solver reports whether a board can be cleared and, if it can, the press set with the fewest presses. HINT shows the first press of that set and SOLVE shows all of it.

The rules as a linear system over GF(2)

Cells are numbered row by row from the top left, 0 to N − 1, where N = n × n is the number of cells on an n × n board. The board is a vector b with b_i = 1 when cell i is lit and 0 when it is dark. A press pattern is a vector x with x_j = 1 when cell j is pressed. A press toggles the pressed cell and its orthogonal neighbours, the cells directly above, below, left and right of it. The set of cells that one press toggles is its neighbourhood1.

GF(2) is the set {0, 1} with addition mod 2, which is XOR (1 ⊕ 1 = 0), and with multiplication AND. Every press is one XOR of a neighbourhood into the board, so presses combine by XOR and their order does not matter. Pressing a cell twice cancels, which is why a minimum solution never presses a cell twice.

21
A\,x = b \pmod{2}, \qquad A_{ij} = 1 \text{ if } j \in N(i), \text{ otherwise } 0

Here A is the N × N matrix whose row i is the neighbourhood N(i) of cell i, and x and b are the press pattern and the board. The equation for cell i says that the presses on the cells of N(i) XOR to the lit state b_i, so that cell ends dark. The matrix is symmetric, because j \in N(i) exactly when i \in N(j).

3
b' = b \oplus \bigoplus_{p \in P} N(p)

Here P is the set of pressed cells, N(p) is the neighbourhood of cell p, and b' is the board after the presses. The code stores every board, neighbourhood and press set as a bit mask, an integer whose bit i is 1 when cell i is in the set. A press is then one XOR of a mask into the board.

Gauss-Jordan elimination mod 2

Gaussian elimination solves a linear system by row operations, each of which keeps the set of solutions. Gauss-Jordan elimination adds one step: it clears the pivot column in every other row, above as well as below. The result is the reduced row echelon form, in which every pivot column holds a single 1. The augmented matrix [A \mid b] is A with b added as one extra column on the right.

A pivot is a row chosen to hold the leading 1 of a column. A column with no pivot is a free column, and its variable may be set freely. The number of pivots is the rank of A. The solver works down the columns from 0 to N − 1. For each column it searches the rows from the current rank downward for a 1 in that column, and swaps the row it finds up to the rank4.

Each row is one Python integer, with bit j holding column j and bit N holding the right-hand side. A row operation is then one XOR. Clearing a column uses the rule below, which adds one equation to another and so keeps the solutions unchanged5.

6
\text{row}_s \leftarrow \text{row}_s \oplus \text{row}_{\text{pivot}} \qquad \text{for every row } s \neq \text{pivot} \text{ with a 1 in the pivot column}

Here \text{row}_{\text{pivot}} is the pivot row, which is not changed. Each such row s loses its 1 in the pivot column. The earlier pivot columns stay clear, because the pivot row has a 0 in each of them.

The solver writes each step as a Step: the column, the pivot row, the swap made, and the rows cleared. The server returns the steps only for boards up to 7 × 7, because the comment there says the trace grows roughly with n⁴7. The ELIMINATION panel replays the steps on the starting matrix, so it can show any step the player chooses8.

92
function ELIMINATE(A, b):                     # Gauss-Jordan mod 2 on [A | b]
    rows ← each row of [A | b] as an integer
    rank ← 0
    for each column c from 0 to N − 1:
        find a row r at or below rank with a 1 in column c
        if there is no such row:
            continue                          # column c is free
        swap row r with row rank
        for each row s ≠ rank with a 1 in column c:
            row s ← row s ⊕ row rank          # clears column c above and below
        rank ← rank + 1
    return rows, rank

The simple version is the whole method: find a pivot in each column, swap it up, and clear the column everywhere else. The full version adds the bookkeeping the code keeps. Each row starts with its neighbourhood, so the right-hand side is the cell's own lit state.

A free column gets a Step with no pivot, because its variable is one of the free choices that generate the null space. The row swap and the cleared rows are recorded so the visualizer can replay each step. The clearing loop runs over every other row, not only the rows below, which is what makes each pivot column a single 1.

Example. On a blank 5 × 5 board, the matrix has rank 23 of 25, so the elimination ends with two free columns. Those two columns are the null-space directions described below.

When a board can be cleared

The boards that can be reached from the dark board are the column space of A, the set of all vectors A\,x. Each press adds one column of A, so pressing moves a board only within its class, the set of boards that differ from it by a column combination. A board in another class can never be cleared.

The solver checks solvability after the elimination. Rows from the rank on have no pivot, so their left side is all zeros. If one of those rows has a 1 on the right, it reads 0 = 1 and no press pattern satisfies it10.

10
\text{solvable} \iff \text{rhs}_r = 0 \text{ for every row } r \geq \text{rank}

Here \text{rhs}_r is the right-hand side bit of reduced row r. The test is exact, so it has no tolerance. Solvability is an invariant, a property that no move changes: no sequence of presses can carry a board out of its class, so a board that cannot be cleared stays that way however it is pressed.

The reachable boards are a fixed fraction of all boards. There are 2rank reachable boards out of 2N in total. On 5 × 5 the rank is 23, so one board in four is reachable (223 ÷ 225 = 1/4)11.

The null space and the solution set

A null-space vector is a press pattern v with A\,v = 0. Pressing exactly the cells of v changes no light, so the presses cancel. The nullity is the dimension of the null space, which is the number of vectors in a basis, a smallest set of vectors from which every null vector is a combination. Gauss-Jordan elimination gives the nullity directly, because the free columns are the directions in which a solution can move without changing the board12.

13
\text{nullity} = N - \text{rank}, \qquad |\mathcal{S}| = 2^{\text{nullity}}

Here \mathcal{S} is the set of solutions of a solvable board, and |\mathcal{S}| is its size. The code reports the size as Solution.count. On 5 × 5 the nullity is 2, so every solvable board has 4 solutions. The brute force alternative would try all 225 (about 33 million) press sets on 5 × 5. The solver never does this, since it builds only the 2nullity solutions.

14
x_0[\text{piv}(r)] = \text{rhs}_r \qquad x_0[f] = 0 \text{ for every free column } f

Here x_0 is one particular solution, and \text{piv}(r) is the pivot column of row r. Setting every free press to 0 makes each pivot press equal to its row's right-hand side, since the reduced row has no other pivot columns.

15
v_f = e_f \oplus \bigoplus_{r \,:\, \text{row}_r[f] = 1} e_{\text{piv}(r)}

Here e_c is the vector with a 1 only in cell c, written bit(c) in the code, and v_f is the null-space vector for free column f. Setting f to 1 with the other free presses at 0 forces each pivot press to the bit that its row holds in column f. A reduced pivot row reads x_{\text{piv}(r)} plus the free presses it contains equal to its right-hand side, which is 0 for a null vector.

16
\mathcal{S} = \left\{\, x_0 \oplus \bigoplus_{v \in T} v \;:\; T \subseteq \{v_f\} \,\right\}

Every solution is x_0 XOR a combination of the null-space vectors v_f. The code builds the list by doubling: for each vector v, it appends every existing solution XORed with v, which gives all 2nullity combinations.

17
function NULL-BASIS(rows, pivots):
    basis ← empty list
    for each free column f:                   # a column with no pivot
        v ← bit(f)
        for each pivot row r:
            if bit f of rows[r] is 1:
                v ← v ⊕ bit(pivots[r])
        append v to basis
    return basis

The full version does not use a separate null-space routine: the solver calls it with the reduced rows, and null_space(n) calls the same function on the empty board. The code uses OR here. Each pivot column differs from f and from the other pivot columns, so no bit is set twice, and OR and XOR give the same vector.

The minimum-press solution

The solver returns the lightest solution, the one with the fewest presses. Solutions are compared by a pair: first the number of presses, then the integer value of the press mask. The second key makes ties deterministic, so the same board always gives the same answer18.

19
x^{*} = \operatorname*{arg\,min}_{x \in \mathcal{S}} \Big( |x|, \;\; \textstyle\sum_{c} x_c\, 2^{c} \Big)

Here |x| is the number of presses in x, which the code gets with bit_count(), and the sum is the integer whose bit c is x_c. Pairs are compared in order, first by |x| and then by the sum. HINT shows the first cell of x^{*} in increasing order, and SOLVE shows the whole set20.

21
function SOLVE(board):
    rows, rank ← ELIMINATE(A, board)          # reduce [A | b] mod 2
    for each row r from rank to N − 1:
        if the right-hand side of row r is 1:
            return unsolvable                 # the row reads 0 = 1
    x ← the particular solution, free presses 0
    solutions ← x ⊕ each combination of the NULL-BASIS vectors
    return the solution with the fewest presses

The simple version shows the decision: it either reports the 0 = 1 contradiction or returns the lightest of the solutions. The full version adds the checks and outputs the page uses: the input is validated, the elimination steps are kept only when a trace is requested, and the list of solution sizes is returned along with the presses.

Setting the free presses to 0 gives x_0, and doubling the list adds every null-space combination. The solution with the fewest presses is found by scanning the list once with the key above. This is why the solver never tries all 225 press sets on 5 × 5.

Example. Pressing a1, b2 and d4 on a dark 5 × 5 board lights nine cells. Its four solutions use 3, 13, 13 and 19 presses, and the lightest is a1, b2, d4, which is the press set that made the board.

Random boards

NEW BOARD deals a board with random_board. In solvable mode, each cell is pressed with probability one half, and the chosen presses are applied to a dark board. The result is solvable by construction, because pressing the same set again undoes the presses, since every press is its own inverse22.

In any mode, each light is lit with probability one half, independently of the others. Most of those boards cannot be cleared. A board that is already dark is rejected and drawn again23. The board sizes, 3 to 9 with 5 as the default, are limits set in the board module24.

Keeping the plan valid during play

Pressing a light never calls the server. The page keeps the shown plan valid instead. A press on cell p toggles N(p) on the board, and the plan changes by the single cell p, so the plan is toggled at p rather than solved again25.

26
x' = x \oplus e_p, \qquad b' = b \oplus A\,e_p, \qquad A\,x' = b'

Here e_p is the vector with a 1 only at p, so A\,e_p is the neighbourhood N(p). Since A\,x' = A\,x \oplus A\,e_p = b \oplus A\,e_p = b', the new plan is still a solution. In the code, toggling p means removing it from the plan if it is there and adding it otherwise.

STEP and AUTO press the first cell of the plan, so the plan shrinks by one each time. Their presses count as assists, not as the player's moves27.

Results: board sizes and solution counts

Each value below comes from the solver's elimination on a board with no lit cells, which gives the rank and nullity of A, and from the solvable share 2rank/2N9. Nothing here is tuned or learned.

BoardWhat it measuresValue
3x3Rank of A, nullity, solvable sharerank 9 of 9, nullity 0, all boards solvable, 1 solution
4x4Rank of A, nullity, solvable sharerank 12 of 16, nullity 4, 1 in 16 boards solvable, 16 solutions
5x5 (default)Rank of A, nullity, solvable sharerank 23 of 25, nullity 2, 1 in 4 boards solvable, 4 solutions
6x6, 7x7, 8x8Rank of A, nullity, solvable sharefull rank, nullity 0, all boards solvable, 1 solution
9x9Rank of A, nullity, solvable sharerank 73 of 81, nullity 8, 1 in 256 boards solvable, 256 solutions

Measurements and tests

The 5 × 5 solution sizes were measured by re-running the code: 3,000 solvable boards from random_board(5, random.Random(1), solvable=True). The lightest solution averages 9.89 presses, and the gap between the heaviest and the lightest solution averages 5.19 presses. With seed 0 on the same number of boards, the averages are 9.83 and 5.24, so the averages depend on the sample. No committed result file covers Lights Out28.

The tests compare the solver with brute force. Brute force tries every press set and records the lightest one per board. The check covers all 512 boards of 3 × 3 and 1,500 random boards of 4 × 42930. Another test checks that the 5 × 5 null space has two vectors, that each changes no light, and that a solvable board has exactly 4 solutions31.