Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Back­track­ing

1
Introduction

Contents:

• Backtracking
• N-Queen Problem

2
What is Back­track­ing?

Backtracking is a problem-solving strategy for finding all the solutions to


a problem, by rejecting the solutions that do not satisfy the given
constraint. In Backtracking, we pick a path and start solving the problem
along that path. At any point in time, if we find that the path is leading
to an invalid solution, we stop moving along that path and return back to
explore other paths.

3
What is Back­track­ing?

• Recur­sion is the key in back­track­ing. As the name sug­gests


we back­track to find the solu­tion.

• We start with one pos­si­ble move out of many avail­able


moves and try to solve the prob­lem if we are able to solve
the prob­lem with the selected move then we will print the
solu­tion else we will back­track and select some other
move and try to solve it.

• If none of the moves work out, we will claim that there is


no solu­tion for the problem.
4
What is Back­track­ing?

5
When Backtracking is used?
• Backtracking is used when there are multiple
solutions for a problem and you need all
solutions.
• It’s not used for Optimization problem.

6
Problems on Backtracking
• Problems on Backtracking Algorithm:
– N-Queen problem
– Subset Sum Problem
– Hamiltonian Circuit problem
• Depth-First Search or DFS algorithm is a recursive
algorithm that uses the backtracking principle.

7
Gen­er­al­ized Algorithm
Pick a starting point.
while (Problem is not solved)
For each path from the starting point.
check if selected path is safe,
if yes select it and make recursive call to rest of the
problem
If recursive calls returns true, then return true
else undo the current move and return false.
End For
If none of the move works out, return false
NO SOLUTON

8
N-Queen Problem

• Objec­tive : In chess, a queen can move as far as she


pleases, hor­i­zon­tally, ver­ti­cally, or diag­o­nally. A normal
chess board has 8 rows and 8 columns.
• The stan­dard 8 by 8 Queen’s prob­lem asks how to place
8 queens on an ordi­nary chess board so that none of
them can attack any other in one move.

9
N-Queen Problem

• Is it possible to arrange them this way?


• Yes, it is possible. There will be more than one solution
and we need to find all of them

10
N-Queen Problem

• The idea is to place queens one by one in different columns,


starting from the leftmost column.

• When we place a queen in a column, we check for clashes


with already placed queens.

• In the current column, if we find a row for which there is no


clash, we mark this row and column as part of the solution.

• If we do not find such a row due to clashes then we


backtrack and return false.
11
N-Queen Problem
• The backtracking strategy is as follows:
1) Place a queen on the first available square in row 1.

2) Move onto the next row, placing a queen on the first available
square there (that doesn't conflict with the previously placed
queens).

3) Continue in this fashion until either:


a) you have solved the problem, or
b) you get stuck.

When you get stuck, remove the queens that got you there, until you
get to a row where there is another valid square to try.
12
N-Queen Problem

• Rules
The Queens will not be in the
– Same row
– Same column
– Same diagonal

13
4-Queen Problem

• The 4-Queens Problem consists of placing four queens on


a 4 x 4 chessboard in such a way that no two queens can
attack each other. That is, no two queens are allowed to
be placed on the same row, the same column or the
same diagonal.

There are 4 queens and place them on 4 x 4


chessboard. How many ways the queens can
be places?
There are 16 cells for 4 queens.
There will be 16C4 = 1820

14
4-Queen Problem

No Place for 3rd Queen

No Place for 4th Queen

15
4-Queen Problem

That’s a solution…

16
State space tree of the 4-queen problem

17
Tree organization of the 4-Queen solution space

A possible tree organization for the case n = 4. A tree such as this is called
a permutation tree. The edges are labeled by possible values of x_{i}
Edges from level 1 to level 2 nodes specify the values for x_{1} . Thus, the
leftmost subtree contains all solutions with x_{1} = 1 ; its leftmost subtree
contains all solutions with x_{1} = 1 and x_{2} = 2 and so on. Edges from
level i to level i + 1 are labeled with the values of x_{i} . The solution space
is defined by all paths from the root node to a leaf node.

There are 4! = 24 leaf nodes in the tree.

The total number of nodes in the tree = 1+ 4 + (4*3) + (4*3*2) +


(4*3*2*1)
= 65

18
Portion of the tree that is generated during backtracking

19
4-Queen Problem (Solution)

20
Two solutions of 4 Queen Problem

21
How many solutions for 8-Queen
Problem?

• The 8-queens problem has 92 distinct solutions.

• If rotations and reflections are considered equivalent,


there are 12 unique solutions.

22
Thank you

23

You might also like