0% found this document useful (0 votes)
13 views9 pages

Backtracking-N Queen Problems

DAA3

Uploaded by

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

Backtracking-N Queen Problems

DAA3

Uploaded by

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

Backtracking

• Some problems can be solved, by exhaustive search.


• Backtracking is a more intelligent variation of this
approach.
• The principal idea is to construct solutions one
component at a time and evaluate such partially
constructed candidates as follows.
– If a partially constructed solution can be developed,
continue.
– If there is no legitimate option for the next component,
backtrack to replace the last component of the partially
constructed solution with its next option.

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.

• So let us consider the 4-queens problem and solve it


by the backtracking technique.

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.

Note: This tree shows expansion of


nodes till it gets a solution. For multiple
solutions it need to be expanded further

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

You might also like