The 0/1 Knapsack problem is a classic dynamic programming problem. It involves selecting items from a set, each with its own weight and value, to maximize the total value while keeping the total weight within a given limit.
Here's an example question:
Question: Given a list of items, each with a weight and value, and a maximum weight capacity, determine the maximum value that can be obtained by selecting a subset of items, where the sum of the weights is less than or equal to the capacity.
Now let's go through the solution using the 0/1 Knapsack pattern:
Solution:
Step 1: Understand the problem and identify the problem type.
Step 2: Define the problem variables.
weights
: An array representing the weights of the items.values
: An array representing the values of the items.capacity
: The maximum weight capacity.n
: The total number of items.Step 3: Formulate the recurrence relation.
dp
as a 2D array of size (n+1) x (capacity+1)
, where dp[i][j]
represents the maximum value that can be obtained by considering items up to index i
and with a maximum capacity of j
.dp[i][j] = max(dp[i-1][j], dp[i-1][j-weights[i]] + values[i])
Here, dp[i-1][j]
represents the maximum value obtained by excluding the current item, and dp[i-1][j-weights[i]] + values[i]
represents the maximum value obtained by including the current item.
Step 4: Initialize the base cases.
i = 0
), the maximum value is always 0, regardless of the capacity (dp[0][j] = 0
).j = 0
), the maximum value is always 0, regardless of the number of items (dp[i][0] = 0
).Step 5: Determine the iteration order.
i = 1
to n
and the capacities from j = 1
to capacity
.Step 6: Implement the solution and handle edge cases. Here's a Python implementation of the 0/1 Knapsack problem:
def knapsack(weights, values, capacity): n = len(weights) dp = [[0] * (capacity + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, capacity + 1): if weights[i - 1] <= j: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]) else: dp[i][j] = dp[i - 1][j] return dp[n][capacity]
Step 7: Test the solution with sample inputs. Now, let's test the knapsack
function with a sample input:
weights = [2, 3, 4, 5] values = [3, 4, 5, 6] capacity = 5 print(knapsack(weights, values, capacity))
When you run this code, it will output the maximum value that can be obtained by selecting items from the given list, considering their weights and values, while respecting the weight capacity.
In this case, the output will be 8
, which represents the maximum value achievable by selecting items with weights [2, 3]
and values [3, 4]
.
Note that this implementation assumes that the weights and values lists are 0-based indexed, meaning the weight and value of the first item are weights[0]
and values[0]
respectively. If your lists are 1-based indexed, you'll need to adjust the indices accordingly in the implementation.