Ads & Aa Unit 4 Part 1
Ads & Aa Unit 4 Part 1
Introduction:
In back tracking technique, we will solve problems in an efficient way, when compared to
other methods like greedy method and dynamic programming. The solution is based on finding one
or more vectors that maximize, minimize, or satisfy a criterion function P(x1, …. xn). Form a solution
at any point seems not promising, ignore it. All possible solutions require a set of constraints divided
into two categories:
1. Explicit Constraint: Explicit constraints are rules that restrict each xi to take on values only
from a given set. Ex: xn= 0 or 1.
2. Implicit Constraint: Implicit Constraints are rules that determine which of the tuples in the
solutions space of I satisfy the criterion function.
Implicit constraints for this problem are that no two queens can be on the same diagonal.
Back tracking is a modified depth first search tree. Backtracking is a procedure whereby, after
determining that a node can lead to nothing but dead end, we go back (backtrack) to the nodes parent
and proceed with the search on the next child. State space tree exists implicitly in the algorithm
because it is not actually constructed.
Terminologies which is used in this method:
1. Solution Space: All tuples that satisfy the explicit constraints define a possible solution space
for a particular instance T of the problem.
A
Example:
B C
B C
3. Solution States: These are the problem states S for which the path form the root to S defines a
tuple in the solution space.
Here, square nodes ( ) indicate solution. For the above solution space, there exists 3 solution states.
These solution states represented in the form of tuples i.e., (ghk-,B,D),(A,C,F) and (A,C,G) are the
solution states.
Example: A
B C
Fig: Solution State
D E F G
4. State Space Tree: Is the set of paths from root node to other nodes. State space tree is the tree
organization of the solution of the solution space.
Example: State space tree of a 4-queen problem.
Page 1
UNIT- IV BACKTRACKING
1
x1=1
x1=4
x1=2 x1=3
2 18 34 50
3 8 13 19 24 29 35 40 45 51 56 61
3 1 1 2 1 1 2 1 1
4 4 3 4 4 2 3 3 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 4 3 4 4 4 4 2 3 3 2
3 2 2 3 1 3 1 2 1 1 2 1 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
Here are C, D are answer states. (A, C) and (A, C, D) are solution states.
6. Live Node: A node which has been generated but whose children have not yet been generated
is live node.
Example 1: 1
This node 1 is called as live node since the children of node 1 have not been generated.
1
2 3 Example 2:
In this, node 1 is not a live node but node 2, node 3 are live nodes.
Page 2
UNIT-V BACKTRACKING
Example 3: 1
2 3
4 5
Here, 4, 5, 3 are live nodes because the children of these nodes not yet been generated.
7. E-Node: The live nodes whose children are currently being generated is called the E-node
(node being expanded).
Example 1: 1
This node 1 is live node and its children are currently being generated (expanded).
E-node E-node E-node
1 2 1 3 1
Example 2:
2 2 3
2 3
4
(b)
Fig: Dead nodes
General Method:
The basic idea of backtracking is to build up a vector, one component at a time and to test
whether the vector being formed has any chance of success.
The major advantage of this algorithm is that we can realize the fact that the partial vector
generated does not lead to an optimal solution. In such a situation that vector can be ignored.
Backtracking algorithm determines the solution by systematically searching the solution space
(i.,e set of all feasible solutions) for the given problem.
Backtracking is a depth first search with some bounding function. All solutions using
backtracking are required to satisfy a complex set of constraints. The constraints may be explicit or
implicit.
Page 3
UNIT-V BACKTRACKING
Applications of Backtracking
Backtracking is an algorithm design technique that can effectively solve the larger instances of
combinational problems. It follows a systematic approach for obtaining solution to a problem. The
applications of backtracking include,
1) N-Queens Problem: This is generalization problem. If we take n=8 then the problem is called as 8
queens problem. If we take n=4then the problem is called 4 queens problem. A classic combinational
problem is to place n queens on a n*n chess board so that no two attack, i.,e no two queens are on the
same row, column or diagonal.
Algorithm of n-queens problem is given below:
Page 4
UNIT-V BACKTRACKING
(a)
The second queen should not be in first row and second column. It should be placed in second
row and in second, third or fourth column. It we place in second column, both will be in same diagonal,
so place it in third column.
1 1
2
(b) (c)
We are unable to place queen 3 in third row, so go back to queen 2 and place it somewhere
else.
1 1
2 2
(d) (e)
Page 5
UNIT-V BACKTRACKING
Now the fourth queen should be placed in 4th row and 3rd column but there will be a diagonal
attack from queen 3. So go back, remove queen 3 and place it in the next column. But it is not possible,
so move back to queen 2 and remove it to next column but it is not possible. So go back to queen 1 and
move it to next column.
1 1
2
(f) (g)
1 1
2 2
3 3
4
(h) (i)
Fig: Example of Backtrack solution to the 4-queens problem
Hence the solution of to 4-queens’s problem is x1=2, x2=4, x3=1, x4=3, i.,e first queen is
placed in 2nd column, second queen is placed in 4th column and third queen is placed in first column
and fourth queen is placed in third column.
1
Row 1
x1=1 x1=2
2 3 Row 2
x2=2 x2=1 x2=3 x2=4
x2=3 x2=4
4 5 6 7 8 9 Row 3
B x3=2 x3=4 x3=2 B B
x3=1
10 11 12 13 15 Row 4
B B x4=3 B x4=3
14 16
B
Fig: Portion of the tree that is generated during Backtracking
8-queens problem
A classic combinatorial problem is to place 8 queens on a 8*8 chess board so that no two
attack, i.,e no two queens are to the same row, column or diagonal.
Now, we will solve 8 queens problem by using similar procedure adapted for 4 queens
problem. The algorithm of 8 queens problem can be obtained by placing n=8, in N queens algorithm.
We observe that, for every element on the same diagonal which runs from the upper left to the lower
right, each element has the same “row-column” value. Also every element on the same diagonal
which goes from upper right to lower left has the same “row+column” value.
Page 6
UNIT-V BACKTRACKING
If two queens are placed at positions (i,j) and (k,l). They are on the same diagonal only
if i-j=k-l ……………….(1) or
i+j=k+l ……………….(2).
From (1) and (2) implies
j-l=i-k and
j-l=k-i
Two queens lie on the same diagonal iff
|j-l|=|i-k|
But how can we determine whether more than one queen is lying on the same diagonal? To
answer this question, a technique is deviced. Assume that the chess board is divided into rows
1....8, 1....8
rows columns
And columns say A:
This can be diagrammatically represented as follows
1 2 3 4 5 6 7 8
1
2
3 Q
4
5
6
7
8
Now, assume that, we had placed a queen at position (3,2).
Now, its diagonal cells includes (2,1)(4,3)(5,4)….(if we traverse from upper left to lower
right). If we subtract values in these cells say 2-1=1,4-3=1,5-4=1, we get same values, also if we
traverse from upper right to lower left say (2,3) (1,4)(4,1)….we get common values when we add the
bits of these cells i.,e 2+3=5, 1+4=5, 4+1=5. Hence, we say that, on traversing from upper left to
lower right, if (m,n)(a,b) are the diagonal elements(of a cell) than m-n=a-b or on traversing from
upper right to lower left if(m,n)(a,b) are the diagonal elements(of a cell) then m+n=a+b.
The solution of 8 queens problem can be obtained similar to the solution of 4 queens.
problem.X1=3, X2=6, X3=2, X4=7, X5=1, X6=4, X7=8, X8=5,
The solution can be shown as
1
2
3
4
5
6
7
8
Page 7
UNIT-V BACKTRACKING
Time complexity: The solution space tree of 8-queens problem contains 88 tuples. After imposing
implicit constraints, the size of solution space is reduced to 8! tuples.
The state space tree for the above solution is given
x1=1 x3=1
x2=2 2
7
3 4 5 6 1 2 4 x2=6
B 3 4 5
x3=2 x3=2
4 567 8 1
5 6 7
B x4=7
x4=2 4 6 7 8 4 5
8
x5=4
7 8 B B B x5=1
6
9
x6=7
7 8 x6=4
10 14
x7= 8 x3=1 x7=8
7
11 12
B
x8=2
B
x8=5
13
12
Page 8
UNIT-V BACKTRACKING
represents that no decision is yet taken on any input. We assume that, the elements of the given set are
arranged increasing order.
Page 9
UNIT-V BACKTRACKING
The left child of the root node indicates that, we have to include the first element and right
child of the root node indicates that, we have to exclude the first element and so on for other nodes.
Each node stores the sum of the partial solution element. If at any stage, the number equals to ‘M’
then the search is successful. At this time search will terminate or continues if all the possible
solutions need to be obtain. The dead end in the tree occurs only when either of the two inequalities
exists.
The sum of S’ is too large.
The sum of S’ is too small.
Thus we take back one step and continue the search.
ALGORITHM:
Page 10
UNIT-V BACKTRACKING
x1=1 x1=0
5 0
x2=1 x2=0 x2=0
x2=1
15 5 10 0
27 15 17 5
22 12
x4=1 x4=0 x4=1 x4=0
x4=0 x4=0
28 15 30 5
22 12
B
x5=1 x5=1 x5=0
30 20
A 12
x6=1
30
C
3) Graph Coloring
Let G be a graph and m be a given positive integer. The graph coloring problem is to find if
the nodes of G can be colored in such a way that no two adjacent nodes have the same color, yet only
m colors are used. This is termed the m-colorability decision problem. The m-colorability
optimization problem asks for the smallest integer m for which the graph G can be colored. This
Page 11
UNIT-V BACKTRACKING
integer is referred to as the chromatic number of the graph.
Page 12
UNIT-V BACKTRACKING
Page 13
UNIT-IV BACKTRACKING
Page 14
UNIT-IV BACKTRACKING
-------------------------------------------------------------------------------------------------------------------
Problem Definition
We are given n objects and a knapsack or bag. The objective is to obtain a filling of the knapsack that maximizes
the total profit earned.
Given
n = number of weights
w = weights
P =profits m= knapsack capacity
The problem is similar to the zero-one (0/1) knapsack optimization problem is dynamic programming algorithm.
We are given ‘n’ positive weights Wi and ’n’ positive profits Pi, and a positive number ‘m’ that is the knapsack
capacity, the is problem calls for choosing a subset of the weights such that,
Solution space:
The Solution space is the same as that for the sum of subset’s problem.
Bounding functions are needed to help kill some live nodes without expanding them.
A good bounding function for this problem is obtained by using an upper bound on the value of the best feasible
solution obtainable by expanding the given live node.
The profits and weights are assigned in descending order depend upon the ratio.
(i.e.)
Pi/Wi
After assigning the profit and weights, we have to take the first object weights and check if the first weight is less
Page 15
UNIT-IV BACKTRACKING
than or equal to the capacity, if so then we include that object (i.e.) the unit is 1.(i.e.) K-> 1.
Then We are going to the next object, if the object weight is exceeded that object does not fit. So unit of that object
is ‘0’.(i.e.) K=0.
Then We are going to the bounding function, this function determines an upper bound on the best solution
obtainable at level K+1.
Repeat the process until we reach the optimal solution.
Algorithm
It determines an upper bound on the best solution obtainable by expanding any node Z at level K+1 of the state
space tree.
The object weights and profits are w[i] and p[i].
It is assumed that p[i]/w[i] >= p[i+1]/w[i+1]
Page 16
UNIT-IV BACKTRACKING
Page 17