0% found this document useful (0 votes)
14 views36 pages

Backtrack Nqueen

Backtracking is a problem-solving approach that involves exploring all possible solutions by constructing a state-space tree, often using a depth-first search method. It is particularly useful for problems like the N-Queens problem, where solutions must satisfy certain criteria, and can be optimized through search pruning and parallelization. The algorithm systematically tests each possibility and backtracks when a path does not lead to a solution, ultimately aiming to find one or more valid configurations.

Uploaded by

subratsahu016
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)
14 views36 pages

Backtrack Nqueen

Backtracking is a problem-solving approach that involves exploring all possible solutions by constructing a state-space tree, often using a depth-first search method. It is particularly useful for problems like the N-Queens problem, where solutions must satisfy certain criteria, and can be optimized through search pruning and parallelization. The algorithm systematically tests each possibility and backtracks when a path does not lead to a solution, ultimately aiming to find one or more valid configurations.

Uploaded by

subratsahu016
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/ 36

BACKTRACKING

ALGORITHM
BACKTRACK
 The term backtracking was coined by American
Mathematician D. H. Lehmer in 1950’s.
 Backtracking is the approach of finding the

solution of any problem by constructing a state-


space tree of a given problem.
 Backtracking uses the brute-force approach, and

this approach is used when the problem has many


solutions and we want to explore all the solutions.
 Backtracking is a methodical way of trying out

various sequences of decisions until you find the


correct one that “works”.
BACKTRACK
 Let we have 3 kids (2 Boys, 1 Girl) and we have 3 Chairs.
 There are 3!=6 arrangements of these kids, in a given 3
chairs.
 We can maintain these arrangements using a state-space
tree.
 State-space tree represents all possible Solutions to the
given problem. B1 G1
B2
 A state space B2tree is a method of displaying all of the
G1 B1 G1 B1 B
possible states of a given problem. 2

G1 B2 G1 B2 B1
B1
BACKTRACK
 Let we have 3 kids (2 Boys, 1 Girl) and we have 3 Chairs.

B1 B2 G1

B1 G1
B2
B2 G1 B1 G1 B1 B
2
Bounding Kill
G1 B2 B1
function G1

S1 S2 S3 S4

Backtracking follows DFS


BACKTRACK

 Backtracking algorithm try each


possibility, until it finds the right
one.
 During the search if alternate does

not work, the search backtrack to


the choice point, and try for the next
alternatives, and so on.
 If all the alternatives are exhausted

(at all choice point), and no solution


found then search fails.
BACKTRACKING APPROACH
 Problems searching for a set of solutions or
which require an optimal solution can be
solved using the backtracking method.
 Backtracking is used to solve problem in

which a sequence of objects is chosen from


a specified set so that the sequence
satisfies some criterion.
 The desired solution is expressed as an n-

tuple (x1,. . . ., xn) where each x i ЄS, S


being a finite set.
 The solution is based on finding one or more

vectors that maximize, minimize, or satisfy


a criterion function P (x1,. . . . ., xn ).
BACKTRACKING APPROACH
 Form a solution and check at every
step if this has any chance of
success. If the solution at any point
seems not promising, ignore it.
 All solutions require a set of
constraints divided into two
categories: explicit and implicit
constraints.
N QUEEN'S PROBLEM
N Queen is the problem of placing N
chess queens on an N˟N chess board so
that no two queens attack to each other.
 Two queen’s attack to each other, when

they are in the same row/column or


diagonal.
 For example, the following is one of the

solutions for 4 Queen Problem. Each


queen is placed such that it does not
attack any other queen on the board.
4 QUEEN PROBLEM
A Queen can attack
vertically, horizontally,
and diagonally.

So,no two queen


"attack", means, no two
of them are on the
same row, column, or
diagonal.
4 QUEEN PROBLEM
How to solve N=4 Queen's problem(𝑄1, 𝑄2,
𝑄3, 𝑄4 )using Backtracking and a state space

 We place queen row-by-row (i.e.𝑄1in row 1, 𝑄2 in


tree for 4 Queen’s.

row 2 and so on). Backtracking gives "all possible


solutions". If you want an optimal solution, then
Step 0: Initialize a 4×4 board.
go for Dynamic programming.
4 QUEEN PROBLEM
 Step 1:
 Put our first Queen (Q1) in the (0,0) cell .

 ‘x‘ represents the cells which is not safe i.e.

they are under attack by the Queen (Q1).


 After this move to the next row [ 0 -> 1 ].
4 QUEEN PROBLEM
Step 2:
Put our next Queen (Q2) in the (1,2) cell .
After this move to the next row [ 1 -> 2 ].
4 QUEEN PROBLEM
Step 3:
At row 2 there is no cell which are safe to place Queen (Q3) .
So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
Step 4:
There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
Put Queen ( Q2 ) at cell ( 1, 3).
4 QUEEN PROBLEM
Step 5: Put queen ( Q3 ) at cell ( 2, 1 ).
4 QUEEN PROBLEM
 Step 6: There is no any cell to place Queen ( Q4 ) at row
3.
 Backtrack and remove Queen ( Q3 ) from row 2.
 Again there is no other safe cell in row 2, So backtrack
again and remove queen ( Q2 ) from row 1.
 Queen ( Q1 ) will be remove from cell (0,0) and move to
next safe cell i.e. (0 , 1).
 Step 7: Place Queen Q1 at cell (0 , 1), and move to next
row.
4 QUEEN PROBLEM
 Step 8: Place Queen Q2 at cell (1 , 3), and move to
next row.
4 QUEEN PROBLEM
Step 9:Place Queen Q3 at cell (2 , 0), and move to
next row.
4 QUEEN PROBLEM
 Step 10: Place Queen Q4 at cell (3 , 2), and
move to next row.
 This is one possible configuration of solution
BACKTRACKING
 Suppose you have to make a series of
decisions, among various choices, where
 You don’t have enough information to know what
to choose
 Each decision leads to a new set of choices
 Some sequence of choices (possibly more than
one) may be a solution to your problem
 Backtracking is a methodical way of trying
out various sequences of decisions, until you
find the correct one that “works”.
BACKTRACKING
 Backtracking is used to solve problems in
which a sequence of objects is chosen from a
specified set so that the sequence satisfies
some criterion.
 Backtracking is a modified depth-first search

of a tree.
 It is the procedure whereby, after

determining that a node can lead to nothing


but dead nodes, we go back (“backtrack”) to
the node’s parent and proceed with the
search on the next child.
BACKTRACK ALGORITHM

 Based on depth-first recursive search


 Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recursive
c) If recursion returns a solution, return it
4. If no choices remain, return failure
 Some times called “search tree”
IMPROVING BACKTRACKING
 Search pruning will help us to reduce the
search space and hence get a solution faster.

 The idea is to avoid those paths that may not


lead to a solutions as early as possible by
finding contradictions so that we can
backtrack immediately without the need to
build a hopeless solution vector.
BACKTRACKING EXAMPLES
 The backtracking can be used in this cases:
 Solving a maze
 Coloring a map
 Solving a puzzle
 N queens problem etc.,
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM

 The 8-queens problem is a classical


combinatorial problem in which it is required
to place eight queens on an 8 x 8 chessboard
so no two can attack each other.

 A queen can attack another queen if it exists


in the same row, column or diagonal as the
queen.

24
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
 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.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)

 It is an empty 8 x 8
chess board. We
have to place the
queens in this
board.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)

 We have placed the


first queen on the
chess board
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
 Then we have
placed the second
queen on the board.
 The darken place

should not have the


queens because
they are horizontal,
vertical, diagonal to
the placed queens.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)

 We have placed the


third queen on
board.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)

 We have placed the


4th queen on the
board.
 We have placed that

in the wrong spot,


so we backtrack and
change the place of
that one.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
 In this way, we have
to continue the
process untill our is
reached ie., we must
place 8 queens on
the board.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
 Backtracking provides the hope to solve
some problem instances of nontrivial sizes
by pruning non-promising branches of the
state-space tree.

 The success of backtracking varies from


problem to problem and from instance to
instance.

 Backtrackingpossibly generates all


possible candidates in an exponentially
growing state-space tree.
PARALLELIZING BACKTRACK
ALGORITHM

 First, we have to parallelize the root node of


the algorithm.
 Then the sub nodes and the child nodes

should be parallelized independently using


the other processors.
 For example, if we take the 8 queens

problem then it can be easily implemented in


parallel.
 The N-queens problem can be parallelized in

this way.
PARALLELIZING BACKTRACK
ALGORITHM
 The solutions to the n-queens problem can be
generated in parallel by using the master-
worker technique.
 The manager generates the upper portion of

the search tree by generating those nodes of


fixed depth d, for some d.
 The manager dynamically passes each of

these sequences to an idle worker, who in turn


continues to search for sequences with n-
queens property that contain the fixed
subsequence of length d.
 The master-worker technique is particularly

well-suited for implementation with MPI


PARALLELIZING BACKTRACK
ALGORITHM
 Parallelizing the backtrack algorithm will
gives us a good speedup and efficiency when
compared to the normal algorithm.
 The speedup and the efficiency will gets

drastically increased when it is done in the


parallel.
THANK YOU!!!

You might also like