Professional Documents
Culture Documents
Top-down implementation
Bottom-up implementation
Problem
• Given a knapsack of fixed capacity, c, and n items of fixed integral weights(w1, w2, …, wn)
and prices(p1 , p2, …, pn), find a way to pack the knapsack such that the value of the items in
the knapsack is the maximum.
• The weights and values are integers
• The '0-1' signifies that the item is included in the knapsack or not included.
Problem characteristics
Is this an optimization problem? - Yes
We can pack the knapsack in many ways(possibly 2n, capacity allowing). But what is required is the
packing that gives the maximum value.
where pi is the price of the i'th item and V c – w is the optimal value of the knapsack for the
i
remaining capacity.
As the problem has an optimal substructure there is no need to calculate the cost of all possibly 2n
packings and then find the one that provides the maximum price. It is sufficient if we find the cost
of n packings formulated above and find the maximum among them. Each of the n packings
obtained are the optimally maximum w.r.t. containing the given item.