Module 5 Complete Notes
Module 5 Complete Notes
Introduction:
Some problems can be solved, by exhaustive search. The exhaustive-search technique
suggests generating all candidate solutions and then identifying the one (or the ones) with a
desired property.
Backtracking is a more intelligent variation of this approach. The principal idea is to
construct solutions one component at a time and evaluate such partially constructed
candidates as follows. If a partially constructed solution can be developed further without
violating the problem’s constraints, it is done by taking the first remaining legitimate
option for the next component. If there is no legitimate option for the next component, no
Alternatives for any remaining component need to be considered. In this case, the
algorithm backtracks to replace the last component of the partially constructed solution
with its next option.
It is convenient to implement this kind of processing by constructing a tree of choices being
made, called the state-space tree. Its root represents an initial state before the search for a
solution begins. The nodes of the first level in the tree represent the choices made for the
first component of a solution; the nodes of the second level represent the choices for the
second component, and so on. A node in a state-space tree is said to be promising if it
corresponds to a partially constructed solution that may still lead to a complete solution;
otherwise, it is called non-promising. Leaves represent either non-promising dead Ends or
complete solutions found by the algorithm.
In the majority of cases, a state space tree for a backtracking algorithm is constructed in the
manner of depth-first search. If the current node is promising, its child is generated by
adding the first remaining legitimate option for the next component of a solution, and the
processing moves to this child. If the current node turns out to be non-promising, the
algorithm backtracks to the node’s parent to consider the next possible option for its last
component; if there is no such option, it backtracks one more level up the tree, and so on.
Finally, if the algorithm reaches a complete solution to the problem, it either stops (if just
one solution is required) or continues searching for other possible solutions.
DAA-18CS42
We start with the empty board and then place queen 1 in the first possible position of its row,
which is in column 1 of row . Then we place queen 2, after trying unsuccessfully columns 1
and 2, in the first acceptable position for it, which is square (2, 3),
the square in row 2 and column 3. This proves to be a dead end because there is no acceptable
position for queen 3. So, the algorithm backtracks and puts queen 2 in the next possible
position at (2, 4). Then queen 3 is placed at (3, 2), which proves to be another dead end. The
algorithm thenbacktracks all the way to queen 1 and moves it to (1, 2). Queen 2 then goes to
(2, 4), queen 3 to (3, 1), and queen 4 to (4, 3), which is a solution to the problem. The state-
space tree of this search is shown in figure.
If other solutions need to be found, the algorithm can simply resume its operations at the leaf
at which it stopped. Alternatively, we can use the board’s symmetry for this purpose.
Finally, it should be pointed out that a single solution to the n-queens problem for any n ≥ 4
can be found in linear time.
ADA-BCS401
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:
Page 4
ADA-BCS401
Example 2: Apply backtracking to solve the following instance of the subset sum problem: A
= {3, 4, 5,6} and d = 13.
Page 5
ADA-BCS401
It is convenient to order the items of a given instance in descending order by their value-to-
weight ratios.
Each node on the ith level of state space tree, 0 ≤ i ≤ n, represents all the subsets of n items
that include a particular selection made from the first i ordered items. This particular
selection is uniquely determined by the path from the root to the node: a branch going to the
left indicates the inclusion of the next item, and a branch going to the right indicates its
exclusion.
Page 6
ADA-BCS401
We record the total weight w and the total value v of this selection in the node, along
with some upper bound ub on the value of any subset that can be obtained by adding
zero or more items to this selection. 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).
Example: Consider the following problem. The items are already ordered in
descendingorder of their value-to-weight ratios.
Let us apply the branch-and-bound algorithm. At the root of the state-space tree (see
Figure 12.8), no items have been selected as yet. Hence, both the total weight of the items
already selected w and their total value v are equal to 0. The value of the upper bound is
100.
Node 1, the left child of the root, represents the subsets that include item 1. The total
weight and value of the items already included are 4 and 40, respectively; the value of the
upper bound is 40
+ (10 − 4) * 6 = 76.
Node 2 represents the subsets that do not include item 1. Accordingly, w = 0, v = 0, and
ub = 0 +(10 − 0) * 6 = 60. Since node 1 has a larger upper bound than the upper bound of
node 2, it is more promising for this maximization problem, and we branch from node 1
first. Its children— nodes 3 and 4—represent subsets with item 1 and with and without
item 2, respectively. Since the total weight w of every subset represented by node 3
exceeds the knapsack’s capacity, node 3can be terminated immediately.
Node 4 has the same values of w and v as its parent; the upper bound ub is equal to 40 + (10
− 4) * 5 = 70. Selecting node 4 over node 2 for the next branching (Due to better ub), we
get nodes 5 and 6 by respectively including and excluding item 3. The total weights and
values as well as the upper bounds for these nodes are computed in the same way as for
the preceding nodes.
Branching from node 5 yields node 7, which represents no feasible solutions, and node 8,
which represents just a single subset {1, 3} of value 65. The remaining live nodes 2 and 6
have smaller upper-bound values than the value of the solution represented by node 8.
Hence, both can be terminated making the subset {1, 3} of node 8 the optimal solution to
the problem.
Page 7
ADA-BCS401
Greedy Algorithms for the Knapsack Problem We can think of several greedy approaches to
this problem. One is to select the items in decreasing order of their weights; however, heavier
items may not be the most valuable in the set. Alternatively, if we pick up the items in
decreasing order of their value, there is no guarantee that the knapsack’s capacity will be used
efficiently. Can we find a greedy strategy that takes into account both the weights and values?
Yes, we can, by computing the value-to-weight ratios vi/wi, i = 1, 2, . . . , n, and selecting the
items in decreasing order of these ratios. Here is the algorithm based on this greedy heuristic.
Page 8
ADA-BCS401
Computing the value-to-weight ratios and sorting the items in nonincreasing order of these
efficiency ratios yields
The greedy algorithm will select the first item of weight 4, skip the next item of weight 7, select
the next item of weight 5, and skip the last item of weight 3. The solution obtained happens to
be optimal for this instance .
Greedy algorithm for the continuous knapsack problem
Step 1 Compute the value-to-weight ratios vi/wi, i = 1, . . . , n, for the items given.
Step 2 Sort the items in nonincreasing order of the ratios computed in Step 1.(Ties can be
broken arbitrarily.)
Step 3 Repeat the following operation until the knapsack is filled to its full capacity or no item
is left in the sorted list: if the current item on the list fits into the knapsack in its entirety, take it
and proceed to the next item; otherwise, take its largest fraction to fill the knapsack to its full
capacity and stop
The algorithm will take the first item of weight 4 and then 6/7 of the next item on the sorted list
to fill the knapsack to its full capacity.
Page 9
ADA-BCS401
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 sa (k)
with any predefined accuracy level:
where k is an integer parameter in the range 0 ≤ k < n. The first approximation scheme was
suggested by S. Sahni in 1975 [Sah75]. This algorithm generates all subsets of k items or less,
and for each one that fits into the knapsack it adds the remaining items as the greedy algorithm
would do (i.e., in nonincreasing order of their value-to-weight ratios). The subset of the highest
value obtained in this fashion is returned as the algorithm’s output.
Example :A small example of an approximation scheme with k = 2 is provided in below figure
The algorithm yields {1, 3, 4}, which is the optimal solution for this instance.
For each of those subsets, it needs O(n) time to determine the subset’s possible extension. Thus,
the algorithm’s efficiency is in O(knk+1).
Page 10
ADA-BCS401
Page 11
ADA-BCS401
print(‘0’); failure
Page 12
ADA-BCS401
Now, one more concept: given decision problems P and Q, if an algorithm can transform a
solution for P into a solution for Q in polynomial time, it’s said that Q is poly-time reducible
(or just reducible) to P.
The most famous unsolved problem in computer science is “whether P=NP or P≠NP? ”
Figure: Commonly believed Figure: Commonly believed relationship between P, NP, NP-
relationship between P and NP Complete and NP-hard problems
Page 13
ADA-BCS401
NP-Complete problems have the property that it can be solved in polynomial time if all other
NP-Complete problems can be solved in polynomial time. i.e if anyone ever finds a poly-time
solution to one NP-complete problem, they’ve automatically got one for all the NP-complete
problems; that will also mean that P=NP.
Example for NP-complete is CNF-satisfiability problem. The CNF-satisfiability problem deals
with boolean expressions. This is given by Cook in 1971. The CNF-satisfiability problem asks
whether or not one can assign values true and false to variables of a given boolean expression in
its CNF form to make the entire expression true.
Over the years many problems in NP have been proved to be in P (like Primality Testing). Still,
there are many problems in NP not proved to be in P. i.e. the question still remains whether
P=NP? NP Complete Problems helps in solving this question. They are a subset of NP
problems with the property that all other NP problems can be reduced to any of them in
polynomial time. So, they are the hardest problems in NP, in terms of running time. If it can be
showed that any NP-Complete problem is in P, then all problems in NP will be in P (because of
NP-Complete definition), and hence P=NP=NPC.
NP Hard Problems - These problems need not have any bound on their running time. If any
NP-Complete Problem is polynomial time reducible to a problem X, that problem X belongs to
NP-Hard class. Hence, all NP-Complete problems are also NP-Hard. In other words if a NP-
Hard problem is non-deterministic polynomial time solvable, it is a NP- Complete problem.
Example of a NP problem that is not NPC is Halting Problem.
If a NP-Hard problem can be solved in polynomial time then all NP-Complete can be solved in
polynomial time.
“All NP-Complete problems are NP-Hard but not all NP-Hard problems are not NP- Complete.”
NP-Complete problems are subclass of NP-Hard
The more conventional optimization version of Traveling Salesman Problem for finding the
shortest route is NP-hard, not strictly NP-complete.
Page 14
Decision Tree:
Sorting and searching algorithms work by comparing items of their inputs. We can study the
performance of such algorithms with a device called a decision tree.
Each internal node of a binary decision tree represents a key comparison indicated in the node.
The nodes left subtree contains the information about subsequent comparisons made if k < k,
and its right subtree does the same for the case of k >k. Each leaf represents a possible outcome
of the algorithm’s run on some input of size n. An important point is that the number of leaves
must be at least as large as the number of possible outcomes. The number of comparisons in
the worst case is equal to the height of the algorithm’s decision tree.
h ≥ _log2 l.
Decision tree for the tree-element selection sort. A triple above a node indicates the state of the
array being sorted. Note two redundant comparisons b <a with a single possible outcome
because of the results of some previously made comparisons.
Most sorting algorithms work by comparing elements in a list. By analysing the decision trees
used in these algorithms, we can determine important lower bounds on their time efficiency. In
general, the number of possible outcomes for sorting an arbitrary n-element list is equal to n!.
The height of a binary decision tree for any comparison-based sorting algorithm and hence the
worst-case number of comparisons made by such an algorithm cannot be less than _log2 n!.
Cworst(n) ≥ [log2 n!]. Merge sort takes number of comparisons is n log2 n in its worst case
and hence is asymptotically optimal. This also implies that the asymptotic lower bound n log2
n is tight and therefore cannot be substantially improved.
Decision tree for the three-element insertion sort.
Consider Binary search to search a given key in a finite set of elements. The No. of comparisons
in th worst case is
We dealing here with three-way comparisons in which search key K is compared with some
element A[i] to see whether K <A[i], K = A[i], or K >A[i]. Hence it is natural to try using ternary
decision trees. The internal nodes of that tree indicate the arrays elements being compared with
the search key. The leaves indicate either a matching element in the case of a successful search
or a found interval that the search key belongs to in the case of an unsuccessful search.
For an array of n elements, all such decision trees will have 2n + 1 leaves (n for successful
searches and n + 1 for unsuccessful ones). Since the minimum height h of a ternary tree with l
leaves is [log3 l], We get the following lower bound on the number of worst-case comparisons:
Cworst(n) ≥ [log3(2n + 1)].
The lower bound is smaller than [log2(n + 1)].