CS603PC Daa Unit-5
CS603PC Daa Unit-5
Branch and bound is an algorithm design paradigm which is generally used for solving
combinatorial optimization problems. These problems are typically exponential in terms of time
complexity and may require exploring all possible permutations in worst case. The Branch and
Bound Algorithm technique solves these problems relatively quickly.
Let us consider the 0/1 Knapsack problem to understand Branch and Bound.
There are many algorithms by which the knapsack problem can be solved:
• Greedy Algorithm for Fractional Knapsack
• DP solution for 0/1 Knapsack
• Backtracking Solution for 0/1 Knapsack.
Let’s see the Branch and Bound Approach to solve the 0/1 Knapsack problem: The Backtracking
Solution can be optimized if we know a bound on best possible solution subtree rooted with every
node. If the best in subtree is worse than current best, we can simply ignore this node and its
subtrees. So we compute bound (best solution) for every node and compare the bound with current
best solution before exploring the node.
Example bounds used in below diagram are, A down can give $315, B down can $275, C down
can $225, D down can $125 and E down can $30.
In this tutorial, earlier we have discussed Fractional Knapsack problem using Greedy approach.
We have shown that Greedy approach gives an optimal solution for Fractional Knapsack.
However, this chapter will cover 0-1 Knapsack problem and its analysis.
In 0-1 Knapsack, items cannot be broken which means the thief should take the item as a whole
or should leave it. This is reason behind calling it as 0-1 Knapsack.
Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1, where other constraints
remain the same.
0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an
optimal solution. In many instances, Greedy approach may give an optimal solution.
The following examples will establish our statement.
Example-1
Let us consider that the capacity of the knapsack is W = 25 and the items are as shown in the
following table.
Item A B C D
Profit 24 18 18 10
Weight 24 10 10 7
Without considering the profit per unit weight (pi/wi), if we apply Greedy approach to solve this
problem, first item A will be selected as it will contribute maximum profit among all the
elements.
Item A B C
Weight 10 40 20
Ratio 10 7 6
Using the Greedy approach, first item A is selected. Then, the next item B is chosen. Hence, the
total profit is 100 + 280 = 380. However, the optimal solution of this instance can be achieved by
selecting items, B and C, where the total profit is 280 + 120 = 400.
Hence, it can be concluded that Greedy approach may not give an optimal solution.
To solve 0-1 Knapsack, Dynamic Programming approach is required.
Problem Statement
A thief is robbing a store and can carry a maximal weight of W into his knapsack. There
are n items and weight of ith item is wi and the profit of selecting this item is pi. What items
should the thief take?
Dynamic-Programming Approach
Let i be the highest-numbered item in an optimal solution S for W dollars. Then S' = S - {i} is an
optimal solution for W - wi dollars and the value to the solution S is Vi plus the value of the sub-
problem.
We can express this fact in the following formula: define c[i, w] to be the solution for items 1,2,
… , i and the maximum weight w.
The algorithm takes the following inputs
• The maximum weight W
• The number of items n
• The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …, wn>
Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
For example, consider the graph shown in figure on right side. A TSP tour in the graph is 0-1-3-
2-0. The cost of the tour is 10+25+30+15 which is 80.
We have discussed following solutions
1) Naive and Dynamic Programming
2) Approximate solution using MST
• In cases of a maximization problem, an upper bound tells us the maximum possible solution
if we follow the given node. For example in 0/1 knapsack we used Greedy approach to find
an upper bound.
Lower bound(2) =
Old lower bound - ((second minimum edge cost of 1 +
minimum edge cost of 2)/2)
+ edge cost 1-2)
Note: The only change in the formula is that this time we have included second minimum edge
cost for 1, because the minimum edge cost has already been subtracted in previous level.
Time Complexity: The worst case complexity of Branch and Bound remains same as that of
the Brute Force clearly because in worst case, we may never get a chance to prune a node.
Whereas, in practice it performs very well depending on the different instance of the TSP.
The complexity also depends on the choice of the bounding function as they are the ones
deciding how many nodes to be pruned.
FIFO Branch and Bound solution
What is an E node?
An E node is the one that is being explored at the moment.
In this article, we have briefly discussed the FIFO branch and bound.
To proceed further with the FIFO branch and bound we use a queue.
In FIFO search the E node is assumed as node 1. After that, we need to generate children
of Node 1. The children are the live nodes, and we place them in the queue accordingly.
Then we delete node 2 from the queue and generate children of node 2.
Next, we delete another element from the queue and assume that as the E node. We then
generate all its children.
In the same process, we delete the next element and generate their children. We keep
repeating the process till the queue is covered and we find a node that satisfies the
conditions of the problem. When we have found the answer node, the process terminates.
Then we delete node 1 from the queue, take node 2 as the E node, and generate all its
children:
3 4 5 6
We delete the next element and generate children for the next node, assuming it to be the
E node.
4 5 6
We repeat the same process. The generated children of 4, that is node 9 are killed by the
boundary function.
5 6
Similarly, we proceed further, but the children of nodes 5, meaning the nodes 10, and 11
are also generated and killed by boundary function. The last standing node in the queue is
6, the children of node 6 are 12, and it satisfies all the conditions for the problem,
therefore it is the answer node.
The basic concept of Lower Bound Theory
The lower bound theory is used to find the lowest complexity algorithm to solve a
problem.
The lower bound is necessary for any algorithm as after we have completed it, we can
compare it with the actual complexity of the algorithm. If the complexity and the order of
the algorithm are the same, then we can declare the algorithm optimal. One of the best
examples of optimal algorithms is merge sort. The upper bound should match the lower
bound, that is, L(n) = U(n).
The easiest method to find the lower bound is the trivial lower bound method. If we can
easily observe the lower bounds on the basis of the number of inputs taken and outputs
produced, then it is known as the trivial lower bound method. For example, multiplication
of n x n matrix.
Even in searching, we need to compare the elements, hence it is also a good example of
this method.
While looking at the complex computational theory, we notice that the non -deterministic
have the capability to allow multiple continuations at every step.
It is also widely used for game AI. It is used by the game engine to make non -player
characters or referred to as NPC to learn the behavior of the game, such as the tactics and
the pattern. This feature is very useful in combat games where an element of
unpredictability can be added to make the game more interesting. This is done so that the
scenarios are not as repetitive and the players do not get bored of it. This keeps the
players hooked and hence increases the game-play life.
What are the major differences between deterministic and non-deterministic algorithms?
Deterministic algorithm Non-deterministic algorithm
The deterministic algorithm can determine the The non-deterministic algorithm cannot solve problems
next step. in polynomial time, and cannot determine the next step.
The output is not random. The output has a certain degree of randomness to it.
In a deterministic algorithm, the next step can In a non-deterministic algorithm, the next step cannot be
be determined. determined.
If the language fails to satisfy the first criteria but fulfills the second one it is considered
to be NP-hard. The NP-hard problem cannot be solved in polynomial time. But, if a
problem is proved to be NPC, we do not focus on finding the most efficient algorithm for
it, instead, we focus on designing an approximation algorithm.
Proof:
For TSP to be NP-complete it must fulfill the criteria we discussed above, so we start by
proving that TSP belongs to NP. In the TSP problem we check if the tour of the listed
cities contains each vertex once. Then we calculate the total cost of the edges of the tour.
In the end, we decide which tour has the least cost and can be completed in polynomial
time. Therefore, we can conclude that TSP belongs to NP.
The second criterion is to prove that TSP is NP-hard, we do this by showing that the
Hamiltonian cycle is less than or equal to TSP, as we already know that the Hamiltonian
cycle is NP-Hard.
We assume a graph, that has G= (V, E).
Let G be the graph for the Hamiltonian cycle.
We construct an instance of TSP, the graph for which is denoted by: G’= (V’, E’).
Let us suppose the Hamiltonian cycle h, exists in G. It implies that the cost of each edge
is zero, in G’, and each edge belongs to E. Therefore, h also has a cost of 0 in G’. So if
the graph of the Hamiltonian cycle G has a cost of 0, then graph G’for TSP, also has a
total tour cost of zero.
And vice versa.
Hence, we can conclude that TSP is NP-hard, and hence NP-complete.
Cook’s Theorem
Stephen Cook presented four theorems in his paper “The Complexity of Theorem Proving
Procedures”. These theorems are stated below. We do understand that many unknown terms are
being used in this chapter, but we don’t have any scope to discuss everything in detail.
Following are the four theorems by Stephen Cook −
Theorem-4
If the set S of strings is accepted by a non-deterministic machine within time T(n) = 2n, and
if TQ(k) is an honest (i.e. real-time countable) function of type Q, then there is a constant K,
so S can be recognized by a deterministic machine within time TQ(K8n).
• First, he emphasized the significance of polynomial time reducibility. It means that if we
have a polynomial time reduction from one problem to another, this ensures that any
polynomial time algorithm from the second problem can be converted into a
corresponding polynomial time algorithm for the first problem.
• Second, he focused attention on the class NP of decision problems that can be solved in
polynomial time by a non-deterministic computer. Most of the intractable problems
belong to this class, NP.
• Third, he proved that one particular problem in NP has the property that every other
problem in NP can be polynomially reduced to it. If the satisfiability problem can be
solved with a polynomial time algorithm, then every problem in NP can also be solved in
polynomial time. If any problem in NP is intractable, then satisfiability problem must be
intractable. Thus, satisfiability problem is the hardest problem in NP.
• Fourth, Cook suggested that other problems in NP might share with the satisfiability
problem this property of being the hardest member of NP.
A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A problem
is NP-hard if all problems in NP are polynomial time reducible to it, even though it may not be
in NP itself.
If a polynomial time algorithm exists for any of these problems, all problems in NP would be
polynomial time solvable. These problems are called NP-complete. The phenomenon of NP-
completeness is important for both theoretical and practical reasons.
Definition of NP-Completeness
A language B is NP-complete if it satisfies two conditions
• B is in NP
Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each edge
in h is 0 in G' as each edge belongs to E. Therefore, h has a cost of 0 in G'. Thus, if graph G has
a Hamiltonian cycle, then graph G' has a tour of 0 cost.
Conversely, we assume that G' has a tour h' of cost at most 0. The cost of edges
in E' are 0 and 1 by definition. Hence, each edge must have a cost of 0 as the cost of h' is 0. We
therefore conclude that h' contains only edges in E.
We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of cost at
most 0. TSP is NP-complete.