Fractional vs 0/1 knapsack problem

Before understanding the differences between the fractional and 0/1 knapsack problems, we should know about the fractional and 0/1 knapsack problems separately.

What is a knapsack problem?

Suppose you have 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 the knapsack, or we can say 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:

  • It is a combinatorial optimization-related problem.
  • Given a set of N items - usually numbered from 1 to N; each of these items has a mass wi and a value vi.
  • It determines the number of each item to be included in a collection so that the total weight M is less than or equal to a given limit and the total value is as large as possible.
  • The problem often arises in resource allocation where there are financial constraints.

Suppose we have a knapsack of 15 kg, so M = 15 kg. It means that the total weight of the knapsack is 15 kg. The total weight of the items to be included in the knapsack should have the weight less than or equal to the M. Some items are given which have some key features, i.e., weight of the item, and the value of the item. As we can observe below that the first item has the weight as 12kg and the value as $4, second item has the weight as 2kg and the value as $2, third item has the weight as 1 kg and the value as $1, fourth item has the weight as 4 kg and the value as $10, and the fifth item has the weight as 1 kg and the value as the $2. Here, we have to decide whether we have to include the item or not.

Knapsack problem variants

There are two variants of knapsack problem:

  • 0/1 knapsack problem: In the case of 0/1 knapsack problem, items are indivisible. Here, indivisible means that we cannot break an item. In this, we can either take an item or not. We either take the item completely and keep in the knapsack or we leave the item completely. There is no possibility that we keep some fraction of the item in the knapsack.

Here, '0' means that we are not taking that item and '1' means that we are taking the item completely. This type of problem is solved by using the dynamic programming approach.

Some important points related to the 0/1 Knapsack problem are:

  • In this problem, we cannot take the fraction of the items. Here, we have to decide whether we have to take the item, i.e., x = 1 or not, i.e., x = 0.
  • The greedy approach does not provide the optimal result in this problem.
  • Another approach is to sort the items by cost per unit weight and starts from the highest until the knapsack is full. Still, it is not a good solution. Suppose there are N items. We have two options either we select or exclude the item. The brute force approach has O(2N) exponential running time. Instead of using brute force approach, we use the dynamic programming approach to obtain the optimal solution.
  • Fractional knapsack problem: In this problem, items are divisible. Here, divisible means that you can take any fraction of an item. This problem is solved by using a greedy approach.

Some important points related to the fractional knapsack problem are:

  • As we know that the fractional knapsack problem uses the fraction of the items so greedy approach is used in this problem.
  • The fractional knapsack problem can be solved by first sorting the items according to their values, and it can be done in O(NlogN) This approach starts with finding the most valuable item, and we consider the most valuable item as much as possible, so start with a highest value item denoted by vi. Then, we consider the next item from the sorted list, and in this way, we perform the linear search in O(N) time complexity.
  • Therefore, the overall running time would be O(NlogN) plus O(N) equals to O(NlogN). We can say that the fractional knapsack problem can be solved much faster than the 0/1 knapsack problem.

Differences between the 0/1 Knapsack problem and Fractional knapsack problem.

Fractional vs 0/1 knapsack problem
0/1 knapsack problemFractional knapsack problem
This problem is solved using dynamic programming approach.This problem is solved using greedy approach.
For example: Suppose we have 10 amount of space. The amount of A is 5, the amount of B is 4, and the amount of C is 3. First, we put A then we put B in the knapsack. The total space occupied by the knapsack is 9. Since the total space in knapsack is 10 so we cannot put 'C' in the knapsack.For example, suppose we have 10 amount of space. The amount of A is 5, the amount of B is 4, and the amount of C is 3. First, we put A then we put B in the knapsack. Till now, the total space occupied by the knapsack is 9. 1 space is remaining in the knapsack. So, we can take 1 amount from the C and put it in the knapsack. Now, the space in the knapsack is completed.
This problem either takes an item or not. It does not take a part of it.In this problem, we can also take a fraction of item.
It has optimal structure.It has a optimal structure.
It finds a most valuable subset item with a total value less than or equal to weight.It finds item with a total value equal to weight.