Daa Backtracking
Daa Backtracking
BACKTRACKING
Backtracking: General method, Applications- N-QUEEN Problem, Sum of Sub Sets problem,
Graph Coloring, Hamiltonian Cycles.
Introduction
ALGORITHM try(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.
1
In case of greedy and dynamic programming techniques, we
will use Brute force approach. It means, we will evaluate
all possible solutions, among which, we select one
solution as optimal solution. In backtracking technique,
we will get same optimal solution with less number of
steps. So we use backtracking technique. We can solve
problems in an efficient way when compared to other
methods like greedy method and dynamic programming. In
this we will use bounding functions (criterion functions),
implicit and explicit conditions. While explaining the
general method of backtracking technique, there we will
see implicit and explicit constraints. The major advantage
of backtracking method is, if a partial solution
(x1,x2,x3…..,xi) can’t lead to optimal solution then
(xi+1…xn) solution may be ignored entirely.
3
Fig) Tree organization of the 4 queen solution space
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 C are live nodes.
4
8) E-node : The live node whose children are currently
being generated is called E-node.( node being expanded).
5
Applications:
1) n-Queens Problem ( 4-Queens and 8-Queens Problem)
6
Consider the queen at a[4,2]. The squares that are
diagonal to this queen (running from upper left to lower
right) are a[3,1],a[5,3], a[6,4], a[7,5],a[8,6]. All these
squares have a (row – column) value of 2. Also every
element on the same diagonal that goes from the upper
right to the lower left has the same (row + column) value.
7
The array x[1..n] is a global array. Let (x1,x2,x3,…xn) be
the solution vector where xi is the column number on which
the ith queen is placed. (i may be row number).
Algorithm place(k,i)
{
for j:=1 to k-1 do
if ((x[j]=i) or (abs(x[j]-i) = abs(j-k)) then
return false;
return true;
}
Algorithm nqueens(k,n)
{
for i:=1 to n do
{
if (place(k,i) then
{
X[k]:=i;
if (k=n) then
write(x[i:n);
else
nqueens(k+1,n);
}
}
}
For a 4x4 chess board there are 16C4 possible ways to place
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.
8
To place second queen in second row 1 2 3 4
Start with first column. 1 1
It is under attack 2 - - 2
Second column also Under attack 3
Third column not under attack by other queens. 4
So we place queen in 3rd column. X[2]=3
X[2]=3
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 col 4. 4
Now X[2]=4 X[2]=4
9
1 2 3 4
1 1
First queen is moved to 2nd column 2
X[1]=2 3
4
X[1]=2
1 2 3 4
Second queen in second row
1 1
First col under attack
Second column under attack 2 - - - 2
Third col under attack 3
4th col not under attck 4
So place queen in 4th col 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
10
Figure shows the part of the solution space tree that is
generated. The tree generated as per the above
processing. Nodes are numbered in the order in which they
are generated. A node that gets killed as a result of
backtracking has a B under it.
nqueens(k,n) place(k,i)
11
2) GRAPH COLORING
No of vertices= n
No of colors= m
Solution vector = X[1], X[2], X[3]....X[n]
The values of solution vector may belongs 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) then return;
}until (false);
}
13
Adjacency Matrix G
1 2 3 4
1 0 1 0 1
2 1 0 1 0
3 0 1 0 1
4 1 0 1 0
x[1]=1 ,x[2]=0,x[3]=0,x[4]=0
x[1]=1 ,x[2]=2,x[3]=0,x[4]=0
14
assume that the number mentioned outside the node belongs
to color
x[1]=1 ,x[2]=2,x[3]=1,x[4]=0.
x[1]=1 ,x[2]=2,x[3]=1,x[4]=2
15
HAMILTONIAN CYCLES
17
The algorithm nextvalue(k) which determines a possible
next vertex for the proposed cycle.
Algorithm nextvalue(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
{
For j:=1 to k-1 do
if (x[j]=x[k]) then
break;
if (j=k) then
if((k<n) or ((k=n)and G[x[n],x[1]]!=0))then
return;
}
}until (false);
}
Algorithm to generate next vertex.
Algorithm Hamiltonian(k)
{
Repeat
{
nextvalue(k);
if (x[k]=0) then return;
if (k=n) then write (x[1:n]);
else
Hamiltonian(k+1);
}until(false);
}
18
Algorithm to find all Hamiltonian cycles.
Example)
No of vertices n=8
Adjacency matrix G Solution vertex
1 2 3 4 5 6 7 8
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
19
Hamiltonian(3) k=3
Nextvalue(3) k=3
X[3]=(x[3]+1) mod (8+1)=(0+1)mod 9 = 1
X[3]=1
Is there an edge between k and k-1
If (G[x[k-1],x[k]]!=0)
G[2,1] edge True
If (G[x[k-1],x[k]]!=0)
G[2,2] edge False
If (G[x[k-1],x[k]]!=0)
G[2,3] edge True
J=1 is x[j]=x[k]
Is 1=2 no false
J=2 is 2=3 no false
Hamiltonian(4) k=4
Nextvalue(4) k=4
20
X[4]=(x[4]+1) mod (8+1)
X[4]= 2
If (G[x[k-1],x[k]]!=0)
G[3,2] edge True
J=2 is x[j]=x[k]
is x[2]=x[4]
2=2 True Break
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=1 is x[j]=x[k] X[7] 0
Is 1=4 no false X[8] 0
J=2 is 2=4 no false
J=3 is 3=4 no false
as k < n return still we need to add vertices
Hamiltonian(5) k=5
Nextvalue(5) k=5
If (G[x[k-1],x[k]]!=0)
G[4,1] no edge False
If (G[x[k-1],x[k]]!=0)
G[4,2] no edge False
21
= (3+1)mod 9 = 3
If (G[x[k-1],x[k]]!=0)
G[4,3] edge True
If (G[x[k-1],x[k]]!=0)
G[4,5] edge True
Hamiltonian(6) k=6
Nextvalue(6) k=6
22
SUM OF SUBSETS
Suppose we are given n distinct positive numbers( usually
called weights) and we desire to find all combinations of
these numbers whose sum are m.
23
s – sum of all selected elements
k – denotes the index of chosen element
r – initially sum of all elements. After selection of
some element from the set subtract the chosen value
from r each time.
W(1:n) – represents set containing n elements.
X[i]-solution vector 1<=i<=k
Algorithm sumofsubsets(s,k,r)
{
X[k]:=1;
if (s+w[k]=m) then write (x[1:k]); // subset found
else
if (s+w[k]+w[k+1]<=m) then
sumofsubsets(s+w[k],k+1,r-w[k]);
Solution A = {1,1,1,0}
Solution B = {1,0,0,1}
24
Ex2) n=6, m=30 and w[1:6]={5,10,12,13,15,18}.
Portion of the state space tree generated by sum of
subsets
Solution A = (1,1,0,0,1)
Solution B = (1,0,1,1)
Solution C = (0,0,1,0,0,1)
25