Unit 5
Unit 5
COM
UNIT-V
FIGURE Complete state-space tree of the backtracking algorithm applied to the instance
The state-space tree can be constructed as a binary tree like that in the instance A =
{3, 5, 6, 7} and d = 15.
STUDENTSFOCUS.COM
The root of the tree represents the starting point, with no decisions about the given
elements made as yet.
Its left and right children represent, respectively, inclusion and exclusion of a1 in a
set being sought. Similarly, going to the left from a node of the first level
corresponds to inclusion of a2 while going to the right corresponds to its
exclusion, and so on.
Thus, a path from the root to a node on the ith level of the tree indicates which of
the first I numbers have been included in the subsets represented by that node. We
record the value of s, the sum of these numbers, in the node.
If s is equal to d, we have a solution to the problem. We can either report this
result and stop or, if all the solutions need to be found, continue by backtracking
to the node‘s parent.
If s is not equal to d, we can terminate the node as non-promising if either of the
following two inequalities holds:
+ i+1 > [the sum s is too
large], s +
= +1 j < d [the sum s is too small].
APPROXIMATION ALGORITHM FOR NP HARD PROBLEMS
A polynomial-time approximation algorithm is said to be a approximation algorithm,
where c ≥ 1, if the
accuracy ratio of the approximation it produces does not exceed c for any instance of the
problem in question: r(sa) ≤ c.
KNAPSACK PROBLEM
The knapsack problem, given n items of known weights w1, . . . , wn and values v1, .
. . , vn and a knapsack of weight capacity W, find the most valuable subset of the
items that fits into the knapsack.
K-APPROXIMATION SCHEME
Approximation Schemes We now return to the discrete version of the knapsack
problem. For this problem, unlike the traveling salesman problem, there exist
polynomial-time approximation schemes, which are parametric families of algorithms
that allow us to get approximations s(k) a with any predefined accuracy level:
For Example,
Multifragment-heuristic algorithm
Step 1 Sort the edges in increasing order of their weights. (Ties can be broken
arbitrarily.) Initialize the set of tour edges to be constructed to the empty set.
Step 2 Repeat this step n times, where n is the number of cities in the instance being
solved: add the next edge on the sorted edge list to the set of tour edges, provided this
addition does not create a vertex of degree 3 or a cycle of length less than n; otherwise,
skip the edge.
Step 3 Return the set of tour edges.
As an example, applying the algorithm to the above graph yields {(a, b), (c, d), (b, c), (a, d)}.
This set of edges forms the same tour as the one produced by the nearest-neighbor algorithm. In general,
the multifragment-heuristic algorithm tends to produce significantly better tours than the nearest-neighbor
algorithm, as we are going to see from the experimental data quoted at the end of this section. But the
performance ratio of the multifragment-heuristic algorithm is also unbounded, of course.
Minimum-Spanning-Tree–Based Algorithms
There are approximation algorithms for the traveling salesman problem that exploit a connection between
Hamiltonian circuits and spanning trees of the same graph. Since removing an edge from
a Hamiltonian circuit yields a spanning tree, we can expect that the structure of a minimum spanning tree
provides a good basis for constructing a shortest tour approximation. Here is an algorithm that
implements this idea in a rather straightforward fashion.
Twice-around-the-tree algorithm
Step 1 Construct a minimum spanning tree of the graph corresponding to a given instance of the traveling
salesman problem.
STUDENTSFOCUS.COM
Step 2 Starting at an arbitrary vertex, perform a walk around the minimum spanning tree recording all the
vertices passed by. (This can be done by a DFS traversal.)
Step 3 Scan the vertex list obtained in Step 2 and eliminate from it all repeated occurrences of the same
vertex except the starting one at the end of the list. (This step is equivalent to making shortcuts in the
walk.) The vertices remaining on the list will form a Hamiltonian circuit, which is the output of the
algorithm.
EXAMPLE 2 Let us apply this algorithm to the graph in Figure 12.11a. The
minimum spanning tree of this graph is made up of edges (a, b), (b, c), (b, d), and
(d, e) (Figure 12.11b). A twice-around-the-tree walk that starts and ends at a is a, b, c, b, d, e, d, b, a.
Eliminating the second b (a shortcut from c to d), the second d, and the third b (ashortcut from e
to a) yields the Hamiltonian circuita, b, c, d, e, a of length 39.
be solved in polynomial time are called tractable, and problems that cannot be
solved in polynomial time are called intractable.
Class P is a class of decision problems that can be solved in polynomial
time by (deterministic) algorithms. This class of problems is called
polynomial. (Class P)
Examples: Searching, Element uniqueness, primality test, graph acyclicity
1. it belongs to class NP
2. every problem in NP is polynomially reducible to D
STUDENTSFOCUS.COM
State space tree for 4-queen problem. Similar to other queen problem also.
Algorithm place(k,I)
{
for j := 1 to k-1 do
if(x[j]=I) or(abs(x[j]-I)=abs(j-k))) then return false;
return true;
STUDENTSFOCUS.COM
}
Algorithm Nqueens(k,n)
{
for I:= 1 to n do
{
if( place(k,I) then
{
x[k]:= I;
if(k=n) then write(x[1:n]);
else
Nqueens(k+1,n) } } }
Assignment Problem
Let us illustrate the branch-and-bound approach by applying it to the problem of assigning n people to n
jobs so that the total cost of the assignment is as smallas possible. We introduced this problem in Section
3.4, where we solved it by exhaustive search. Recall that an instance of the assignment problem is
specified by an n × n cost matrix C so that we can state the problem as follows: select one element in
each row of the matrix so that no two selected elements are in the same column and their sum is the
smallest possible.
STUDENTSFOCUS.COM
STUDENTSFOCUS.COM
How can we find a lower bound on the cost of an optimal selection withoutactually solving the
problem?We can do this by several methods. For example, it is clear that the cost of any solution,
including an optimal one, cannot be smaller than the sum of the smallest elements in each of the
matrix’s rows. For the instance here, this sum is 2 + 3+ 1+ 4 = 10. It is important to stress that this is not
the cost of any legitimate selection (3 and 1 came from the same column of the matrix); it is just a lower
bound on the cost of any legitimate selection. We can and will apply the same thinking to partially
constructed solutions. For example, for any legitimate selection that selects 9 from the first row, the lower
bound will be9 + 3 + 1+ 4 = 17.
STUDENTSFOCUS.COM
Knapsack Problem
A simple way to compute the upper bound ub is to add to v, the total value of the items already selected,
the product of the remaining capacity of the knapsack W − w and the best per unit payoff among the
remaining items, which is vi+1/wi+1:
ub = v + (W − w)(vi+1/wi+1).
Consider the following problem,
STUDENTSFOCUS.COM
The solution is the items 1 and 3 are included with maximum profit value of 65
To reduce the amount of potential work, we take advantage of twoobservations made in Section 3.4. First,
without loss of generality, we can consider only tours that start at a. Second, because our graph is
undirected, we can generate only tours in which b is visited before c. In addition, after visiting n − 1= 4
cities,a tour has no choice but to visit the remaining unvisited city and return to the
starting one.
The optimal solution is the tour with a,b,d,e,c,a with tour length of 16.