Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9:

Slides:



Advertisements
Similar presentations
Can you master it?. Sudoku Hype Sudoku is a logic-based number placement puzzle. The objective is to fill a 9×9 grid so that each column, each row, and.
Advertisements

Nonnegative Matrix Factorization with Sparseness Constraints S. Race MA591R.
CREATED BY: CALVIN WILLIAMS Bidirectional Search.
Linear Algebra Applications in Matlab ME 303. Special Characters and Matlab Functions.
Number puzzles in the paper Did you know there’s a whole range of number puzzles you can try in most newspapers? Click on the puzzles below to complete.
Playing Fair at Sudoku Joshua Cooper USC Department of Mathematics.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
MATH 685/ CSI 700/ OR 682 Lecture Notes
Solving Systems of Linear Equations Part Pivot a Matrix 2. Gaussian Elimination Method 3. Infinitely Many Solutions 4. Inconsistent System 5. Geometric.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Motion Analysis (contd.) Slides are from RPI Registration Class.
MAE 552 – Heuristic Optimization Lecture 26 April 1, 2002 Topic:Branch and Bound.
SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)
LIAL HORNSBY SCHNEIDER
Copyright © Cengage Learning. All rights reserved. 7.6 The Inverse of a Square Matrix.
SOLVING THE KAKURO PUZZLE Andreea Erciulescu Department of Mathematics, Colorado State University, Fort Collins (Mentor: A. Hulpke)
Matrices Write and Augmented Matrix of a system of Linear Equations Write the system from the augmented matrix Solve Systems of Linear Equations using.
Solving Sudoku Mark PTTLS Micro teach. Solving Sudoku What is a Sudoku? A Sudoku is a number puzzle based on a 9x9 grid made up from smaller 3x3 blocks.
Mathematical Methods In Solving Sudoku Puzzles By: Cory Trenkamp Wright State University May 24, 2006.
CS194 Project: Sudoku Difficulty Kha Chu Advisor: Professor Sahai.
SOLVING SUDOKU WITH MATLAB VERIFICATION FUNCTION correctness verification of the puzzle: checks if the current element appears twice in the same line,
Copyright © 2011 Kristina Fornažar Agić. Look! This column already has numbers 4, 3 and 1. 2 The missing number is 2. Now we’ll fill the blank spot with.
Section 3.6: An Introduction to Cryptography
Row Reduction Method Lesson 6.4.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Practical Dynamic Programming in Ljungqvist – Sargent (2004) Presented by Edson Silveira Sobrinho for Dynamic Macro class University of Houston Economics.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Whiteboardmaths.com © 2008 All rights reserved
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 Solver Comparison A comparative analysis of algorithms for solving Sudoku.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
N-space Snakes are special maximal length loops through an N-space cube. They ’ re full of intriguing symmetries, puzzles and surprises. They ’ re simple.
Sudoku Solver Cs671 Matt Lehmann. Algorithm Selected Local Hill Climbing/Distributed Breakout Not-complete Not guaranteed to terminate(infinite loops)
1 Project Management Example Solving Sudoku 2 What is Sudoku?  Sudoku is a game with 9 columns and 9 rows and 9 “boxes” composed of a 3 x 3 Grid  Numbers.
4 © 2012 Pearson Education, Inc. Vector Spaces 4.4 COORDINATE SYSTEMS.
The parity bits of linear block codes are linear combination of the message. Therefore, we can represent the encoder by a linear system described by matrices.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
How Do I Solve This Thing?. You are familiar with Sudoku puzzles. For this variation, place the integers 1 – 9 into each row or column such that the.
Backtracking & Brute Force Optimization Intro2CS – weeks
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
CSG3F3/ Desain dan Analisis Algoritma
Problem Solving: Brute Force Approaches
Monster (“Mega”) Sudoku
Database Normalization
A CIS 5603 Course Project By: Qizhong Mao, Baiyi Tao, Arif Aziz
CSCI 104 Backtracking Search
Data Structures and Algorithms
Perturbation method, lexicographic method
Sit-In Lab 1 Ob-CHESS-ion
Review Graph Directed Graph Undirected Graph Sub-Graph
Algorithm Analysis CSE 2011 Winter September 2018.
Lecture 3: Symmetric Key Encryption
Problem Solving: Brute Force Approaches
Graph Paper Programming
Unit-2 Divide and Conquer
Graph Paper Programming
Modeling Sudoku as a CNF Formula
4. Computational Problem Solving
adapted from Recursive Backtracking by Mike Scott, UT Austin
Basics of Recursion Programming with Recursion
A General Backtracking Algorithm
Sudoku.
Modeling Sudoku as a CNF Formula
Basic circuit analysis and design
Requirements Analysis and Negotiation
The Yelp Dataset Challenge
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Matrix Algebra THE INVERSE OF A MATRIX © 2012 Pearson Education, Inc.
Presentation transcript:

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 5 6 9 7 8 4 4 2 5

What number must go here? Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 5 6 9 7 8 4 4 2 5 What number must go here?

1, as 2 and 3 already appear in this column. Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 5 6 9 7 8 4 4 2 5 1, as 2 and 3 already appear in this column. 1

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 1 5 6 9 7 8 4 4 2 5

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 1 5 6 9 7 8 4 4 2 5 3

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 1 5 3 6 9 7 8 4 4 2 5

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 1 5 3 6 9 7 8 4 4 2 5 2

Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 1 3 8 5 7 6 1 3 9 8 1 2 5 7 3 1 8 9 8 2 1 5 2 3 6 9 7 8 4 4 2 5 And so on…

The unique solution for this easy puzzle. Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9: 2 4 9 5 7 1 6 3 8 8 6 1 4 3 2 9 7 5 5 7 3 9 8 6 1 4 2 7 2 5 6 9 8 4 1 3 6 9 8 1 4 3 2 5 7 3 1 4 7 2 5 8 6 9 9 3 7 8 1 4 2 5 6 1 5 2 3 6 9 7 8 4 4 8 6 2 5 7 3 9 1 The unique solution for this easy puzzle.

This Talk We show how to develop a program that can solve any Sudoku puzzle in an instant; Start with a simple but impractical program, which is improved in a series of steps; Emphasis on pictures rather than code, plus some lessons about algorithm design.

Representing a Grid type Grid = Matrix Char type Matrix a = [Row a] type Row a = [a] A grid is essentially a list of lists, but matrices and rows will be useful later on.

Extracting Rows 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 rows :: Matrix a  [Row a] rows m = m

… Columns 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 cols :: Matrix a  [Row a] cols m = transpose m

… And Boxes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16 boxs :: Matrix a  [Row a] boxs m = <omitted>

A direct implementation, without concern for efficiency. Validity Checking Let us say that a grid is valid if it has no duplicate entries in any row, column or box: valid :: Grid  Bool valid g = all nodups (rows g)  all nodups (cols g)  all nodups (boxs g) A direct implementation, without concern for efficiency.

Making Choices Replace each blank square in a grid by all possible numbers 1 to 9 for that square: 3 4 1 2 3 4 5 6 3 7 8 9 4 4 5 6 choices :: Grid  Matrix [Char]

Collapsing Choices Transform a matrix of lists into a list of matrices by considering all combinations of choices: 1 3 4 1 2 3 4 2 1 2 3 4 1 2 collapse :: Matrix [a]  [Matrix a]

A Brute Force Solver solve :: Grid  [Grid] solve = filter valid . collapse . choices Consider all possible choices for each blank square, collapse the resulting matrix, then filter out the valid grids.

Simple, but impractical! Does It Work? The easy example has 51 blank squares, resulting in 951 grids to consider, which is a huge number: 4638397686588101979328150167890591454318967698009 Simple, but impractical! > solve easy ERROR: out of memory

Reducing The Search Space Many choices that are considered will conflict with entries provided in the initial grid; For example, an initial entry of 1 precludes another 1 in the same row, column or box; Pruning such invalid choices before collapsing will considerably reduce the search space.

Pruning Remove all choices that occur as single entries in the corresponding row, column or box: 1 1 2 4 1 3 3 4 1 2 4 3 3 4 prune :: Matrix [Char]  Matrix [Char]

And Again Pruning may leave new single entries, so it makes sense to iterate the pruning process: 1 2 4 3 3 4

And Again Pruning may leave new single entries, so it makes sense to iterate the pruning process: 1 2 4 3 4

And Again Pruning may leave new single entries, so it makes sense to iterate the pruning process: 1 2 3 4

We have now reached a fixpoint of the pruning function. And Again Pruning may leave new single entries, so it makes sense to iterate the pruning process: 1 2 3 4 We have now reached a fixpoint of the pruning function.

An Improved Solver solve’ :: Grid  [Grid] solve’ = filter valid . collapse . fix prune . choices For the easy example, the pruning process alone is enough to completely solve the puzzle: Terminates instantly! > solve’ easy

No solution after two hours - we need to think further! But… For a gentle example, pruning leaves around 381 grids to consider, which is still a huge number: 443426488243037769948249630619149892803 No solution after two hours - we need to think further! > solve' gentle

Reducing The Search Space After pruning there may still be many choices that can never lead to a solution; But such bad choices will be duplicated many times during the collapsing process; Discarding these bad choices is the key to obtaining an efficient Sudoku solver.

Key idea - a blocked matrix can never lead to a solution. Blocked Matrices Let us say that a matrix is blocked if some square has no choices left, or if some row, column, or box has a duplicated single choice: 1 1 2 1 3 4 3 Key idea - a blocked matrix can never lead to a solution.

Expanding One Choice Transform a matrix of lists into a list of matrices by expanding the first square with choices: 1 2 3 4 1 2 2 3 1 3 expand :: Matrix [a]  [Matrix [a]]

Our Final Solver solve’’ :: Grid  [Grid] solve’’ = search . prune . choices search :: Matrix [Char]  [Grid] search m | blocked m = [] | complete m = collapse m | otherwise = [g | m'  expand m , g  search (prune m')]

This program can solve any newspaper Sudoku puzzle in an instant. The Result This program can solve any newspaper Sudoku puzzle in an instant. My addiction is cured!!