The Knapsack Problem Flashcards
What is the Knapsack Problem
It is a combinatorial optimization problem that aims to maximize the total value of items placed in a knapsack of limited capacity.
What is the recurrence relation used in a knapsack problem
K(i,w) = {
0 iff i = 0 or w = 0,
K(i-1, w) if wi > w,
max( K(i-1, w-w), [vi + K(i-1, w-wi)])
}
* where i K(i,w) represents the maximum value of the items that can be placed in the knapsack for the first ith items
* w is the capacity of the knapsack
* wi is the weight of the ith item
* vi is the value of the ith item
Solve an example.
Ask Ai to give you an example to solve
What is the time complexity of the Knapsack problem?
O(nW)
What is the space complexity of the Knapsack problem?
O(nW)