Professional Documents
Culture Documents
O/I Knapsack Problem: Design and Analysis of Algorithms Unit V Branch and Bound
O/I Knapsack Problem: Design and Analysis of Algorithms Unit V Branch and Bound
O/I Knapsack Problem: Design and Analysis of Algorithms Unit V Branch and Bound
A B&B algorithm searches the complete space of solutions for a given problem for the best solution.
Ø Branch and bound is a systematic method for solving optimization problems
Ø B&B is a rather general optimization technique that applies where the greedy method and
dynamic programming fail.
Ø However, it is much slower. Indeed, it often leads to exponential time complexities in the
worst case.
Ø On the other hand, if applied carefully, it can lead to algorithms that run reasonably fast on
average.
Ø The general idea of B&B is a BFS-like search for the optimal solution, but not all nodes get
expanded (i.e., their children generated). Rather, a carefully selected criterion determines
which node to expand and when, and another criterion tells the algorithm when an optimal
solution has been found.
Suppose that K = 16 and n = 4, and we have the following set of objects ordered by their value
density.
i vi si vi/si
1 $45 3 $15
2 $30 5 $ 6
3 $45 9 $ 5
4 $10 5 $ 2
We will construct the state space where each node contains the total current value in the knapsack,
the total current size of the contents of the knapsack, and maximum potential value that the
knapsack can hold. In the algorithm, we will also keep a record of the maximum value of any node
(partially or completely filled knapsack) found so far. When we perform the depth-first traversal of
the state-space tree, a node is "promising" if its maximum potential value is greater than this current
best value.
We begin the state space tree with the root consisting of the empty knapsack. The current weight
and value are obviously 0. To find the maximum potential value we treat the problem as if it were
the fractional knapsack problem and we were using the greedy algorithmic solution to that
problem. We have shown that the greedy approach to the fractional knapsack problem yields an
In general, for a node at level i in the state space tree (the first i items have been considered for
selection) and for the kth object as the one that will not completely fit into the remaining space in
the knapsack, these formulae can be written:
k-1
totalSize = currentSize + S sj
j=i+1
k-1
bound = currentValue + S vj + (K - totalSize) * (vk/sk)
j=i+1
totalSize = 0 + s1 + s2 = 0 + 3 + 5 = 8
The computation of the bound and the selection criteria for promising nodes is the same as
before. We must replace the depth-first traversal of the state space tree with a breadth first
traversal. In the depth-first traversal the auxiliary data structure used to store Nodes was
(implicitly) the stack. In breath-first traversal, the auxiliary data structure is explicitly the queue.
Ø Because a tour must leave every vertex exactly once, a lower bound on the length of a tour is
b (lower bound) minimum cost of leaving every vertex.
o The lower bound on the cost of leaving vertex v1 is given by the minimum of all the
nonzero entries in row 1 of the adjacency matrix.
o …
Algorithm TSP
1. First, find out all (n -1)! Possible solutions, where n is the number of cities.
2. Next, determine the minimum cost by finding out the cost of everyone of these
(n -1)! Solutions.
3. Finally, keep the one with the minimum cost.
Input
Number of cities n
Cost of traveling between the cities.
c (i, j) i, j = 1, . . , n.
Output
Vector of cities and total cost.
Main Steps
Initialization
c← 0
Cost ← 0
visits ← 0
e = 1 /* pointer of the visited city */
For 1 ≤ r ≤ n
Do {
Choose pointer j with
minimum = c (e, j) = min{c (e, k); visits (k) = 0 and 1 ≤ k ≤ n }
cost ← cost + minimum - cost
e=j
C(r) ← j
C(n) = 1
cost = cost + c (e, 1)
NP-Complete means something very specific and you have to be careful or you will get the
definition wrong. First, an NP problem is a yes/no problem such that
1. There is polynomial-time proof for every instance of the problem with a "yes" answer that
the answer is "yes", or (equivalently)
2. There exists a polynomial-time algorithm (possibly using random variables) that has a non-
zero probability of answering "yes" if the answer to an instance of the problem is "yes" and
will say "no" 100% of the time if the answer is "no." In other words, the algorithm must
have a false-negative rate less than 100% and no false positives.
A problem X is NP-Complete if
1. X is in NP, and
2. For any problem Y in NP, there is a "reduction" from Y to X: a polynomial-time algorithm
that transforms any instance of Y into an instance of X such that the answer to the Y-
instance is "yes" if and only if the answer X-instance is "yes".
What is NP?
NP is the set of all decision problems (question with yes-or-no answer) for which the 'yes'-answers
can be verified in polynomial time (O(n^k) where n is the problem size, and k is a constant) by a
deterministic Turing machine. Polynomial time is sometimes used as the definition of fast or quickly.
What is P?
P is the set of all decision problems which can be solved in polynomial time by a deterministic
Turing machine. Since it can solve in polynomial time, it can also be verified in polynomial time.
Therefore P is a subset of NP.
What is NP-Complete?
A problem x that is in NP is also in NP-Complete if and only if every other problem in NP can be
quickly (ie. in polynomial time) transformed into x. In other words:
1. x is in NP, and
2. Every problem in NP is reducible to x
What is NP-Hard?
NP-Hard are problems that are at least as hard as the hardest problems in NP. Note that NP-
Complete problems are also NP-hard. However not all NP-hard problems are NP (or even a
decision problem), despite having 'NP' as a prefix. That is the NP in NP-hard does not mean 'non-
Basic concepts
• Solvability of algorithms
– There are algorithms for which there is no known solution, for example, Turing’s Halting
Problem
Decision problem
Given an arbitrary deterministic algorithm A and a finite input I
Will A with input I ever terminate, or enter an infinite loop?
– Halting problem cannot be solved by any computer, no matter how much time is provided
In algorithmic terms, there is no algorithm of any complexity to solve this problem
• Efficient algorithms
– Efficiency measured in terms of speed
– For some problems, there is no known efficient solution
– Distinction between problems that can be solved in polynomial time and problems for
which no polynomial time algorithm is known
• Theory of NP-completeness
– Show that many of the problems with no polynomial time algorithms are computationally related
– The group of problems is further subdivided into two classes
NP-complete. A problem that is NP-complete can be solved in polynomial time iff all other
NP-complete problems can also be solved in polynomial time
NP-hard. If an NP-hard problem can be solved in polynomial time then all NP-complete
problems can also be solved in polynomial time
– All NP-complete problems are NP-hard but some NP-hard problems are known not to be NP-
complete
Summary
Ø Branch and bound is a systematic method for solving optimization problems. B&B is a
rather general optimization technique that applies where the greedy method and dynamic
programming fail.
Key Terms
>> Branch and Bound >> 0/1 Knapsack
>> Traveling Salesman Problem >> NP-completeness
>> NP-hard
4. NP complete problems
(a)Belongs to class NP and is polynomial reducible
(b)Belongs to class P and is polynomial reducible
(c)Not belongs to class NP and polynomial reducible
(d)None of the above
5. Class NP problems
(a)Decision problems that are solved by deterministic polynomial algorithm
(b)Decision problems that are solved by nondeterministic polynomial algorithm
(c)Both are true
(d)None of the above
6. Class P problems
(a)Decision problems that are solved by nondeterministic polynomial algorithm
(b)Comes under NP category
(c)Problems that are solved by deterministic algorithms
(d)None of the above
9. The minimum number of queens need to solve the n queen problem properly is
(a)1 (b)2 (c)3 (d)4 (Ans)4
Review Questions
Two Mark Questions
Big Questions
1. Explain briefly branch and bound technique for solving problems.
2. job 1 job 2 job 3 job 4
a 9 2 7 8
b 6 4 3 7
c 5 8 1 8
d 7 6 9 4
Consider the above matrix for assignment problem involving persons and jobs. Explain in
detail how branch and bound technique is useful is solving assignment problems
3. Write an algorithm for 0/1-knapsack problem.
4. Write the three reasons to terminate a search path at the current node in a state-space tree of
a branch-and-bound algorithm.
5. Apply the branch-and-bound technique in solving the Traveling Salesman Problem.
6. Solve the following instance of the knapsack problem by the branch-and-bound algorithm,
with W = 16. (8)
Item Weight Value
1 10 100
2 7 63
3 8 56
4 4 12
7. Give short notes on decision problems, undecidable problem, and NP-Complete problem.
Lesson Lab
1. Apply TSP and calculate the shortest path for the following graph