DP-Knapsack Problem
DP-Knapsack Problem
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
◼ Goal:
Solve only subproblems that are necessary and solve it only once
◼
◼ Memorization is another way to deal with overlapping subproblems in
dynamic programming
◼ With memorization, we implement the algorithm recursively:
◼ If we encounter a new subproblem, we compute and store the
solution.
◼ If we encounter a subproblem we have seen, we look up the answer
◼ Most useful when the algorithm is easiest to implement recursively
◼ Especially if we do not need solutions to all subproblems.