## 0/1 Knapsack problemHere knapsack is like a container or a bag. Suppose we have given some items which have some weights or profits. We have to put some items in the knapsack in such a way total value produces a maximum profit. For example, the weight of the container is 20 kg. We have to select the items in such a way that the sum of the weight of items should be either smaller than or equal to the weight of the container, and the profit should be maximum. There are two types of knapsack problems: - 0/1 knapsack problem
- Fractional knapsack problem
We will discuss both the problems one by one. First, we will learn about the 0/1 knapsack problem. ## What is the 0/1 knapsack problem?The 0/1 knapsack problem means that the items are either completely or no items are filled in a knapsack. For example, we have two items having weights 2kg and 3kg, respectively. If we pick the 2kg item then we cannot pick 1kg item from the 2kg item (item is not divisible); we have to pick the 2kg item completely. This is a 0/1 knapsack problem in which either we pick the item completely or we will pick that item. The 0/1 knapsack problem is solved by the dynamic programming. ## What is the fractional knapsack problem?The fractional knapsack problem means that we can divide the item. For example, we have an item of 3 kg then we can pick the item of 2 kg and leave the item of 1 kg. The fractional knapsack problem is solved by the Greedy approach. ## Example of 0/1 knapsack problem.Consider the problem having weights and profits are: Weights: {3, 4, 6, 5} Profits: {2, 3, 1, 4} The weight of the knapsack is 8 kg The number of items is 4 The above problem can be solved by using the following method: x = {0, 0, 0, 1} = {0, 1, 0, 1} The above are the possible combinations. 1 denotes that the item is completely picked and 0 means that no item is picked. Since there are 4 items so possible combinations will be:
Another approach to solve the problem is dynamic programming approach. In dynamic programming approach, the complicated problem is divided into sub-problems, then we find the solution of a sub-problem and the solution of the sub-problem will be used to find the solution of a complex problem. ## How this problem can be solved by using the Dynamic programming approach?First, we create a matrix shown as below:
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:
w
w
w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 2 is 4, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 3 is 5, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
The weight corresponding to the value 4 is 6, i.e., w
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 value. Now we will compare 5 value 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:
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:
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:
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:
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:
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. Next TopicDUTCH NATIONAL FLAG |