0% found this document useful (0 votes)
7 views

Backtracking NQueens Problem

The document introduces backtracking as a method for solving problems that require optimal solutions, specifically focusing on the nQueens problem. It explains the concepts of explicit and implicit constraints, the organization of solution space into a tree structure, and the algorithms for placing queens on a chessboard without them attacking each other. The document also details the depth-first search approach and the bounding function used in backtracking to optimize the solution process.

Uploaded by

22z374
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)
7 views

Backtracking NQueens Problem

The document introduces backtracking as a method for solving problems that require optimal solutions, specifically focusing on the nQueens problem. It explains the concepts of explicit and implicit constraints, the organization of solution space into a tree structure, and the algorithms for placing queens on a chessboard without them attacking each other. The document also details the depth-first search approach and the bounding function used in backtracking to optimize the solution process.

Uploaded by

22z374
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/ 15

Introduction to Backtracking and

Solving nQueens Problem

Dr.C.Kavitha
Associate Professor
Dept. of CSE
PSG College of Technology
• Problems searching for a set of solutions or which require an
optimal solution can be solved using the backtracking
method .

• To apply the backtrack method, the solution must be


expressible as an n-tuple(x1 ,…,xn ), where the xi are chosen
from some finite set si

• The solution vector must satisfy the criterion function P(x1 ,


….. , xn ).

• criterion functions Pi (x1 ...xn ) called bounding functions are


used to test whether the partial vector (x1 ,x2 ,......,xi ) can
lead to an optimal solution. If (x1 ,...xi ) is not leading to a
solution, mi+1 ,....,mn possible test vectors may be ignored.
EXPLICIT CONSTRAINTS are rules which restrict the values of xi.
Explicit constraints are rules that restrict each xi to take on values only from a given
set.
Explicit constraints depend on the particular instance I of problem being solved
All tuples that satisfy the explicit constraints define a possible solution space for I
Examples of explicit constraints
• xi ≥ 0, or all nonnegative real numbers
• xi = {0, 1}
• li ≤ xi ≤ ui
Explicit constraints using 8-tuple formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤ 8
IMPLICIT CONSTRAINTS describe the way in which the xi must relate to each
other .
Implicit constraints are rules that determine which of the tuples in the solution
space of I satisfy the criterion function.
Implicit constraints describe the way in which the xi must relate to each other.
implicit constraints
No two xi can be the same, or all the queens must be in different columns
· All solutions are permutations of the 8-tuple (1, 2, 3, 4, 5, 6, 7, 8) · Reduces the
size of solution space from 8power 8 to 8! tuples
• Tuples that satisfy the explicit constraints define a
solution space.
• The solution space can be organized into a tree.
• Each node in the tree defines a problem state.
• All paths from the root to other nodes define the
state-space of the problem.
• Solution states are those states leading to a tuple
in the solution space.
• Answer nodes are those solution states leading to
an answer-tuple(i.e. tuples which satisfy implicit
constraints)
•Live node: A node which has been generated and
all of whose children have not yet generated
• E-node (Node being expanded) : The live node
whose children are currently being generated
• Dead node : A node that is not expanded further
or all of whose children have been generated
•DEPTH FIRST NODE GENERATION- In this, as soon as a
new child C of the current E-node R is generated, C will
become the new E-node.
•BOUNDING FUNCTION - will be used to kill live nodes
without generating all their children.
•DFS: Depth first node generation with bounding
function is called backtracking.
4 - Queens solution space with nodes
numbered in DFS
Two queens are placed at positions (i ,j) and (k ,l)
They are on the same diagonal only if
i - j = k - l (upper left to lower right diagonal)
or 1 2 3 4
1
i + j =k + l (Upper right to lower left) 2
3
4
The first equation implies j – l = i – k
The second implies j –l= k – i
Two queens lies on the same diagonal iff
| j – l| = |i - k|
• For instance P1=(2,3) and P2=(3,4)

i - j = k - l (upper left to lower right)


So i =2, j=3 and k=3, l=4
2-3=3-4 =-1
Hence P1 and P2 are on the same diagonal

• For instance P1=(2,3) and P2=(3,2)


i + j =k + l (Upper right to lower left)
So i =2, j=3 and k=3, l=2
2+3 = 3+ 2=5
Hence P1 and P2 are on the same diagonal
Algorithm Place(K , i)
// Return true if a queen can be placed in Kth row and ith
column otherwise false
// x[] is a global array whose first (k-1) value have been
set. Abs(r) returns absolute value of r
{
For j= 1 to k-1 do
If ((x[ j ] = i ) // two in the same column
Or ( Abs ( x [ j ]- i) = Abs ( j- k))) // same diagonal
then return false
Return true
}
Computing time O(k-1)
Algorithm NQueens( K, n)
// Using backtracking it prints all possible placements of n //Queens
on a n*n chessboard so that they are not attacking
{
For i= 1 to n do
{
if place(K ,i) then
{
x[K] := i
If ( k = n ) then
write ( x[1:n]) //print the sequence
Else Nqueens(K+1 ,n)
}
}
}

You might also like