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

Slides:



Advertisements
Similar presentations
Maximum flow Main goals of the lecture:
Advertisements

Review: Search problem formulation
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
Graph & BFS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Representing and Using Graphs
Connected Components Fun with graphs, searching, and queues.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Review: Tree search Initialize the frontier using the starting state While the frontier is not empty – Choose a frontier node to expand according to search.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Lecture 3: Uninformed Search
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Ricochet Robots Mitch Powell Daniel Tilgner. Abstract Ricochet robots is a board game created in Germany in A player is given 30 seconds to find.
COSC 2007 Data Structures II
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Review: Tree search Initialize the frontier using the starting state
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
CSE 421: Introduction to Algorithms
Graph & BFS.
Graphs Representation, BFS, DFS
Concepts of Computation
Presentation transcript:

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