|
3 | 3 | import java.util.Arrays; |
4 | 4 |
|
5 | 5 | /** |
6 | | - * A Dynamic Programming based solution for the 0-1 Knapsack problem. |
7 | | - * This class provides a method, `knapSack`, that calculates the maximum value that can be |
8 | | - * obtained from a given set of items with weights and values, while not exceeding a |
9 | | - * given weight capacity. |
| 6 | + * 0/1 Knapsack Problem - Dynamic Programming solution. |
10 | 7 | * |
11 | | - * @see <a href="https://en.wikipedia.org/?title=0-1_Knapsack_problem">0-1 Knapsack Problem </a> |
| 8 | + * This algorithm solves the classic optimization problem where we have n items, |
| 9 | + * each with a weight and a value. The goal is to maximize the total value |
| 10 | + * without exceeding the knapsack's weight capacity. |
| 11 | + * |
| 12 | + * Time Complexity: O(n * W) |
| 13 | + * Space Complexity: O(W) |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * values = {60, 100, 120} |
| 17 | + * weights = {10, 20, 30} |
| 18 | + * W = 50 |
| 19 | + * Output: 220 |
| 20 | + * |
| 21 | + * @author Arpita |
| 22 | + * @see <a href="https://en.wikipedia.org/wiki/Knapsack_problem">Knapsack Problem</a> |
12 | 23 | */ |
13 | 24 | public final class Knapsack { |
14 | 25 |
|
15 | | - private Knapsack() { |
16 | | - } |
| 26 | + private Knapsack() {} |
17 | 27 |
|
| 28 | + /** |
| 29 | + * Validates the input to ensure correct constraints. |
| 30 | + */ |
18 | 31 | private static void throwIfInvalidInput(final int weightCapacity, final int[] weights, final int[] values) { |
19 | 32 | if (weightCapacity < 0) { |
20 | 33 | throw new IllegalArgumentException("Weight capacity should not be negative."); |
21 | 34 | } |
22 | 35 | if (weights == null || values == null || weights.length != values.length) { |
23 | | - throw new IllegalArgumentException("Input arrays must not be null and must have the same length."); |
| 36 | + throw new IllegalArgumentException("Weights and values must be non-null and of the same length."); |
24 | 37 | } |
25 | 38 | if (Arrays.stream(weights).anyMatch(w -> w <= 0)) { |
26 | | - throw new IllegalArgumentException("Input array should not contain non-positive weight(s)."); |
| 39 | + throw new IllegalArgumentException("Weights must be positive."); |
27 | 40 | } |
28 | 41 | } |
29 | 42 |
|
30 | 43 | /** |
31 | | - * Solves the 0-1 Knapsack problem using Dynamic Programming. |
| 44 | + * Solves the 0/1 Knapsack problem using Dynamic Programming (bottom-up approach). |
32 | 45 | * |
33 | 46 | * @param weightCapacity The maximum weight capacity of the knapsack. |
34 | | - * @param weights An array of item weights. |
35 | | - * @param values An array of item values. |
36 | | - * @return The maximum value that can be obtained without exceeding the weight capacity. |
37 | | - * @throws IllegalArgumentException If the input arrays are null or have different lengths. |
| 47 | + * @param weights The array of item weights. |
| 48 | + * @param values The array of item values. |
| 49 | + * @return The maximum total value achievable without exceeding capacity. |
38 | 50 | */ |
39 | | - public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) throws IllegalArgumentException { |
| 51 | + public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) { |
40 | 52 | throwIfInvalidInput(weightCapacity, weights, values); |
41 | 53 |
|
42 | | - // DP table to store the state of the maximum possible return for a given weight capacity. |
43 | 54 | int[] dp = new int[weightCapacity + 1]; |
44 | 55 |
|
| 56 | + // Fill dp[] array iteratively |
45 | 57 | for (int i = 0; i < values.length; i++) { |
46 | | - for (int w = weightCapacity; w > 0; w--) { |
47 | | - if (weights[i] <= w) { |
48 | | - dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); |
49 | | - } |
| 58 | + for (int w = weightCapacity; w >= weights[i]; w--) { |
| 59 | + dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); |
50 | 60 | } |
51 | 61 | } |
52 | 62 |
|
53 | 63 | return dp[weightCapacity]; |
54 | 64 | } |
| 65 | + |
| 66 | + /** |
| 67 | + * Example main method for demonstration. |
| 68 | + */ |
| 69 | + public static void main(String[] args) { |
| 70 | + int[] values = {60, 100, 120}; |
| 71 | + int[] weights = {10, 20, 30}; |
| 72 | + int weightCapacity = 50; |
| 73 | + |
| 74 | + int maxValue = knapSack(weightCapacity, weights, values); |
| 75 | + System.out.println("Maximum value = " + maxValue); // Output: 220 |
| 76 | + } |
55 | 77 | } |
0 commit comments