You are on page 1of 15

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).
• 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.
• 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.
• 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 these tuples in the solution space that
satisfy the criterion function.
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 .
• The solution is an 8 tuple (x1,x2,.....,x8) where xi is the column on
which queen i is placed.
• The explicit constraints are :

1
• Si = {1,2,3,4,5,6,7,8} 1 ≤ i ≤ n or 1 ≤ xi ≤ 8
i = 1,.........8
• The solution space consists of 88 8- tuples
• The implicit constraints are :
• (i) no two xis can be the same that is, all queens must be on
different columns.
• (ii) no two queens can be on the same diagonal.
• reduces the size of solution space from 88 to 8! 8 – tuples.
• Two solutions are (4,6,8,2,7,1,3,5) and (3,8,4,7,1,6,2,5)

BACKTRAC

1 2 3

Solution Space :

1
• 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.
2
• 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

3
4 Q
2
• The problem may be solved by systematically generating the problem
states determining which are solution states, and determining the
answer states.
• Let us see the following terminology
• LIVE NODE A node which has been generated and all of whose
children are not yet been generated .
• E-NODE (Node being expanded) - The live node whose children are
currently being generated .
• DEAD NODE - A node that is either not to be expanded further, or
for which all of its children have been generated.
• DEPTH FIRST NODE GENERATION- In this, as soon as a new
child C of the current E-node R is generated, C will become the new
E-node.
R will become E-node again when C has been fully explored.
• BOUNDING FUNCTION - will be used to kill live nodes without
generating all their children.
• BACTRACKING-is depth – first node generation with bounding
functions.
• BRANCH-and-BOUND is a method in which E-node remains E-node
until it is dead.
• 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.

3
BACKTRAC
Example : 4 Queens p
1 1
BACKTRAC . . 2

. If (x1….xi) is the path to the current E-node , a bounding function has the
criterion that (x1..xi+1) represents a chessboard configuration, in which no
queens are attacking.
1 x1 =11
• A node that gets killed as a result of the bounding function has a B
under it.

2
x2=2 3 3 4
4
• We start with root node as the only live node. The path is ( ); we
generate a child node 2.
• The path is (1).This corresponds to placing queen 1 on column 1 .
• Node 2 becomes the E node. Node 3 is generated and immediately
killed. (because x1=1,x2=2).
• As node 3 is killed, nodes 4,5,6,7 need not be generated.
• 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
• GENERAL BACKTRACKING METHOD (Contd

• All answer nodes are to be found
• If (x1…..xi) is a path from root to a node then T (x1,….,xi) be the set
of all possible values for Xi+1, such that (x1,x2,…….,xi,xi+1) is also
a path from root to a problem state.
• B(x1…xi+1) or Bxi+1is false for the path (x1,..,xi+1) if the path
cannot reach an answer node.
• The solution vector X (1: n) are those values which are generated by T
and satisfy Bi+1.
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-1) ) determine// //those elements which
satisfy the//
// implicit constraints//
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-1) ) 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)
K← 1
while K > 0 do

5
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
if (X (1),…, X(k) ) is a path to an answer node then
print ( X (1) ,…X (k) )
end if
K← K+1 // consider next set //
else K← K-1 // backtrack to previous set
endif
repeat
end Backtrack
EXAMPLE OF ALGORITHM WITH 4 QUEENS PROBLEMS
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
(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 K←K-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)
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) ) and Bk (X(1)..,X(k-1), X(k) )= true do
if ( X (1) ,….,X(k) ) is a path to an answer node
then print ( X(1),……,X(k) ) end if
If (k<n)
CALL RBACKTRACK (k+1)
endif
repeat
end RBACKTRACK

6
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.
EFFICIENCY OF BACKTRACKING ALGORITHM
• The time required by a backtracking algorithm or the efficiency
depends on four factors
(i) The time to generate the next X(k);
(ii) The no. of X(k) satisfying the explicit constraints
(iii) The time for bounding functions Bi
(iv) The no. of X(k) satisfying the Bi for all i.
• 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 .
The n-queens problem and solution
• In implementing the n – queens problem we imagine the chessboard
as a two-dimensional array A (1 : n, 1 : n).
• The condition to test whether two queens, at positions (i, j) and (k, l)
are on the same row or column is simply to check I = k or j = l
• The conditions to test whether two queens are on the same diagonal or
not are to be found

7
The n-queens p
c

• Observe that
• Thus two queens are placed at positions (i, j) and (k, l), then they are
on the same diagonal only if
i – j = k - l or i + j = k+l
or j - l = i - k or j - l = k - i
i) For the element
|j – l| = |i - k|
The n-queens problem -Algorithm
the upper left to
• Two queens lie on the same diagonal if and only if

Procedure PLACE (k)


Right diagonal,
//returns true if a queen can be placed in the kth row and X(k)th column
w.r.t.already placed rows 1..k-1//
global X (1:k); integer i, k
for i ← 1 to k-1 do column values a
// test if X (k) is on the same column or same// //diagonal with any of
X(1) ….X(k-1)//
if X(i) = X(k)// two are in the same column// or row- column
or ABS(X(i) –X(k)) = ABS(i-k) // in the same diagonal //
then Return (false)
end if
repeat
e.g. 1-1=2-2=3-
ii) For the element
return (true)
end PLACK
Above procedure is used in the solution to n queens problem

diagonal, row + 8

1+4=2+3=3+2=
Procedure N queens(n)
//solves n queens problem, gives all solutions //
integer k,n; X(1:n)
X(1)← 0 ; k←1 // k is the current row,//
//X(k) is the current column //
while k > 0 do
X(k) ⇓ X(k) + 1 // move to the next column //
While X(k) ≤ n and not PLACE(k)
// queen k cannot be placed at X(k) th column //
X(k) ⇓ X(k) + 1 // move to the next column //
repeat
if X(k) ≤ n // place(k) is true i.e.a position X(k) //
//is found for queen k //
then if k = n // Is a solution complete ? //
then print(X)
else k ← k+1;
X(k) = 0; //compute next component X (k) //
endif
else // no X(k) is suitable for placing queen k //
k← k-1 //backtrack//
endif
repeat
end NQUEENS
Example :
k←1, X (1) ← 0
k > 0 so X(1) ← 0+1 = 1
Place (1) is true so queen 1
is placed in X(1) = 1st column
X(1) ≤ 4 but 1 ≠ 4 so k←1+1=2, X(2)←0
while loop is repeated
X(2)← 0+1=1
X(2) ≤ 4 and PLACE (2) is false
as X(1)=1=X(2) =1
PLACK(3) is true
GRAPH COLOURING PROBLEM
• Let G be a graph and m be a positive integer .
• The problem is to colour the vertices of G using only m colours in
such a way that no two adjacent nodes / vertices have the same color.
• It is necessary to find the smallest integer m.

9
m is referred to as the chromatic number of G.
• A special case of graph colouring problem is the four colour problem
for planar graphs
• A graph is planar iff it can be drawn in a plane in such a way that no
two edges cross each other.
• 4- colour problem for planar graphs Given any map, can the regions
be coloured in such a way that no two adjacent regions have the same
colour with only four colours?

GRAPH C
PROBLE
Solving the Graph Colouring Problems
• The graph is represented by its adjacency matrix Graph (1:n,1:n)
where GRAPH (i,j) = true if <i,j> is an edge and Graph (i,j) = false
otherwise.
4
• The colours will be represented by the integers 1,2….m and the
5
solution with n–tuple (X(1),….X(n)), where X(i) is the colour of
node i.
• The solution can be represented as a state space tree.
• Each node at level i has m children corresponding to m possible

2
assignments to X(i) 1≤i≤m.
• Nodes at level n+1, are leaf nodes. The tree has degree m with height
n+1.

1
3 10
State space tree for m
n=3a
Graph Colouring Problem- Algorithm
Procedure MCOLORING (k)
// Recursive backtracking for//
// graph colouring problem // X(1)=1
// k is the index of the next vertex to colour //
global integer m,n,X(1:n);
boolean GRAPH (1:n,1:n)
integer k
loop //generates values for X(k
Solving the Graph Colouring Problems
2
if X(k) = 0 then exit endif
// no new colour is possible //
X(2)=1
Call NEXTVALUE(K) // assign to X(k) a legal colour //

if k = n then Print X
// m colours are assigned to n vertices // 2 3
else call MCOLOURING(k+1)
endif
repeat 3
end MCOLOURING
Procedure NEXTVALUE(k)
X(3)=1
// X(1)….X(k-1) have been assigned //
// to vertices 1,…k-1 //
// X(k) is the next highest numbered // 2 3
11
colour different from those adjacent to k // global integer m,n X(1:n)
boolean GRAPH(1:n,1:n)
integer j,k
loop
X(k)⇓( X(k) +1 ) mod(m+1)
//next highest colour//
if X(k)=0 then return endif
//All colours have been exhausted//
for j⇓ 1 to n do
//Check if this colour is distinct from //
//adjacent colours//
if GRAPH(k,j) and // if (k,j) is an edge //
X(k)=X(j)
then exit endif
repeat
if j = n+1 then return endif
// the loop is exited with the last value //
// i.e. j = n+1 and hence new colour is found //
repeat // try to find another colour //
end NEXTVALUE

Solving the G
Problem
Example for NEXTVALUE
HAMILTONIAN CYCLES

Let m = 3 n = 4
• Let G=(V,E) be a connected graph with n vertices .

Nextvalue(1) 12
• A Hamiltonian cycle is a round path along n edges of G which visits
every vertex once and returns to its starting position.
• The tour of a traveling salesperson problem is a Hamiltonian cycle.
• A tour may exist or not.

HAMILTO
(C

• The backtracking solution is a vector (x1,……,xn) where xi represents
the ith visited vertex of the cycle.
• To avoid printing of the same cycle n times we require X(1) = 1 (as
128765431, 287654312, 87654312)
• We compute X(k) given (x1…..xk-1) have already been chosen.
• Two procedures NEXTVALUE(k) and HAMILTONIAN are used,to
find the tour.
1 2
• We initialize Graph (1:n,1:n) and X(2:n)⇓0, X(1)⇓1 and start with
3
HAMILTONIAN (2).
• algorithem
Procedure HAMILTONIAN(k)
// uses recursive backtracking //
// All the H- cycles are to be found //
// All cycles begin at vertex 1//
global integer X(1:n)
local integer k,n
loop // generate values for X(k) //
call NEXTVALUE(k)
// assign a legal vertex to X(k) //

8 7 13
6
if X(k)=0 then return endif
if k = n
then print (X, ’1’)
// a cycle is printed //
else print (HAMILTONIAN (K+1))
endif
repeat
end HAMILTONIAN
Procedure NEXTVALUE(k)
// X(1) ,.,x(k-1) is a path of k-1vertices,//
//X(k) is the next highest value vertex,//
//X(k) = 0 means, no vertex has been //
//assigned to X(k) //
global integer n,X(1:n);
boolean GRAPH(1:n,1:n)
integer k, j

loop
X(k)⇓ (X(k)+1) mod(n+1) // next vertex //
if X(k) = 0 then return endif
if GRAPH (X(k-1),X(k)) // if there is an edge //
then for j⇓1 to k-1 do
// check whether the same vertex is generated //
if X(j) = X(k)
then exit // for loop exit //
endif
repeat // for //

if j = k // the for loop is exited with //


// last value I.e. j = k –1+1 = k //
then
if k < n or (k = n and GRAPH(X(n),1))
then return endif
endif
endif
repeat
end NEXTVALUE

Example:
Let n = 8

14
X(1) = 1, HAMILTONIAN(2) i.e.H(2)
is called, so NEXTVALUE(2) i.e.N(2) is called.
Initially X(2)=0
X(2)=0+1 mod 9 = 1 but X(1) = X(2)
so loop is repeated and X(2) = 2 mod 9 = 2
X(1)≠X(2) and j=k=2, k < 8 so return 2
NV(3)=8 as Graph(2,3), Graph(2,5)
Graph(2,6),Graph(2,7),Graph(2,4) are false.
Thus NV(4) = 7,NV(5) = 6,NV(6) = 5 NV(7) = 4, NV(8) = 3.
At NV(8), k = 8 and GRAPH(X(8),1)
is satisfied. Thus
The cycle is printed.

15