0% found this document useful (0 votes)
45 views

Ads & Aa Unit 4 Part 1

Uploaded by

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

Ads & Aa Unit 4 Part 1

Uploaded by

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

UNIT- IV BACKTRACKING

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

Fig: Tree organization of a solution space


D E
2. Problem State: A problem state is the state that is defined by all the nodes within the tree
organization.
A
Example:
Fig: Problem State

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

x2=3 x2=3 x2=2 x2=2

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

Fig: Tree organization of the 4-queens solution space


In the above figure nodes are numbered as in depth first search. Initially, (x1=1 or 2 or 3 or 4,
it means we can place first queen in either first, second, third or fourth column. If x1=1 then x2 can be
placed in either 2nd, 3rd or 4th columns. If (x2=2) then, x2 can be placed either in 3rd, or 4th column.
If x3=3, then x4=4. So nodes 1-2-3-4-5 is one solution in solution space. It may not be a feasible
solution.
5. Answer States: These solution states S, for which the path from the root node to S defines a
tuple that is a member of the set of solutions (i.e., it satisfies the implicit constraints) of the problem.
A
B C
D EF

Tree (Answer States)

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

Here, node 2 is E-node.


8. Dead Node: It is generated node, that is either not to be expanded further or one for which all
of its children has been generated.
Since node 1’s children generated and node 2, 3 are not expanded. Assumed that node 2 generated one
more node, So, 1, 3, 4 are dead nodes.
1

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

Algorithm: Recursive 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:

Algorithm: All solutions to the n-queens problem

Page 4
UNIT-V BACKTRACKING

Algorithm: Can a new queen be placed?


4-Queens problem:
Consider a 4*4 chessboard. Let there are 4 queens. The objective is place there 4 queens on
4*4 chessboard in such a way that no two queens should be placed in the same row, same column or
diagonal position.
The explicit constraints are 4 queens are to be placed on 4*4 chessboards in 44 ways.
The implicit constraints are no two queens are in the same row column or diagonal.
Let{x1, x2, x3, x4} be the solution vector where x1 column on which the queen i is
placed. First queen is placed in first row and first column.
1

(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

2) Sum of Subsets Problem


Given a set of n objects with weights (w1,w2,…..w3) and a positive integer M. We have to find a
subset S’ of the given set S, such that
 S’ S
 Sum of the elements of subset S’ is equal to M.
For example, if a given set S=(1,2,3,4) and M=5, then there exists sets S’(3,2) and S’=(1,4) whose
sum is equal to M.
It can also be noted that some instance of the problem does not have any solution.
For example, if a given set S=(1,3,5) and M=7, then no subset occurs for which the sum is equal to
M=7.
The sum of subsets problem can be solved by using the back tracking approach. In this
implicit tree is created, which is a binary tree. The root of the tree is selected in such a way that it

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:

Algorithm: Recursive backtracking algorithm for sum of subsets


Example:
Let m=31 and w= {7, 11, 13, 24} draw a portions of state space tree.
Solution: Initially we will pass some subset (0, 1, 55). The sum of all the weights from w is 55, i.e.,
7+11+13+24=55. Hence the portion of state –space tree can be
Here Solution A={1, 1, 1, 0} i.e., subset {7, 11, 13}
And Solution B={1, 0, 0,1} i.e. subset {7, 24}
Satisfy given condition=31;
Example: Consider a set S={5, 10, 12, 13, 15, 18} and N=30.

Page 10
UNIT-V BACKTRACKING

Subset {Empty} Sum=0 Initially subset is 0


5 5
5, 10 15
5, 10, 12 27
5, 10, 12, 13 40 Sum exceeds N=30,Hence Backtrack
5, 10, 12, 15 Not Feasible
5, 10, 12, 18 Not feasible
5, 10 List ends. Backrack
5, 10, 13 28
5, 10, 13, 15 33 Not feasible. Backtrack
5, 10 15
5, 10, 15 30 Solution obtained
We can represent various solutions to sum of subset by a state space tree as,
0

x1=1 x1=0

5 0
x2=1 x2=0 x2=0
x2=1

15 5 10 0

x3=1 x3=0 x3=1 x3=0 x3=1 x3=1

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

Figure. An Ex. Of graph coloring

Algorithm: Finding all m-colorings of graph

Page 13
UNIT-IV BACKTRACKING

Algorithm: Finding next color

Fig. A map and it’s planar graph representation


To color the above graph chromatic number is 4. And the order of coloring is X1=1, X2=2,
X3=3, X4=4, X5=1
Time Complexity: At each internal node O(mn) time is spent by Nextcolor to determine the children
corresponding to legal coloring. Hence the total time is bounded by,O(nmn).

Page 14
UNIT-IV BACKTRACKING

-------------------------------------------------------------------------------------------------------------------

0/1 KNAPSACK PROBLEM USING 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]

Below example shows the working of the above algorithm:

Page 16
UNIT-IV BACKTRACKING

Page 17

You might also like