U5
U5
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.
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.
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.
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.
1. it belongs to class NP
2. every problem in NP is polynomially reducible to D
4. Solve n-Queens problem. Or Explain 8-Queens problem with an algorithm. Explain
why backtracking is the default procedure for solving problems. Or Explain 8
Queens problem with example.[M-14][N-13] [N-14]
The n-queens problem is to place n queens on an n × n chessboard so that no two
queens attack each other by being in the same row or in the same column or on the
same diagonal.
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;
}
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) } } }
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.