|
3 | 3 | import java.util.Arrays;
|
4 | 4 | import java.util.Comparator;
|
5 | 5 |
|
6 |
| -// Problem Link: https://en.wikipedia.org/wiki/Continuous_knapsack_problem |
7 |
| - |
| 6 | +/** |
| 7 | + * The FractionalKnapsack class provides a method to solve the fractional knapsack problem |
| 8 | + * using a greedy algorithm approach. It allows for selecting fractions of items to maximize |
| 9 | + * the total value in a knapsack with a given weight capacity. |
| 10 | + * |
| 11 | + * The problem consists of a set of items, each with a weight and a value, and a knapsack |
| 12 | + * that can carry a maximum weight. The goal is to maximize the value of items in the knapsack, |
| 13 | + * allowing for the inclusion of fractions of items. |
| 14 | + * |
| 15 | + * Problem Link: https://en.wikipedia.org/wiki/Continuous_knapsack_problem |
| 16 | + */ |
8 | 17 | public final class FractionalKnapsack {
|
9 | 18 | private FractionalKnapsack() {
|
10 | 19 | }
|
11 |
| - // Function to perform fractional knapsack |
| 20 | + |
| 21 | + /** |
| 22 | + * Computes the maximum value that can be accommodated in a knapsack of a given capacity. |
| 23 | + * |
| 24 | + * @param weight an array of integers representing the weights of the items |
| 25 | + * @param value an array of integers representing the values of the items |
| 26 | + * @param capacity an integer representing the maximum weight capacity of the knapsack |
| 27 | + * @return the maximum value that can be obtained by including the items in the knapsack |
| 28 | + */ |
12 | 29 | public static int fractionalKnapsack(int[] weight, int[] value, int capacity) {
|
13 |
| - // Create a 2D array to store item indices and their value-to-weight ratios. |
14 | 30 | double[][] ratio = new double[weight.length][2];
|
15 | 31 |
|
16 |
| - // Populate the ratio array with item indices and their value-to-weight ratios. |
17 | 32 | for (int i = 0; i < weight.length; i++) {
|
18 |
| - ratio[i][0] = i; // Assign item index. |
19 |
| - ratio[i][1] = value[i] / (double) weight[i]; // Calculate and assign value-to-weight ratio. |
| 33 | + ratio[i][0] = i; |
| 34 | + ratio[i][1] = value[i] / (double) weight[i]; |
20 | 35 | }
|
21 | 36 |
|
22 |
| - // Sort items by their value-to-weight ratios in descending order. |
23 | 37 | Arrays.sort(ratio, Comparator.comparingDouble(o -> o[1]));
|
24 | 38 |
|
25 |
| - int finalValue = 0; // Variable to store the final knapsack value. |
26 |
| - double current = capacity; // Variable to track the remaining capacity of the knapsack. |
| 39 | + int finalValue = 0; |
| 40 | + double current = capacity; |
27 | 41 |
|
28 |
| - // Iterate through the sorted items to select items for the knapsack. |
29 | 42 | for (int i = ratio.length - 1; i >= 0; i--) {
|
30 |
| - int index = (int) ratio[i][0]; // Get the item index. |
| 43 | + int index = (int) ratio[i][0]; |
31 | 44 | if (current >= weight[index]) {
|
32 |
| - // If the entire item can fit in the knapsack, add its value. |
33 | 45 | finalValue += value[index];
|
34 | 46 | current -= weight[index];
|
35 | 47 | } else {
|
36 |
| - // If only a fraction of the item can fit, add a proportionate value. |
37 | 48 | finalValue += (int) (ratio[i][1] * current);
|
38 |
| - break; // Stop adding items to the knapsack since it's full. |
| 49 | + break; |
39 | 50 | }
|
40 | 51 | }
|
41 | 52 | return finalValue;
|
|
0 commit comments