Professional Documents
Culture Documents
Daa Unit-4
Daa Unit-4
UNIT IV
Backtracking: The General Method, The 8-Queens problem, sum of subsets, Graph coloring,
Hamiltonian cycles, knapsack problem.
Backtracking:
State space is the set of paths from root node to other nodes. State space tree is the tree
organization of the solution space. The state space trees are called static trees. This terminology
follows from the observation that the tree organizations are independent of the problem instance
being solved.
Terminology:
Problem state is each node in the depth first search tree.
Solution states are the problem states „S‟ for which the path from the root node to „S‟ defines a
tuple in the solution space.
N-Queens Problem:
All solutions to the 8-queens problem can be represented as 8-tuples (x1, ......... , x8), where xi is
the column of the ith row where the ith queen is placed.
The promising function must check whether two queens are in the same column or diagonal:
Suppose two queens are placed at positions (i, j) and (k, l) Then:
Example:
Suppose we start with the feasible sequence 7, 5, 3, 1.
Steps:
1. Start with an empty set
2. Add the next element from the list to the set
3. If the subset is having sum M, then stop with that subset as solution.
4. If the subset is not feasible or if we have reached the end of the set, then backtrack
through the subset until we find the most suitable value.
5. If the subset is feasible (sum of seubset < M) then go to step 2.
6. If we have visited all the elements without finding a suitable subset and if no
backtracking is possible then stop without solution.
Example:
Consider the sum-of-subset problem, n = 4, Sum = 13, and w = (3, 4, 5, 6). Find a
solution to the problem using backtracking. Show the state-space tree leading to the solution.
Also, number the nodes in the tree in the order of recursion calls.
Set X = [0, 0, 0, 0]
Sum > M, so backtrack and remove the previously added item from the solution set.
X[i] = X[4] = 1 ⇒ X =[1, 1, 0, 1] A complete state space tree for given data is shown in Fig. (b)
Graph Coloring
Solution: Firstly, we start our search with vertex 'a.' this vertex 'a' becomes the root of our
implicit tree.
Branch and Bound refers to all state space search methods in which all children of the E-node are
generated before any other live node becomes the E-node
Branch and Bound is the generalization of both graph search strategies, BFS and D- search.
A BFS like state space search is called as FIFO (First in first out) search as the list of live
nodes in a first in first out list (or queue).
A D search like state space search is called as LIFO (Last in first out) search as the list of
live nodes in a last in first out (or stack).
Live node is a node that has been generated but whose children have not yet been
generated.
E-node is a live node whose children are currently being explored. In other words, an E-
node is a node currently being expanded.
Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.
Branch-an-bound refers to all state space search methods in which all children of an E-
node are generated before any other live node can become the E-node.
For this we will use a data structure called Queue. Initially Queue is empty
In FIFO search, first we will take E-node as a node 1. Next we generate the children of node 1.
We will place all these live nodes in a queue.
2 3 4 5
Now one level is completed. Once I take first job, then we can consider either j2, j3 or j4. If we
follow the route then it says that we are doing jobs j1 and j4 so we will not consider jobs j2 and
j3.
Now we will consider the node 3. In this case, we are doing job j2 so we can consider either job
j3 or j4. Here, we have discarded the job j1.
4 5 6 7 8 9 10
Finally,
For this we will use a data structure called stack. Initially stack is empty.
On each expansion, the node will be pushed into the stack shown as below:
Now the expansion would be based on the node that appears on the top of the stack. Since the
node 5 appears on the top of the stack, so we will expand the node 5. We will pop out the node 5
from the stack. Since the node 5 is in the last job, i.e., j4 so there is no further scope of
expansion.
There is one more method that can be used to find the solution and that method is Least cost
branch and bound. In this technique, nodes are explored based on the cost of the node. The cost
of the node can be defined using the problem and with the help of the given problem, we can
define the cost function. Once the cost function is defined, we can define the cost of the node.
Now we will expand the node 1. The node 1 will be expanded into four nodes named as 2, 3, 4
and 5 shown as below:
Let's assume that cost of the nodes 2, 3, 4, and 5 are 25, 12, 19 and 30 respectively.
Since it is the least cost branch n bound, so we will explore the node which is having the least
cost. In the above figure, we can observe that the node with a minimum cost is node 3. So, we
will explore the node 3 having cost 12.
Since the node 3 works on the job j2 so it will be expanded into two nodes named as 6 and 7
shown as below:
Knapsack problem is maximization problem but branch and bound technique is applicable for
only minimization problems. In order to convert maximization problem into minimization
problem we have to take negative sign for upper bound and lower bound.
Therefore,
Upper bound (U) = -32
Lower bound (L) = -38
We choose the path, which has minimum difference of upper bound and lower bound. If the
difference is equal then we choose the path by comparing upper bounds and we discard node
with maximum upper bound.
Let G = (V', E) be a directed graph defining an instance of the traveling salesperson problem. Let
Cij equal the cost of edge (i, j), Cij = ∞ if (i, j) != E, and let IVI = n, without loss of generality,
we can assume that every tour starts and ends at vertex 1.
Row Reduction:
a) Take the minimum element from first row, subtract it from all elements of first row, next
take minimum element from the second row and subtract it from second row. Similarly apply the
same procedure for all rows.
b) Find the sum of elements, which were subtracted from rows.
c) Apply column reductions for the matrix obtained after row reduction.
Column Reduction:
d) Take the minimum element from first column, subtract it from all elements of first column,
next take minimum element from the second column and subtract it from second column.
Similarly apply the same procedure for all columns.
Associate the cumulative reduced sum to the starting state as lower bound and α as upper bound.