SlideShare a Scribd company logo
3
Most read
5
Most read
8
Most read
BACKTRACKING
METHODOLOGY
1
Backtracking methodology
• Many problems which deal with searching for a set of
solutions or for a optimal solution satisfying some constraints can
be solved using the backtracking formulation.
• In Backtracking method, the desired solution is expressed as
an n-tuple vector : (x1…………… xi ……………….xn ) where xi is
chosen from some finite set Si.
• The problem is to find a vector, which maximizes or
minimizes a criterion function P(x1…………… xi ……………….xn ).
• The major advantage of this method is, once we know that a
partial vector(x1,…xi) will not lead to an optimal solution then
the generation of remaining components i.e.,(xi+1………..xn) are
ignored entirely.
backtracking 8 Queen.pptx
backtracking 8 Queen.pptx
1
34
18
50
2
8 13 19
3
5 7 12
10 15 17 23
21 26 28 33
31 37 39 44
42 47 9 55
53 58 60 65
63
4 6 11
9 14 16 22
20 25 27 32
30 36 38 43
41 46 48 54
52 57 59 64
62
29 35 40
24 51 56 61
45
X1=1
X1=2 X1=3
X1=4
2 3 4 1 3 4
1 2 4 1 2 3
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
x2=
x3
=
x4
=
Tree organization of the 4-queens solution space. Nodes are
numbered as in depth first search.
State space
Tree.
State Space Tree Terminology
1. Criterion function: It is a function P(X1, X2… X n)
which needs to be maximized or minimized for a
given problem.
2. Solution Space: All tuples that satisfy the explicit
constraints define a possible solution space for a
particular instance ‘I’ of the problem
3. Problem state: Each node in the tree organization
defines a problem state.
4. Solution States: These are those problem states S for
which the path form the root to S define a tuple in
the solution space.
5. State space tree: If we represent solution space in the
form of a tree then the tree is referred as the state
space tree.
6. Answer States: These solution states S for which the
path from the root to S defines a tuple which is a
member of the set of solution (i.e. it satisfies the
implicit constraints) of the problem.
State Space Tree Terminology..contd
7. Live node: A node which has been generated and
all of whose children have not yet been
generated is live node.
8. E-node: The live nodes whose children are
currently being generated is called E-node (node
being expanded)
9. Dead node: It is a generated node that is either
not to be expanded further or one for which all of
its children has been generated.
10. Bounding function: It will be used to kill live
nodes without generating all their children
11. Branch and bound: It is a method in which E-
node remains E-node until it is dead
Applications of Backtracking
1. Producing all permutations of a set of values
2. Parsing languages
3. Games: anagrams, crosswords, word jumbles,
8 queens
4. Combinatory and logic programming
Example Problems
1. N queen’s problem
2. Sum of subsets problem.
3. Graph colorig problem
4. 0/1 knapsck problem
5. Hamiltonian cycle problem
In summary, backtracking consists of
Doing a depth-first search of a state
space tree by applying boundary
conditions.
4-Queens Problem
Failure (Diagonal Attack)
So The solver has to backtrack
4-Queens Problem
No Row Attack, No Column Attack
& No Diagonal Attack
Hence It is Solution 1
8-Queens Problem
8-Queens Problem
8-Queens Problem
8-Queens Problem
8-Queens Problem
n– queens : Defining the problem:-
The problem is to place n queens on an n x n chessboard
so that
no two “attack” that is no two queens on the same row,
same
column, or same diagonal.
 Assume rows and columns of chessboard are
numbered 1 through n. Queens also be numbered 1
through n.
 Condition to ensure - No two queens on same Row:
Each queen must be on a different row ,hence queen i
is to be placed on row i. Therefore all solutions to the
n-queens problem can be represented as n-tuples (
x1,x2,…..xn), where xi is the column on which queen i is
placed.
 Condition to ensure - No two queens on same
Column:
Select a distinct value for each xi
If two queens are placed at positions ( i, j ) and ( k, l ).
They are on the same diagonal then following
conditions hold good.
1) Every element on the same diagonal that runs from
the upper left to the lower right has the same row –
column value. i – j = k – l -----(1)
2) Similarly, every element on the on the same
diagonal that goes from the upper right to the
lower left has the same row + column value.
i + j = k + l -----(2)
• First equation implies : j - l = i – k
Second equation implies : j – l = k – i
• Therefore, two queens lie on the same diagonal if
and only if
j – l = i – k
 Condition to ensure - No two queens on
same Diagonal
Algorithm place( k, i )
// It returns true if a queen can be placed in kth row and ith
// column . Otherwise it returns false. x[] is a goal array whose
// first( k-1) values have been set. Abs(r) returns the absolute
value of r.
{
for j = 1 to k-1 do
{ // Two in the same column or in the same
diagonal
if ( ( x [ i ] = i ) or ( Abs( x[ j ] - i ) = Abs( j - k )
) ) then
return false;
}
return true ;
}
Algorithm Nqueen(k, n)
// Using backtracking, this procedure prints all
possible placements
// of n queens on an n×n chessboard so that they
are nonattacking.
{
for i = 1 to n do // check place of
column for queen k
{
if place( k, i ) then
{
x[ k ] = i;
if( k = n ) then write ( x[1:n]
);
else NQueens( k+1, n);
}
}
1
18
2
8 13
19
3
15 31
11
9 14 16 30
29
24
x1=1 x1=2
x2=2 x2=3 x2=4 x2=4
x3=1
x4=3
Portion of the tree that is generated during
backtracking( n=4 ).
B
B B
B
B
B B

More Related Content

DOC
algorithm Unit 4
DOC
Unit 4 jwfiles
PDF
module5_backtrackingnbranchnbound_2022.pdf
PDF
DAA UNIT-4 (1).pdf
PPTX
811109685-CS3401-Algorithms-Unit-IV.pptx
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
PPTX
Chapter Five remadenChapter 3&5event it theory programming.pptx.pptx
algorithm Unit 4
Unit 4 jwfiles
module5_backtrackingnbranchnbound_2022.pdf
DAA UNIT-4 (1).pdf
811109685-CS3401-Algorithms-Unit-IV.pptx
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Chapter Five remadenChapter 3&5event it theory programming.pptx.pptx

Similar to backtracking 8 Queen.pptx (20)

PPT
BackTracking Algorithm: Technique and Examples
PPTX
Design Algorithms - - Backtracking.pptx
PDF
Kakuro: Solving the Constraint Satisfaction Problem
PDF
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
PPTX
designanalysisalgorithm_unit-v-part2.pptx
PPTX
N-queens.pptx
PPSX
Chapter 4- Learning Outcome 2_Mathematics for Technologists
PDF
unit 4 of Unit 4 of design and analysis of algorithms
PPTX
data structures- back tracking
PPS
Greedy Algorithms with examples' b-18298
PDF
Algorithm Performance For Chessboard Separation Problems
PPT
G9-Math-Q1-Week-9-Solving-Quadratic-Function.ppt
PDF
Foundations of Machine Learning - Module 1 (LINEAR ALGEBRA )
PDF
Nbhm m. a. and m.sc. scholarship test 2010
PPTX
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
PPTX
Signals and Systems Homework Help.pptx
PDF
Cbse 12 Class Maths Sample Paper
PPTX
Unit 3- Greedy Method.pptx
PPTX
PPTX
it is a ppt discussing important topic of daa such as branch and bound.pptx
BackTracking Algorithm: Technique and Examples
Design Algorithms - - Backtracking.pptx
Kakuro: Solving the Constraint Satisfaction Problem
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
designanalysisalgorithm_unit-v-part2.pptx
N-queens.pptx
Chapter 4- Learning Outcome 2_Mathematics for Technologists
unit 4 of Unit 4 of design and analysis of algorithms
data structures- back tracking
Greedy Algorithms with examples' b-18298
Algorithm Performance For Chessboard Separation Problems
G9-Math-Q1-Week-9-Solving-Quadratic-Function.ppt
Foundations of Machine Learning - Module 1 (LINEAR ALGEBRA )
Nbhm m. a. and m.sc. scholarship test 2010
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
Signals and Systems Homework Help.pptx
Cbse 12 Class Maths Sample Paper
Unit 3- Greedy Method.pptx
it is a ppt discussing important topic of daa such as branch and bound.pptx
Ad

Recently uploaded (20)

PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Artificial Intelligence
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPT
introduction to datamining and warehousing
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Occupational Health and Safety Management System
PDF
Abrasive, erosive and cavitation wear.pdf
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
III.4.1.2_The_Space_Environment.p pdffdf
Current and future trends in Computer Vision.pptx
Artificial Intelligence
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
introduction to datamining and warehousing
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Soil Improvement Techniques Note - Rabbi
UNIT 4 Total Quality Management .pptx
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Occupational Health and Safety Management System
Abrasive, erosive and cavitation wear.pdf
Ad

backtracking 8 Queen.pptx

  • 2. Backtracking methodology • Many problems which deal with searching for a set of solutions or for a optimal solution satisfying some constraints can be solved using the backtracking formulation. • In Backtracking method, the desired solution is expressed as an n-tuple vector : (x1…………… xi ……………….xn ) where xi is chosen from some finite set Si. • The problem is to find a vector, which maximizes or minimizes a criterion function P(x1…………… xi ……………….xn ). • The major advantage of this method is, once we know that a partial vector(x1,…xi) will not lead to an optimal solution then the generation of remaining components i.e.,(xi+1………..xn) are ignored entirely.
  • 5. 1 34 18 50 2 8 13 19 3 5 7 12 10 15 17 23 21 26 28 33 31 37 39 44 42 47 9 55 53 58 60 65 63 4 6 11 9 14 16 22 20 25 27 32 30 36 38 43 41 46 48 54 52 57 59 64 62 29 35 40 24 51 56 61 45 X1=1 X1=2 X1=3 X1=4 2 3 4 1 3 4 1 2 4 1 2 3 3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2 4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1 x2= x3 = x4 = Tree organization of the 4-queens solution space. Nodes are numbered as in depth first search. State space Tree.
  • 6. State Space Tree Terminology 1. Criterion function: It is a function P(X1, X2… X n) which needs to be maximized or minimized for a given problem. 2. Solution Space: All tuples that satisfy the explicit constraints define a possible solution space for a particular instance ‘I’ of the problem 3. Problem state: Each node in the tree organization defines a problem state. 4. Solution States: These are those problem states S for which the path form the root to S define a tuple in the solution space. 5. State space tree: If we represent solution space in the form of a tree then the tree is referred as the state space tree. 6. Answer States: These solution states S for which the path from the root to S defines a tuple which is a member of the set of solution (i.e. it satisfies the implicit constraints) of the problem.
  • 7. State Space Tree Terminology..contd 7. Live node: A node which has been generated and all of whose children have not yet been generated is live node. 8. E-node: The live nodes whose children are currently being generated is called E-node (node being expanded) 9. Dead node: It is a generated node that is either not to be expanded further or one for which all of its children has been generated. 10. Bounding function: It will be used to kill live nodes without generating all their children 11. Branch and bound: It is a method in which E- node remains E-node until it is dead
  • 8. Applications of Backtracking 1. Producing all permutations of a set of values 2. Parsing languages 3. Games: anagrams, crosswords, word jumbles, 8 queens 4. Combinatory and logic programming Example Problems 1. N queen’s problem 2. Sum of subsets problem. 3. Graph colorig problem 4. 0/1 knapsck problem 5. Hamiltonian cycle problem In summary, backtracking consists of Doing a depth-first search of a state space tree by applying boundary conditions.
  • 9. 4-Queens Problem Failure (Diagonal Attack) So The solver has to backtrack
  • 10. 4-Queens Problem No Row Attack, No Column Attack & No Diagonal Attack Hence It is Solution 1
  • 16. n– queens : Defining the problem:- The problem is to place n queens on an n x n chessboard so that no two “attack” that is no two queens on the same row, same column, or same diagonal.  Assume rows and columns of chessboard are numbered 1 through n. Queens also be numbered 1 through n.  Condition to ensure - No two queens on same Row: Each queen must be on a different row ,hence queen i is to be placed on row i. Therefore all solutions to the n-queens problem can be represented as n-tuples ( x1,x2,…..xn), where xi is the column on which queen i is placed.  Condition to ensure - No two queens on same Column: Select a distinct value for each xi
  • 17. If two queens are placed at positions ( i, j ) and ( k, l ). They are on the same diagonal then following conditions hold good. 1) Every element on the same diagonal that runs from the upper left to the lower right has the same row – column value. i – j = k – l -----(1) 2) Similarly, every element on the on the same diagonal that goes from the upper right to the lower left has the same row + column value. i + j = k + l -----(2) • First equation implies : j - l = i – k Second equation implies : j – l = k – i • Therefore, two queens lie on the same diagonal if and only if j – l = i – k  Condition to ensure - No two queens on same Diagonal
  • 18. Algorithm place( k, i ) // It returns true if a queen can be placed in kth row and ith // column . Otherwise it returns false. x[] is a goal array whose // first( k-1) values have been set. Abs(r) returns the absolute value of r. { for j = 1 to k-1 do { // Two in the same column or in the same diagonal if ( ( x [ i ] = i ) or ( Abs( x[ j ] - i ) = Abs( j - k ) ) ) then return false; } return true ; }
  • 19. Algorithm Nqueen(k, n) // Using backtracking, this procedure prints all possible placements // of n queens on an n×n chessboard so that they are nonattacking. { for i = 1 to n do // check place of column for queen k { if place( k, i ) then { x[ k ] = i; if( k = n ) then write ( x[1:n] ); else NQueens( k+1, n); } }
  • 20. 1 18 2 8 13 19 3 15 31 11 9 14 16 30 29 24 x1=1 x1=2 x2=2 x2=3 x2=4 x2=4 x3=1 x4=3 Portion of the tree that is generated during backtracking( n=4 ). B B B B B B B