Javatpoint Logo
Javatpoint Logo

Knapsack Problem Java

One of the prominent combinatorial optimization problems is the Knapsack problem Java. The Knapsack problem has two categories.

  • 0-1 Kanpsack Problem
  • Factorial Knapsack Problem

Let's discuss each of them.

0 - 1 Knapsack Problem

Given values and weights of n different items. One has to put these items in a knapsack of capacity C such that the knapsack has the maximum value. Also, the sum of weights of all the items present in the knapsack should not exceed the capacity C. Since it is a 0 - 1 knapsack problem; hence, splitting the item is not allowed, i.e., one can never break any given item, either do not pick it or pick it (0 - 1 property).

Knapsack Problem Java

The knapsack problem can be solved either by using the exhaustive search or using dynamic programming.

Using Exhaustive Search

Exhaustive search means applying the brute force approach. In this approach, every set of items are tried, and for every set, the value is calculated. The set that generates the maximum value is the answer.

The following program implements the brute force approach using recursion.

FileName: KnapsackExample.java

Output:

The maximum value is: 220

Explanation: The input arrays are in such a way that their values are mapped, i.e., for item i, the weight is weight[i], and its value is values[i]. In each recursive call, there is a decision made whether the item should be included in the solution or not. If the item is not included, we move further without considering its value. If the item is included, then the capacity of the knapsack is reduced by the weight of the item, and we move further by including its value. Other items are treated in a similar way. The maximum value generated by adding the values of the included items gives our answer.

Note that the approach is not very conducive as it takes a lot of time to give the result. The time complexity of the approach is O(2 ^ n), where n is the number of elements present.

Using Dynamic Programming

The need of dynamic programming occurred because the above approach is taking a large amount of time to generate the result. Therefore, in the long run, the above method fails. Therefore, we need to look at another approach which is dynamic programming.

FileName: KnapsackExample1.java

Output:

The maximum value is: 220

Explanation: Similar to the first approach, this time also we are including and excluding every item present in the list. However, this time we are having a better time complexity of O(n^2). It is because, unlike the above approach, we are taking the help of the solution build for the smaller list of items to build the solution for the complete list of items (property of dynamic programming). Therefore, the number of iterations is less. Note that in both the approaches, we are completely excluding or including the items thus following the 0 - 1 property.

Fractional Knapsack Problem

The fractional knapsack problem is similar to the 0 - 1 knapsack problem. The only difference is one can choose an item partially. Thus, the liberty is given to break any item then put it in the knapsack, such that the total value of all the items (broken or not broken) present in the knapsack is maximized.

FileName: KnapsackExample1.java

Output:

Maximum value that can be obtained is = 240.0

Explanation: The greedy approach is used to solve the fractional knapsack problem. In this approach, we consider those items first which can give maximum value while consuming less weight. In other words, those items whose value / weight ratio is maximum. Therefore, we are sorting the list in non-decreasing order by taking the help of the value / weight ratio. Thus, the item with the highest value / weight ratio sits at the first index, the item with the second-highest value / weight ratio takes the second index, and so on.







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