![]() |
VOOZH | about |
Suppose you have been given a knapsack or bag with a limited weight capacity, and each item has some weight and value. The problem here is that "Which item is to be placed in the knapsack such that the weight limit does not exceed and the total value of the items is as large as possible?".
Consider the real-life example. Suppose there is a thief and he enters the museum. The thief contains a knapsack, or we can say a bag that has limited weight capacity. The museum contains various items of different values. The thief decides what items are should he keep in the bag so that profit would become maximum.
Some important points related to the knapsack problem are:
A knapsack means a bag. It is used for solving knapsack problems. This problem is solved by using a dynamic programming approach. In this problem, the items are either completely filled or no items are filled in a knapsack. 1 means items are completely filled or 0 means no item in the bag. For example, we have two items having weights of 12kg and 13kg, respectively. If we pick the 12kg item then we cannot pick the 10kg item from the 12kg item (because the item is not divisible); we have to pick the 12kg item completely.
Pseudocode of 0/1 knapsack problem:
DP-01KNAPSACK(p[], w[], n, M) // n: number of items; M: capacity
for ̟j := 0 to M C[0, ̟ j] := 0;
for i := 0 to n C[i, 0] := 0;
for i := 1 to n
for ̟ := 1 to M
if (w[i] >j ̟) // cannot pick item i
C[i, ̟j] := C[i - 1, ̟];
else
if ( p[i] + C[i-1, ̟ - w[i]]) > C[i-1, ̟j])
C[i, j] := p[i] + C[i - 1, ̟j - w[i]];
else
C[i, j ̟] := C[i - 1, j ̟];
return C[n, M];
Below is the implementation of above approach:
The maximum profit is: 220
Time Complexity: O(N*W))
Auxiliary Space: O(N*C)
This problem is also used for solving real-world problems. It is solved by using the Greedy approach. In this problem we can also divide the items means we can take a fractional part of the items that is why it is called the fractional knapsack problem. For example, if we have an item of 13 kg then we can pick the item of 12 kg and leave the item of 1 kg. To solve the fractional problem, we first compute the value per weight of each item.
Pseudocode of Fractional knapsack problem:
GREEDY_FRACTIONAL_KNAPSACK(X, V, W, M)
S ← Φ // Set of selected items, initially empty
SW ← 0 // weight of selected items
SP ← 0 // profit of selected items
i ← 1while i ≤ n do
if (SW + w[i]) ≤ M then
S ← S ∪ X[i]
SW ← SW + W[i]
SP ← SP + V[i]
else
frac ← (M - SW) / W[i]
S ← S ∪ X[i] * frac // Add fraction of item X[i]
SP ← SP + V[i] * frac // Add fraction of profit
SW ← SW + W[i] * frac // Add fraction of weight
end
i ← i + 1
end
Below is the implementation of the above approach:
Maximum value we can obtain = 240
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
| Sr. No | 0/1 knapsack problem | Fractional knapsack problem |
|---|---|---|
| 1. | The 0/1 knapsack problem is solved using dynamic programming approach. | Fractional knapsack problem is solved using a greedy approach. |
| 2. | The 0/1 knapsack problem has an optimal structure. | The fractional knapsack problem also has an optimal structure. |
| 3. | In the 0/1 knapsack problem, we are not allowed to break items. | Fractional knapsack problem, we can break items for maximizing the total value of the knapsack. |
| 4. | 0/1 knapsack problem, finds a most valuable subset item with a total value less than equal to weight. | In the fractional knapsack problem, finds a most valuable subset item with a total value equal to the weight. |
| 5. | In the 0/1 knapsack problem we can take objects in an integer value. | In the fractional knapsack problem, we can take objects in fractions in floating points. |
| 6. | The 0/1 knapsack does not have greedy choice property | The fraction knapsack do have greedy choice property. |