Академический Документы
Профессиональный Документы
Культура Документы
UNIT IV BACKTRACKING
Objectives
Backtracking
Definition: Backtracking is a process where steps are taken towards the final solution and the
details are recorded. If these steps do not lead to a solution some or all of them may have to be
retraced and the relevant details discarded. In theses circumstances it is often necessary to search
through a large number of possible situations in search of feasible solutions.
1. General method
• Useful technique for optimizing search under some constraints
• Express the desired solution as an n-tuple (x1, . . . , xn) where each xi 2 Si, Si being a finite set
• The solution is based on finding one or more vectors that maximize, minimize, or satisfy a
criterion function P(x1, . . . , xn)
• Sorting an array a[n]
– Find an n-tuple where the element xi is the index of ith smallest element in a
– Criterion function is given by a[xi] _ a[xi+1] for 1 _ i < n
– Set Si is a finite set of integers in the range [1,n]
• Brute force approach
– Let the size of set Si be mi
– There are m = m1m2 · · ·mn n-tuples that satisfy the criterion function P
– In brute force algorithm, you have to form all the m n-tuples to determine the optimal
solutions
• Backtrack approach
– Requires less than m trials to determine the solution
– Form a solution (partial vector) and check at every step if this has any chance of
success
– If the solution at any point seems not-promising, ignore it
– If the partial vector (x1, x2, . . . , xi) does not yield an optimal solution, ignore mi+1 ·
· ·mn possible test vectors
even without looking at them
State-space search methods in problem solving have often been illustrated using tree diagrams.
We explore a set of issues related to coordination in collaborative problem solving and design,
A node in the state space tree is promising if it corresponds to the partials constructed solution
that may lead to the complete solution otherwise the nodes are called non-promising. Leaves of
the tree represent either the non- promising dead end or complete solution found by the
algorithm.
Idea of solution:
Algorithm
void NQueens(int k, int n)
// Using backtracking, this procedure prints all
// possible placements of n queens on an nXn
// chessboard so that they are nonattacking.
{
for (int i=1; i<=n; i++) {
if (Place(k, i)) {
x[k] = i;
if (k==n) { for (int j=1;j<=n;j++)
cout << x[j] << ' '; cout << endl;}
else NQueens(k+1, n);
}
}
}
An instance of the Subset Sum problem is a pair (S, t), where S = {x1, x2, ..., xn} is a set of
positive integers and t (the target) is a positive integer. The decision problem asks for a subset of
S whose sum is as large as possible, but not larger than t. This problem is NP-complete.
In sum of subset problem, there are n positive integers wi and a positive integer W. The goal is to
find the all subsets of the integers that sum to W. It is similar to the 0-1 knapsack problem, of
which the solution is only one. If the values and weights of each item are equal, we have the sum
of subsets problem.
• Problem: Given n positive integers w1, ... wn and a positive integer S. Find all subsets of
w1, ... wn that sum to S.
• Example:
n=3, S=6, and w1=2, w2=4, w3=6
Example
The figure shows the pruned state space tree when backtracking is used with n = 4, W = 13, and
w1 = 3, w2 = 4, w3 = 5, w4 = 6
The graph (or vertex) coloring problem, which involves assigning colors to vertices in a graph
such that adjacenct vertices have distinct colors, arises in a number of scientific and engineering
applications such as scheduling , register allocation , optimization and parallel numerical
computation.
Mathmatically, a proper vertex coloring of an undirected graph G=(V,E) is a map c: V -> S such
that c(u) != c(v) whenever there exists an edge (u,v) in G. The elements of set S are called the
available colors. The problem is often to determine the minimum cardinality (the number of
colors) of S for a given graph G or to ask whether it is able to color graph G with a certain
number of colors.
However, coloring a general graph with the minimum number of colors (the cardinality of set S)
is known to be an NP-complete problem.
n 3-color example
The knapsack is nothing but a sack where in which we need to place the given items according
to the capacity of the knapsack. In case of backtracking we consider the profits but in dynamic
programming we consider weights.
i pi wi pi / wi
1 $40 2 $20
2 $30 5 $6
3 $50 10 $5
4 $10 5 $2
• Note the the items are in the correct order
b) bound = profit + p2 + (C - 7) X p3 / w3
= $40 + $30 + (16 -7) X $50/10 =$115
Ø A node in the state space tree is promising if it corresponds to the partials constructed
solution that may lead to the complete solution otherwise the nodes are called non-
promising. Leaves of the tree represent either the non- promising dead end or complete
solution found by the algorithm.
Ø The tree organization of the solution space is state space tree. Each node in the state
space tree defines problem state. Solution state is those problem states s for which the
path from the root to s defines a tuple in the solution space
1. Define backtracking
(a) Is the process of finding the optimal solution by bounding the unwanted solution
(b)Backtracking is the process of finding the optimal solution by tracing back the previous
solutions t
(c)Is the process of finding the optimal solution by performing the minimum route tracing.
(d)None of the above
3. Promising nodes
(a)Are the nodes that do not lead to the solution
(b)Are the nodes that contains minimum cost
(c)The nodes that forms a minimum spanning tree
(d)Are the nodes that lead to the solution in the state space tree
5. N queen problem
(a) is a problem with n queens and n-1x n-1 squares.
(b)Is a problem with n-1 queens and n x n squares
10. What is the minimum number of colors is needed to color the following graph?
11. What is the minimum number of colors is needed to color the following graph?
Review Questions
Big Questions
Lesson Labs
1. Apply backtracking to solve the following instances of subset sum problem S={1,3,4,5}
and d=11.