Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 22, 2025
Merged
Prev Previous commit
Next Next commit
Feat:add 0/1knapsack and 0/1knapsacktabulation along with their tests
  • Loading branch information
o000SAI000o committed Jul 22, 2025
commit 72ce8a257bc313f1d5e4753c4108d34e24168469
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -37,4 +38,41 @@ public void zeroCapacity() {

assertEquals(0, KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount));
}

@Test
public void negativeCapacity() {
int[] values = {10, 20, 30};
int[] weights = {1, 1, 1};
int capacity = -10;
int itemCount = values.length;

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount));
assertEquals("Capacity must not be negative.", exception.getMessage());
}

@Test
public void mismatchedLengths() {
int[] values = {60, 100}; // Only 2 values
int[] weights = {10, 20, 30}; // 3 weights
int capacity = 50;
int itemCount = 2; // Matches `values.length`

// You could either expect 0 or throw an IllegalArgumentException in your compute function
assertThrows(IllegalArgumentException.class, () -> { KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount); });
}

@Test
public void nullInputs() {
int[] weights = {1, 2, 3};
int capacity = 10;
int itemCount = 3;

IllegalArgumentException exception1 = assertThrows(IllegalArgumentException.class, () -> KnapsackZeroOneTabulation.compute(null, weights, capacity, itemCount));
assertEquals("Values and weights arrays must not be null.", exception1.getMessage());

int[] values = {1, 2, 3};

IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class, () -> KnapsackZeroOneTabulation.compute(values, null, capacity, itemCount));
assertEquals("Values and weights arrays must not be null.", exception2.getMessage());
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy