Constraint Propagation, Graph Traversal, and Backtracking Constraint Satisfaction
Total Page:16
File Type:pdf, Size:1020Kb
Solving Sudoku Puzzles Constraint propagation, Graph traversal, and Backtracking Constraint Satisfaction • Given some constraints, can I find a solution? • e.g. Given the contents of my fridge, is there a nutritious dinner to be made? • e.g. Can we find some values such that this equation evaluates to True? A & ~B Constraint Propagation • Can we reduce the number of constraints to our problem? • e.g. We have hour long classes. • 1 at 10, 1 at 10:30, 2 at 11. • How many classrooms will we need? Graph Traversal • One of the most fundamental graph problems is to traverse every edge and vertex in a graph. • For correctness, we must do the traversal in a systematic way so that we don’t miss anything. • For efficiency, we must make sure we visit each edge at most twice. • Since a maze is just a graph, such an algorithm must be powerful enough to enable us to get out of an arbitrary maze. Marking Vertices • The key idea is that we must mark each vertex when we first visit it, and keep track of what have not yet completely explored. • Each vertex will always be in one of the following three states: 1. Undiscovered: the vertex in its initial, untouched state. 2. Discovered: the vertex after we have encountered it, but before we have checked out all its incident edges. 3. Processed: the vertex after we have visited all its incident edges. • A vertex cannot be processed before we discover it, so over the course of the traversal the state of each vertex progresses from undiscovered to discovered to processed. To Do List • We must also maintain a structure containing all the vertices we have discovered but not yet completely explored. • Initially, only a single start vertex is considered to be discovered. • To completely explore a vertex, we look at each edge going out of it. For each edge which goes to an undiscovered vertex, we mark it discovered and add it to the list of work to do. • Note that regardless of what order we fetch the next vertex to explore, each edge is considered exactly twice, when each of its endpoints are explored. In what order should we process vertices? • Breadth first search: First-in, First-out • Depth first search: Last-in, First-out Depth-first Search Depth-first Search http://logicalgenetics.com/solving-sudoku-puzzles-using-depth-first-search/ Backtracking • A general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems. - Wikipedia • Term coined in the 1950’s by D. H. Lehmer Eight Queens Problem • Give all arrangements of eight queens on an 8x8 chessboard so that no queen attacks another. • No two queens are on the same row, column, or diagonal • Problem invented by Max Bezel in 1848 Algorithm Start in the leftmost column If all queens are placed, return true for (every possible choice among the rows in this column): if the queen can be placed safely there, make that choice and then recursively try to place the rest of the queens if recursion successful, return true if ! successful, remove queen and try another row in this column if all rows have been tried and nothing worked, return false to trigger backtracking .