Javatpoint Logo
Javatpoint Logo

Fractional Knapsack Problem

The fractional knapsack problem is also one of the techniques which are used to solve the knapsack problem. In fractional knapsack, the items are broken in order to maximize the profit. The problem in which we break the item is known as a Fractional knapsack problem.

This problem can be solved with the help of using two techniques:

  • Brute-force approach: The brute-force approach tries all the possible solutions with all the different fractions but it is a time-consuming approach.
  • Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and accordingly, we will select the item. The item with the highest ratio would be selected first.

There are basically three approaches to solve the problem:

  • The first approach is to select the item based on the maximum profit.
  • The second approach is to select the item based on the minimum weight.
  • The third approach is to calculate the ratio of profit/weight.

Consider the below example:

Objects:         1     2     3     4     5     6     7

Profit (P):         10     15     7     8     9     4

Weight(w):       1     3     5     4     1     3     2

W (Weight of the knapsack): 15

n (no of items): 7

First approach:

First approach:

Object Profit Weight Remaining weight
3 15 5 15 - 5 = 10
2 10 3 10 - 3 = 7
6 9 3 7 - 3 = 4
5 8 1 4 - 1 = 3
7 7 * ¾ = 5.25 3 3 - 3 = 0

The total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = 47.25

Second approach:

The second approach is to select the item based on the minimum weight.

Object Profit Weight Remaining weight
1 5 1 15 - 1 = 14
5 7 1 14 - 1 = 13
7 4 2 13 - 2 = 11
2 10 3 11 - 3 = 8
6 9 3 8 - 3 = 5
4 7 4 5 - 4 = 1
3 15 * 1/5 = 3 1 1 - 1 = 0

In this case, the total profit would be equal to (5 + 7 + 4 + 10 + 9 + 7 + 3) = 46

Third approach:

In the third approach, we will calculate the ratio of profit/weight.

Objects:         1     2     3     4     5     6     7

Profit (P):         5     10     15     7     8     9     4

Weight(w):       1     3     5    4     1     3     2

In this case, we first calculate the profit/weight ratio.

Object 1: 5/1 = 5

Object 2: 10/3 = 3. 33

Object 3: 15/5 = 3

Object 4: 7/4 = 1.7

Object 5: 8/1 = 8

Object 6: 9/3 = 3

Object 7: 4/2 = 2

P:w:         5     3.3     3     1.7     8     3     2

In this approach, we will select the objects based on the maximum profit/weight ratio. Since the P/W of object 5 is maximum so we select object 5.

Object Profit Weight Remaining weight
5 8 1 15 - 8 = 7

After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select object 1 shown in the below table:

Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13

After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select object 2 having profit/weight ratio as 3.3.

Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10

After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select object 3 having profit/weight ratio as 3.

Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5

After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object 6 having profit/weight ratio as 3.

Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5 - 3 = 2

After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object 7 having profit/weight ratio as 2.

Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5 - 3 = 2
7 4 2 2 - 2 = 0

As we can observe in the above table that the remaining weight is zero which means that the knapsack is full. We cannot add more objects in the knapsack. Therefore, the total profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51.

In the first approach, the maximum profit is 47.25. The maximum profit in the second approach is 46. The maximum profit in the third approach is 51. Therefore, we can say that the third approach, i.e., maximum profit/weight ratio is the best approach among all the approaches.


Next TopicHuffman Codes





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA