From 4795c0913947825c550977b5e268c560367d4c8b Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Tue, 14 Oct 2025 23:10:04 +0530 Subject: [PATCH 1/6] Added 0/1 Knapsack Algorithm using Dynamic Programming in Java --- .../dynamicprogramming/Knapsack.java | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index 134561766830..cf437dd7e225 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -3,53 +3,75 @@ import java.util.Arrays; /** - * A Dynamic Programming based solution for the 0-1 Knapsack problem. - * This class provides a method, `knapSack`, that calculates the maximum value that can be - * obtained from a given set of items with weights and values, while not exceeding a - * given weight capacity. + * 0/1 Knapsack Problem - Dynamic Programming solution. * - * @see 0-1 Knapsack Problem + * This algorithm solves the classic optimization problem where we have n items, + * each with a weight and a value. The goal is to maximize the total value + * without exceeding the knapsack's weight capacity. + * + * Time Complexity: O(n * W) + * Space Complexity: O(W) + * + * Example: + * values = {60, 100, 120} + * weights = {10, 20, 30} + * W = 50 + * Output: 220 + * + * @author Arpita + * @see Knapsack Problem */ public final class Knapsack { - private Knapsack() { - } + private Knapsack() {} + /** + * Validates the input to ensure correct constraints. + */ private static void throwIfInvalidInput(final int weightCapacity, final int[] weights, final int[] values) { if (weightCapacity < 0) { throw new IllegalArgumentException("Weight capacity should not be negative."); } if (weights == null || values == null || weights.length != values.length) { - throw new IllegalArgumentException("Input arrays must not be null and must have the same length."); + throw new IllegalArgumentException("Weights and values must be non-null and of the same length."); } if (Arrays.stream(weights).anyMatch(w -> w <= 0)) { - throw new IllegalArgumentException("Input array should not contain non-positive weight(s)."); + throw new IllegalArgumentException("Weights must be positive."); } } /** - * Solves the 0-1 Knapsack problem using Dynamic Programming. + * Solves the 0/1 Knapsack problem using Dynamic Programming (bottom-up approach). * * @param weightCapacity The maximum weight capacity of the knapsack. - * @param weights An array of item weights. - * @param values An array of item values. - * @return The maximum value that can be obtained without exceeding the weight capacity. - * @throws IllegalArgumentException If the input arrays are null or have different lengths. + * @param weights The array of item weights. + * @param values The array of item values. + * @return The maximum total value achievable without exceeding capacity. */ - public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) throws IllegalArgumentException { + public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) { throwIfInvalidInput(weightCapacity, weights, values); - // DP table to store the state of the maximum possible return for a given weight capacity. int[] dp = new int[weightCapacity + 1]; + // Fill dp[] array iteratively for (int i = 0; i < values.length; i++) { - for (int w = weightCapacity; w > 0; w--) { - if (weights[i] <= w) { - dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); - } + for (int w = weightCapacity; w >= weights[i]; w--) { + dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); } } return dp[weightCapacity]; } + + /** + * Example main method for demonstration. + */ + public static void main(String[] args) { + int[] values = {60, 100, 120}; + int[] weights = {10, 20, 30}; + int weightCapacity = 50; + + int maxValue = knapSack(weightCapacity, weights, values); + System.out.println("Maximum value = " + maxValue); // Output: 220 + } } From 1e164923220187552aeed8fa9a6aa564486c0f37 Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Tue, 14 Oct 2025 23:45:25 +0530 Subject: [PATCH 2/6] Fix Checkstyle whitespace issues in Knapsack.java --- .../java/com/thealgorithms/dynamicprogramming/Knapsack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index cf437dd7e225..bd5d3b598dc5 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -23,7 +23,7 @@ */ public final class Knapsack { - private Knapsack() {} + private Knapsack() { } /** * Validates the input to ensure correct constraints. From 7b8057a94c6b8ea3f338d0da048f9eee5d24807c Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Wed, 15 Oct 2025 20:57:30 +0530 Subject: [PATCH 3/6] Formatted Knapsack.java to pass linter and added 0/1 Knapsack algorithm --- .../java/com/thealgorithms/dynamicprogramming/Knapsack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index bd5d3b598dc5..35789166d137 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -72,6 +72,6 @@ public static void main(String[] args) { int weightCapacity = 50; int maxValue = knapSack(weightCapacity, weights, values); - System.out.println("Maximum value = " + maxValue); // Output: 220 + System.out.println("Maximum value = " + maxValue); // Output : 220 } } From 1b9077d333bd0e58c8a2481b640ca8c48a61e09a Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Wed, 15 Oct 2025 20:59:14 +0530 Subject: [PATCH 4/6] Formatted Knapsack.java to pass linter and added 0/1 Knapsack algorithm --- .../java/com/thealgorithms/dynamicprogramming/Knapsack.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index 35789166d137..e1bee09afbb9 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -23,8 +23,8 @@ */ public final class Knapsack { - private Knapsack() { } - + private Knapsack() { + } /** * Validates the input to ensure correct constraints. */ From 5bd7036c7933ca5d089c23e17951eb3aedb90a4e Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Wed, 15 Oct 2025 21:12:38 +0530 Subject: [PATCH 5/6] Formatted Knapsack.java to pass linter and added 0/1 Knapsack algorithm --- .../com/thealgorithms/dynamicprogramming/Knapsack.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index e1bee09afbb9..21be0fc819af 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -23,8 +23,7 @@ */ public final class Knapsack { - private Knapsack() { - } + private Knapsack() {} /** * Validates the input to ensure correct constraints. */ @@ -65,7 +64,7 @@ public static int knapSack(final int weightCapacity, final int[] weights, final /** * Example main method for demonstration. - */ + public static void main(String[] args) { int[] values = {60, 100, 120}; int[] weights = {10, 20, 30}; @@ -73,5 +72,5 @@ public static void main(String[] args) { int maxValue = knapSack(weightCapacity, weights, values); System.out.println("Maximum value = " + maxValue); // Output : 220 - } + } */ } From a9d7b51dc6bd9c45e0fd423f5d927d3b2c04726e Mon Sep 17 00:00:00 2001 From: Arpita Patil Date: Wed, 15 Oct 2025 21:34:21 +0530 Subject: [PATCH 6/6] Formatted Knapsack.java to pass linter and added 0/1 Knapsack algorithm --- .../thealgorithms/dynamicprogramming/Knapsack.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java index 21be0fc819af..0d4c8d501f9f 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java @@ -23,7 +23,9 @@ */ public final class Knapsack { - private Knapsack() {} + private Knapsack() { + } + /** * Validates the input to ensure correct constraints. */ @@ -62,15 +64,15 @@ public static int knapSack(final int weightCapacity, final int[] weights, final return dp[weightCapacity]; } - /** - * Example main method for demonstration. - + /* + // Example main method for local testing only. public static void main(String[] args) { int[] values = {60, 100, 120}; int[] weights = {10, 20, 30}; int weightCapacity = 50; int maxValue = knapSack(weightCapacity, weights, values); - System.out.println("Maximum value = " + maxValue); // Output : 220 - } */ + System.out.println("Maximum value = " + maxValue); // Output: 220 + } + */ }