Unit 14 - Backtracking
Unit 14 - Backtracking
Unit 14 Backtracking
Structure:
14.1 Introduction
Objectives
14.2 Backtracking Strategy
14.3 8-Queens Problem
14.4 Sum of Subsets
14.5 Knapsack Problem
14.6 Summary
14.7 Terminal Questions
14.8 Answers
14.1 Introduction
In the previous unit, we discussed the concept of dynamic programming and
now in the search for fundamental principles of algorithm design, we will
discuss about backtracking, which represents one of the most general
techniques. Many problems which deal with searching for a set of solutions
or which ask for an operational solution satisfying some constraints can be
solved using the backtracking formulation. The name backtrack was first
coined by D.H Lehmer in the 1950s. Early workers who studied the process
were R.J. Woler, who gave an algorithmic account of it in 1960, and S.
Golomb and L.Barmert who presented a very general description of it in the
form of applications.
Objectives
After studying this unit, you should be able to:
analyze the 8-queen problem
solve the problem of the Sum of subsets
use the applications of the Knapsack 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
Algorithm 14.1
Algorithm Place (k, i)
// Returns true if a queen can be placed
// in kth row and ith columns 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 (i:=1) to k – l) do
if ((x [j] – l // Two in the same column
or (Abs (x [j] = 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,j) then
{
x [ k] : = i;
if (k=n) then write (x[1:n])
else Nqueen (k+1, n);
}
}
}
At this point, we can see how effective function Nqueen is over the brute
64
force approach. For an 8x8 chessboard, there are possible ways to
8
place 8 pieces or approximately 4.4 billion 8 tuples to examine. However, by
Fig. 14.2: A possible space organization for the sum of subsets problem.
Nodes are numbered as in breath first search.
The tree organization of the solution space is referred to as the state space
tree.
Suppose we are given distinct positive number and we desire to find all
combinations of these numbers whose sums are m. This is called the sum
of subsets problem. The example in above shows how we could formulate
this problem using either fixed or variable sized tuples. We consider a back
tracking solution using the fixed tuple size strategy. In this case, the element
xi of the solution vector is either one or zero depending on whether the
weight wi, is included or not.
The children of any node in Fig 14.3 are easily generated. For a node at
level i, the left child corresponds to xi = 1 and the right to xi = 0.
A simple choice for the bounding functions Bk (x1, ……. xi) = true if
k n
w
i 1
i xi w m
i k 1
i
Clearly, x1, ……..,xk cannot lead to an answer node if this condition is not
satisfied. The bounding functions can be strengthened if we assure that the
wi’s are initially in non-decreasing order. In this case, x1…… xk cannot lead
to an answer node if
w
i 1
i xi wk 1 m
The bounding functions we use are therefore Bk (x1, …………….,xk) = true iff
k n k
wi xi
i 1
wi m and
i k 1
w
i 1
i xi Wk 1 m
Since our algorithms will not make use of Bn, we need not be concerned by
the appearance of Wn+1 in this function. Although we have now specified all
that is needed is to directly use either of the backtracking scheme, a simpler
algorithms results if we tailor either of these schemes to the problem at
hand. This simplification results from the realization that if xk = 1, then
k n
w i xi w i m
i 1 i k 1
Algorithm 14.2
Algorithm SumOfSub (s,k,r)
// Find all subsets of w(k....n) that sum to m
// The values of x[j], 1 j < k, have already
k 1
// been determined S w[ j] X[ j]
j 1
n
//and r w [ j ] . The w[j]’s are in
j k
{
// Generate left child. Note: S+tw[j] m
// since Bk–1 is true.
x [k] : = 1;
If (s + w[k] = m) then write (x [1: k]);
Algorithm 14.3
Algorithm Bound (cp, cw, k)
// cp is the current profit, cw is the current
// weight total k is the index of the last removed
// item : and m is the knapsack size
{
b:=cp; c:= cw;
for i = k +1 to n do
{
c: = c + w [i]
if (c < m) then b:= b + p [i]
else return b + (cI – (c-m)) / w[i] * p [i];
} return b:
{
From bound it follows that the bound for a feasible left child of a node z is
the same as that for z. Hence, the bounding function need not be used
whenever the backtracking algorithm makes a move to the left child of a
node. The resulting algorithm is Bknap. It was obtained from the recursive
backtracking scheme, Initially set fp: = -1: This algorithm is invoked as
Bknap (1, 0, 0):
n
When fp —1, x[i], 1 i n is such that pi xi f p . The path y [i], 1 i
i 1
k, is the path to the current node. The current weight
k 1 k 1
cw w i y i and c p pi y i
i 1 i 1
Algorithm 14.4
Algorithm Bknap (k, cp, cw)
// m is the size of the knapsack; is the
// no of weights and profits w[ ] and p [ ]
// are the weights and profits
pi
// p [i] /w [i] p[i +1] fw p[ i 1 ] fw is the final
w i
// weight of knapsack: fp is the final
14.6 Summary
In this unit, we studied that any problem can be divided into two
categories explicit and implicit.
We also studied in this unit a classic combinational problem called as
8-queens problem.
14.8 Answers
Self Assessment Questions
1. Explicit constraints
7
j
2. 1 (8 i) 69,281
j 0 i 0
64
3.
8
k n
4. wi xi
i 1
w m
i k 1
i
5. subsets
p( i ) p( i 1 )
6. ;1i n
w [ i ] w( i 1)
Terminal Questions
1. A classic combinational problem is to place eight queens on 8x8
chessboard so that no two “attack” that is, not two of them are on the
same row, colours or diagonal. Let us number the rows and columns of
the chessboard 1 through 8 (Fig 14.1). The queens can also be
numbered 1 through 8. Since each queen must be on a different row, we
can without loss of generality assume that queen i is to be placed on row
i. All solutions to the 8-queens problem can therefore be represented as
8-tuples (x1…… x8), where xi is the column on which queen i is placed.
The explicit constraints using this formulation are Si = {1, 2, 3, 4, 5, 6, 7,
8}; 1i8. Therefore the solution space consists of 88-tuples. The implicit
constraints for this problem are that no two queens can be on the same
diagonal. The first of these two constraints implies that all solutions are
permutations of the 8-tuple (1, 2, 3, 4, 5, 6, 7, 8). This realization
reduces the size of the solution space from 88 tuples to 8! Tuples. (Refer
Section 14.3)
2. Refer Section 14.4
3. Refer Section 14.5
___________________