CS 270 Math Foundations of CS

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

SAT Solver CS 680 Formal Methods Jeremy Johnson. 2 Disjunctive Normal Form  A Boolean expression is a Boolean function  Any Boolean function can be.
PROLOG 8 QUEENS PROBLEM.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
The N-Queens Problem lab01.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
CHAPTER 4 Searching. Algorithm Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found,
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Constraints.py.
SAT Solver Math Foundations of Computer Science. 2 Boolean Expressions  A Boolean expression is a Boolean function  Any Boolean function can be written.
Vlad Furash & Steven Wine.  Problem surfaced in 1848 by chess player Max Bezzel as 8 queens (regulation board size)  Premise is to place N queens on.
Announcements.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Two Dimensional Arrays
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
HW #2. Due Mar 27 23:59 Do NOT forget to submit both a hardcopy solution to the HW box and a softcopy solution to TA (Youngjoo Kim):
HW 6: Problems 2 & 3 Simulating Connect 4. HW 6: Overview Connect 4: Variation of Tic-Tac-Toe – Board: Vertical 7x6 – Two players take alternating move.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
From … to Induction CS 270 Math Foundations of CS Jeremy Johnson.
Backtracking & Brute Force Optimization Intro2CS – weeks
Satisfiability and SAT Solvers CS 270 Math Foundations of CS Jeremy Johnson.
INTRO2CS Tirgul 6. Understanding the Traceback # in file t.py: def a(L): return b(L) def b(L): return L.len() #should have been len(L) # in the python.
Bit-Vector Optimization ALEXANDER NADER AND VADIM RYVCHIN INTEL TACAS 2016.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
CSSE 230 Day 25 Skip Lists.
CSP in Python.
CS100J Lecture 19 Previous Lecture This Lecture
Intro to Computer Science II
CSCI 104 Backtracking Search
Fundamentals of Programming II Backtracking with Stacks
Disjunctive Normal Form
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 19 More Recursive Backtracking
Using a Stack Chapter 6 introduces the stack data type.
Logic Synthesis CNF Satisfiability.
Recursion Copyright (c) Pearson All rights reserved.
Elementary Metamathematics
EECS 110: Lec 10: Definite Loops and User Input
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Jordi Cortadella Department of Computer Science
Chapter 3: Finite Constraint Domains
NP-Complete Problems.
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
HW #2. Due Apr 25 23:59 Do NOT forget to submit both a hardcopy solution to the HW box and a softcopy solution to TA.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Data Structures & Programming
Presentation transcript:

CS 270 Math Foundations of CS N Queens CS 270 Math Foundations of CS Jeremy Johnson

N-Queens Problem Given an N x N chess board Find a placement of N queens such that no two queens can take each other

N Queens

N Queens Backtrack

N Queens Backtrack

N Queens

N Queens Backtrack

N Queens No Solution

N Queens

N Queens

N Queens Backtrack

N Queens

N Queens

N Queens Backtrack

N Queens Backtrack

N Queens

N Queens

N Queens

N Queens Solution Found

Recursive Solution to N-Queens Define Queens(board, current, size) Input: board a size x size chess board with placement of current queens in positions without conflict only using the first current columns Output: true if board is a conflict free placement of size queens if (current = size) then return true for row = 0 to size-1 do position := (row,column+1) if ConflictFree(board,position) Update(board,position) done := Queens(board,column+1,size) if done = true return true return false

N-Queens as a SAT Problem Introduce variables Bij for 0 ≤ i,j < N Bij = T if queen at position (i,j) F otherwise Constraints Exactly one queen per row Rowi = Bij, j=0…N-1 Exactly one queen per column Columnj = Bij, i=0…N-1 At most one queen on diagonal Diagonalk- = Bij, i-j = k = -N+1…,N-1 Diagonalk+ = Bij, i+j = k = 0…,2N-2 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

4-Queens SAT input Exactly one queen in row i Bi0  Bi1  Bi2  Bi3 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

4-Queens SAT input Exactly one queen in column j B0j  B1j  B2j  B3j 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

4-Queens SAT input At most one queen in diagonal k- B20 B31 … B00 B11  B22  B33 B11 B22  B33 B22 B33 B02 B13 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

4-Queens SAT input At most one queen in diagonal k+ B01 B10 … B30 B21  B12  B03 B21 B12  B03 B12 B03 B32 B23 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

nqueens.py #!/usr/bin/env python # python script to generate SAT encoding of N-queens problem # # Jeremy Johnson and Mark Boady import sys #Helper Functions #cnf formula for exactly one of the variables in list A to be true def exactly_one(A): temp="" temp=temp+atleast_one(A) temp=temp+atmost_one(A) return temp #cnf formula for atleast one of the variables in list A to be true def atleast_one(A): for x in A: temp = temp +" " +str(x) temp=temp+" 0\n"

nqueens.py #cnf formula for atmost one of the variables in list A to be true def atmost_one(A): temp="" for x in A: for y in A[A.index(x)+1:]: temp = temp +" -"+str(x)+" -"+str(y)+" 0\n" return temp #function to map position (r,c) 0 <= r,c < N, in an NxN grid to the integer # position when the grid is stored linearly by rows. def varmap(r,c,N): return r*N+c+1 #Read Input if len(sys.argv)>1: N=int(sys.argv[1]) else: N=3 #Check for Sane Input if N<1: print("Error N<1") sys.exit(0)

nqueens.py #Start Solver print("c SAT Expression for N="+str(N)) spots = N*N print("c Board has "+str(spots)+" positions") #Exactly 1 queen per row temp="" for row in range(0,N): A=[] for column in range(0,N): position = varmap(row,column,N) A.append(position) temp = temp+exactly_one(A) #Exactly 1 queen per column

nqueens.py #Atmost 1 queen per negative diagonal from left for row in range(N-1,-1,-1): A=[] for x in range(0,N-row): A.append(varmap(row+x,x,N)) temp=temp+atmost_one(A) #Atmost 1 queen per negative diagonal from top for column in range(1,N): for x in range(0,N-column): A.append(varmap(x,column+x,N)) #Atmost 1 queen per positive diagonal from right A.append(varmap(row+x,N-1-x,N)) #Atmost 1 queen per positive diagonal from top for column in range(N-2,-1,-1): for x in range(0,column+1): A.append(varmap(x,column-x,N)) print 'p cnf ' + str(N*N) + ' ' + str(temp.count('\n')) + '\n' print(temp) 00 01 02 03 13 10 11 12 20 21 22 23 33 30 31 32

4-Queens DIMACS Input (rows) c SAT Expression for N=4 c Board has 16 positions p cnf 16 84 1 2 3 4 0 -1 -2 0 -1 -3 0 -1 -4 0 -2 -3 0 -2 -4 0 -3 -4 0 5 6 7 8 0 -5 -6 0 -5 -7 0 -5 -8 0 -6 -7 0 -6 -8 0 -7 -8 0 9 10 11 12 0 -9 -10 0 -9 -11 0 -9 -12 0 -10 -11 0 -10 -12 0 -11 -12 0 13 14 15 16 0 -13 -14 0 -13 -15 0 -13 -16 0 -14 -15 0 -14 -16 0 -15 -16 0 1 2 3 4 8 5 6 7 9 10 11 12 16 13 14 15

4-Queens DIMACS Input (cols) c SAT Expression for N=4 c Board has 16 positions p cnf 16 84 1 5 9 13 0 -1 -5 0 -1 -9 0 -1 -13 0 -5 -9 0 -5 -13 0 -9 -13 0 2 6 10 14 0 -2 -6 0 -2 -10 0 -2 -14 0 -6 -10 0 -6 -14 0 -10 -14 0 3 7 11 15 0 -3 -7 0 -3 -11 0 -3 -15 0 -7 -11 0 -7 -15 0 -11 -15 0 4 8 12 16 0 -4 -8 0 -4 -12 0 -4 -16 0 -8 -12 0 -8 -16 0 -12 -16 0 1 2 3 4 8 5 6 7 9 10 11 12 16 13 14 15

4-Queens DIMACS Input (diag) c SAT Expression for N=4 c Board has 16 positions p cnf 16 84 -9 -14 0 -5 -10 0 -5 -15 0 -10 -15 0 -1 -6 0 -1 -11 0 -1 -16 0 -6 -11 0 -6 -16 0 -11 -16 0 -2 -7 0 -2 -12 0 -7 -12 0 -3 -8 0 -12 -15 0 -8 -11 0 -8 -14 0 -11 -14 0 -4 -7 0 -4 -10 0 -4 -13 0 -7 -10 0 -7 -13 0 -10 -13 0 -3 -6 0 -3 -9 0 -6 -9 0 -2 -5 0 1 2 3 4 8 5 6 7 9 10 11 12 16 13 14 15

4-Queens Output SAT -1 -2 3 -4 5 -6 -7 -8 -9 -10 -11 12 -13 14 -15 -16 0 1 2 3 4 8 5 6 7 9 10 11 12 16 13 14 15