N-Queens Problem
N-Queens Problem
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
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
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
GCET-CSE N-QUEENS 13
Pruned states: Save time
No solution branch
First solution
GCET-CSE N-QUEENS 14
Pruned states: Save time
7
17-Oct-19
No solution branch
8
17-Oct-19
8 QUEEN: Solutions
GCET-CSE N-QUEENS 17
This will help us to model the chessboard not as a 2-D array, but as
a set of rows, columns and diagonals.
GCET-CSE N-QUEENS 18
9
17-Oct-19
PositionInRow
GCET-CSE N-QUEENS 19
Suppose that we
have placed two
queens
T F T F
GCET-CSE N-QUEENS 20
10
17-Oct-19
F
T
GCET-CSE N-QUEENS 21
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
GCET-CSE N-QUEENS 24
12
17-Oct-19
GCET-CSE N-QUEENS 25
COUNTING 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
GCET-CSE N-QUEENS 28
14