8

This variation of the knapsack problem requires a minimum weight. The goal is to minimize the cost while achieving at least the minimum weight.

For example, we have 6 items with weights {1, 1, 1, 5, 13, 3} and costs {1, 1, 1, 5, 10, 12}. Assume a minimum weight of 15.

The optimal solution is items {1, 2, 5} for a total weight of 15 and cost 12.

How should I go about implementing this algorithm as efficiently as possible? Greedy choices don't work, so should I modify the original dynamic programming solution to fit this problem? If so, how?

If it matters, I'm planning to write this in Java.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Nimbus
  • 117
  • 2
  • 5

3 Answers3

4

Let minCost[i] denote the minimum value that a knapsack with capacity i can hold, costs[i] represent the cost of the ith item, and weights[i] represent the weight of the ith item. Then, for each i, minVal[i] is the minimum of minVal[i - weights[j]] + costs[j] for all j from 1 to the number of items.

Then, the answer is the minimum value in the minCost array in the range from the minimum weight to the maximum weight.

final int[] weights = {1, 1, 1, 5, 13, 3}, costs = {1, 1, 1, 5, 10, 12};
final int minWeight = 15;
int maxWeight = 0;
for(final int weight: weights){
    maxWeight += weight;
}
final int[] minCost = new int[maxWeight + 1];
for(int i = 1; i <= maxWeight; i++){
    minCost[i] = Integer.MAX_VALUE;
}
for(int i = 0; i < weights.length; i++){
    for(int j = maxWeight; j >= weights[i]; j--){
        if(minCost[j - weights[i]] != Integer.MAX_VALUE){
            minCost[j] = Math.min(minCost[j], minCost[j - weights[i]] + costs[i]);
        }
    }
}
int answer = Integer.MAX_VALUE;
for(int i = minWeight; i <= maxWeight; i++){
    answer = Math.min(answer, minCost[i]);
}
System.out.println(answer);

Demo

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

Lets define the function f(i,j) that gives the minimum cost for choosing items from the first i+1 items (0,1...i) where the sum of the weights of these items is exactly equal to j, then the minimum cost to get at least the weight(minW=15) would be calculated like this:

min(f(i,j)) where  i=0,1...n-1, j=minW,minW+1,.... maxW 
- n is the number of items 
- minW=15 in your case
- maxW is the maximum possible sum of all the given weights

you can refer to this C++ code(very similar to Java):

    const int maxW = 100;//the maximum weight, a problem constraint
    const int maxN = 100;//the maximum number of items, a problem constraint

    int n = 6; //input
    int w[maxN] = { 1, 1, 1, 5, 13, 3 };//the weights(should be read as an input)
    int c[maxN] = { 1, 1, 1, 5, 10, 12 };//the costs(should be read as an input)
    
    int f[maxN][maxW];
    for (int i = 0; i < maxN; i++)
        for (int j = 0; j < maxW; j++)
            f[i][j] = 1000000;//some big value

    int minW = 15;//the minimum weight(should be read as an input)
    
    int result = 1000000;
    
    f[0][w[0]] = c[0];//initialization
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < maxW; j++) {
            
            f[i][j] = f[i - 1][j];//don't pick the i-th item

            if (j - w[i] >= 0) {//pick the i-th item if possible
                if (f[i][j] > f[i - 1][j - w[i]] + c[i])
                    f[i][j] = f[i - 1][j - w[i]] + c[i];
            }

            if (j >= minW and f[i][j] < result) {
                result = f[i][j];//store the minimum cost when the weight is >= minW
            }
        }
    }
    cout << result << endl;//print the result(12 in this case)
VFX
  • 496
  • 4
  • 13
1

We can actually achieve O(n * targetMinWeight) instead of O(n * maximum weight) by slightly modifying @Unmitigated's answer

class Solution:


    def knapsack(self, n, costs, weights, target):
        MAX = float('inf')
        dp = [[0 for _ in range(n + 1)] for _ in range(target + 1)]
        for t in range(target + 1):
            dp[t][0] = MAX
        for i in range(n + 1):
            dp[0][i] = MAX

        for t in range(1, target + 1):
            for i in range(1, n + 1):
                if t > weights[i - 1]:  # i - 1 because of the offset
                    dp[t][i] = min(dp[t][i - 1], dp[t - weights[i - 1]][i - 1] + costs[i - 1])
                else:
                    dp[t][i] = min(dp[t][i - 1], costs[i - 1])

        return min(dp[target])

sol = Solution()
print(sol.knapsack(6, [1, 1, 1, 5, 10, 12], [1, 1, 1, 5, 13, 3], 15)) # returns 12


ryanym
  • 11
  • 2