From your DP table, we know f [i] [w] = the maximum total value of a subset of elements 1..i, which has a total weight less than or equal to w.
We can use the table to restore the optimal packaging:
def reconstruct(i, w): # reconstruct subset of items 1..i with weight <= w # and value f[i][w] if i == 0: # base case return {} if f[i][w] > f[i-1][w]: # we have to take item i return {i} UNION reconstruct(i-1, w - weight_of_item(i)) else: # we don't need item i return reconstruct(i-1, w)
Niklas B.
source share