Dynamic Programming Algorithm to Solve 0-1 Knapsack 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 only take at most one copy of 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
8

0/1 Knapsack Problem solved by Dynamic Programming Algorithm

We can use dp[i][j] to represent the maximum value we can get for the first i-items with capacity j in the knapsack. As each item we have two choices, either choose or skip, then we have the following Dynamic Programming equation:

tex_7d42751ebead62f5a545481f5fa8c41a Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem algorithms c / c++ dynamic programming Dynamic Programming Knapsack Problems math python

w[i] is the weight for item i, and v[i] is the value we gain if we pick item i.

For more explanation, please see this post: 0/1 Knapsack Problem

C++ Algorithm to solve the 0/1 knapsack problem.

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

Python Algorithm to solve the 0/1 knapsack problem:

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

Both implementation have time complexity O(NC) where N is the number of items and C is the capacity of the knapsack. To avoid checking for boundaries of the array, we allocate one more (shift one to the right) for the DP array.

Knapsack problem: Algorithms Series: 0/1 BackPack – Dynamic Programming and BackTracking

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

Knapsack Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
580 words
Last Post: Teaching Kids Programming - Minimal Number of Brackets Needed to Make a Valid Parenthese String
Next Post: Algorithms to Check Sum of Two Numbers in Binary Search Trees

The Permanent URL is: Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem

Leave a Reply