Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Data Structure and Algorithms

Dr. D. P. Acharjya
Professor, SCOPE
22-Mar-17 Dr. D. P. Acharjya 1
Backtracking
Suppose you have to make a series of decisions,
among various choices, where
You dont 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
one that works.
22-Mar-17 Dr. D. P. Acharjya 2
Graphical View

22-Mar-17 Dr. D. P. Acharjya 3


Terminology: Tree
A tree is an acyclic graph composed of nodes.
There are three kinds of nodes: Root node, Internal nodes,
Leaf nodes.

Backtracking can be thought of as searching a tree for a


particular goal leaf node.
22-Mar-17 Dr. D. P. Acharjya 4
Various Problems

The 8-Queens problem


The Knapsack problem
Sum of Subsets problem
Graph Coloring problem
Hamiltonian Cycles

22-Mar-17 Dr. D. P. Acharjya 5


N Queens Problem
Find an arrangement of n - queens
on a (n n) board such that no two
queens are attacking one another.

In chess, queens can move all the way


down any row, column or diagonal
(so long as no pieces are in the way).

Due to the first two restrictions, it's


clear that each row and column of the
board will have exactly one queen.

22-Mar-17 Dr. D. P. Acharjya 6


Backtracking Strategy
Place a Queen on the first available square in row
1.
Move onto the next row, placing a queen on the
first available square there (that doesn't conflict
with the previously placed queens).
Continue in this fashion until either:
You have solved the problem, or
You get stuck: Remove the queens that got you
there, until you get to a row where there is
another valid square to try.
22-Mar-17 Dr. D. P. Acharjya 7
4-Queens Problem
Suppose a queen is placed at the cell (1,2) (1st row,
2nd Col)
Cells (1, 0), (1, 1), (1, 3) are in attack according
to 1st row.
Cells (0, 2), (2, 2), (3, 2) are in attack according
to 2nd column.
Cells (0, 1), (2, 3) are in attack according to
diagonal 1. This is based on (row-column) value.
Cells (0, 3), (2, 1), (3. 0) are in attack according
to diagonal 2. This is based on (row + column)
value.
22-Mar-17 Dr. D. P. Acharjya 8
Our recursion will be 4-level deep. At
the 0th level recursion we are going to
place 0th queen 0th row; 1st queen on
1st row in 1st level and so on such
that no queen attack previously
placed queen.

22-Mar-17 Dr. D. P. Acharjya 9


Solution to 4-Queens Problem

0 1 2 3 0 1 2 3
Level 0 Q (0, 0) 0 Q

0 1 2 3
1 Q
Level 1 Q (1, 2)
2
0 1 2 3
3
Level 2

22-Mar-17 Dr. D. P. Acharjya 10


0 1 2 3 0 1 2 3
Level 0 Q (0, 0) 0 Q
0 1 2 3
1 Q
Level 1 Q (1, 3)
2 Q
0 1 2 3
Level 2 3
Q (2, 1)
0 1 2 3
Level 3

22-Mar-17 Dr. D. P. Acharjya 11


0 1 2 3 0 1 2 3
Level 0 Q (0, 1) 0 Q
0 1 2 3
1 Q
Level 1 Q (1, 3)
2 Q
0 1 2 3
Level 2 3 Q
Q (2, 1)
0 1 2 3
Level 3 Q (3, 2)

22-Mar-17 Dr. D. P. Acharjya 12


Algorithm (Placing of new Queen)
Algorithm Place (k, i)

1. for j = 1 to (k-1) do
2. {
3. if ((x[j]=i) or (Abs(x[j]-i)=Abs(j-k)))
4. then return false
5. }
6. return true

22-Mar-17 Dr. D. P. Acharjya 13


Algorithm (Solution to n-Queens
Problem)
Algorithm NQueens (k, n)

1. for i =1 to n do
2. {
3. if Place (k, i) then
4. {
5. x[k]=i
6. if (k=n) then write (x[1:n])
7. else NQueens (k+1,n)
8. }
9. }

22-Mar-17 Dr. D. P. Acharjya 14

You might also like