Backtracking-N Queen Problems
Backtracking-N Queen Problems
1
Backtracking
• State-space tree, represents the processing
• Its root represents an initial state
• The nodes of the first level in the tree represent the
choices made for the first component of a solution
• The nodes of the second level represent the choices for the
second component, and so on.
• A node in a state-space tree is said to be promising
– if it corresponds to a partially constructed solution that
may still lead to a complete solution;
– otherwise, it is called nonpromising.
• Leaves represent either nonpromising dead ends or
complete solutions found by the algorithm.
2
Backtracking
• In the majority of cases, a states-pace tree for a
backtracking algorithm is constructed in the manner
of depth-first search.
• If the algorithm reaches a complete solution to the
problem, it either stops (if just one solution is
required) or continues searching for other possible
solutions.
3
N-Queens Problem
Problem Definition
• The problem is to place n queens on an n × n
chessboard so that no two queens attack each other
by being in the same row or in the same column or
on the same diagonal.
4
4-Queens problem
• Since each of the four queens has to be placed in its
own row, all we need to do is to assign a column for
each queen on the board presented in figure.
5
4-Queens problem
• The state-space tree of this search is shown in figure.
6
4-queens problem
• If other solutions need to be found, the algorithm
can simply resume its operations at the leaf at which
it stopped.
• Alternatively, we can use the board’s symmetry for
this purpose.
• Finally, it should be pointed out that a single solution
to the n-queens problem for any n ≥ 4 can be found
in linear time.
7
N-queens prblem
Algorithm to find all solutions of n-queens problem
8
N-queens prblem