0% found this document useful (0 votes)
16 views14 pages

N-Queens Problem

Uploaded by

keshavpal583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

N-Queens Problem

Uploaded by

keshavpal583
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

17-Oct-19

N QUEENS PROBLEM USING


BACK TRACKING

8 QUEEN PROBLEM?
 The eight queens puzzle is the problem of placing
eight chess queens on an 8×8 chessboard so that no two
queens attack each other.
 Thus, a solution requires that no two queens share the same
row, column, or diagonal.
 This problem can be solved by trying to place the first queen,
then the second queen so that it cannot attack the first, and
then the third so that it is not conflicting with previously
placed queens.
 The eight queens puzzle is an example of the more
general n-queens problem of placing n queens on
an n×n chessboard, where solutions exist for all natural
numbers n with the exception of 1, 2 and 3.
GCET-CSE N-QUEENS 2

1
17-Oct-19

QUEENS NON ATTACKING POSITIONS

Formulation :
States: any arrangement
of 0 to 8 queens on the
board
Initial state: 0 queens on
the board
Successor function: add a
queen in any square
Goal test: 8 queens on the
board, none attacked

GCET-CSE N-QUEENS 3

Programming approaches
1. Divide & Conquer
2. Greedy
3. Dynamic
4. Brute force
5. Backtracking
6. Branch and Bound

GCET-CSE N-QUEENS 4

2
17-Oct-19

Brute Force
Discover all possibilities

GCET-CSE N-QUEENS 5

Programming approaches
1. Divide & Conquer
2. Greedy
3. Dynamic
4. Brute force
5. Backtracking
6. Branch and Bound

GCET-CSE N-QUEENS 6

3
17-Oct-19

Backtracking State space tree


1

2 9 16

4 12 18

• Left recursive
• Solution at node 14 Pruned states: Save time
GCET-CSE N-QUEENS 7

BACK TRACKING
 Backtracking is a general algorithm for finding all (or some)
solutions to some computational problem, that incrementally
builds candidates to the solutions, and abandons each partial
candidate ‘c’ ("backtracks") as soon as it determines that ‘c’
cannot possibly be completed to a valid solution.
 Backtracking is an important tool for solving constraint
satisfaction problems, such as crosswords, verbal arithmetic,
Sudoku, n-queens and many other puzzles.
 The good example of the use of backtracking is the eight
queens puzzle, that asks for all arrangements of eight queens
on a standard chessboard so that no queen attacks any other.

GCET-CSE N-QUEENS 8

4
17-Oct-19

BACK TRACKING
 In the common backtracking approach, the partial candidates
are arrangements of k queens in the first k rows of the board,
all in different rows and columns.
 Any partial solution that contains two mutually attacking
queens can be abandoned, since it cannot possibly be
completed to a valid solution
 The n-queens puzzle was originally proposed in 1848 by the
chess player Max Bezzel, and over the years,
many mathematicians, including Gauss.

GCET-CSE N-QUEENS 9

BACK TRACKING(n-queens)
 Each recursive call attempts to place a queen in a specific
row/column.
 For a given call previous placements of queens are known.
 Current step backtracking: If a placement within the
row/column does not lead to a solution, the queen is removed
and moved “next/down" the row/column
 Previous step backtracking: When all column/rows in a
row/column have been tried, the call terminates and
backtracks to the previous call (in the previous
row/column) , This known as Pruning.
 Using this approach we can reduce the number of potential
solutions even more
GCET-CSE N-QUEENS 10

5
17-Oct-19

GCET-CSE N-QUEENS 11

GCET-CSE N-QUEENS 12

6
17-Oct-19

4-way state space tree

GCET-CSE N-QUEENS 13
Pruned states: Save time

4-way state space tree

No solution branch

First solution
GCET-CSE N-QUEENS 14
Pruned states: Save time

7
17-Oct-19

4-way state space tree

No solution branch

First solution Second solution


GCET-CSE N-QUEENS 15
Pruned states: Save time

4-way state space tree

Mirror image of first branch

No solution branch No solution

First solution Second solution


GCET-CSE N-QUEENS 16
Pruned states: Save time

8
17-Oct-19

8 QUEEN: Solutions

 The solution possibilities are discovered only up to 23 queen.

 The eight queens puzzle has 92 distinct solutions.

 If solutions that differ only by symmetry operations(rotations and


reflections) of the board are counted as one the puzzle has
12 unique (or fundamental) solutions

GCET-CSE N-QUEENS 17

8 Queen Problem: Implementation


 We know that for queens:
each row will have exactly one queen
each column will have exactly one queen
each diagonal will have at most one queen

 This will help us to model the chessboard not as a 2-D array, but as
a set of rows, columns and diagonals.

 To simplify the presentation, we will study for smaller chessboard,


4 by 4

GCET-CSE N-QUEENS 18

9
17-Oct-19

Implementing the Chessboard


• First: we need to define an array to store the location of so far placed
queens

PositionInRow

GCET-CSE N-QUEENS 19

Implementing the Chessboard Cont’d


• We need an array to keep track of the availability status of the column
when we assign queens.

Suppose that we
have placed two
queens

T F T F

GCET-CSE N-QUEENS 20

10
17-Oct-19

Implementing the Chessboard Cont’d


• We have 7 left diagonals, we want to keep track of available diagonals
after the so far allocated queens

F
T

GCET-CSE N-QUEENS 21

Implementing the Chessboard Cont’d


• We have 7 left diagonals, we want to keep track of available diagonals
after the so far allocated queens

F
F

GCET-CSE N-QUEENS 22

11
17-Oct-19

Backtracking

[4,3]

[3,1]

[2,4]

[1,2]

GCET-CSE N-QUEENS 23

The putQueen Recursive Method


static void putQueen(int row){
for (int col=0;col<squares;col++)
if (column[col]==available && leftDiagonal[row+col]==available &&
rightDiagonal[row-col+norm]== available)
{
positionInRow[row]=col;
column[col]=!available;
leftDiagonal[row+col]=!available;
rightDiagonal[row-col+norm]=!available;
if (row< squares-1)
putQueen(row+1);
else
System.out.println(" solution found");
column[col]=available;
leftDiagonal[row+col]=available;
rightDiagonal[row-col+norm]= available;
}
}

GCET-CSE N-QUEENS 24

12
17-Oct-19

GCET-CSE N-QUEENS 25

COUNTING SOLUTIONS

 The following table gives the number of solutions for


placing n queens on an n × n board, both unique and distinct
for n=1–26.
 Note that the six queens puzzle has fewer solutions than the
five queens puzzle.
 There is currently no known formula for the exact number of
solutions.

GCET-CSE N-QUEENS 26

13
17-Oct-19

Order
(“N”) Total Solutions Unique Solutions Exec time
---------------------------------------------------------
1 1 1 < 0 seconds
2 0 0 < 0 seconds
3 0 0 < 0 seconds
4 2 1 < 0 seconds
5 10 2 < 0 seconds
6 4 1 < 0 seconds
7 40 6 < 0 seconds
8 92 12 < 0 seconds
9 352 46 < 0 seconds
10 724 92 < 0 seconds
11 2,680 341 < 0 seconds
12 14,200 1,787 < 0 seconds
13 73,712 9,233 < 0 seconds
14 365,596 45,752 0.2s

GCET-CSE N-QUEENS 27

15 2,279,184 285,053 1.9 s


16 14,772,512 1,846,955 11.2 s
17 95,815,104 11,977,939 77.2 s
18 666,090,624 83,263,591 9.6 m
19 4,968,057,848 621,012,754 75.0 m
20 39,029,188,884 4,878,666,808 10.2 h
21 314,666,222,712 39,333,324,973 87.2 h
22 2,691,008,701,644 336,376,244,042 31.9
23 24,233,937,684,440 3,029,242,658,210 296 d
24 227,514,171,973,736 28,439,272,956,934 ?
25 2,207,893,435,808,352 275,986,683,743,434 ?
26 22,317,699,616,364,044 2,789,712,466,510,289 ?

(s = seconds m = minutes h = hours d = days)

GCET-CSE N-QUEENS 28

14

You might also like