Feat(Improved): Add 0/1 Knapsack Problem: Recursive and Tabulation (Bottom-Up DP) Implementations in Java along with their corresponding Tests (#6425)

* feat: Add 0/1 Knapsack and its tabulation implementation with their corresponding tests

* feat: Add 0/1 Knapsack and its tabulation implementation with their corresponding tests

* feat: Add 0/1 Knapsack and its tabulation implementation with their corresponding tests

* Feat:add 0/1knapsack and 0/1knapsacktabulation along with their tests

* Feat:add 0/1knapsack and 0/1knapsacktabulation along with their tests

* Feat:add 0/1knapsack and 0/1knapsacktabulation along with their tests

---------

Co-authored-by: Oleksandr Klymenko <alexanderklmn@gmail.com>
This commit is contained in:
Vishwajeet Deshmane
2025-07-22 22:47:24 +05:30
committed by GitHub
parent bbbc1dd946
commit cfd784105b
4 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package com.thealgorithms.dynamicprogramming;
/**
* The {@code KnapsackZeroOne} provides Recursive solution for the 0/1 Knapsack
* problem. Solves by exploring all combinations of items using recursion. No
* memoization or dynamic programming optimizations are applied.
*
* Time Complexity: O(2^n) — explores all subsets.
* Space Complexity: O(n) — due to recursive call stack.
*
* Problem Reference: https://en.wikipedia.org/wiki/Knapsack_problem
*/
public final class KnapsackZeroOne {
private KnapsackZeroOne() {
// Prevent instantiation
}
/**
* Solves the 0/1 Knapsack problem using recursion.
*
* @param values the array containing values of the items
* @param weights the array containing weights of the items
* @param capacity the total capacity of the knapsack
* @param n the number of items
* @return the maximum total value achievable within the given weight limit
* @throws IllegalArgumentException if input arrays are null, empty, or
* lengths mismatch
*/
public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {
if (values == null || weights == null) {
throw new IllegalArgumentException("Input arrays cannot be null.");
}
if (values.length != weights.length) {
throw new IllegalArgumentException("Value and weight arrays must be of the same length.");
}
if (capacity < 0 || n < 0) {
throw new IllegalArgumentException("Invalid input: arrays must be non-empty and capacity/n "
+ "non-negative.");
}
if (n == 0 || capacity == 0 || values.length == 0) {
return 0;
}
if (weights[n - 1] <= capacity) {
final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
final int exclude = compute(values, weights, capacity, n - 1);
return Math.max(include, exclude);
} else {
return compute(values, weights, capacity, n - 1);
}
}
}