Upload
scarlett-perry
View
222
Download
4
Embed Size (px)
Citation preview
1 Copyright © Gyora Benedek, 2003
2 Copyright © Gyora Benedek, 2003
Solving Lights Out with BFS
by Dr. Gyora Benedek
3 Copyright © Gyora Benedek, 2003
The Lights Out game
• Lights Out™ is a hand-held puzzle game by Tiger Electronics.
4 Copyright © Gyora Benedek, 2003
Basic definitions• Lights Out consists of a 5 by 5 grid of buttons
which also have lights in them.• By pressing a button, its light and those of the
(non-diagonally) adjacent buttons will change. • Given a pattern of lights, you have to switch
them all off by pressing the correct buttons. • Try to do it in as few moves as possible.
DemoDemo
5 Copyright © Gyora Benedek, 2003
Example 1a
Before After
6 Copyright © Gyora Benedek, 2003
Example 1b
AfterBefore
7 Copyright © Gyora Benedek, 2003
Example 2a
AfterBefore
8 Copyright © Gyora Benedek, 2003
Example 2b
9 Copyright © Gyora Benedek, 2003
Example 2c
10 Copyright © Gyora Benedek, 2003
Example 2d
11 Copyright © Gyora Benedek, 2003
Example 2e
12 Copyright © Gyora Benedek, 2003
Example 2f
13 Copyright © Gyora Benedek, 2003
Example 2g - end
14 Copyright © Gyora Benedek, 2003
Notes
• Notation: Let lit=1; unlit=0;
• There are 2^25 positions – only 2^23 have solutions.
• Repeating same move = undo.
• Moves order does not matter.
• Can be solved with Linear Algebra (beyond our scope).
15 Copyright © Gyora Benedek, 2003
Many Variants
• Different board sizes
• Different neighborhood definitions
• 3 or more colors
• Restrictions on moves
Links to Solutions, Tricks, Research papers, Algorithms, etc
• http://www.geocities.com/jaapsch/puzzles/lights.htm#desc
• http://qcunix1.qc.edu/~lteitelm/algs/lo_links.html
16 Copyright © Gyora Benedek, 2003
Lit Only variant
• You may only press lit buttons.
demodemo
17 Copyright © Gyora Benedek, 2003
Example 3a
18 Copyright © Gyora Benedek, 2003
Example 3b
19 Copyright © Gyora Benedek, 2003
Example 3c
20 Copyright © Gyora Benedek, 2003
Example 3d - end
21 Copyright © Gyora Benedek, 2003
Example 4a
22 Copyright © Gyora Benedek, 2003
Example 4b
23 Copyright © Gyora Benedek, 2003
Example 4c
24 Copyright © Gyora Benedek, 2003
Example 4c
25 Copyright © Gyora Benedek, 2003
Example 4d
26 Copyright © Gyora Benedek, 2003
Example 4e
27 Copyright © Gyora Benedek, 2003
Example 4f
28 Copyright © Gyora Benedek, 2003
Example 4g
29 Copyright © Gyora Benedek, 2003
Example 4h
30 Copyright © Gyora Benedek, 2003
Example 4i
31 Copyright © Gyora Benedek, 2003
Example 4j
32 Copyright © Gyora Benedek, 2003
Example 4k
33 Copyright © Gyora Benedek, 2003
Example 4l - end
34 Copyright © Gyora Benedek, 2003
Lit Only variant
• You may only press lit buttons.
• Moves’ order does matter.
• The set of “Lit Only solvable puzzles” is obviously a subset of “solvable puzzles”.
• Can be proved that all solvable puzzles are Lit Only solvable.
• Lit Only solution may be longer.
35 Copyright © Gyora Benedek, 2003
How to solve Lit Only?
• We want to be able to compute an optimal solution to a given puzzle efficiently.
• We can make a lengthy preparation step and use a lot of memory.
• Preparation puts all positions in a dictionary: key = position (25 bits)value= solution length (8 bits)
• GetSolLen(pos) returns solution length for pos.
36 Copyright © Gyora Benedek, 2003
Background
• The Lit Only game corresponds to a directed graph G where each vertex is a position and the moves are the edges.
• We set one vertex (all 0 position) as the target T and are interested in the distance of each vertex from T.
• Vertices (positions) from which there is no path to T are unsolvable.
37 Copyright © Gyora Benedek, 2003
Background (2)
• Usually a graph is given explicitly.
• Here the edges are implicit, but given a vertex we can find all the out edges and also all the in edges.
38 Copyright © Gyora Benedek, 2003
How to solve a positionvoid Solve(tPos Pos){int NewL, L = GetSolLen(Pos); tPos NewPos;
if (L==NoSolution) {print “No Solution”; return;}while (L>0){
for all valid moves let NewPos be result of moveif ((NewL=GetSolLen(NewPos))<L){
Pos = NewPos; L = NewL; print move;break; // for
} } } }
39 Copyright © Gyora Benedek, 2003
Notes on Solve
• Moves are printed from left to right.
• Assuming that GetSolLen( ) returns correct values, L will decrease exactly by 1 in each iteration of the while loop.
• Time complexity O(nMoves · SolutionLen)here nMoves=25 so we get O(SolutionLen)
40 Copyright © Gyora Benedek, 2003
Preparation
• To fill the dictionary we start from the target T (all 0) and go backwards.
• We use BFS (Breadth First Search) on G with its edges reversed.
41 Copyright © Gyora Benedek, 2003
BFS
Queue Open; Dictionary Closed; int L; tPos P1, P2;Open.Enqueue({T,0}); // If many sourcesClosed.Insert(T, 0); // loop to enqueue allWhile (not Open.IsEmpty( )){
{P1,L} = Open.Dequeue( );For each edge (P1,P2)
if (not Closed.Find(P2)){Open.Enqueue({P2,L+1});Closed.Insert(P2,L+1);
} }
42 Copyright © Gyora Benedek, 2003
BFS demo
0 OpenT
A B
C D E
F
T,0
@While
P1=?
L=?
43 Copyright © Gyora Benedek, 2003
BFS demo
0 OpenT
A B
C D E
F
empty
@For
P1=T
L=0
44 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
OpenT
A B
C D E
F
A,1B,1
@While
P1=T
L=0
45 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
OpenT
A B
C D E
F
B,1
@For
P1=A
L=1
46 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2
OpenT
A B
C D E
F
B,1
C,2
D,2
@While
P1=A
L=1
47 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2
OpenT
A B
C D E
F
C,2
D,2
@For
P1=B
L=1
48 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2 2
OpenT
A B
C D E
F
D,2
E,2
@For
P1=C
L=2
Note that T and D did not change; they were closed already
49 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2 2
3
OpenT
A B
C D E
F
E,2
F,3
@For
P1=D
L=2
50 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2 2
3
OpenT
A B
C D E
F
F,3
@For
P1=E
L=2
No edges to D.
51 Copyright © Gyora Benedek, 2003
BFS demo
0
1 1
2 2 2
3
OpenT
A B
C D E
F
empty
@For
P1=F
L=3
T and F already closed.
52 Copyright © Gyora Benedek, 2003
BFS correctness
• When a vertex {v, L} is added to Closed, L is the shortest distance from one of the sources to v. There is no need to modify them anymore.
• At any time in the Open queue there are vertexes with L and L+1 only, so that those with L are before those with L+1.
53 Copyright © Gyora Benedek, 2003
BFS Complexity
• Each node enters Closed at most once.
• Each edge is traveled at most once.
• Time complexity = O(m)Where m is the number of edges in the relevant connected component; assuming O(1) for each operation on the dictionary.
54 Copyright © Gyora Benedek, 2003
BFS for Lit Only
• Given a position P1 we need to find all edges (P2,P1) in G.
• To get a P2 press an unlit button in P1.
• To find all positions from which P1 can be reached, press all unlit buttons in P1 (starting from P1 each time).
DemoDemo
55 Copyright © Gyora Benedek, 2003
Example 5: step back from T
56 Copyright © Gyora Benedek, 2003
Example 5
Now we are 1 step away from T
57 Copyright © Gyora Benedek, 2003
Example 5
This is also 1 step away from T…
58 Copyright © Gyora Benedek, 2003
Example 5
• There are 25 different positions 1 step away from T.
59 Copyright © Gyora Benedek, 2003
Example 6: step back…
The only possibility
60 Copyright © Gyora Benedek, 2003
Example 6: step back…
The only position from which we can reach the above position.
61 Copyright © Gyora Benedek, 2003
Implementing Closed
• A Hash table?
• Actually there are 2^25 =32M positions. An array of 2^25 Bytes with Pos as index will do.
• Init: For all i do Closed[i]=255;
• Max distance must be < 255.
62 Copyright © Gyora Benedek, 2003
Implementing Open
• Option1: a queue containing pos (25 bits) and L (8 bits). This may be very large (~ 4*2^23 ).
• Option2: no queue. Run on Closed and look for each pos with L. When done L++.The order in which positions are handled may be different, the result the same.Takes long time when few positions per L.
63 Copyright © Gyora Benedek, 2003
Implementing Open (2)
• Option3: Combine the two options. Implement a queue in a relatively short array. Use this queue as in Option1. If this array is full revert to Option2.
• Limited memory overhead, saves time when queue is not full.
64 Copyright © Gyora Benedek, 2003
Time Complexity
• Option1: O(nPositions + nMoves·nSolvablePositions) = O(n+m)=c · (2^25+25·2^23)
• Option2:O(nPositions · MaxSolutionLen + nMoves · nSolvablePositions)
65 Copyright © Gyora Benedek, 2003
Optimizations
• It so happens that all solvable positions are uniquely defined by the 1st 23 bits. Closed can be 2^23 = 8MBytes ‘only’.
• By using board symmetry this can be reduced to ~ 1/8; slightly more than 1MBytes.
66 Copyright © Gyora Benedek, 2003
Problems
• High Memory & Time complexity!
• Not practical for larger boards (6x6).
• A few years ago even 5x5 was not practical.
67 Copyright © Gyora Benedek, 2003