Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 270 Math Foundations of CS

Similar presentations


Presentation on theme: "CS 270 Math Foundations of CS"— Presentation transcript:

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

2 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

3 N Queens

4 N Queens Backtrack

5 N Queens Backtrack

6 N Queens

7 N Queens Backtrack

8 N Queens No Solution

9 N Queens

10 N Queens

11 N Queens Backtrack

12 N Queens

13 N Queens

14 N Queens Backtrack

15 N Queens Backtrack

16 N Queens

17 N Queens

18 N Queens

19 N Queens Solution Found

20 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

21 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

22 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

23 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

24 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

25 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

26 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"

27 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)

28 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

29 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

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

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

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

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


Download ppt "CS 270 Math Foundations of CS"

Similar presentations


Ads by Google