Skip to content

Commit 626e2f1

Browse files
authored
Merge branch 'master' into bitmask_new_algo
2 parents 9ff0d71 + 169a01e commit 626e2f1

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,50 @@
33
import java.util.Arrays;
44
import java.util.Comparator;
55

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+
*/
817
public final class FractionalKnapsack {
918
private FractionalKnapsack() {
1019
}
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+
*/
1229
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.
1430
double[][] ratio = new double[weight.length][2];
1531

16-
// Populate the ratio array with item indices and their value-to-weight ratios.
1732
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];
2035
}
2136

22-
// Sort items by their value-to-weight ratios in descending order.
2337
Arrays.sort(ratio, Comparator.comparingDouble(o -> o[1]));
2438

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;
2741

28-
// Iterate through the sorted items to select items for the knapsack.
2942
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];
3144
if (current >= weight[index]) {
32-
// If the entire item can fit in the knapsack, add its value.
3345
finalValue += value[index];
3446
current -= weight[index];
3547
} else {
36-
// If only a fraction of the item can fit, add a proportionate value.
3748
finalValue += (int) (ratio[i][1] * current);
38-
break; // Stop adding items to the knapsack since it's full.
49+
break;
3950
}
4051
}
4152
return finalValue;

0 commit comments

Comments
 (0)