Professional Documents
Culture Documents
Backtracking: General Method
Backtracking: General Method
GENERAL METHOD
• 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).
1
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING (Contd..)
• Suppose there are m n-tuples which are
possible candidates for satisfying the
function P.
• Then m= m1, m2…..mn where mi is size of
set si 1<=i<=n.
• The brute force approach would be to form
all of these n-tuples and evaluate each one
with P, saving the optimum.
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING (Contd..)
• The backtracking algorithm has the ability to yield
the same answer with far fewer than m-trials.
• In backtracking, the solution is built one
component at a time.
• Modified 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. 3
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING (Contd..)
• The constraints may be of two categories.
• EXPLICIT CONSTRAINTS are rules which restrict the
values of xi. Examples xi 0 or x1= 0 or 1 or li xi ui.
• All tuples that satisfy the explicit constraints define a
possible solution space for I.
• IMPLICIT CONSTRAINTS describe the way in which
the xi must relate to each other .
• Implicit constraints allow to find those tuples in the
solution space that satisfy the criterion function.
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Example : 8 queens problem
• The problem is to place eight queens on an 8 x 8
chess board so that no two queens attack i.e. no two
of them are on the same row, column or diagonal.
• Strategy : The rows and columns are numbered
through 1 to 8.
• The queens are also numbered through 1 to 8.
• Since each queen is to be on a different row
without loss of generality, we assume queen i is to
be placed on row i .
5
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
8 queens problem (Contd..)
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
8 queens problem (Contd..)
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
8 queens problem (Contd..)
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q 8
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree representation
Solution Space :
• 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).
9
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree representation contd..
10
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree representation contd..
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree representation contd..
12
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree representation contd..
• BREADTH-FIRST-SEARCH : Branch-and
Bound with each new node placed in a
queue .
The front of the queen becomes the new E-
node.
• DEPTH-SEARCH (D-Search) : New nodes
are placed in to a stack.
The last node added is the first to be
explored.
13
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Example : 4 Queens problem
1 1 1 1
. . 2 2 2
3
. . . .
1 1
2
3
. . 4
14
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree: 4 Queens problem
1
x1 = 1 x1=2
2 18
x2=2 3 4 x2=1 x2=3 x2 = 4
B 3 8 13 19 24 29
x3=3 x3=4 2 3 B B
4 6 14 16 x3 = 1
x4=4 3 B 30
5 7 15 x4 = 3
B 31
15
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree: 4 Queens problem contd..
16
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree: 4 Queens problem contd..
17
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
State space tree: 4 Queens problem contd..
• Node 8 is generated, and the path is (1,3).
• Node 8 gets killed as all its children
represent board configurations that cannot
lead to answer. We backtrack to node 2 and
generate another child node 13.
• But the path (1,4) cannot lead to answer
nodes.
• So , we backtrack to 1 and generate the
path (2) with node 18. We observe that the
path to answer node is (2 4 1 3 )
• Other solution is (3, 1, 4, 2)
18
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GENERAL BACKTRACKING METHOD
19
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GENERAL BACKTRACKING METHOD
(Contd..)
Procedure backtrack (n) // solution vectors are X(1:n)//
// and printed as soon as generated //
// T {X(1),….X(k-1)} gives all possible values of X(k)
// given that X(1),…..,X(k-1) have already been chosen //
// The predicates Bk (X(1),….X(k) ) determine those
elements //
// which satisfy the implicit constraints //
// Implicit constraints are “no two X is can be the same” //
Integer k, n; local X (1:n)
20
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
GENERAL BACKTRACKING METHOD (Contd..)
K 1
while K <> 0 do
if there remained an untried X(k) such that X(k)
T ( X (1) , …..X(k-1) ) and Bk (X (1) ,…,X(k) ) = true then
21
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
EXAMPLE OF ALGORITHM WITH
4 QUEENS PROBLEMS
k 1
loop 1 X0 =1,1 {1,2,3,4} and B1 (X (1)) = true
1 is not a path to answer node
K 1+1=2
repeat K 2
loop 2
X(2){2,3,4}and B2 (1,2) is False but there
remains untried X(k)=3 and X(K) =4
22
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
EXAMPLE OF ALGORITHM WITH
4 QUEENS PROBLEMS (Contd..)
B2 (1,3)=true , but (1,3) Is not a path to
answer node ; so K K+1=3
There is no X(3) such that
B3(1,3,2) or B3 (1,3,4) is true .
So backtrack K K-1=2
consider X (2) 4
23
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
EXAMPLE OF ALGORITHM WITH
4 QUEENS PROBLEMS (Contd..)
(1,4) is not a path to answer
(1,4,2) is not a path to answer
B4(1,4,2,3)=false.
Thus X(2)=4 is false
With X(1)=1, we have seen that there is no X(2)
satisfying the constraints so KK-1=2-1=1
There remained untried X(k)=2,3,4.
Repeating with X(1)=2, we observe (2,4,1, 3) is a path to an
answer node .
Similarly the other solution is (3,1,4,2)
24
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING ALGORITHM WITH
RECURSION
Procedure RBACKTRACK (k)
// on entering the first K-1 values X(1),…..,X(k-1)//
// of the solution vector X(1:n) have been assigned //
global n , X(1:n)
for each X(k) such that
X(k) T ( X (1),..X(k-1) ) Do
if Bk (X(1)..,X(k-1), X(k) )= true then {
if ( X (1) ,….,X(k) ) is a path to an answer node
then print ( X(1),……,X(k) ) endif
if (k<n)
CALL RBACKTRACK (k+1)
endif }// end of if Bk (X(1)…//
repeat // end of for each X(k)…//
end RBACKTRACK 25
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING ALGORITHM WITH
RECURSION (Contd..)
26
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
BACKTRACKING ALGORITHM WITH
RECURSION (Contd..)
but (1,3,2) is not Answer node , so , RB(4) is
called B4 (1,3,2,4) = false.
(1,3,4,2) is not bounded so , X(2) = 4 is tried
(1,4,2,3) is not bounded . With X(1)=1 no solution
Now for X(1) = 2,3,4 repeat
(2,4,1,3) is an answer node, other paths with X(1)
= 2 are not leading to answer node.
With X (1) = 3 , (3,1,4,2) is an answer node.
X (1) = 4 - no solution.
27
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
EFFICIENCY OF BACKTRACKING (BT)
ALGORITHM
28
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
EFFICIENCY OF BACKTRACKING
ALGORITHM (Contd..)
• The first three are relatively independent of
the problem instance being solved.
• The complexity for the first three is of
polynomial complexity .
• If the number of nodes generated is 2n , then
the worst case complexity for a
backtracking algorithm is O(P(n)2n) where
P(n) is a polynomial in n .
29
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Estimation Of Nodes generated in a
BT Algorithm
• Generate a random path in the state space tree.
• Let X be a node at level i on this path.
• Let mi be the children of X ( at level i+1 ) that do
not get bounded. (i.e. mi are the nodes which can
be considered for getting an answer node ).
• Choose randomly one of the mi.
• Continue this until this node is either is a leaf or
all its children are bounded.
30
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Estimation of Nodes generated in a
BT Algorithm (Contd..)
• Let m be the no. of unbounded nodes to be
generated.
• Let us assume that the bounding functions
are static, i.e., the BT algorithm does not
change its bounding functions.
• The number of estimated number of
unbounded nodes
=1+m1+m1m2+….. +m1m2m3..mi where mi is
the estimated no. of nodes at level i+ 1.
31
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Estimation of Nodes generated in a
BT Algorithm (Contd..)
• The number of unbounded nodes on level one
is 1.
• The number of unbounded nodes on level 2 is
m1
• The total no. of nodes generated till level 2 is
1 + m1
The total number of nodes generated till level
i+1 is 1+m1+…+m1…mi .
– The above procedure can be written as an algorithm.
32
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
Estimation of Nodes generated in a BTAlgorithm (Contd..)
Procedure Estimate
// This procedure follows a random path and estimates//
// number of unbounded nodes //
m1; r1; k1
loop
Tk {X (k): X(k) T ( X(1) ,….X(k-1)) and Bk (X (1), ….,
X(k)) is TRUE}
If size (Tk) = 0 then exit endif // SIZE returns the //
r r * SIZE (Tk) // size of the set Tk //
m m+r
X (k) CHOOSE (Tk) // CHOOSE makes a random
choice of an element in Tk //
k k+1
repeat
return (m)
end estimate
33
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)
The n-queens problem and solution
34
Create PDF files without this message by purchasing novaPDF printer (http://www.novapdf.com)