1Copyright © Gyora Benedek, 2003
2 Solving Lights Out with BFS by Dr. Gyora Benedek
3Copyright © Gyora Benedek, 2003 The Lights Out game Lights Out™ is a hand-held puzzle game by Tiger Electronics.
4Copyright © 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.Demo
5Copyright © Gyora Benedek, 2003 Example 1a BeforeAfter
6Copyright © Gyora Benedek, 2003 Example 1b AfterBefore
7Copyright © Gyora Benedek, 2003 Example 2a AfterBefore
8Copyright © Gyora Benedek, 2003 Example 2b
9Copyright © Gyora Benedek, 2003 Example 2c
10Copyright © Gyora Benedek, 2003 Example 2d
11Copyright © Gyora Benedek, 2003 Example 2e
12Copyright © Gyora Benedek, 2003 Example 2f
13Copyright © Gyora Benedek, 2003 Example 2g - end
14Copyright © 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).
15Copyright © 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
16Copyright © Gyora Benedek, 2003 Lit Only variant You may only press lit buttons.demo
17Copyright © Gyora Benedek, 2003 Example 3a
18Copyright © Gyora Benedek, 2003 Example 3b
19Copyright © Gyora Benedek, 2003 Example 3c
20Copyright © Gyora Benedek, 2003 Example 3d - end
21Copyright © Gyora Benedek, 2003 Example 4a
22Copyright © Gyora Benedek, 2003 Example 4b
23Copyright © Gyora Benedek, 2003 Example 4c
24Copyright © Gyora Benedek, 2003 Example 4c
25Copyright © Gyora Benedek, 2003 Example 4d
26Copyright © Gyora Benedek, 2003 Example 4e
27Copyright © Gyora Benedek, 2003 Example 4f
28Copyright © Gyora Benedek, 2003 Example 4g
29Copyright © Gyora Benedek, 2003 Example 4h
30Copyright © Gyora Benedek, 2003 Example 4i
31Copyright © Gyora Benedek, 2003 Example 4j
32Copyright © Gyora Benedek, 2003 Example 4k
33Copyright © Gyora Benedek, 2003 Example 4l - end
34Copyright © 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.
35Copyright © 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.
36Copyright © 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.
37Copyright © 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.
38Copyright © Gyora Benedek, 2003 How to solve a position void 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 move if ((NewL=GetSolLen(NewPos))<L){ Pos = NewPos; L = NewL; print move; break; // for }}}}
39Copyright © 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)
40Copyright © 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.
41Copyright © Gyora Benedek, 2003 BFS Queue Open; Dictionary Closed; int L; tPos P1, P2; Open.Enqueue({T,0});// If many sources Closed.Insert(T, 0);// loop to enqueue all While (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);}
42Copyright © Gyora Benedek, 2003 BFS demo 0 Open T AB CD E F P1=? L=?
43Copyright © Gyora Benedek, 2003 BFS demo 0 Open T AB CD E F P1=T L=0
44Copyright © Gyora Benedek, 2003 BFS demo 0 11 Open T AB CD E F A,1 P1=T L=0
45Copyright © Gyora Benedek, 2003 BFS demo 0 11 Open T AB CD E F P1=A L=1
46Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F B,1 C,2 P1=A L=1
47Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F C,2 P1=B L=1
48Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F D,2 P1=C L=2 Note that T and D did not change; they were closed already
49Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F E,2 P1=D L=2
50Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F P1=E L=2 No edges to D.
51Copyright © Gyora Benedek, 2003 BFS demo Open T AB CD E F P1=F L=3 T and F already closed.
52Copyright © 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.
53Copyright © 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.
54Copyright © 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).Demo
55Copyright © Gyora Benedek, 2003 Example 5: step back from T
56Copyright © Gyora Benedek, 2003 Example 5 Now we are 1 step away from T
57Copyright © Gyora Benedek, 2003 Example 5 This is also 1 step away from T…
58Copyright © Gyora Benedek, 2003 Example 5 There are 25 different positions 1 step away from T.
59Copyright © Gyora Benedek, 2003 Example 6: step back… The only possibility
60Copyright © Gyora Benedek, 2003 Example 6: step back… The only position from which we can reach the above position.
61Copyright © 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.
62Copyright © 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.
63Copyright © 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.
64Copyright © 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)
65Copyright © Gyora Benedek, 2003 Optimizations It so happens that all solvable positions are uniquely defined by the 1 st 23 bits. Closed can be 2^23 = 8MBytes ‘only’. By using board symmetry this can be reduced to ~ 1/8; slightly more than 1MBytes.
66Copyright © Gyora Benedek, 2003 Problems High Memory & Time complexity! Not practical for larger boards (6x6). A few years ago even 5x5 was not practical.
67Copyright © Gyora Benedek, 2003