You are on page 1of 2

0-1 Knapsack problem

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.

Brute force solution


• We can pack the knapsack in possibly 2n ways provided the packing fits into the knapsack.
The count of 2n is arrived at recognizing that the packing can contain or not contain each of
the n items.
• The brute force solution involves calculating the cost of all these packings and determining
the maximum among them. Thus the running time may be exponential

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.

Does the optimization problem have an optimal substructure? - Yes


Let Vc be the maximal value obtainable for a knapsack of capacity c by packing it in an optimal
way. Then it can be described in terms of its subproblems as below,
V n=max ( p1 +V c−w , p2 +V c−w ,... , pn +V c−w )
1 2 n

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.

Are the subproblems involved in a candidate solution independent?


Every candidate solution involves only one candidate solution only. So the question of
independence does not arise.

Characteristics of the subproblem space


• Are there overlapping subproblems? - Yes.
◦ Total number of subproblems - 2n
◦ Total number of feasible subproblems – c , one each for every indivisible unit of
capacity
◦ Distinct number of subproblems = n < c
It is clear from the recursive formulation that the subproblem space consists of just n
distinct subproblems( V c−w , V c−w ,... , V c−w )
1 2 n

• Is the entire distinct subproblem space solved? - No.


◦ n distinct subproblems are solved
◦ Only n among the possible c distinct subproblems are solved.
◦ Thus, a top down implementation will be more efficient.
Running time
• Naive Divide and conquer solution
◦ Let T(n) denote the number of recursive calls made to solve a 0-1 knapsack problem.
Then,
T ( 0)=1, as there is an initial call
AND
n
T (n)≤1+ ∑ T (i) , signifying the initial call and subsequent recursive calls to solve
i=1
the subproblem for a particular capacity ,i.
Note that the lesser than signcompensates for not all subproblems being solved
◦ Solving this simple recurrence problem gives T (n)=O(2n )
T(0) = 1 = 20
T(1) = 1 + T(0) = 1 + 20 = 21
T(2) = 1 + T(0) + T(1) = 1 + 20 + 21 = 22
T(3) = 1 + T(0) + T(1) + T(2) = 1 + 20 + 21 + 22 = 23

T(n) = 1 + T(0) + … + T(n-1) = 1 + 20 + … + 2n-1 = 1 + 2n - 1 = 2n
• Top down method with memoization
◦ Solves each of the n subproblems once with n recursive calls. Each subproblem runs a
for loop for a maximum of c times to determine the maximum revenue. Thus
T (n)=Θ ( c n)
• Bottom up method with memoization
◦ The running time is easy to see. There is a nested for loop which results in
T (n)=Θ (cn)

You might also like