Jordi Cortadella Department of Computer Science

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Introduction to Programming (in C++) Advanced examples Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Sudoku Pseudo code 1.decide the prototype 2.verbalize the solution in terms of a recursive call to the prototype 3.write the code for the recursion being.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
SOLVING SUDOKU WITH MATLAB VERIFICATION FUNCTION correctness verification of the puzzle: checks if the current element appears twice in the same line,
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Announcements.
Data types and their representation Jordi Cortadella Department of Computer Science.
Chars and strings Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Sequences Jordi Cortadella Department of Computer Science.
Jordi Cortadella Dept. of Computer Science, UPC
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Recursion: Backtracking
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Two Dimensional Arrays
Data structure design Jordi Cortadella Department of Computer Science.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
EXAMPLES OF RECURSION Towers of Hanoi Writing Linked Lists Backwards Recursive Insert 8 Queens Recognizing Simple Languages Prefix Expressions Conversion.
Iterators CS Chakrabarti What is an iterator?  Thus far, the only data structure over which we have iterated was the array for (int ix = 0;
Department of Computer Science 1 Recursion & Backtracking 1.The game of NIM 2.Getting out of a maze 3.The 8 Queen’s Problem 4.Sudoku.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Backtracking & Brute Force Optimization Intro2CS – weeks
Vectors Jordi Cortadella Department of Computer Science.
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Satisfiability and SAT Solvers CS 270 Math Foundations of CS Jeremy Johnson.
CompSci Problem Solving: Sudoku  Rules of the Game Sudoku is played with a 9 by 9 "board" consisting of nine 3 by 3 sub-boards. The symbols 1 -
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
Combinatorial problems Jordi Cortadella Department of Computer Science.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Backtracking, Search, Heuristics
Reasoning with invariants
Jordi Cortadella Department of Computer Science
CSCI 104 Backtracking Search
For loops, nested loops and scopes
Data Structures and Algorithms
Combinatorial problems
Sit-In Lab 1 Ob-CHESS-ion
Constraint Satisfaction Problem
Jordi Cortadella Department of Computer Science
Analysis and design of algorithm
adapted from Recursive Backtracking by Mike Scott, UT Austin
Multidimensional Arrays
Search algorithms for vectors
Containers: Queue and List
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
Sudoku.
Recursion as a Problem-Solving Technique
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Introduction to Programming (in C++) Advanced examples
Presentation transcript:

Jordi Cortadella Department of Computer Science Sudoku Jordi Cortadella Department of Computer Science

Solving a Sudoku Input 530070000 600195000 098000060 800060003 400803001 700020006 060000280 000419005 000080079 Is it correct (conflict free)? Is it complete (no empty cells)? If incomplete, can we find a solution? Introduction to Programming © Dept. CS, UPC

Sudoku: data structures // A 9x9 matrix to store values. // The matrix contains values in {0,…,9}, // where 0 means “empty”. using Grid = vector< vector<int> >; Introduction to Programming © Dept. CS, UPC

Detection of conflicts  1 2 3 4 5 6 7 8 9 Boolean matrix for row conflicts 5 3 7 6 1 9 5 9 8 6 8 6 3 4 8 3 1 7 2 6 6 2 8 4 1 9 5 8 7 9 Similar matrices can be used for column and square conflicts // A 9x10 Boolean matrix to indicate used values. // The 0-column is not used. using Used = vector< vector<bool> >; Introduction to Programming © Dept. CS, UPC

Sudoku: data structures struct Sudoku { Grid G; // the main grid Used Rows; // conflicts for rows Used Columns; // conflicts for columns Used Squares; // conflicts for squares }; // Post: The Sudoku initialized as empty. void initSudoku(Sudoku& S) { S.G = Grid(9, vector<int>(9, 0)); S.Rows = Used(9, vector<bool>(10, false)); S.Columns = Used(9, vector<bool>(10, false)); S.Squares = Used(9, vector<bool>(10, false)); } Introduction to Programming © Dept. CS, UPC

Sudoku: indexing the Used matrices struct Sudoku { Grid G; // the main grid Used Rows; // conflicts for rows Used Columns; // conflicts for columns Used Squares; // conflicts for squares }; Sudoku S; 1 2 3 4 5 6 7 8 v row r, column c, value v For row conflicts  S.Rows[r][v] For column conflicts  S.Columns[c][v] For square conflicts  S.Squares[3(r/3) + c/3][v] Introduction to Programming © Dept. CS, UPC

Sudoku: writing a cell struct Sudoku { Grid G; // the main grid Used Rows; // conflicts for rows Used Columns; // conflicts for columns Used Squares; // conflicts for squares }; // Pre: S is a Sudoku partially filled in. // Post: Cell (r,c) is filled in with value v if no conflict is // produced. The Sudoku is not changed in case of conflict. // Returns true if no conflict, or false otherwise. bool writeCell(Sudoku& S, int r, int c, int v) { int sq = 3(r/3) + c/3; if (S.Rows[r][v] or S.Columns[c][v] or S.Squares[sq][v]) return false; S.Rows[r][v] = S.Columns[c][v] = S.Squares[sq][v] = true; S.G[r][c] = v; return true; } Introduction to Programming © Dept. CS, UPC

Reading a Sudoku 530070000 600195000 098000060 800060003 400803001 700020006 060000280 000419005 000080079 Introduction to Programming © Dept. CS, UPC

Reading a Sudoku // Pre: the input has 81 digits in {‘0’,…,‘9’} // Post: The Sudoku S has been read from cin. // Returns true if the Sudoku is correct, or false otherwise. bool readSudoku(Sudoku& S) { initSudoku(S); // empty Sudoku for (int r = 0; r < 9; ++r) { // Read all rows and columns for (int c = 0; c < 9; ++c) { char digit; cin >> digit; int n = int(digit - '0'); // Convert to int if (n != 0 and not writeCell(S, r, c, n)) return false; } } return true; // Correct sudoku } Introduction to Programming © Dept. CS, UPC

Is the Sudoku complete? // Pre: S is a correct Sudoku. // Returns true if the Sudoku is complete (no empty cells), // or false otherwise. bool completeSudoku(const Sudoku& S) { for (int r = 0; r < 9; ++r) { for (int c = 0; c < 9; ++c) { if (S.G[r][c] == 0) return false; // Empty cell } } return true; // Complete Sudoku } Introduction to Programming © Dept. CS, UPC

Main program // The program reads a Sudoku and reports // whether it is correct and complete. int main() { Sudoku S; if (not readSudoku(S)) { cout << “The Sudoku is incorrect.” << endl; return 1; } if (not completeSudoku(S)) { cout << “The Sudoku is not complete.” << endl; return 1; } cout << “The Sudoku is complete and correct.” << endl; } Introduction to Programming © Dept. CS, UPC

Challenge: solving an incomplete Sudoku Introduction to Programming © Dept. CS, UPC

Main program // The program reads a Sudoku and tries to solve it. // In case it is solvable, it writes a solution. int main() { Sudoku S; if (not readSudoku(S)) { cout << “The Sudoku is incorrect.” << endl; return 1; } if (not solveSudoku(S)) { cout << “The Sudoku has no valid solution.” << endl; return 1; } writeSudoku(S); } Introduction to Programming © Dept. CS, UPC

Write a Sudoku // Pre: S is a complete Sudoku. // Post: The Sudoku has been printed into cout. void writeSudoku(const Sudoku& S) { for (int r = 0; r < 9; ++r) { for (int c = 0; c < 9; ++c) cout << S.G[r][c]; cout << endl; } } Introduction to Programming © Dept. CS, UPC

Number of Sudokus 1 3 4 5 6 7 8 9 r=0, c=0 2 1 3 4 5 6 7 8 9 r=0, c=1    1 3 4 5 7 8 9    r=0, c=2 6,670,903,752,021,072,936,960  6.67  1021 Introduction to Programming © Dept. CS, UPC

Solving a Sudoku Doing progress from a partially filled Sudoku    1 (r=2,c=3) (r=2,c=0) (r=2,c=4) (r=2,c=5) 4 6 8 9 1 2 1 2 3 7 2 3 4 8 1 3 1 2 1 1 3 3 2 2 9 4 1 2 1 2 3 9 1 2 3 4    1 2       Introduction to Programming © Dept. CS, UPC

Solving a Sudoku Doing progress from a partially filled Sudoku    1 (r=2,c=3) (r=2,c=0) (r=2,c=4) (r=2,c=5) 4 6 8 9 1 2 1 2 3 7 2 3 4 8 1 3 1 2 1 1 3 3 2 2 9 4 1 2 1 2 3 9 1 2 3 4    1 2       Introduction to Programming © Dept. CS, UPC

Erasing a cell // Pre: The Sudoku S has a value in cell (r,c). // Post: Cell (r,c) has been erased. void eraseCell(Sudoku& S, int r, int c) { int v = S.G[r][c]; // Gets the value S.G[r][c] = 0; // Erases the value // Cleans the conflict matrices for the value int sq = 3*(r/3) + c/3; S.Rows[r][v] = S.Columns[c][v] = S.Squares[sq][v] = false; } Introduction to Programming © Dept. CS, UPC

Recursive Sudoku // Pre: S has been filled in up to cell (r,c) without conflicts, without including cell (r,c). // Returns true if the Sudoku is solvable with the // pre-filled cells, or false otherwise. // Post: S contains a solution if the Sudoku is solvable. // S is not modified if the Sudoku is unsolvable. bool solveSudokuRec(Sudoku& S, int r, int c); (r,c) Cells are filled in by rows, starting from cell (0,0). Introduction to Programming © Dept. CS, UPC

Recursive Sudoku Basic case: (r=9, c=0) The Sudoku has been completed without conflicts. A solution has been found! We are done (return true). (r=9,c=0) Introduction to Programming © Dept. CS, UPC

Recursive Sudoku Simple case: cell is not empty Don’t touch and solve from the next cell 4 4 Introduction to Programming © Dept. CS, UPC

Recursive Sudoku Difficult case: cell is empty Write 1 in (r,c) and check for conflicts. If no conflict, solve from next cell. If successful, done! (return true) else, erase 1, write 2 in (r,c) and check for conflicts. If no conflict, solve from next cell. If successful, done! (return true) … else, erase 8, write 9 in (r,c) and check for conflicts. If no conflict, solve from next cell. If successful, done! (return true) else, failure (return false). ? Introduction to Programming © Dept. CS, UPC

Recursive Sudoku bool solveSudokuRec(Sudoku& S, int r, int c) { if (r == 9) return true; // Yupee! (Sudoku completed) int next_r = r + c/8; // Next row (increase when c=8) int next_c = (c + 1)%9; // Next column (0 if c+1=9) // If cell is not empty, don’t touch and go to next cell if (S.G[r][c] != 0) return solveSudokuRec(S, next_r, next_c); // Try all possible values from 1 to 9 for (int v = 1; v <= 9; ++v) { if (writeCell(S, r, c, v)) { if (solveSudokuRec(S, next_r, next_c)) return true; // Yupee! eraseCell(S, r, c); // Backtrack } } return false; } Introduction to Programming © Dept. CS, UPC

Back to main program int main() { Sudoku S; if (not readSudoku(S)) { cout << “The Sudoku is incorrect.” << endl; return 1; } if (not solveSudoku(S)) { cout << “The Sudoku cannot be solved.” << endl; return 1; } writeSudoku(S); } // Pre: S is a correct and possibly incomplete Sudoku. // Returns true if the Sudoku is solvable, or false otherwise. // Post: If solvable, the S contains a solution. bool solveSudoku(Sudoku& S) { return solveSudokuRec(S, 0, 0); } Introduction to Programming © Dept. CS, UPC

Try from here Source: http://www.7sudoku.com/very-difficult Introduction to Programming © Dept. CS, UPC

The 8 queens puzzle Place eight queens on an 8×8 chessboard so that no two queens threaten each other Introduction to Programming © Dept. CS, UPC

Generalization to n queens Introduction to Programming © Dept. CS, UPC

The 8 queens in action Introduction to Programming © Dept. CS, UPC

Solving a maze S D Introduction to Programming © Dept. CS, UPC

Summary Backtracking is a common technique used to solve constraint satisfaction problems. Strategy: build partial solutions and backtrack when some constraint is not satisfied. Backtracking avoids the exhaustive enumeration of all possible solutions. Introduction to Programming © Dept. CS, UPC