0-1 Knapsack

We want to discuss a classic dynamic programming problem, which is 0-1 knapsack. Given a series of objects with a weight and a value and a knapsack that can carry a set amount of weight, what is the maximum object value we can put in our knapsack without exceeding the weight constraint?

Input

  • weights: an array of integers that denote the weights of objects
  • values: an array of integers that denote the values of objects
  • max_weight: the maximum weight capacity of the knapsack

Output

the maximum value in the knapsack

Examples

Example 1:

Input:

1weights = [3, 4, 7]
2values = [4, 5, 8]
3max_weight = 7

Output: 9

Explanation:

We have a knapsack of max limit 7 with 3 objects of weight-value pairs of [3,4], [4,5], [7,8], then the maximal value we can achieve is using the first 2 objects to obtain value 4 + 5 = 9.

The other possibilities would all be only 1 object in our knapsack, which would only yield values 4, 5, and 9.

Try it yourself

Solution

Brute Force | DFS | Combinatorial Search

A brute force method would enumerate all the possibilities such that for every object we try including it into our knapsack which would result in time complexity O(2^n) where n is the total number of objects. This can be done with a recursive combinatorial search where, for every item, we either choose to include it or not, then checking which possibility results in the greatest value while not exceeding the maximum weight.

Here is an illustration of the idea, where the left represents picking up the item and the right giving up the item:

Note that the largest value while not exceeding the maximum weight is 9.

1// helper function that returns the maximum value when considering
2// the first n items and remaining available weight remaining_weight
3int knapsackHelper(std::vector<int> weights, std::vector<int> values, int remaining_weight, int n) {
4  // base case: if there are no items or no available weight in the knapsack to use, the maximum value is 0
5  if (n == 0 || remaining_weight == 0) {
6    return 0;
7  }
8  // if the weight of the current item exceeds the available weight, 
9  // skip the current item and process the next one
10  if (weights[n - 1] > remaining_weight) {
11    return knapsackHelper(weights, values, remaining_weight, n - 1);
12  }
13  // recurrence relation: choose the maximum of two possibilities:
14  //   (1) pick up the current item: current value + maximum value with the rest of the items
15  //   (2) give up the current item: maximum value with the rest of the items
16  return std::max(
17        values[n - 1] + knapsackHelper(weights, values, remaining_weight - weights[n - 1], n - 1),
18        knapsackHelper(weights, values, remaining_weight, n - 1));
19}
20int knapsack(std::vector<int> weights, std::vector<int> values, int max_weight) {
21  int n = weights.size();
22  return knapsackHelper(weights, values, max_weight, n);
23}
24
1// helper function that returns the maximum value when considering
2// the first n items and remaining available weight remainingWeight
3public static int knapsackHelper(List<Integer> weights, List<Integer> values, int remainingWeight, int n) {
4  // base case: if there are no items or no available weight in the knapsack to use, the maximum value is 0
5  if (n == 0 || remainingWeight == 0) {
6    return 0;
7  }
8  // if the weight of the current item exceeds the available weight, 
9  // skip the current item and process the next one
10  if (weights.get(n - 1) > remainingWeight) {
11    return knapsackHelper(weights, values, remainingWeight, n - 1);
12  }
13  // recurrence relation: choose the maximum of two possibilities:
14  //   (1) pick up the current item: current value + maximum value with the rest of the items
15  //   (2) give up the current item: maximum value with the rest of the items
16  return Math.max(
17    values.get(n - 1) + knapsackHelper(weights, values, remainingWeight - weights.get(n - 1), n - 1),
18    knapsackHelper(weights, values, remainingWeight, n - 1));
19}
20
21public static int knapsack(List<Integer> weights, List<Integer> values, int maxWeight) {
22  int n = weights.size();
23  return knapsackHelper(weights, values, maxWeight, n);
24}
25
1// helper function that returns the maximum value when considering
2// the first n items and remaining available weight remainingWeight
3function knapsackHelper(weights, values, remainingWeight, n) {
4  // base case: if there are no items or no available weight in the knapsack to use, the maximum value is 0
5  if (n === 0 || remainingWeight === 0) {
6    return 0;
7  }
8  // if the weight of the current item exceeds the available weight, 
9  // skip the current item and process the next one
10  if (weights[n - 1] > remainingWeight) {
11    return knapsackHelper(weights, values, remainingWeight, n - 1);
12  }
13  // recurrence relation: choose the maximum of two possibilities:
14  //   (1) pick up the current item: current value + maximum value with the rest of the items
15  //   (2) give up the current item: maximum value with the rest of the items
16  return Math.max(
17    values[n - 1] + knapsackHelper(weights, values, remainingWeight - weights[n - 1], n - 1),
18    knapsackHelper(weights, values, remainingWeight, n - 1));
19}
20function knapsack(weights, values, maxWeight) {
21  let n = weights.length;
22  return knapsackHelper(weights, values, maxWeight, n);
23}
24
1# helper function that returns the maximum value when considering
2# the first n items and remaining available weight remaining_weight
3def knapsack_helper(weights: List[int], values: List[int], remaining_weight: int, n: int) -> int:
4  # base case: if there are no items or no available weight in the knapsack to use, the maximum value is 0
5  if n == 0 or remaining_weight == 0:
6    return 0
7  # if the weight of the current item exceeds the available weight, 
8  # skip the current item and process the next one
9  if weights[n - 1] > remaining_weight:
10    return knapsack_helper(weights, values, remaining_weight, n - 1)
11  # recurrence relation: choose the maximum of two possibilities:
12  #   (1) pick up the current item: current value + maximum value with the rest of the items
13  #   (2) give up the current item: maximum value with the rest of the items
14  return max(values[n - 1] + knapsack_helper(weights, values, remaining_weight - weights[n - 1], n - 1),
15            knapsack_helper(weights, values, remaining_weight, n - 1))
16
17def knapsack(weights: List[int], values: List[int], max_weight: int) -> int:
18  n = len(weights)
19  return knapsack_helper(weights, values, max_weight, n)
20

DFS + Memoization

We can optimize the brute force solution by storing answers that have already been computed in a 2D array called dp. In this case dp[n][remaining_weight] stores the maximum value when considering the first n items with a maximum available weight of remaining_weight. If the answer already exists for dp[n][remaining_weight], then we immediately use that result. Otherwise, we recurse and store the result of the recurrence.

1int knapsackHelper(std::vector<int> weights, std::vector<int> values, std::vector<std::vector<int>> memo, int remaining_weight, int n) {
2  if (n == 0 || remaining_weight == 0) {
3    return 0;
4  }
5  if (memo[n][remaining_weight] != -1) {
6    return memo[n][remaining_weight];
7  }
8  int res;
9  if (weights[n - 1] > remaining_weight) {
10    res = knapsackHelper(weights, values, memo, remaining_weight, n - 1);
11  } else {
12    res = std::max(values[n - 1] + knapsackHelper(weights, values, memo, remaining_weight - weights[n - 1], n - 1),
13                    knapsackHelper(weights, values, memo, remaining_weight, n - 1));
14  }
15  return memo[n][remaining_weight] = res;
16}
17
18int knapsack(std::vector<int> weights, std::vector<int> values, int max_weight) {
19  int n = weights.size();
20  std::vector<std::vector<int>> memo(n + 1, std::vector<int>(max_weight + 1, -1));
21  return knapsackHelper(weights, values, memo, max_weight, n);
22}
23
1public static int knapsackHelper(List<Integer> weights, List<Integer> values, int[][] memo, int remainingWeight, int n) {
2  if (n == 0 || remainingWeight == 0) {
3    return 0;
4  }
5  if (memo[n][remainingWeight] != -1) {
6    return memo[n][remainingWeight];
7  }
8  int res;
9  if (weights.get(n - 1) > remainingWeight) {
10    res = knapsackHelper(weights, values, memo, remainingWeight, n - 1);
11  } else {
12    res = Math.max(
13      values.get(n - 1) + knapsackHelper(weights, values, memo, remainingWeight - weights.get(n - 1), n - 1),
14      knapsackHelper(weights, values, memo, remainingWeight, n - 1));
15  }
16  return memo[n][remainingWeight] = res;
17}
18
19public static int knapsack(List<Integer> weights, List<Integer> values, int maxWeight) {
20    int n = weights.size();
21    int[][] memo = new int[n + 1][maxWeight + 1];
22    for (int i = 0; i < n + 1; i++) {
23        Arrays.fill(memo[i], -1);
24    }
25    return knapsackHelper(weights, values, memo, maxWeight, n);
26}
27
1function knapsackHelper(weights, values, memo, remainingWeight, n) {
2  if (n === 0 || remainingWeight === 0) {
3    return 0;
4  }
5  if (memo[n][remainingWeight] !== -1) {
6    return memo[n][remainingWeight];
7  }
8  var res;
9  if (weights[n - 1] > remainingWeight) {
10    res = knapsackHelper(weights, values, memo, remainingWeight, n - 1);
11  } else {
12    res = Math.max(
13      values[n - 1] + knapsackHelper(weights, values, memo, remainingWeight - weights[n - 1], n - 1),
14      knapsackHelper(weights, values, memo, remainingWeight, n - 1));
15  }
16  memo[n][remainingWeight] = res;
17  return res;
18}
19function knapsack(weights, values, maxWeight) {
20  let n = weights.length;
21  let memo = new Array(n + 1);
22  for (let i = 0; i <= n; i++) {
23    memo[i] = new Array(maxWeight + 1).fill(-1);
24  }
25  return knapsackHelper(weights, values, memo, maxWeight, n);
26}
27
1def knapsack_helper(weights: List[int], values: List[int], memo: List[List[int]], remaining_weight: int, n: int) -> int:
2  if n == 0 or remaining_weight == 0:
3    return 0
4  if memo[n][remaining_weight] != -1:
5    return memo[n][remaining_weight]
6  res = 0
7  if weights[n - 1] > remaining_weight:
8    res = knapsack_helper(weights, values, memo, remaining_weight, n - 1)
9  else:
10    res = max(values[n - 1] + knapsack_helper(weights, values, memo, remaining_weight - weights[n - 1], n - 1), 
11              knapsack_helper(weights, values, memo, remaining_weight, n - 1))
12  memo[n][remaining_weight] = res
13  return res
14
15def knapsack(weights: List[int], values: List[int], max_weight: int) -> int:
16  n = len(weights)
17  memo = [[-1 for i in range(max_weight + 1)] for j in range(n + 1)]
18  return knapsack_helper(weights, values, memo, max_weight, n)
19

The time and space complexity will be O(n * w) where n is the number of items and w is the max weight because we have O(n * w) states and the time complexity of computing each state is O(1). Similarly, the only additional memory we use is an n * w array, so the space complexity is O(n * w).

Bottom-up DP

This can even be done iteratively. This is similar to the recursive version, except instead of going from top-down, we build our solution from the bottom-up. Here is the iterative implementation:

1int knapsack(std::vector<int> weights, std::vector<int> values, int max_weight) {
2  int n = weights.size();
3  // 2D dp array, where maxValue[i][j] is the maximum knapsack value when
4  // considering the first i items with a max weight capacity of j
5  int maxValue[n + 1][max_weight + 1];
6  // iterate through all items
7  for (int i = 0; i <= n; i++) {
8    // and all possible available weights
9    for (int w = 0; w <= max_weight; w++) {
10      // if we consider no items or no weight, the max value is 0
11      if (i == 0 || w == 0) {
12        maxValue[i][w] = 0;
13      // if the weight of the current item exceeds the max available weight,
14      // then the answer is the max value when considering the first i - 1 items
15      } else if (w < weights[i - 1]) {
16        maxValue[i][w] = maxValue[i - 1][w];
17      // otherwise, we choose the best option between either:
18      // picking up: item's value + max value when considering the rest of the items and a new weight
19      // giving up: similar to the condition above
20      } else {
21        maxValue[i][w] = std::max(values[i - 1] + maxValue[i - 1][w - weights[i - 1]],
22                            maxValue[i - 1][w]);
23      }
24    }
25  }
26  // the answer is the max value when considering all n items and available weight of max_weight
27  return maxValue[n][max_weight];
28}
29
1public static int knapsack(List<Integer> weights, List<Integer> values, int remainingWeight) {
2  int n = weights.size();
3  // 2D dp array, where maxValue[i][j] is the maximum knapsack value when
4  // considering the first i items with a max weight capacity of j
5  int[][] maxValue = new int[n + 1][remainingWeight + 1];
6  // iterate through all items
7  for (int i = 0; i <= n; i++) {
8    // and all possible available weights
9    for (int w = 0; w <= remainingWeight; w++) {
10      // if we consider no items or no weight, the max value is 0
11      if (i == 0 || w == 0) {
12        maxValue[i][w] = 0;
13      // if the weight of the current item exceeds the max available weight,
14      // then the answer is the max value when considering the first i - 1 items
15      } else if (w < weights.get(i - 1)) {
16        maxValue[i][w] = maxValue[i - 1][w];
17      // otherwise, we choose the best option between either:
18      // picking up: item's value + max value when considering the rest of the items and a new weight
19      // giving up: similar to the condition above
20      } else {
21        maxValue[i][w] = Math.max(values.get(i - 1) + maxValue[i - 1][w - weights.get(i - 1)],
22                                maxValue[i - 1][w]);
23      }
24    }
25  }
26  // the answer is the max value when considering all n items and available weight of max_weight
27  return maxValue[n][remainingWeight];
28}
29
1function knapsack(weights, values, remainingWeight) {
2  let n = weights.length;
3  // 2D dp array, where maxValue[i][j] is the maximum knapsack value when
4  // considering the first i items with a max weight capacity of j
5  let maxValue = new Array(n + 1);
6  for (let i = 0; i <= n; i++) {
7    maxValue[i] = new Array(remainingWeight + 1).fill(0);
8  }
9  // iterate through all items
10  for (let i = 0; i <= n; i++) {
11    // and all possible available weights
12    for (let w = 0; w <= remainingWeight; w++) {
13      // if we consider no items or no weight, the max value is 0
14      if (i === 0 || w === 0) {
15        maxValue[i][w] = 0;
16      // if the weight of the current item exceeds the max available weight,
17      // then the answer is the max value when considering the first i - 1 items
18      } else if (w < weights[i - 1]) {
19        maxValue[i][w] = maxValue[i - 1][w];
20      // otherwise, we choose the best option between either:
21      // picking up: item's value + max value when considering the rest of the items and a new weight
22      // giving up: similar to the condition above
23      } else {
24        maxValue[i][w] = Math.max(values[i - 1] + maxValue[i - 1][w - weights[i - 1]],
25                                maxValue[i - 1][w]);
26      }
27    }
28  }
29  // the answer is the max value when considering all n items and available weight of max_weight
30  return maxValue[n][remainingWeight];
31}
32
1def knapsack(weights: List[int], values: List[int], max_weight: int) -> int:
2  n = len(weights)
3  # 2D dp array, where maxValue[i][j] is the maximum knapsack value when
4  # considering the first i items with a max weight capacity of j
5  max_value = [[0 for i in range(max_weight + 1)] for j in range(n + 1)]
6  # iterate through all items
7  for i in range(n + 1):
8    # and all possible available weights
9    for w in range(max_weight + 1):
10        # if we consider no items or no weight, the max value is 0
11        if i == 0 or w == 0:
12          max_value[i][w] = 0
13        # if the weight of the current item exceeds the max available weight,
14        # then the answer is the max value when considering the first i - 1 items
15        elif w < weights[i - 1]:
16          max_value[i][w] = max_value[i - 1][w]
17        # otherwise, we choose the best option between either:
18        # picking up: item's value + max value when considering the rest of the items and a new weight
19        # giving up: similar to the condition above
20        else:
21          max_value[i][w] = max(values[i - 1] + max_value[i - 1][w - weights[i - 1]],
22                              max_value[i - 1][w])
23  # the answer is the max value when considering all n items and available weight of max_weight
24  return max_value[n][max_weight]
25

For an intuitive explanation, consider the recursive version again. If we were to translate it to an iterative version while maintaining the same recurrence, we need to build our solution from the bottom-up since maxValue[i][w] depends on the values in the previous row maxValue[i - 1]. Also, since the weights for the items are arbitrary, we will need to calculate the maximum value for all weights from 0 to max_weight to ensure we have the answer for the recurrence, since maxValue[i][w] depends on maxValue[i - 1][w - weights[i - 1]] where 0 <= weights[i - 1] <= w .

2D to 1D Optimization

We have discussed how to do knapsack using 2-D DP but now we discuss how we can optimize this into 1-D DP. We realize that for the first dimension keeping track of the objects that we only ever use the previous row so therefore, we can simply remove that dimension from out DP without consequence.

The basic idea is to maintain a 1-D array that keeps track of the maximal value we can get for a certain amount of weight. We can loop from the largest value to the smallest value to ensure we do not use a given object twice. Looping backwards ensures we only ever use DP values from the previous row which is equivalent to the 2-D DP except we can save some memory.

The dp state can then be calculated using dp[j] = max(dp[j], dp[j - weight[i]] + value[i]). We first set each array element to be -1 which means we have not reached that weight. If we have not reached that weight we should skip it and make sure to not compute the value for that index.

Here is a graphic to demonstrate this idea. Note that when a weight is smaller than the array index we stop considering the index as it means our weight is greater than the current capacity of the knapsack.

1from typing import List
2
3def knapsack(weights: List[int], values: List[int], max_weight: int) -> int:
4  # initialize the array and set values to -1 except for index 0
5  dp = [-1] * (max_weight + 1)
6  dp[0] = 0
7  # loop through the objects
8  for i in range(len(weights)):
9    # loop through the dp indexes from largest value to smallest one
10    for j in range(max_weight, weights[i] - 1, -1):
11      # check if we have reached the weight value before
12      if dp[j - weights[i]] != -1:
13        dp[j] = max(dp[j], dp[j - weights[i]] + values[i])
14  return max(dp)
15
16if __name__ == '__main__':
17    weights = [int(x) for x in input().split()]
18    values = [int(x) for x in input().split()]
19    max_weight = int(input())
20    res = knapsack(weights, values, max_weight)
21    print(res)
22
1import java.util.Arrays;
2import java.util.List;
3import java.util.Scanner;
4import java.util.stream.Collectors;
5
6class Solution {
7    public static int knapsack(List<Integer> weights, List<Integer> values, int maxWeight) {
8      // make a dp array and fill the array with -1 making sure to set weight 0 to value 0
9      int [] dp = new int[maxWeight + 1];
10      Arrays.fill(dp, -1);
11      dp[0] = 0;
12      // loop through every object which is simply length of weights array, values array also works as they should be same length
13      for (int i = 0; i < weights.size(); i++) {
14        // as discussed make sure to loop from highest value backwards to avoid reusing the same object
15        for (int j = maxWeight; j >= weights.get(i); j--) {
16          // if we establish a particular weight is achievable we update out current weight maximum value
17          if(dp[j - weights.get(i)] != -1) {
18            dp[j] = Math.max(dp[j], dp[j - weights.get(i)] + values.get(i));
19          }
20        }
21      }
22      int maxValue = -1;
23      for (int i = 0; i < maxWeight + 1; i++) {
24        maxValue = Math.max(maxValue, dp[i]);
25      }
26      return maxValue;
27    }
28
29    public static List<String> splitWords(String s) {
30        return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
31    }
32
33    public static void main(String[] args) {
34        Scanner scanner = new Scanner(System.in);
35        List<Integer> weights = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
36        List<Integer> values = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
37        int maxWeight = Integer.parseInt(scanner.nextLine());
38        scanner.close();
39        int res = knapsack(weights, values, maxWeight);
40        System.out.println(res);
41    }
42}
43
1function knapsack(weights, values, maxWeight) {
2  // initialize the dp array and set values to -1 except for index 0
3  const dp = Array(maxWeight + 1).fill(-1);
4  dp[0] = 0;
5  // loop through the objects
6  for (let i = 0; i < weights.length; i++) {
7    // loop through the dp indexes from largest value to smallest value
8    for (let j = maxWeight; j >= weights[i]; j--) {
9      // check if we have reached the weight value before
10      if (dp[j - weights[i]] !== -1) {
11        dp[j] = Math.max(dp[j], dp[j - weights[i]] + values[i]);
12      }
13    }
14  }
15  return Math.max(...dp);
16}
17
18function splitWords(s) {
19    return s == "" ? [] : s.split(' ');
20}
21
22function* main() {
23    const weights = splitWords(yield).map((v) => parseInt(v));
24    const values = splitWords(yield).map((v) => parseInt(v));
25    const maxWeight = parseInt(yield);
26    const res = knapsack(weights, values, maxWeight);
27    console.log(res);
28}
29
30class EOFError extends Error {}
31{
32    const gen = main();
33    const next = (line) => gen.next(line).done && process.exit();
34    let buf = '';
35    next();
36    process.stdin.setEncoding('utf8');
37    process.stdin.on('data', (data) => {
38        const lines = (buf + data).split('\n');
39        buf = lines.pop();
40        lines.forEach(next);
41    });
42    process.stdin.on('end', () => {
43        buf && next(buf);
44        gen.throw(new EOFError());
45    });
46}
47
1#include <algorithm> // copy, max, max_element
2#include <iostream> // cin, cout, streamsize
3#include <iterator> // back_inserter, istream_iterator
4#include <limits> // numeric_limits
5#include <sstream> // istringstream
6#include <string> // getline, string
7#include <vector> // vector
8
9int knapsack(std::vector<int> weights, std::vector<int> values, int max_weight) {
10  int dp[max_weight + 1];
11  std::fill(dp, dp + max_weight + 1, -1);
12  dp[0] = 0;
13
14  for (int i = 0; i < weights.size(); i++) {
15    for (int j = max_weight; j >= weights[i]; j--) {
16      if (dp[j - weights[i]] != -1) {
17        dp[j] = std::max(dp[j], dp[j - weights[i]] + values[i]);
18      }
19    }
20  }
21  return *std::max_element(dp, dp + max_weight + 1);
22}
23
24template<typename T>
25std::vector<T> get_words() {
26    std::string line;
27    std::getline(std::cin, line);
28    std::istringstream ss{line};
29    std::vector<T> v;
30    std::copy(std::istream_iterator<T>{ss}, std::istream_iterator<T>{}, std::back_inserter(v));
31    return v;
32}
33
34void ignore_line() {
35    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
36}
37
38int main() {
39    std::vector<int> weights = get_words<int>();
40    std::vector<int> values = get_words<int>();
41    int max_weight;
42    std::cin >> max_weight;
43    ignore_line();
44    int res = knapsack(weights, values, max_weight);
45    std::cout << res << '\n';
46}
47

Time Complexity: O(n * max_weight)
Space Complexity: O(n * max_weight)


Still not clear?Β SubmitΒ the part you don't understand to our editors.