Daa Unit 5
Daa Unit 5
Branch and bound is one of the techniques used for problem solving. It is similar to the backtracking
since it also uses the state space tree. It is used for solving the optimization problems and
minimization problems. If we have given a maximization problem then we can convert it using the
Branch and bound technique by simply converting the problem into a maximization problem.
Different search techniques in branch and bound: The Branch algorithms incorporate different
search techniques to traverse a state space tree. Different search techniques used in B&B are listed
below:
➢ LC search
➢ BFS
➢ DFS
1. LC search (Least Cost Search): It uses a heuristic cost function to compute the bound values at
each node. Nodes are added to the list of live nodes as soon as they get generated.
The node with the least value of a cost function selected as a next E-node.
2.BFS (Breadth First Search): It is also known as a FIFO search. It maintains the list of live nodes
in first-in-first-out order i.e., in a queue, The live nodes are searched in the FIFO order to make them
next E-nodes.
3. DFS (Depth First Search): It is also known as a LIFO search. It maintains the list of live nodes in
last-in-first-out order i.e., in a stack. The live nodes are searched in the LIFO order to make them next
E-nodes.
Types of Branch and Bound Solutions: The solution of the Branch and the bound problem can be
represented in two ways:
1. Variable size solution: Using this solution, we can find the subset of the given set that gives the
optimized solution to the given problem. For example, if we have to select a combination of elements
from {A, B, C, D} that optimizes the given problem, and it is found that A and B together give the
best solution, then the solution will be {A, B}.
2. Fixed-size solution: There are 0s and 1s in this solution, with the digit at the ith position indicating
whether the ith element should be included, for the above example, the solution will be given by {1,
1, 0, 0}, here 1 represent that we have select the element which at ith position and 0 represent we
don’t select the element at ith position.
Classification of Branch and Bound Problems: The Branch and Bound method can be classified
into three types based on the order in which the state space tree is searched.
1. FIFO Branch and Bound
2. LIFO Branch and Bound
3. Least Cost-Branch and Bound
1. FIFO Branch and Bound: First-In-First-Out is an approach to the branch and bound problem that
uses the queue approach to create a state-space tree. In this case, the breadth-first search is performed,
that is, the elements at a certain level are all searched, and then the elements at the next level are
searched, starting with the first child of the first node at the previous level.
For a given set {A, B, C, D}, the state space tree will be constructed as follows:
The above diagram shows that we first consider element A, then element B, then element C and
finally we’ll consider the last element which is D. We are performing BFS while exploring the nodes.
So, once the first level is completed. We’ll consider the first element, then we can consider either B,
C, or D. If we follow the route then it says that we are doing elements A and D so we will not
consider elements B and C. If we select the elements A and D only, then it says that we are selecting
elements A and D and we are not considering elements B and C.
Now, we will expand node 3, as we have considered element B and not considered element A, so, we
have two options to explore that is elements C and D. Let’s create nodes 9 and 10 for elements C and
D respectively.
Now, we will expand node 4 as we have only considered elements C and not considered elements A
and B, so, we have only one option to explore which is element D. Let’s create node 11 for D.
Till node 5, we have only considered elements D, and not selected elements A, B, and C. So, we have
no more elements to explore, therefore on node 5, there won’t be any expansion.
Now, we will expand node 6 as we have considered elements A and B, so, we have only two option
to explore that is element C and D. Let’s create node 12 and 13 for C and D respectively.
Now, we will expand node 7 as we have considered elements A and C and not consider element B,
so, we have only one option to explore which is element D. Let’s create node 14 for D.
Till node 8, we have considered elements A and D, and not selected elements B and C, So, we have
no more elements to explore, therefore on node 8, there won’t be any expansion.
Now, we will expand node 9 as we have considered elements B and C and not considered element A,
so, we have only one option to explore which is element D. Let’s create node 15 for D.
2. LIFO Branch and Bound: The Last-In-First-Out approach for this problem uses stack in creating
the state space tree. When nodes are added to a state space tree, they are added to a stack. After all
nodes of a level have been added, we pop the topmost element from the stack and explore it. For a
given set {A, B, C, D}, the state space tree will be constructed as follows:
Now the expansion would be based on the node that appears on the top of the stack. Since node 5
appears on the top of the stack, so we will expand node 5. We will pop out node 5 from the stack.
Since node 5 is in the last element, i.e., D so there is no further scope for expansion.
The next node that appears on the top of the stack is node 4. Pop-out node 4 and expand. On
expansion, element D will be considered and node 6 will be added to the stack shown below:
The next node is 6 which is to be expanded. Pop-out node 6 and expand. Since node 6 is in the last
element, i.e., D so there is no further scope for expansion.
The next node to be expanded is node 3. Since node 3 works on element B so node 3 will be
expanded to two nodes, i.e., 7 and 8 working on elements C and D respectively. Nodes 7 and 8 will
be pushed into the stack.
The next node that appears on the top of the stack is node 8. Pop-out node 8 and expand. Since node 8
works on element D so there is no further scope for the expansion.
The next node that appears on the top of the stack is node 7. Pop-out node 7 and expand. Since node 7
works on element C so node 7 will be further expanded to node 9 which works on element D and
node 9 will be pushed into the stack.
The next node is 6 which is to be expanded. Pop-out node 6 and expand. Since node 6 is in the last
element, i.e., D so there is no further scope for expansion.
The next node that appears on the top of the stack is node 9. Since node 9 works on element D, there
is no further scope for expansion.
The next node that appears on the top of the stack is node 2. Since node 2 works on the element A so
it means that node 2 can be further expanded. It can be expanded up to three nodes named 10, 11, 12
working on elements B, C, and D respectively. There new nodes will be pushed into the stack shown
as below:
3. Least Cost-Branch and Bound: To explore the state space tree, this method uses the cost
function. The previous two methods also calculate the cost function at each node but the cost is not
been used for further exploration.
In this technique, nodes are explored based on their costs, the cost of the node can be defined using
the problem and with the help of the given problem, we can define the cost function. Once the cost
function is defined, we can define the cost of the node.
Now, consider a node whose cost has been determined. If this value is greater than U0, this node or
its children will not be able to give a solution. As a result, we can kill this node and not explore its
further branches. As a result, this method prevents us from exploring cases that are not worth it,
which makes it more efficient for us.
Let’s first consider node 1 having cost infinity shown below: In the following diagram, node 1 is
expanded into four nodes named 2, 3, 4, and 5.
Assume that cost of the nodes 2, 3, 4, and 5 are 12, 16, 10, and 315 respectively. In this method, we
will explore the node which is having the least cost. In the above figure, we can observe that the node
with a minimum cost is node 4. So, we will explore node 4 having a cost of 10.
During exploring node 4 which is element C, we can notice that there is only one possible element
that remains unexplored which is D (i.e, we already decided not to select elements A, and B). So, it
will get expanded to one single element D, let’s say this node number is 6.
Now, Node 6 has no element left to explore. So, there is no further scope for expansion. Hence the
element {C, D} is the optimal way to choose for the least cost.
In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits and weights
of items. Here we have not taken the weight 8 directly, problem is divided into sub-problems, i.e., 0,
1, 2, 3, 4, 5, 6, 7, 8. The solution of the sub-problems would be saved in the cells and answer to the
problem would be stored in the final cell. First, we write the weights in the ascending order and
profits according to their weights shown as below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}
The first row and the first column would be 0 as there is no item for w=0
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
When i=1, W=1: w1 = 3; Since we have only one item in the set having weight 3, but the capacity of
the knapsack is 1. We cannot fill the item of 3kg in the knapsack of capacity 1 kg so add 0 at M [1][1]
shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
When i = 1, W = 2: w1 = 3; Since we have only one item in the set having weight 3, but the capacity
of the knapsack is 2. We cannot fill the item of 3kg in the knapsack of capacity 2 kg so add 0 at M
[1][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0
2 0
3 0
4 0
When i=1, W=3: w1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is also 3; therefore, we can fill the knapsack with an item of weight equal to 3.
We put profit corresponding to the weight 3, i.e., 2 at M [1][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2
2 0
3 0
4 0
When i=1, W = 4: W1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is 4; therefore, we can fill the knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 at M [1][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2
2 0
3 0
4 0
When i=1, W = 5: W1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is 5; therefore, we can fill the knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 at M [1][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2
2 0
3 0
4 0
When i =1, W=6: W1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is 6; therefore, we can fill the knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 at M [1][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2
2 0
3 0
4 0
When i=1, W = 7: W1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is 7; therefore, we can fill the knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 at M [1][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2
2 0
3 0
4 0
When i =1, W =8: W1 = 3; Since we have only one item in the set having weight equal to 3, and
weight of the knapsack is 8; therefore, we can fill the knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 at M [1][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0
3 0
4 0
When i =2, W = 1: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have only
one item in the set having weight equal to 4, and the weight of the knapsack is 1. We cannot put the
item of weight 4 in a knapsack, so we add 0 at M [2][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0
3 0
4 0
When i =2, W = 2: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have only
one item in the set having weight equal to 4, and the weight of the knapsack is 2. We cannot put the
item of weight 4 in a knapsack, so we add 0 at M [2][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0
3 0
4 0
When i =2, W = 3: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 3. We can put the item of
weight 3 in a knapsack, so we add 2 at M [2][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2
3 0
4 0
When i =2, W = 4: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 4. We can put item of
weight 4 in a knapsack as the profit corresponding to weight 4 is more than the item having weight 3,
so we add 3 at M [2][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3
3 0
4 0
When i = 2, W = 5: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 5. We can put item of
weight 4 in a knapsack and the profit corresponding to weight is 3, so we add 3 at M [2][5] shown as
below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3
3 0
4 0
When i = 2, W = 6: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 6. We can put item of
weight 4 in a knapsack and the profit corresponding to weight is 3, so we add 3 at M [2][6] shown as
below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3
3 0
4 0
When i = 2, W = 7: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 7. We can put item of
weight 4 and 3 in a knapsack and the profits corresponding to weights are 2 and 3; therefore, the total
profit is 5, so we add 5 at M [2][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5
3 0
4 0
When i = 2, W = 8: The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two
items in the set having weights 3 and 4, and the weight of the knapsack is 7. We can put item of
weight 4 and 3 in a knapsack and the profits corresponding to weights are 2 and 3; therefore, the total
profit is 5, so we add 5 at M [2][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0
4 0
Now the value of 'i' gets incremented, and becomes 3.
When i = 3, W = 1: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set having weights 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither
of the items in a knapsack, so we add 0 at M [3][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0
4 0
When i = 3, W = 2: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set having weight 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither
of the items in a knapsack, so we add 0 at M [3][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0
4 0
When i = 3, W = 3: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively and weight of the knapsack is 3. The item with a
weight 3 can be put in the knapsack and the profit corresponding to the item is 2, so we add 2 at M
[3][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2
4 0
When i = 3, W = 4: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively, and weight of the knapsack is 4. We can keep the
item of either weight 3 or 4; the profit (3) corresponding to the weight 4 is more than the profit
corresponding to the weight 3 so we add 3 at M [3][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3
4 0
When i = 3, W = 5: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively, and weight of the knapsack is 5. We can keep the
item of either weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits
corresponding to the weight 3 and 5 so we add 3 at M [3][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3
4 0
When i =3, W = 6: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively, and weight of the knapsack is 6. We can keep the
item of either weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits
corresponding to the weight 3 and 5 so we add 3 at M [3][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3
4 0
When i =3, W = 7: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively, and weight of the knapsack is 7. In this case, we
can keep both the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so
we add 5 at M [3][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5
4 0
When i = 3, W = 8: The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three
items in the set of weight 3, 4, and 5 respectively, and the weight of the knapsack is 8. In this case, we
can keep both the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so
we add 5 at M [3][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0
When i = 4, W = 1: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 1. The weight
of all the items is more than the weight of the knapsack, so we cannot add any item in the knapsack;
Therefore, we add 0 at M [4][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0
When i = 4, W = 2: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 2. The weight
of all the items is more than the weight of the knapsack, so we cannot add any item in the knapsack;
Therefore, we add 0 at M [4][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0
When i = 4, W = 3: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 3. The item
with a weight 3 can be put in the knapsack and the profit corresponding to the weight 4 is 2, so we
will add 2 at M [4][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2
When i = 4, W = 4: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 4. The item
with a weight 4 can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we
will add 3 at M [4][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3
When i = 4, W = 5: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 5. The item
with a weight 4 can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we
will add 3 at M [4][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3
When i = 4, W = 6: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 6. In this case,
we can put the items in the knapsack either of weight 3, 4, 5 or 6 but the profit, i.e., 4 corresponding
to the weight 6 is highest among all the items; therefore, we add 4 at M [4][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4
When i = 4, W = 7: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 7. Here, if we
add two items of weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so
we add 5 at M [4][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5
When i = 4, W = 8: The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four
items in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 8. Here, if we
add two items of weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so
we add 5 at M [4][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
As we can observe in the above table that 5 is the maximum profit among all the entries. The pointer
points to the last row and the last column having 5 values. Now we will compare 5 values with the
previous row; if the previous row, i.e., i = 3 contains the same value 5 then the pointer will shift
upwards. Since the previous row contains the value 5 so the pointer will be shifted upwards as shown
in the below table:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
Again, we will compare the value 5 from the above row, i.e., i = 2. Since the above row contains the
value 5 so the pointer will again be shifted upwards as shown in the below table:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
Again, we will compare the value 5 from the above row, i.e., i = 1. Since the above row does not
contain the same value so we will consider the row i=1, and the weight corresponding to the row is 4.
Therefore, we have selected the weight 4 and we have rejected the weights 5 and 6 shown below:
x = {1, 0, 0}
The profit corresponding to the weight is 3. Therefore, the remaining profit is (5 - 3) equals to 2. Now
we will compare this value 2 with the row i = 2. Since the row (i = 1) contains the value 2; therefore,
the pointer shifted upwards shown below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
Again, we compare the value 2 with a above row, i.e., i = 1. Since the row i =0 does not contain the
value 2, so row i = 1 will be selected and the weight corresponding to the i = 1 is 3 shown below:
X = {1, 1, 0, 0}
The profit corresponding to the weight is 2. Therefore, the remaining profit is 0. We compare 0 value
with the above row. Since the above row contains a 0 value but the profit corresponding to this row is
0. In this problem, two weights are selected, i.e., 3 and 4 to maximize the profit.