Skip to content

Commit 36fd4b2

Browse files
committed
feat: Add 0/1 Knapsack and its tabulation implementation with their corresponding tests
1 parent 8a8d1e2 commit 36fd4b2

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/ZeroOneKnapsack.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ private ZeroOneKnapsack() {
2222
* @param n the number of items
2323
* @return the maximum total value achievable within the given weight limit
2424
*/
25-
public static int KnapsackCompute(int[] values, int[] weights, int capacity, int n) {
25+
public static int compute(int[] values, int[] weights, int capacity, int n) {
2626
if (n == 0 || capacity == 0) {
2727
return 0;
2828
}
2929

3030
if (weights[n - 1] <= capacity) {
31-
int include = values[n - 1] + KnapsackCompute(values, weights, capacity - weights[n - 1], n - 1);
32-
int exclude = KnapsackCompute(values, weights, capacity, n - 1);
31+
int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
32+
int exclude = compute(values, weights, capacity, n - 1);
3333
return Math.max(include, exclude);
3434
} else {
35-
return KnapsackCompute(values, weights, capacity, n - 1);
35+
return compute(values, weights, capacity, n - 1);
3636
}
3737
}
3838
}

src/main/java/com/thealgorithms/dynamicprogramming/ZeroOneKnapsackTab.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* The {@code ZeroOneKnapsackTab} class provides a method to solve the 0-1 Knapsack problem
55
* using dynamic programming (tabulation approach).
66
*
7-
* <p>0-1 Knapsack Problem -
7+
* <p>0-1 Knapsack Problem -
88
* Given weights and values of n items, and a maximum weight W,
99
* determine the maximum total value of items that can be included in the knapsack
1010
* such that their total weight does not exceed W. Each item can be picked only once.
11-
*
11+
*
1212
* Problem Link: https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
1313
*/
1414
public final class ZeroOneKnapsackTab {
@@ -26,7 +26,7 @@ private ZeroOneKnapsackTab() {
2626
* @param n the number of items
2727
* @return the maximum value that can be put in the knapsack
2828
*/
29-
public static int kcompute(int[] val, int[] wt, int W, int n) {
29+
public static int compute(int[] val, int[] wt, int W, int n) {
3030
int[][] dp = new int[n + 1][W + 1];
3131

3232
for (int i = 1; i <= n; i++) {

src/test/java/com/thealgorithms/dynamicprogramming/ZeroOneKnapsackTabTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void testKnownValues() {
2020
int n = val.length;
2121

2222
// Expected result is 220 (items with weight 20 and 30)
23-
assertEquals(220, ZeroOneKnapsackTab.kcompute(val, wt, W, n), "Maximum value for capacity 50 should be 220.");
23+
assertEquals(220, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for capacity 50 should be 220.");
2424
}
2525

2626
@Test
@@ -31,7 +31,7 @@ public void testZeroCapacity() {
3131
int n = val.length;
3232

3333
// With zero capacity, the result should be 0
34-
assertEquals(0, ZeroOneKnapsackTab.kcompute(val, wt, W, n), "Maximum value for capacity 0 should be 0.");
34+
assertEquals(0, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for capacity 0 should be 0.");
3535
}
3636

3737
@Test
@@ -42,7 +42,7 @@ public void testZeroItems() {
4242
int n = val.length;
4343

4444
// With no items, the result should be 0
45-
assertEquals(0, ZeroOneKnapsackTab.kcompute(val, wt, W, n), "Maximum value with no items should be 0.");
45+
assertEquals(0, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value with no items should be 0.");
4646
}
4747

4848
@Test
@@ -53,6 +53,6 @@ public void testExactFit() {
5353
int n = val.length;
5454

5555
// All items fit exactly into capacity 6
56-
assertEquals(30, ZeroOneKnapsackTab.kcompute(val, wt, W, n), "Maximum value for exact fit should be 30.");
56+
assertEquals(30, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for exact fit should be 30.");
5757
}
5858
}

src/test/java/com/thealgorithms/dynamicprogramming/ZeroOneKnapsackTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public void testKnapsackBasic() {
1717
int[] val = {15, 14, 10, 45, 30};
1818
int[] wt = {2, 5, 1, 3, 4};
1919
int W = 7;
20-
assertEquals(75, ZeroOneKnapsack.KnapsackCompute(val, wt, W, val.length),
21-
"Expected maximum value is 75.");
20+
assertEquals(75, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 75.");
2221
}
2322

2423
/**
@@ -29,8 +28,7 @@ public void testZeroCapacity() {
2928
int[] val = {10, 20, 30};
3029
int[] wt = {1, 1, 1};
3130
int W = 0;
32-
assertEquals(0, ZeroOneKnapsack.KnapsackCompute(val, wt, W, val.length),
33-
"Expected maximum value is 0 for zero capacity.");
31+
assertEquals(0, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 0 for zero capacity.");
3432
}
3533

3634
/**
@@ -41,8 +39,7 @@ public void testNoItems() {
4139
int[] val = {};
4240
int[] wt = {};
4341
int W = 10;
44-
assertEquals(0, ZeroOneKnapsack.KnapsackCompute(val, wt, W, 0),
45-
"Expected maximum value is 0 when no items are available.");
42+
assertEquals(0, ZeroOneKnapsack.compute(val, wt, W, 0), "Expected maximum value is 0 when no items are available.");
4643
}
4744

4845
/**
@@ -53,7 +50,6 @@ public void testExactFit() {
5350
int[] val = {60, 100, 120};
5451
int[] wt = {10, 20, 30};
5552
int W = 50;
56-
assertEquals(220, ZeroOneKnapsack.KnapsackCompute(val, wt, W, val.length),
57-
"Expected maximum value is 220 for exact fit.");
53+
assertEquals(220, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 220 for exact fit.");
5854
}
5955
}

0 commit comments

Comments
 (0)