Dynamic Programming Algorithm to Solve the Poly Knapsack (Ubounded) Problem


You are given two lists of integers weights and values which have the same length and an integer capacity. weights[i] and values[i] represent the weight and value of the ith item.

Given that you can take at most capacity weights, and that you can take any number of copies for each item, return the maximum amount of value you can get.

Constraints
n ≤ 250 where n is the length of weights and values
capacity ≤ 250
Example 1
Input
weights = [1, 2, 3]
values = [1, 5, 3]
capacity = 5
Output
11

Unbounded Knapsack Problem solved by Dynamic Programming Algorithm

This is a typical unbounded knapsack problem where you can pack as many as items per item-type as long as there is enough capacity. The DP (Dynamic Programming) equation is:

tex_80e09ea07ec1800ccebfb0de72fe0a66 Dynamic Programming Algorithm to Solve the Poly Knapsack (Ubounded) Problem algorithms c / c++ dynamic programming Dynamic Programming Knapsack Problems math python where c is the capacity and i is the index between 0 to N-1 where N is the number of items. w is the weights and v is the values array.

1
2
3
4
5
6
7
8
class Solution:
    def uboundedKnapsack(self, weights, values, capacity):
        dp = [0] * (capacity + 1)
        for i in range(1, capacity + 1):
            for j in range(len(weights)):
                if i >= weights[j]:
                    dp[i] = max(dp[i], dp[i - weights[j]] + values[j])
        return max(dp)
class Solution:
    def uboundedKnapsack(self, weights, values, capacity):
        dp = [0] * (capacity + 1)
        for i in range(1, capacity + 1):
            for j in range(len(weights)):
                if i >= weights[j]:
                    dp[i] = max(dp[i], dp[i - weights[j]] + values[j])
        return max(dp)

The above Python implementation of Dynamic Programming to solve the Unbounded Knapsack problem has time complexity O(CW) and space complexity O(C).

C++ implementation of the same DP algorithm:

1
2
3
4
5
6
7
8
9
10
11
int uboundedKnapsack(vector<int>& weights, vector<int>& values, int capacity) {
    vector<int> dp(capacity + 1, 0);
    for (int i = 1; i <= capacity; ++ i) {
        for (int j = 0; j < weights.size(); ++ j) {
            if (i >= weights[j]) {
                dp[i] = max(dp[i], dp[i - weights[j]] + values[j]);
            }
        }
    }
    return *max_element(begin(dp), end(dp));
}
int uboundedKnapsack(vector<int>& weights, vector<int>& values, int capacity) {
    vector<int> dp(capacity + 1, 0);
    for (int i = 1; i <= capacity; ++ i) {
        for (int j = 0; j < weights.size(); ++ j) {
            if (i >= weights[j]) {
                dp[i] = max(dp[i], dp[i - weights[j]] + values[j]);
            }
        }
    }
    return *max_element(begin(dp), end(dp));
}

See also: Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem

Knapsack Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
480 words
Last Post: Teaching Kids Programming - Find the Single Number in Array
Next Post: Teaching Kids Programming - Algorithms to Determine a Happy Number

The Permanent URL is: Dynamic Programming Algorithm to Solve the Poly Knapsack (Ubounded) Problem

Leave a Reply