Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Techniques for Solving Logic Puzzles. Logic Puzzles Logic puzzles operate using deductive logic. Logic puzzles operate using deductive logic. A well-designed.
How to play SUDOKU adapted from Tilda Rosander.
Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
Solving Sudoku1 Website: Labelling scheme.
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.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
1 CSC 421: Algorithm Design & Analysis Spring 2013 See online syllabus: (also on BlueLine2) Course.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Is solving harder than checking? Oded Goldreich Weizmann Institute of Science.
Finding Similar Items.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)
SOLVING THE KAKURO PUZZLE Andreea Erciulescu Department of Mathematics, Colorado State University, Fort Collins (Mentor: A. Hulpke)
Get started with PivotTable reports Make your data work for you Imagine an Excel worksheet of sales figures. It lays out thousands of rows of data about.
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.
SUDOKU Via Relaxation Labeling
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.
Data Structures Using C++ 2E Chapter 6 Recursion.
Chapter 6.7 Determinants. In this chapter all matrices are square; for example: 1x1 (what is a 1x1 matrix, in fact?), 2x2, 3x3 Our goal is to introduce.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Data Structures Using C++ 2E Chapter 6 Recursion.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Fundamentals of Algorithms MCS - 2 Lecture # 7
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Computing Science 1P Lecture 21: Wednesday 18 th April 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 Jordi Cortadella Department of Computer Science.
Propositional Satisfiability A compound proposition is satisfiable if there is an assignment of truth values to its variables that make it true. When no.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Su Doku!. Given a grid and some numbers, fill the rest in… They might be in the wrong order…
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
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
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 -
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
CSC 421: Algorithm Design & Analysis
Recursion Topic 5.
Intro to Computer Science II
CSCI 104 Backtracking Search
EGR 115 Introduction to Computing for Engineers
Fundamentals of Programming II Backtracking with Stacks
David Kauchak CS30 – Spring 2016
CSC 421: Algorithm Design & Analysis
Example Fill in the grid so that every row, column and box contains each of the numbers 1 to 9:
Unit-2 Divide and Conquer
Jordi Cortadella Department of Computer Science
adapted from Recursive Backtracking by Mike Scott, UT Austin
Sudoku.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
David Kauchak CS51A – Spring 2019
Backtracking, Search, Heuristics
Presentation transcript:

Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07

Computing Science 1P Tutorial 20 - Simon Gay2 Example: More Sudoku You are probably familiar with sudoku puzzles: Each row, column and 3x3 square must be filled in to contain the digits 1 – 9.

2006/07Computing Science 1P Tutorial 20 - Simon Gay3 Data Structure for Sudoku A sudoku grid is basically a 9x9 matrix, so the natural data structure is a list of lists. (Could use a dictionary, but little point.) We can either use a list of 9 rows, or a list of 9 columns. Each row or column is itself a list of 9 numbers. It doesn't matter whether we choose rows or columns, as long as we remember which it is. Whatever we do, dealing with the 3x3 squares will be a little awkward.

2006/07Computing Science 1P Tutorial 20 - Simon Gay4 Sudoku as a list of rows [ [0,0,3,0,0,9,4,6,0], [0,0,6,0,0,0,1,0,0], [0,0,0,6,3,2,0,0,0], [5,0,0,0,0,1,0,0,2], [0,2,4,0,0,0,6,8,0], [8,0,0,2,0,0,0,0,7], [0,0,0,5,4,7,0,0,0], [0,0,2,0,0,0,8,0,0], [0,4,5,1,0,0,9,0,0] ] 0 represents an empty square.

2006/07Computing Science 1P Tutorial 20 - Simon Gay5 Solving sudoku How do we start to think about writing a program to solve sudoku puzzles? First idea: just try all the possibilities! Try every way of filling in the blank squares with numbers from 1 to 9, and for each one, check to see whether or not it is a solution. (We defined a function to check for a solution, before Easter). This is an example of a brute force approach.

2006/07Computing Science 1P Tutorial 20 - Simon Gay6 Do you think that this idea will be effective? Yes No

2006/07Computing Science 1P Tutorial 20 - Simon Gay7 The brute force approach A typical sudoku might have between 20 and 30 numbers in the grid. Assume that there are 50 blank squares. The number of ways of filling in 50 squares with numbers from 1 to 9 is Assume that on a fast computer we can check 10 million candidate solutions per second. Checking all the possibilities will take seconds, i.e. approx. years. No hope!

2006/07Computing Science 1P Tutorial 20 - Simon Gay8 A more intelligent approach Basic idea: For each square, store a list of numbers which are still possible values for that square. Blank squares are [ 1,2,3,4,5,6,7,8,9] to start with, but the list gets smaller as we fill in other squares. When we discover the value of a particular square, we reduce the list of possibilities for the other squares in the same row, column or 3x3 block. The numbers in the original puzzle get us started.

2006/07Computing Science 1P Tutorial 20 - Simon Gay9 Coordinates We will need to refer to particular squares in the grid, and the obvious way to do this is by using a coordinate system: (row,column) with each ranging from 0 to 8 row 3 column 6 (3,6)

2006/07Computing Science 1P Tutorial 20 - Simon Gay10 Exercise Define a function findRow which is given the coordinates of a square (as a pair, (row,column) ) and returns a list of the squares in the same row. i.e. it returns a list of pairs, each being the coordinates of a square in the same row. def findRow(square):

2006/07Computing Science 1P Tutorial 20 - Simon Gay11 findRow def findRow(square): row = square[0] coords = [] for i in range(9): coords = coords + [(row,i)] return coords findRow( (0,2) ) = [ (0,0),(0,1),(0,2),(0,3),(0,4),(0,5),(0,6), (0,7),(0,8) ]

2006/07Computing Science 1P Tutorial 20 - Simon Gay12 Exercise Similarly, define findCol, which returns a list of the coordinates of the squares in the same column as the given square.

2006/07Computing Science 1P Tutorial 20 - Simon Gay13 findCol def findCol(square): col = square[1] coords = [] for i in range(9): coords = coords + [(i,col)] return coords

2006/07Computing Science 1P Tutorial 20 - Simon Gay14 Exercise Define findBlock, which returns a list of the coordinates of the squares in the same 3x3 block as the given square. findBlock( (1,4) ) = [ (0,3),(0,4),(0,5), (1,3),(1,4),(1,5), (2,3),(2,4),(2,5) ]

2006/07Computing Science 1P Tutorial 20 - Simon Gay15 findBlock def findBlock(square): row = square[0] col = square[1] startRow = (row / 3) * 3 startCol = (col / 3) * 3 coords = [] for i in range(3): for j in range(3): coords = coords + [(startRow+i,startCol+j)] return coords (startRow,startCol) is the top-left corner of the block. E.g. if row = 5, (row / 3) * 3 = (5 / 3) * 3 = 1 * 3 = 3.

2006/07Computing Science 1P Tutorial 20 - Simon Gay16 Exercise It turns out to be more useful to define findRow, findCol and findBlock so that if we call them all for a particular square and combine the results, there is no repetition and the original square is not included. Define new versions.

2006/07Computing Science 1P Tutorial 20 - Simon Gay17 New findRow def findRow(square): row = square[0] col = square[1] coords = [] for i in range(9): if i <> col: coords = coords + [(row,i)] return coords

2006/07Computing Science 1P Tutorial 20 - Simon Gay18 New findCol def findCol(square): row = square[0] col = square[1] coords = [] for i in range(9): if i <> row: coords = coords + [(i,col)] return coords

2006/07Computing Science 1P Tutorial 20 - Simon Gay19 New findBlock def findBlock(square): row = square[0] col = square[1] startRow = (row / 3) * 3 startCol = (col / 3) * 3 coords = [] for i in range(3): for j in range(3): if (startRow+i <> row) and (startCol+j <> col): coords = coords + [(startRow+i,startCol+j)] return coords

2006/07Computing Science 1P Tutorial 20 - Simon Gay20 Example A 4x4 puzzle.

2006/07Computing Science 1P Tutorial 20 - Simon Gay21 Other functions We also need functions remove and initialGrid remove(3,[1,2,3,4,5]) = [1,2,4,5] initialGrid() returns a list of 9 lists, each containing 9 entries, each of which is [1,2,3,4,5,6,7,8,9]

2006/07Computing Science 1P Tutorial 20 - Simon Gay22 Solving Sudoku The main program is based on two functions: eliminate(grid,square,value) removes value from the list of possibilities for square in grid fixValue(grid,square,value) sets square to value and eliminates that value from the possibilities for squares in the same row, column or block

2006/07Computing Science 1P Tutorial 20 - Simon Gay23 eliminate def eliminate(grid,square,value): row = square[0] col = square[1] poss = grid[row][col] if len(poss) > 1: newPoss = remove(value,poss) grid[row][col] = newPoss if len(newPoss) == 1: fixValue(grid,square,newPoss[0])

2006/07Computing Science 1P Tutorial 20 - Simon Gay24 fixValue def fixValue(grid,square,value): row = square[0] col = square[1] grid[row][col] = [value] coords = findRow(square) + findCol(square) + findBlock(square) for s in coords: eliminate(grid,s,value)

2006/07Computing Science 1P Tutorial 20 - Simon Gay25 The top-level function def propagate(grid): g = initialGrid() for i in range(9): for j in range(9): if grid[i][j] <> 0: fixValue(g,(i,j),grid[i][j]) return g grid is the original puzzle: a list of lists of integers, with 0 for blank squares g is the list of lists of lists, showing the possible values for each square.

2006/07Computing Science 1P Tutorial 20 - Simon Gay26 The main program filename = raw_input("Enter filename: ") printGrid(propagate(readGrid(filename)))

2006/07Computing Science 1P Tutorial 20 - Simon Gay27 Do you think this program can solve all sudokus? Yes No Don't know

2006/07Computing Science 1P Tutorial 20 - Simon Gay28 What's missing? This simple technique is not sufficient to solve all sudoku puzzles. Extending our program to solve all puzzles requires backtracking: trying a possibility, but being prepared to undo the choice if we get stuck. Using backtracking, we can examine all possibilities, but in a much more efficient way than the original brute force idea. If one choice of number turns out to be wrong, we do not consider any other possibilities including that choice.