Example of a Knapsack Problem
Problem Statement:
You are given a set of items, each with a weight and a value.
You need to determine the number of each item to include in a knapsack so that the total weight is less than or equal to a given limit, and the total value is as large as possible.
Items:
- Item 1: Weight = $2$, Value = $3$
- Item 2: Weight = $3$, Value = $4$
- Item 3: Weight = $4$, Value = $5$
- Item 4: Weight = $5$, Value = $6$
Knapsack Capacity: $8$
The goal is to maximize the total value without exceeding the knapsack capacity of $8$.
Python Solution:
This is a $dynamic$ $programming$ approach to solve the $0$/$1$ Knapsack Problem.
1 | def knapsack(values, weights, capacity): |
Explanation:
Initialization:
- The function
knapsack
takes three arguments:values
,weights
, andcapacity
. - The $dynamic$ $programming$ ($DP$) table
dp
is initialized with zeros.
The table has dimensions(n+1) x (capacity+1)
, wheren
is the number of items.
- The function
Filling the DP Table:
- The outer loop iterates through the items, and the inner loop iterates through possible capacities from
1
tocapacity
. - For each item and capacity, the $DP$ table is updated by considering two possibilities:
- Not including the item in the knapsack (value remains the same as the previous row).
- Including the item in the knapsack (value is updated based on the remaining capacity and the value of the item).
- The outer loop iterates through the items, and the inner loop iterates through possible capacities from
Result:
- After filling the $DP$ table, the maximum value that can be carried in the knapsack is found in
dp[n][capacity]
.
- After filling the $DP$ table, the maximum value that can be carried in the knapsack is found in
Output:
1 | Maximum value that can be carried in the knapsack: 10 |
In this example, the optimal solution is to include Item $2$ (weight $3$, value $4$) and Item $4$ (weight $5$, value $6$), which gives a total value of $10$ while staying within the capacity limit of $8$.