Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Back Tracking

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

BACKTRACKING

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.

 The desired solution is expressible as an n-tuple(x1,…xn), where


the xi are chosen from some finite set Si.
 If mi is the size of set Si, then there are
m = m1m2m3…mn n-tuples that are possible candidates for
satisfying the criterion function.

 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.

 Solution space: All tuples that satisfy the explicit constraints


State Space Tree:
The tree organization of the solution space is referred to as the
state space tree.

 Problem states : nodes in the tree


 State space of the problem : all paths from the root to other nodes
 Solution states : problem states s for which the path from the root to s
defines a tuple in the solution space.
 Answer states : solution states s for which the path from the root to s
defines a tuple that is a member of the set of solutions
Static Trees : Tree organizations that are independent of the problem
instance being solved.

Dynamic Trees : Tree organizations that are problem instance


dependent.

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.

Dead node : generated node which is not to be expanded further.


Algorithm Backtrack(k)
// This schema describes the backtracking process using recursion. On
// entering, the first k-1 values x[1], x[2],…,x[k-1] of the solution
// vector x[1:n] have been assigned. X[] and n are global.
{
for (each x[k] є T(x[1],…,x[k-1]) do
{
if(Bk(x[1],x[2],…..,x[k] ≠ 0) then
{
if(x[1], x[2],…., x[k] is a path to an answer node)
then write (x[1:k]);
if(k < n) then
Backtrack(k+1);
}
}
}
Algorithm IBacktrack(n)
// This schema describes the backtracking process. All solutions are
// generated in x[1:n] and printed as soon as they are determined.
{
k := 1;
while(k ≠ 0) do
{
if(there remains an untried x[k] є T(x[1], x[2],…, x[k-1])
and Bk(x[1],….., x[k]) is true) then
{
if(x[1],…., x[k] is a path to an answer node)
then write (x[1:k]);
k := k+1; //Consider the next set.
}
else
k := k-1; // backtrack to the previous set.
}
}
Tree organization of the 4-queens solution space
1

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.

How to test whether two queens are on the same diagonal ?


 Every element on the same diagonal that runs from the upper left

to
the lower right has the same row - column value.
 Every element on the same diagonal that goes from the upper right

to lower left has the same row + column value.


 If 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
i.e., two queens lie on the same diagonal if and only if
|j – l | = | i - k |
Algorithm Place(k, i)
// Returns true if a queen can be placed in kth row and ith column.
// Otherwise it returns false. X[] is a global array whose first (k-1)
// values have been set. Abs(r) returns the absolute value of r.
{
for j := 1 to k-1 do
if((x[j] = i) // two in the same column.
or (Abs(x[j] – i) = Abs(j-k)))
// or in the same diagonal.
then return false;
return true;
}
Algorithm NQueens(k, n)
// Using backtracking, this procedure prints all possible placements of n
// queens on an n x n chessboard so that they are non attacking.
{
for i := 1 to n do
{
if(Place(k, i) then
{
x[k] := i;
if(k = n) then
write (x[1:n]);
else
NQueens(k+1, n);
}
}
}
7 j
 The number of nodes in the 8-queens state space tree = 1+∑ ∏ (8-i)
j=0 i=0
Sum Of Subsets
Problem:
Given positive numbers wi, 1 ≤ i ≤ n, and m, this problem
calls for finding all subsets of the wi whose sums are m.
Explicit constraints:
xi є {j | j is an integer and 1 ≤ j ≤ n.
Implicit constraints:
 no two be the same
 the sum of corresponding wi’s be m.
 xi < xi+1, 1 ≤ i < k.

 In this formulation, different solutions may have different sized tuples.


Sum Of Subsets

 In another formulation of sum of subsets problem, each solution


subset is represented by an n-tuple(x1, x2,…., xn) such that xi є {0,1}.
xi = 0 , if wi is not chosen.
xi = 1, if wi is chosen.

 In this formulation, all solutions have fixed size tuple.


 For both the formulations, the solution space consists of 2 n distinct
tuples.
A possible tree organization for sum of sub sets problem

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:

 Time to generate the next xk.


 Number of xk satisfying the explicit constraints.
 Time for the bounding functions Bk, and
 Number of xk satisfying the Bk.
SUM OF SUBSETS
 In the solution vector for this problem, the element xi will be either
one or zero depending whether the weight wi is included or not.
 The bounding functions that are used:
k n
Bk(x1,…, xk) = true iff ∑ wixi + ∑ wi ≥ m
i=1 i=k+1
k
and ∑ wixi + wk+1 ≤ m
i=1
Algorithm SumOfSub(s, k, r)
//Find all subsets of w[1:n] that sum to m. The values of x[j], 1≤ j < k, have already been
k-1 n
//determined. s = ∑w[j] * x[j] and r = ∑w[j]. The w[j]’s are in non decreasing order. It is
j=1 j=k
n
//assumed that w[1] ≤ m and ∑w[i]≥ m.
i=1
{
//Generate left child. Note: s+w[k] ≤m since B k-1 is true.
x[k] := 1;
if(s+w[k] = m) then write (x[1:k]); //Subset found.
//there is no recursive call here as w[j]>0, 1 ≤ j ≤ n.
else if(s+w[k] +w[k+1] ≤ m)
then SumOfSub(s+w[k], k+1, r-w[k]));
//Generate right child and evaluate B k.
if((s+r-w[k] ≥ m) and (s+w[k+1] ≤ m)) then
{
x[k] := 0;
SumOfSub(s, k+1, r-w[k]);
}
}
Problem: n=6, m=30, and w[1:6]={5,10,12,13,15,18}.
0, 1, 73

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

27, 4, 46 15, 4, 46 17, 4, 46 5, 4, 46 10, 4, 46 12, 4, 46 0, 4, 46

X4=0 X4 =1 X4 =0 X4 =0 X4 =0 X4 =1 X4=0

15, 5, 33 5, 5, 33 10, 5, 33 12, 5, 33 13, 5, 33 0, 5, 33


B
X5=1 X5 =0 X5 =0
X5 =1
20, 6, 18 12, 6, 18 13, 6, 18
A

C
GRAPH COLORING

 m-colorability decision problem


 m-colorability optimization problem
 Chromatic number
Algorithm mColoring(k)
// This algorithm was formed using the recursive backtracking schema.
// The graph is represented by its boolean adjacency matrix G[1:n,1:n].
// All assignments of 1, 2,…., m to the vertices of the graph such that
// adjacent vertices are assigned distinct integers are printed. k is the
// index of the next vertex to color.
{
repeat
{ // Generate all legal assignments for x[k].
NextValue(k); // Assign to x[k] a legal color.
if(x[k] = 0) then return; // No new color possible.
if(k=n) then //At most m colors have been used
// to color the n vertices.
write(x[1:n]);
else
mColoring(k+1);
}until(false);
}
Algorithm NextValue(k)
// x[1],…, x[k-1] have been assigned integer values in the range [1, m] such that
// adjacent vertices have distinct integers. A value for x[k] is determined in the
// range [0, m]. x[k] is assigned the next highest numbered color while maintaining
// distinctness from the adjacent vertices of vertex k. If no such color exists, then
// x[k] is 0.
{
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
{ // Check if this color is distinct from adjacent colors.
if((G[k, j] ≠ 0) and (x[k] = x[j]))
// if (k, j) is an edge and if adj. vertices have the same
// color.
then break;
}
if(j = n+1) then return; //new color found.
}until(false); // Otherwise try to find another color.
}
State space for mColoring when n=3 and m=3

X1=1 X1=2 X1=3

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);
}

You might also like