DAA Unit 4 Backtracking
DAA Unit 4 Backtracking
UNIT-VI–BACKTRACKING
Backtracking: General method, Applications-N-QUEEN Problem, Sum of Sub Sets problem,
Graph Coloring, Hamiltonian Cycles, 0/1 Knapsack Problem.
-0-0-0-0-
Introduction
ALGORITHMtry(v1,...,vi)
{
IF(v1,...,vi)is a solution THEN RETURN(v1,...,vi) FOR
each v DO
IF(v1,...,vi,v)is acceptable vector THEN
sol = try(v1,...,vi,v)
IF sol!=()THEN RETURN sol END
END
RETURN()
}
If Si is the domain of vi, then S1 × ... × Sm is the solution
space of the problem. The validity criteria used in checking
for acceptable vectors determines what portion of that space
needs to be searched, and so it also determines the resources
required by the algorithm.
Fig)Tree(Problem State)
Fig)Tree(Solution state)
Fig)Tree(answer states)
In fig (b) node A is not a live node but B,C are live
nodes.
In fig(c)nodes A,B are not live and D, E Care live nodes.
Algorithm place(k,i)
{
forj:=1tok-1 do
if((x[j]=i)or(abs(x[j]-i)=abs(j-k))then return
false;
returntrue;
}
Algorithmnqueens(k,n)
{
fori:=1tondo
{
if(place(k,i)then
{
X[k]:=i;
if (k=n) then
write(x[i:n);
else
nqueens(k+1,n);
}
}
}
Fora4x4chessboardthereare16C4possiblewaystoplace
4 Queens using brute force approach. However by allowing
only placements of queens on distinct rows and columns, we
require the examination of at most 4! Tuples.
1 2 3 4
Go to second row 1 1
Move the queen to another col. 2 - - - 2
Another possibility is column 4. 3
Move to col4. 4
Now X[2]=4 X[2]=4
1 2 3 4
Second queen in second row
First col under attack 1 1
Second column under attack 2 - - - 2
Third col under attack 3
4th col not under attack 4
Soplacequeenin4thcol X[2]=4
X[2]=4
1 2 3 4
To place third queen in third row 1
1
First col not under attack
2 2
So place the queen in first col
3 3
X[3]=1
4
X[3]=1
nqueens(k,n) place(k,i)
nqueens(3,4)place(3,1)returnsFalse
place(3,1)returnsFalse
place(3,1)returnsFalse
place(3,1)returnsFalse
Backtracking
nqueens(3,4)place(3,1)returnsFalse
place(3,2)returnsTruesoX[3]=2
nqueens(4,4)place(4,1) returns False
place(4,2)returnsFalse
place(4,3)returnsFalse
place(4,4)returnsFalse
Backtracking
nqueens(1,4)place(1,2)returnsTruesox[1]=2
nqueens(2,4) place(2,1)returnsFalse
place(2,2) returns False
place(2,3) returns False
place(2,4)returnsTruesoX[2]=4
nqueens(3,4) place(3,1)returnsTruesoX[3]=1
No of vertices=n
No of colors= m
Solution vector=X[1],X[2],X[3] ....... X[n]
The values of solution vector may be longs to{0,1,2,3..m}
Algorithm nextvalue(k)
{
Repeat
{
X[k]=(x[k]+1)mod(m+1); //next highest color
if (x[k]=0) then
return; //all colors have been used
for j:=1 to n do
{
if((G[k,j]!=0)and(x[k]=x[j]))then break;
//g[k,j]an edge and
//vertices k and j have same color
}
if(j=n+1)thenreturn;
}until(false);
}
mcoloring(1) i.e.k=1
nextvalue(1)
k=1
x[1]=(x[1]+1)mod(m+1)
x[1]=0+1mod4 x[1]=1
G[k,j]!=0andx[k]=x[j]
j=1G[1,1]false and true=false
j=2G[1,2]trueandfalse=false
j=3G[1,3]falseandfalse=false
j=4G[1,4]trueandfalse= false
x[1]=1,x[2]=0,x[3]=0,x[4]=0
mcoloring(2) i.e.k=2
nextvalue(2)
k=2
x[2]=(x[2]+1)mod(m+1)
x[2]=0+1mod4 x[2]=1
G[k,j]!=0andx[k]=x[j]
j=1 G[2,1] True and True=True break
G[2,1] is an edge and
adjacent vertices have same color
x[2]=(x[2]+1)mod(m+1)
x[2]=(1+1)mod4=2mod4
x[2]=2
G[k,j]!=0andx[k]=x[j]
j=1G[2,1]TrueandFalse=False
j=2G[2,2]FalseandTrue=False
j=3G[2,3]TrueandFalse=False
j=4G[2,4]FalseandFalse= False
x[1]=1,x[2]=2,x[3]=0,x[4]=0
mcoloring(3) i.e.k=3
nextvalue(3)
k=3
x[3]=(x[3]+1) mod(m+1)
x[3]=0+1mod 4
x[3]=1
G[k,j]!=0and x[k]=x[j]
j=1G[3,1] False and True= False
j=2G[3,2] True and False= False
j=3G[3,3] False and True= False
j=4G[3,4] True and False= False
x[1]=1,x[2]=2,x[3]=1,x[4]=0.
mcoloring(4) i.e.k=4
nextvalue(4)
k=4
x[4]=(x[4]+1)mod(m+1)
x[4]=0+1mod4 x[4]=1
G[k,j]!=0andx[k]=x[j]
j=1G[4,1] true and true=True so break
adjacent vertices have same color
x[4]=(x[4]+1) mod(m+1)
x[4]=1+1mod 4
x[4]=2
G[k,j]!=0 and x[k]=x[j]
j=1G[4,1] True and False = False
j=2G[4,2] False and True = False
j=3G[4,3] True and False = False
j=4G[4,4] False and True = False
x[1]=1,x[2]=2,x[3]=1,x[4]=2
Algorithmnextvalue(k)
{
Repeat
{
X[k]:=(x[k]+1)mod(n+1);
if (x[k]=0) then
return;
if(G[x[k-1],x[k]]]!=0)then
{
Forj:=1tok-1 do
if(x[j]=x[k])then break;
if(j=k)then
if((k<n)or((k=n)andG[x[n],x[1]]!=0))then return;
}
}until(false);
}
Algorithm to generate nextvertex.
AlgorithmHamiltonian(k)
{
Repeat
{
nextvalue(k);
if(x[k]=0)thenreturn;
if(k=n)thenwrite(x[1:n]); else
Hamiltonian(k+1);
}until(false);
}
Example)
No of verticesn=8
Adjacency matrix G Solution vertex
12345678
1 0 1 1 0 0 0 1 1 X[1] 1
2 1 0 1 0 0 0 0 1 X[2] 0
3 1 1 0 1 0 1 0 0 X[3] 0
4 0 0 1 0 1 0 0 0 X[4] 0
5 0 0 0 1 0 1 0 0 X[5] 0
6 0 0 1 0 1 0 1 0 X[6] 0
7 1 0 0 0 0 1 0 1 X[7] 0
8 1 1 0 0 0 0 1 0 X[8] 0
If(G[x[k-1],x[k]]!=0)
G[2,1]edge True
X[3]=(x[3]+1)mod(8+1)
X[3]=(1+1)mod9
=2
If(G[x[k-1],x[k]]!=0)
G[2,2]edge False
X[3]=(x[3]+1)mod(8+1)
X[3]=(2+1)mod9
=3
If(G[x[k-1],x[k]]!=0)
G[2,3]edge True
J=1 isx[j]=x[k]
Is1=2nofalse
J=2 is2=3no false
Hamiltonian(4)k=4
Nextvalue(4) k=4
X[4]=(x[4]+1)mod(8+1) Solution
=(0+1)mod9=1 X[4]=1 vector
Isthereanedgebetweenkandk-1
X[1] 1
If(G[x[k-1],x[k]]!=0) X[2] 2
G[3,1]edge True X[3] 3
X[4] 0
Are there duplicate vertices in the path
X[5] 0
J=1is x[j]=x[k]
X[6] 0
Is x[1]=x[4]
X[7] 0
1=1 True break
X[8] 0
If(G[x[k-1],x[k]]!=0)
G[3,2]edge True
J=2isx[j]=x[k]
isx[2]=x[4]
2=2 True Break
X[4]=(x[4]+1)mod(8+1)
X[4]=3
If(G[x[k-1],x[k]]!=0)
Solution
G[3,3]edge False
vector
X[4]=(x[4]+1)mod(8+1)
X[1] 1
X[4]=4
X[2] 2
X[3] 3
If(G[x[k-1],x[k]]!=0)
X[4] 4
G[3,4]edge True
X[5] 0
X[6] 0
J=1is x[j]=x[k] X[7] 0
Is1=4nofalse X[8] 0
J=2is2=4nofalse
J=3is3=4nofalse
As k<n returns till we need to add vertices
Hamiltonian(5)k=5
Nextvalue(5) k=5
X[5]=(x[5]+1)mod(8+1)
=(0+1)mod9=1 X[5]=1
Isthereanedgebetweenkandk-1
If(G[x[k-1],x[k]]!=0)
G[4,1]no edge False
X[5]=(x[5]+1)mod(8+1)
=(1+1)mod9=2
If(G[x[k-1],x[k]]!=0)
G[4,2]no edge False
X[5]=(x[5]+1)mod(8+1)
X[5]=5
If(G[x[k-1],x[k]]!=0)
G[4,5]edge True
Hamiltonian(6)k=6
Nextvalue(6) k=6
Algorithm sumofsubsets(s,k,r)
{
X[k]:=1;
if(s+w[k]=m)thenwrite(x[1:k]);//subset found else
if (s+w[k]+w[k+1]<=m) then
sumofsubsets(s+w[k],k+1,r-w[k]);
if((s+r-w[k]>=m)and(s+w[k+1]<=m))then
{
X[k]:=0;
sumofsubsets(s,k+1,r-w[k]);
}
}
Ex)n=4,(w1,w2,w3,w4)=(7,11,13,24)and m=31
Solution Vector=(x[1],x[2],x[3],x[4])
Solution A = {1,1,1,0}
Solution B={1,0,0,1}
Solution A = (1,1,0,0,1)
Solution B = (1,0,1,1)
Solution C=(0,0,1,0,0,1)
i = 1 to k wixi M and
i = 1 to k pixi is maximized
Here we use the fixed tuple size formulation. If at node Z the values
k+1 i n and use the greedy method to solve the relaxed problem.
Remark :
// X(1:n), either zero or one ; X(k) = 0 if W(k) is not in the knapsack else X(k)
= 1
1. integer n,k, Y(1:n), i , X(1:n) ; real M, W(1:n), P(1:n), fw, fp, cw, cp ;
3. loop
9. endif
13. repeat
16. repeat
17. k := k+1
18. repeat
33, 43, 45, 55), P = (11, 21, 31, 33, 43, 53, 55, 65), m = 110. Solve the problem using
backtracking approach.
Solution:
Let arrange all items in non-decreasing order of p[i] / w[i].
Initially, cp = cw = 0, fp = – 1, k = 0
For k = 1:
cp = cp + p = 0 + 11 = 11
1
cw = cw + w = 0 + 1 = 1 1
For k=1:
cp = cp + p = 11 + 21 = 32
2
cw = cw + w = 1 + 11 = 12
2
k = k+1=2
For k = 2:
cp = cp + p = 32 + 31 = 63
3
cw = cw + w = 12 + 21 = 33
3
k = k+1=3
For k = 3:
cp = cp + p = 63 + 33 = 96
4
cw = cw + w = 33 + 23 = 56
4
k = k+1=4
For k = 4:
cp = cp + p = 96 + 43 = 139
5
cw = cw + w = 56 + 33 = 89
5
k = k+1=5
For k = 5:
cp = cp + p = 139 + 53 = 192
6
cw = cw + w = 89 + 43 = 132
6
cp = cp – p = 192 – 53 = 139
6
cw = cw – w = 132 – 43 = 89
6
Inclusion of any item from {I , I , I } will exceed the capacity. So let’s backtrack to item 4.
6 7 8