Problem
Ask
Submissions
Solution

Solution: 0/1 Knapsack

Statement

Naive approach

A naive approach would be to generate all combinations of weights and calculate the profit of each combination. We would then choose the combination that yields the highest profit from among those that don’t exceed the knapsack capacity.

For example, suppose we’re given a knapsack with a capacity of 55, and the following list of values and weights:

  • values: [3,5,2,7][3, 5, 2, 7]
  • weights: [3,1,2,4][3, 1, 2, 4]

To find the maximum profit, we try all possible valid combinations, that is, whose weight does not exceed 55:

Weights Values Total Weight Total Value
3,13, 1 3,53, 5 44 88
3,23, 2
...
Problem
Ask
Submissions
Solution

Solution: 0/1 Knapsack

Statement

Naive approach

A naive approach would be to generate all combinations of weights and calculate the profit of each combination. We would then choose the combination that yields the highest profit from among those that don’t exceed the knapsack capacity.

For example, suppose we’re given a knapsack with a capacity of 55, and the following list of values and weights:

  • values: [3,5,2,7][3, 5, 2, 7]
  • weights: [3,1,2,4][3, 1, 2, 4]

To find the maximum profit, we try all possible valid combinations, that is, whose weight does not exceed 55:

Weights Values Total Weight Total Value
3,13, 1 3,53, 5 44 88
3,23, 2
...