Presentation is loading. Please wait.

Presentation is loading. Please wait.

1Copyright © Gyora Benedek, 2003 2 Solving Lights Out with BFS by Dr. Gyora Benedek.

Similar presentations


Presentation on theme: "1Copyright © Gyora Benedek, 2003 2 Solving Lights Out with BFS by Dr. Gyora Benedek."— Presentation transcript:

1

2 1Copyright © Gyora Benedek, 2003

3 2 Solving Lights Out with BFS by Dr. Gyora Benedek

4 3Copyright © Gyora Benedek, 2003 The Lights Out game Lights Out™ is a hand-held puzzle game by Tiger Electronics.

5 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

6 5Copyright © Gyora Benedek, 2003 Example 1a BeforeAfter

7 6Copyright © Gyora Benedek, 2003 Example 1b AfterBefore

8 7Copyright © Gyora Benedek, 2003 Example 2a AfterBefore

9 8Copyright © Gyora Benedek, 2003 Example 2b

10 9Copyright © Gyora Benedek, 2003 Example 2c

11 10Copyright © Gyora Benedek, 2003 Example 2d

12 11Copyright © Gyora Benedek, 2003 Example 2e

13 12Copyright © Gyora Benedek, 2003 Example 2f

14 13Copyright © Gyora Benedek, 2003 Example 2g - end

15 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).

16 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 http://www.geocities.com/jaapsch/puzzles/lights.htm#desc http://qcunix1.qc.edu/~lteitelm/algs/lo_links.html

17 16Copyright © Gyora Benedek, 2003 Lit Only variant You may only press lit buttons.demo

18 17Copyright © Gyora Benedek, 2003 Example 3a

19 18Copyright © Gyora Benedek, 2003 Example 3b

20 19Copyright © Gyora Benedek, 2003 Example 3c

21 20Copyright © Gyora Benedek, 2003 Example 3d - end

22 21Copyright © Gyora Benedek, 2003 Example 4a

23 22Copyright © Gyora Benedek, 2003 Example 4b

24 23Copyright © Gyora Benedek, 2003 Example 4c

25 24Copyright © Gyora Benedek, 2003 Example 4c

26 25Copyright © Gyora Benedek, 2003 Example 4d

27 26Copyright © Gyora Benedek, 2003 Example 4e

28 27Copyright © Gyora Benedek, 2003 Example 4f

29 28Copyright © Gyora Benedek, 2003 Example 4g

30 29Copyright © Gyora Benedek, 2003 Example 4h

31 30Copyright © Gyora Benedek, 2003 Example 4i

32 31Copyright © Gyora Benedek, 2003 Example 4j

33 32Copyright © Gyora Benedek, 2003 Example 4k

34 33Copyright © Gyora Benedek, 2003 Example 4l - end

35 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.

36 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.

37 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.

38 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.

39 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 }}}}

40 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)

41 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.

42 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);}

43 42Copyright © Gyora Benedek, 2003 BFS demo 0 Open T AB CD E F T,0 @While P1=? L=?

44 43Copyright © Gyora Benedek, 2003 BFS demo 0 Open T AB CD E F empty @For P1=T L=0

45 44Copyright © Gyora Benedek, 2003 BFS demo 0 11 Open T AB CD E F A,1 B,1 @While P1=T L=0

46 45Copyright © Gyora Benedek, 2003 BFS demo 0 11 Open T AB CD E F B,1 @For P1=A L=1

47 46Copyright © Gyora Benedek, 2003 BFS demo 0 11 22 Open T AB CD E F B,1 C,2 D,2 @While P1=A L=1

48 47Copyright © Gyora Benedek, 2003 BFS demo 0 11 22 Open T AB CD E F C,2 D,2 @For P1=B L=1

49 48Copyright © Gyora Benedek, 2003 BFS demo 0 11 222 Open T AB CD E F D,2 E,2 @For P1=C L=2 Note that T and D did not change; they were closed already

50 49Copyright © Gyora Benedek, 2003 BFS demo 0 11 222 3 Open T AB CD E F E,2 F,3 @For P1=D L=2

51 50Copyright © Gyora Benedek, 2003 BFS demo 0 11 222 3 Open T AB CD E F F,3 @For P1=E L=2 No edges to D.

52 51Copyright © Gyora Benedek, 2003 BFS demo 0 11 222 3 Open T AB CD E F empty @For P1=F L=3 T and F already closed.

53 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.

54 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.

55 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

56 55Copyright © Gyora Benedek, 2003 Example 5: step back from T

57 56Copyright © Gyora Benedek, 2003 Example 5 Now we are 1 step away from T

58 57Copyright © Gyora Benedek, 2003 Example 5 This is also 1 step away from T…

59 58Copyright © Gyora Benedek, 2003 Example 5 There are 25 different positions 1 step away from T.

60 59Copyright © Gyora Benedek, 2003 Example 6: step back… The only possibility

61 60Copyright © Gyora Benedek, 2003 Example 6: step back… The only position from which we can reach the above position.

62 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.

63 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.

64 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.

65 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)

66 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.

67 66Copyright © Gyora Benedek, 2003 Problems High Memory & Time complexity! Not practical for larger boards (6x6). A few years ago even 5x5 was not practical.

68 67Copyright © Gyora Benedek, 2003


Download ppt "1Copyright © Gyora Benedek, 2003 2 Solving Lights Out with BFS by Dr. Gyora Benedek."

Similar presentations


Ads by Google