Back Tracking
Back Tracking
Back Tracking
General Method:
Backtracking method is used to solve the problems
which deal with searching for a set of solutions or which ask for
an optimal solution satisfying some constraints.
Its basic idea is to build up the solution vector one component at a time
and to use modified criterion functions Pi(x1,…..,xi) (sometimes called
bounding functions) to test whether the vector being formed has any
chance of success.
Advantage:
If it is realized that the partial vector (x1,x2,…,xi) can in no
way lead to an optimal solution, then mi+1…..mn possible test vectors
can be ignored entirely.
Backtracking requires all the solutions satisfy a complex set of constraints.
Constraints can be divided into two categories:
Explicit constraints:
Rules that restricts each xi to take on values only
from a
given set.
Ex:
xi ≥ 0 or Si ={all non negative real numbers}
Implicit constraints:
Rules that determine which of the tuples in the
solution
space of I satisfy the criterion function.
Live node : The node which has been generated and all of whose
children have not yet been generated.
E-node : The live node whose children are currently being generated.
X1 = 1 2 3 4
18 34 50
2
X2=2 3 4 1 3 4 1 2 4 1 2 3
3 8 13 19 24 29 35 40 45 51 56 61
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 6 9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
4-queens problem using backtracking
1
X1 = 1 2
18
2
4
4 3
X2=2 3
24 29
3 8 13 19
2 4 2 3 1
9 11 14 16 30
3 3
15 31
8 – Queens problem
Problem:
To place eight queens on an 8 x 8 chessboard so that no two
“attack”, that is, so that no two of them are on the same row,
column,
or diagonal.
Queen i is to be placed on row i.
All solutions to the 8-queens problem can be represented as 8-tuples (x 1,
….x8), where xi is the column on which queen i is placed.
Explicit constraints are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤ 8.
Solution space consists of 88 8-tuples.
Implicit constraints are
No two xi’s can be the same.
No two queens can be on the same diagonal.
The solution space reduces from 88 tuples to 8! tuples.
8-queens problem
1 2 3 4 5 6 7 8
1 Q
2
Q
3
Q
4
Q
5 Q
6 Q
7
Q
8
Q
THE 8-QUEENS PROBLEM
Let (x1, x2,…., xn) represent a solution for generalization problem,
in which xi is the column of the ith row where the ith queen is
placed.
to
the lower right has the same row - column value.
Every element on the same diagonal that goes from the upper right
X1=1 2 3 4
2 3 4 5
X2=2 3 4 3 4 4
8 9 10 11
6 7
x3 3 4 4 4
12 13 14 15
X4=4
16
Another possible tree organization for sum of sub sets
problem
1
X1=1 0
3
2
X2=1 0 1 0
19 4 5
18
X3=1 0 1 0 1 0 1 0
13 6 7
26 27 20 21 12
X4=1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
30 31 28 29 24 25 22 23 16 17 14 15 10 11 8 9
Factors:
X1 =1 X1 =0
5, 2, 68 0, 2, 68
X2 =0 X2 =1
X2 =1 X2=0
15, 3, 58 5, 3, 58 10, 3, 58 0, 3, 58
X3 =1 X3 =0 X3 =1 X3 =0 X3 =0 X3 =1 X3 =0
X4=0 X4 =1 X4 =0 X4 =0 X4 =0 X4 =1 X4=0
C
GRAPH COLORING
X2=1 2 3
x3
HAMILTONIAN CYCLES
Algorithm NextValue(k)
// x[1:k-1] is a path of k-1 distinct vertices. If x[k]=0, then no vertex has as yet been
// assigned to x[k]. After execution, x[k] is assigned to the next highest numbered
// vertex which does not already appear in x[1:k-1] and is connected by an edge to
// x[k-1]. Otherwise x[k]=0. If k=n, then in addition x[k] is connected to x[1].
{
repeat
{
x[k] := (x[k] + 1) mod (n+1); // Next vertex.
if (x[k] = 0) then return;
if (G[x[k-1], x[k]] ≠ 0) then
{ // Is there an edge?
for j := 1 to k-1 do if (x[j] = x[k]) then break;
//Check for distinctness.
if(j=k) then // If true, then the vertex is distinct.
if((k<n) or ((k=n) and G[x[n],x[1]] ≠ 0))
then return;
}
} until(false);
}
Algorithm Hamiltonian(k)
// This algorithm uses the recursive formulation of backtracking to find all
// the Hamiltonian cycles of a graph. The graph is stored as an adjacency
// matrix G[1:n, 1:n]. All cycles begin at node 1.
{
repeat
{ // Generate values for x[k].
NextValue(k); // Assign a legal next value to x[k].
if(x[k] = 0) then return;
if(k=n) then
write(x[1:n]);
else
Hamiltonian(k+1);
} until(false);
}