Download - The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
The Lights Out Puzzle and Its Variations
Pengfei Li
School of ComputingClemson University
November 30, 2011
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Outline
1 Lights Out ClassicIntroductionSolving the Lights Out Classic
Initial ObservationsLinear Algebraic AnalysisSolving the Puzzle by Hand
2 Variations of the Lights Out PuzzleGeneralizations of the Lights Out
Variations of the Game BoardLights Out 2000
Restrictions on the Lights Out ClassicLit-only Lights OutThe Toggle Game
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Introduction - The Goal and Rules
A lights out game on iPhonea
aImage Source: http://itunes.apple.com
What is the “Lights Out Puzzle”?
An electronic game firstly released in 19955× 5 array of 25 lighted buttonsEach light has 2 states (ON or OFF)The game starts with an initial pattern of lights.
Rules
When one button is toggled, the lights on it and its neighborschange states. (ON → OFF, or OFF → ON)Buttons on the edges/corners have only 3/2 neighbors.
Goal
Turn all lights OFF.Use as few steps of toggling as possible.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Solving the Lights Out Classic (An Example)
Solving a lights out puzzle of the initial pattern with 4 steps.The button with red frame is the one to be toggled in each step.
Pattern 1(initial)
Pattern 2(after step 1)
Pattern 3(after step 2)
Pattern 4(after step 3)
All-OFF Pattern(Done!)
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Questions to Answer...
We might be interested in these questions:
The 25 lights contribute to 225 different patterns in total.
1 Given a random pattern, is it possible to turn all the lights off?
2 If not, how many patterns are solvable?
3 How to solve a pattern if it is solvable?
4 What is the optimal solution? (The one with minimum steps of toggling)
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Initial Observations
What contributes to the light states? (An experiment)
2
33
0 1 0
0
1
1
111
0
1
1
2
5
0 0
00
3
4
2
3
(Odd) � (Even) �
1 Initial state
2 Parity of toggling time of each and its neighbors
Basic Conclusion 1.1
The set of lights affected is irrelevant to the sequence in which the buttons are toggled.
Basic Conclusion 1.2
Toggling a button twice is equivalent to not toggling it at all. (Once is enough!)
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Further Observation
A further conclusion:
Solve Restore
Basic Conclusion 1.3
If one pattern can be solved by toggling a set of buttons, then starting with the all-OFFpattern and toggling the same set of buttons will restore the original one.
Let’s begin the linear algebraic analysis...
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
The Grid Graph & Neighborhood Matrix
Consider the 5× 5 array as a grid graph...
Definition 1.1
The neighborhood matrix of a graph is theadjacency matrix plus an identity matrix ofthe same size: N = A + I.
N =
1 1 0 0 0 1 0 . . . 01 1 1 0 0 0 1 . . . 00 1 1 1 0 0 0 . . . 00 0 1 1 1 0 0 . . . 00 0 0 1 1 0 0 . . . 01 0 0 0 0 1 1 . . . 00 1 0 0 0 1 1 . . . 0...
......
......
......
. . ....
0 0 0 0 0 0 0 . . . 1
All 1s on its diagonal instead of all 0s
Each row/column indicates the lights affected
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Indicator Vectors
Definition 1.2
The toggling indicator vector and pattern indicator vector, denoted x and b respectively,are 25× 1 vectors over Z2 (Z2 = {0, 1}) indicating the sets of buttons to be toggled and thepatterns of the 5× 5 array in row-by-row sequence.
0 0 0 0 00 0 0 0 01 0 0 0 00 1 0 1 00 0 0 0 1
0 0 0 0 01 0 0 0 01 0 0 1 00 1 0 1 00 1 0 0 1
x = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1)T
b = (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1)T
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
What is the product of N and x over Z2?
We use⊕
and⊗
to represent XOR and matrix multiplication over Z2.Suppose x = (1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)T.
N⊗
x =
1 1 0 0 0 1 0 . . . 01 1 1 0 0 0 1 . . . 00 1 1 1 0 0 0 . . . 00 0 1 1 1 0 0 . . . 00 0 0 1 1 0 0 . . . 01 0 0 0 0 1 1 . . . 00 1 0 0 0 1 1 . . . 0...
......
......
......
. . ....
0 0 0 0 0 0 0 . . . 1
1010100...0
=
1100010...0
⊕
0111000...0
⊕
0001100...0
=
1010110...0
The product is the XOR of some columns of N selected by x.It indicates the lights whose states changed (the pattern restored by the toggling of x).
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Basic Conclusion of Solvability
Together with previous conclusions, we have the following:
Theorem 1.1
A pattern of the lights out puzzle is solvable iff there exists a vector x such that N⊗
x = bwhere b is the indicator vector of the pattern.
Now we have reduced the problem to the solvability of a system of linear equations.Although all operations are over Z2, theorems in linear algebra still follow.
(1) Given a random pattern, is it possible to turn all the lights off?No. Since det (N) = 0, the system of linear equations does not always have a solution.
(2), (3) and (4) will be answered after a few more analysis on N...
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Solving the System of Equations N⊗
x = b
We perform elementary row operations on N until it reaches reduced row echelon form E.
N ∼ E =
1 0 0 0 . . . 0 0 10 1 0 0 . . . 0 1 00 0 1 0 . . . 0 1 10 0 0 1 . . . 0 1 0...
......
.... . .
......
...0 0 0 0 . . . 1 1 10 0 0 0 . . . 0 0 00 0 0 0 . . . 0 0 0
=
[I23×23 M23×2O2×23 O2×2
](rank (N) = 23)
N⊗
x = b has a solution ⇔ b belongs to Col N ⇔ b belongs to (Nul NT)⊥
⇔ b belongs to (Nul N)⊥ ⇔ b belongs to (Nul E)⊥ ⇔ b ⊥ an orthogonal basis of Nul E
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Solvability Conclusion by Linear Algebra
An orthogonal basis for Nul E from last 2 columns of E =
[I23×23 M23×2O2×23 O2×2
]Replacing the zero matrix O2×2 with an identity matrix I2×2, we get an orthogonal basis:
n1 = (0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0)T
n2 = (1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1)T
n1 and n2 can be used as test vectors.
Putting all these together, we have:
Theorem 1.2
A pattern of the lights out puzzle is solvable iff b ⊥ n1 and b ⊥ n2 where b is the indicatorvector of the pattern.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Test the Solvability (An Example)
We use⊙
to represent the dot product over Z2. (a ⊥ b⇔ a⊙
b = 0)
c⊙
n1 = 1c⊙
n2 = 0
c 6⊥ n1
m⊙
n1 = 0m⊙
n2 = 0
m ⊥ n1 and m ⊥ n2
The pattern “C” is not solvable, but the pattern “M” is!
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
How Many Patterns Are Solvable?
N⊗
x = b has a solution ⇔ rank (N|b) = rank (N) = 23 (How to satisfy?)
(N|b) =
1 1 0 . . . 0 0 0 b11 1 1 . . . 0 0 0 b20 1 1 . . . 0 0 0 b3...
......
. . ....
......
...0 0 0 . . . 1 1 0 b230 0 0 . . . 1 1 1 b240 0 0 . . . 0 1 1 b25
∼
1 0 0 . . . 0 0 1 e10 1 0 . . . 0 1 0 e20 0 1 . . . 0 1 1 e3...
......
. . ....
......
...0 0 0 . . . 1 1 1 e230 0 0 . . . 0 0 0 00 0 0 . . . 0 0 0 0
If we want rank (N|b) to be 23, and we have assigned b1, b2, . . . , b23 with values of 0s and1s, only one pair of (b24, b25) in {(0, 0), (0, 1), (1, 0), (1, 1)} satisfies!23 of the lights are free, 2 lights are constrained.i.e. Only 1/4 of all patterns are solvable. There are 223 solvable patterns in total.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
How to Solve for x in N⊗
x = b ?
1 Gauss-Jordan elimination on (N|b)
2 N⊗
x = b⇔ x = N−1⊗
b if N is invertible (rank (N) = 23)
A trick: Compute the pseudo-inverse of N:Gauss-Jordan elimination on (N|I)25×50[
N25×25 I25×25]∼[
E25×25 N̂25×25]
Then N̂ is the pseudo-inverse of N.
Algorithm 1.1
For any solvable pattern b, x = N̂⊗
b gives one solution of the pattern.
How to find the optimal solution? This is an O(n2) algorithm. Can we have a better one?
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Stable Togglings
Since n1,n2 ∈ Nul N and Nul N = {x ∈ {0, 1}25 | N⊗
x = 0},n1 and n2 indicate the “stable” togglings!
0 (trivial) n1 n2 n1⊕
n2
These are all the linear combinations of n1 and n2 over Z2. i.e. Only 4 stable togglings exist.For a solvable pattern, there are 4 solutions. The optimal one is one of them.From the all-OFF pattern, every 4 different togglings generate the same pattern.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
How Many Steps Needed at Most?
cba
a d ad
b b d b
d a d a
c b a b c
a
a
bc
b
Partition
n1
n2
n1⊕
n2
Suppose that the optimal solution x has Xbuttons to be toggled. X = A + B + C + Dx⊕
n1 has (8− A) + (8− B) + C + D buttons.X ≤ (8− A) + (8− B) + C + D
X ≤ (8− A) + B + (4− C ) + D
X ≤ A + (8− B) + (4− C ) + D
D ≤ 5
⇒ max (A + B + C + D) = 15
Theorem 1.3
Every solvable pattern of the lights out puzzle canbe solved within 15 steps.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
What Have We Done?
We have answered all the questions.
We know the solvability, the number of solutions and the way to find the optimal one.
But, how to solve the puzzle by hand?
Also, we prefer an algorithm of O(n) than of O(n2).
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Testing the Solvability by Hand
Continue considering the 3 non-trivial stable togglings...
When toggling a button, numbers of the “blue” buttons affected: 0, 2, 4 (Even)i.e. Pattern is solvable ⇒ number of lit buttons overlapped with the “blue” buttons is even.The other direction of this proposition can also be proved.
Actually, only 2 of the 3 non-trivial stable togglings need to be tested.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Testing the Solvability by Hand (An Example)
The Pattern “C”
Test Patterns Overlapped
|{c} ∩ {n1}| = 2 (Even)
|{c} ∩ {n1⊕
n2}| = 5 (Odd)
Result: Not solvable!
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
Chasing the Lights Down
What is the universal way of solving all 223 solvable patterns?
A trick: Chasing the lights Down:
All lit buttons can only be in the last row.In the last row, 3 lights are free, 2 lights are constrained. (if solvable)i.e. We have merged all the 223 cases into 23 = 8 cases.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
The 8 Cases after Chasing
These are the 8 possible last-row patterns after chasing:
The other 25 − 8 = 24 last-row patterns are not solvable!
After chasing, we can decide which case the pattern is in.
In the first case: We have done!
In the other 7 cases: Minutely adjust the pattern to shift it to the first case.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
IntroductionSolving the Lights Out Classic
The Algorithm
Algorithm 1.2
Number the rows 1-5, the columns A-E.
1 Chase the lights down to make all lit lights in row 5.
2 If the light at A5 is on then toggle D1 and E1.
3 If the light at B5 is on then toggle B1 and E1.
4 If the light at C5 is on then toggle D1.
5 Chase the lights down again until all lights off.
6 Delete the redundant buttonsa, and then XOR thesolution with 3 non-trivial stable togglings to get theoptimal one.
aThe buttons been toggled twice
E1D1B1
C5B5A5
This algorithm is in O(n).
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Outline
1 Lights Out ClassicIntroductionSolving the Lights Out Classic
Initial ObservationsLinear Algebraic AnalysisSolving the Puzzle by Hand
2 Variations of the Lights Out PuzzleGeneralizations of the Lights Out
Variations of the Game BoardLights Out 2000
Restrictions on the Lights Out ClassicLit-only Lights OutThe Toggle Game
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
Variations of the Game Board (Examples)
Different game board shapes
Last-Out Lights Out CubeImage Source: http://www.powerstrike.net
Different neighborhood definitions
4 neighbors
8 neighbors
“X” neighbors
“O” neighbors
None of these variations makes the game harder!Just change another neighborhood matrix N and perform the similar analysis.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
Lights Out 2000
T (�) = �T (�) = �T (�) = �
In lights out 2000, each light has 3 states.It is a modulo 3 game instead of modulo 2.
Performing the similar analysis (changing to mod 3), we can get:
There are 22 free lights, 3 constrained lights.
322 = 31, 381, 059, 609 solvable patterns exist.
33 = 27 different stable togglings exist.
Every solvable pattern can be solved within 24 steps.
An O(n2) algorithm by solving a system of linear equations
An O(n) algorithm by “chasing the lights down”
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
Conclusions of the Generalized Lights Out
For any generalizations of the lights out puzzle, if
Number of buttons: m
Number of states of each button: s
Nullitya of the neighborhood matrix: n
Then
Number of free lights: m − n
Number of constrained lights: n
Number of all patterns: sm
Number of solvable patterns: sm−n
Number of different stable togglings: sn
For some game board shapes, we can consider rotations and mirrors to make it simpler.
aNullity is the dimension of the null space
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
Lit-only Lights Out
This game is a little tricky. It is the lights out classic with the restriction:
Restriction 2.1
Only lit buttons are allowed to be toggled:
�⇀ �⇀ �⇀ . . . ⇀ �If you toggle an unlit buttons, the pattern stays unchanged.
Theorem 2.1
A pattern in lit-only lights out can be solved iff it can be solved in lights out classic.
Key points:
1 The toggling sequence is important!
2 We might need to toggle some of the buttons more than once.
3 We might need more steps than solving the same pattern in lights out classic.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
The Algorithm for One Solution
Algorithm 2.1
1 Perform the classic algorithm until you’re not able to take more steps.
2 Find a path from one nearest lit button (source button) if existsa tothe button to be toggled (destination button).
3 Toggle the following buttons:1 The source button2 Buttons on the path (auxiliary buttons) in the path sequence3 The destination button4 The auxiliary buttons in some sequenceb
5 The source button
4 Repeat 1-3 until all lights off.
aIf no lit button exists, we have done!bYou are always able do that. Just keep toggling a lit one then a sequence is found.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
The Algorithm for the Optimal Solution
...
...
...
...
...
...
...
...
The patterns graph
Build a directed patterns graph with 223 nodes.
Each node represents a solvable pattern.
Each edge represents that one pattern is reachable fromanother by one step of toggling.
Only one edge exists between a pair of nodes.
The out-degree of one node is the number of lit buttons.
The out-degree of the all-OFF pattern node is 0.
Algorithm 2.2
Perform Breadth First Search on the patterns graph fromcurrent pattern until the node with 0 out-degree is reached.
This is an O(2n) algorithm!
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
Generalizations of the Lights OutRestrictions on the Lights Out Classic
The Toggle Game
It’s a more difficult game with the following restriction:
Restriction 2.2
You have to alternatively toggle one lit button and one unlit button:
�⇀ �⇀ �⇀ �⇀ . . . ⇀ � or �⇀ �⇀ �⇀ �⇀ . . . ⇀ �If you toggle a wrong button, the pattern stays unchanged.
Key points:
The last step is always toggling a lit pattern to turn all lights off.
The first sequence has an odd number of steps, the second has an even number.
Since all solutions have the same parity, you must decide carefully which button(lit/unlit) for the first step, or you’ll never be able to solve it.
Pengfei Li The Lights Out Puzzle and Its Variations
Lights Out ClassicVariations of the Lights Out Puzzle
References
Marlow Anderson and Todd Feil.Turing lights out with linear algebra.Mathematics Magazine, 71(4):300–303, 1998.
Peter J. Slater Ashok T. Amin and Guo hui Zhang.Parity dimension for graphs - a linear algebraic approach.Linear and Multilinear Algebra, 50(4):327–342, 2002.
Alexander Giffen and Darren B. Parker.On generalizing the “lights out” game and a generalization of parity domination, 2009.
Jaap Scherphuis.Jaap’s puzzle page (lights out).http://www.jaapsch.net/puzzles/lights.htm.[Online; 2010-01-31].
Jaap Scherphuis.Jaap’s puzzle page (the mathematics of lights out).http://www.jaapsch.net/puzzles/lomath.htm.[Online; 2009-09-20].
Pengfei Li The Lights Out Puzzle and Its Variations