Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views8 pages

Backtracking (Unit IV)

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems while discarding those that fail to meet constraints. The document illustrates backtracking through examples such as the two batsmen scoring runs and the 4-queens problem, highlighting the efficiency gained by pruning the state space tree. It concludes with applications of backtracking in decision, optimization, and enumeration problems.

Uploaded by

ashiqmirza97496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Backtracking (Unit IV)

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems while discarding those that fail to meet constraints. The document illustrates backtracking through examples such as the two batsmen scoring runs and the 4-queens problem, highlighting the efficiency gained by pruning the state space tree. It concludes with applications of backtracking in decision, optimization, and enumeration problems.

Uploaded by

ashiqmirza97496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Backtracking - General Method

1. Introduction
Backtracking is a general algorithmic technique that considers searching every possible
combination in order to solve a computational problem. Here, we try to build a solution
incrementally, one piece at a time and removing those solutions that fail to satisfy the
constraints of the problem at any point of time.
Let us consider the case of two batsmen who have to score 18 runs in the last five balls
of the match to win. However, there are certain constraints that the batsmen have to
take into consideration.
1. It is predicted that the bower will bowl Yorkers at ball nos 2 and 4 and the
batsmen will not be able to score more than 1 run for those balls.
2. The batsmen can only either rotate the strike (1 run) or hit a four or a six.
The problem here is to find the number of ways the batsmen can score at each of the
five balls to win the match. If we consider Si to be the score that the batsmen can make
at ball i, then the problem can be formulated as follows:

Find Si for i = 1 to 6 such that S1 + S2 + S3 + S4 + S5 >= 18 subject to constraints S1, S3,


S5 ∈ {1, 4, 6} and S2 = S4 = 1.

The naïve approach would involve creating the complete state space tree for the given
problem which shows all possible solutions to the given problem. A solution in the state
space tree is represented by the path from the root to a leaf node. The state space tree
for the above given example is shown in figure 5.1. It can be observed from figure 5.1
that not all paths from the root to the leaves of the tree represent a feasible solution. In
figure 5.1, the leaf nodes representing feasible solutions are shown in orange.

Figure 5.1: The complete state space tree for the two batsmen problem.
We should remember that creating the complete state space tree and then searching
the solution(s) at leaf nodes is not feasible as the tree grows very large for large
problems. Alternatively we can use backtracking which involves constructing only a
partial state space tree by building the solution vector one component at a time. If it is
realized that a partial vector (s1, s2, …, si) can in no way lead to an optimal solution then
many possible test vectors can be ignored entirely. For example, in figure 5.1, the
nodes marked in red represent nodes which do not lead to a feasible solution.
Backtracking aims to identify such nodes and prune the search, thereby creating a
partial state space tree and saving time. Figure 5.2 shows the partial state space tree
that can be obtained for the two batsmen problem using backtracking.

Figure 5.2: Partial state space tree created for the two batsmen problem using
backtracking.

2. Methodology
The general approach of backtracking involves building the solution one step at a time,
and while building the solution, if it is realized that the current choice does not lead to a
feasible solution, then the process discards the current choice and makes a different
choice to search the solution. If none of the current choices leads to a feasible solution,
then the process takes a backtrack step by making a different choice for the earlier step
and then testing the feasibility of the choices accordingly.
Formally, let (s1, s2, …, si) be a path from the root to a node in the state space tree and
let T(s1, s2, …, si) be the set of all possible values for si+1 such that (s1, s2, …, si, si+1) is
also a path to a problem state. T(s1, s2, …, sn) = ø i.e. no choice is available once we
have exhausted all nodes on the state space tree. We assume the existence of
bounding function Bi+1 (expressed as predicates) such that if Bi+1(s1, s2, …, si+1) is false
for a path (s1, s2, …, si) from the root to a problem state then the path cannot be
extended to reach an answer node. The bounding function are problem specific.
The general recursive algorithm for implementing backtracking is shown in figure 5.3.
This algorithm is initially called as Backtrack(1).
Algorithm Backtrack(k)
//On entering, the first k-1 values //s[1], s[2, …, s[k-1]] of the solution vector s[1:n]
have been assigned. s[] and n are global
{
for (each s[k] ∈ T(s[1], …, s[k-1])) do
{
if(Bk(s[1], s[2],…,s[k])≠ 0) then
{
if(s[1], s[2],…,s[k] is a path to an answer node) then
write (s[1:k]);
if(k < n) then Backtrack(k+1);
}
}
}

Figure 5.3: General Backtracking Algorithm using recursion.


3. Example
In this section we will find the solution to the 4-queens problem using backtracking. The
4-queens problem involves a 4 x 4 chess board and four queens. The aim is to place
these four queens on the chess board such that none of them is attacking any other.
Two queens are said to be attacking each other if they lie in the same row, column or
diagonal. Here, we will simplify the problem by imposing the restriction that only one
queen can be placed in any one row or column. This will eliminate many infeasible
solutions and thereby reduce the state space tree. So in our case we will have to verify
only whether two queens are attacking diagonally for our bounding function.
The complete backtracking process for the 4-queens problem is illustrated in table 5.1.
Here we start with an empty chess board and four queens. The root of the state space
tree is created without any children. The various decisions taken to place the queens on
the chess board are also illustrated in table 5.1.

Table 5.1: Illustration of applying Backtracking approach to the 4-queens problem.

Initialization
Placing the first
queen in the first
column of the first
row.
Placing the
second queen in
the second
column of the
second row. Note
that we have
restricted two
queens from
being placed in
the same row or
column.
Since the second
queen is attacking
the first queen
diagonally, we kill
the current node
and make a
different choice for
the second queen.
Placing the
second queen in
the third column of
the second row.
Placing the third
queen in the
second column of
the third row.
Since the third
queen is attacking
the second queen
diagonally, we kill
the current node
and make a
different choice for
the third queen.
Placing the third
queen in the
fourth column of
the third row.
Since the third
queen is attacking
the second queen
diagonally, we kill
the current node
and make a
different choice for
the third queen.
However, all the
choices for the
third queen in this
case have been
exhausted, thus
we take a
backtracking step
and make a
different decision
for the second
queen in the
second row.
Backtracking
step: Making a
different choice for
the second queen
by placing the
second queen in
the fourth column
of the second row.
Placing the third
queen in the
second column of
the third row.
Placing the fourth
queen in the third
column of the
fourth row.
Since the fourth
queen is attacking
the third queen
diagonally, we kill
the current node
and make a
different choice for
the fourth queen.
However, all the
choices for the
fourth queen in
this case have
been exhausted,
thus we take a
backtracking step
and make a
different decision
for the third
queen in the third
row.
Placing the third
queen in the third
column of the third
row.
Since the third
queen is attacking
the second queen
diagonally, we kill
the current node
and make a
different choice for
the third queen.
However, all the
choices for the
third queen in this
case have been
exhausted, and all
the choices for the
second queen in
the second row
have also been
exhausted, thus
we take a
backtracking step
and make a
different decision
for the first queen
in the first row.
Placing the first
queen in the
second column of
the first row.
Placing the
second queen in
the first column of
the second row.
Since the second
queen is attacking
the first queen
diagonally, we kill
the current node
and make a
different choice for
the second queen.

Placing the
second queen in
the third column of
the second row.
Since the second
queen is attacking
the first queen
diagonally, we kill
the current node
and make a
different choice for
the second queen.

Placing the
second queen in
the fourth column
of the second row.

Placing the third


queen in the first
column of the third
row.
Placing the fourth
queen in the third
column of the
fourth row as it is
the only place
empty since all
other columns for
this row already
have queens
placed in other
rows. The current
node in the state
space tree also
forms an optimal
solution to the 4-
queens problem.

4. Applications
Generally, every constraint satisfaction problem which has clear and well-defined
constraints on any objective solution can be solved by Backtracking. We incrementally
build candidates to the solution and abandon a candidate (“backtrack”) as soon we
determine that the candidate cannot possibly be completed to a valid solution. There
are three types of such problems:
1. Decision Problem – In this, we search for a feasible solution.
2. Optimization Problem – In this, we search for the best solution.
3. Enumeration Problem – In this, we find all feasible solutions.

You might also like