Week 11 - Dynamic Programming KnapSack
Week 11 - Dynamic Programming KnapSack
Knapsack Problem
Components of Dynamic
Programming
• Stages: The problem can be divided into several sub problems, which are called stages. A stage
is a small portion of a given problem
• States: Each stage has several states associated with it. The states for the shortest path problem
was the node reached.
• Decision: At each stage, there can be multiple choices out of which one of the best decisions
should be taken. The decision taken at every stage should be optimal; this is called a stage
decision.
• Optimal policy: It is a rule which determines the decision at each stage; a policy is called an
optimal policy if it is globally optimal. This is known as Bellman principle of optimality.
• Given the current state, the optimal choices for each of the remaining states does not depend
on the previous states or decisions. In the shortest path problem, it was not necessary to know
how we got a node only that we did.
• There exist a recursive relationship that identify the optimal decisions for stage j, given that
stage j+1, has already been solved.
The Knapsack Problem
• Suppose there is a fire in your apartment and you must escape to
save yourself from getting burnt
• You want to save as much of your valuables as you can
• The only thing you can grab to carry the valuables is a bag which can
hold only w kgs of weight
• All you can think of is to pick up the highest value items to put in the
bag while not tearing it apart.
• Each item has a weight w and a value v
• Which items should you put in the bag to take highest value items. i.e.
maximize your savings. Without overflowing the bag beyond capacity
Knapsack Problem
• Objective is to maximize the value of items in your bag
i.e. MAX z :
Subject To:
<= capacity
0-1 Knapsack Problem
• When items can either be taken as whole or not at all
• i.e. no fractions of items could be taken.
• An item is either taken (1) or left (0)
• In contrast to when items can be taken in fractions
• i.e. 1.3 kg of sugar and 2.01 kg of salt etc
• The solution to 0-1 Knapsack is a bit array 00110011 representing a 1
for item that will be taken and 0 for the one left out.
Example
Knapsack – Brute Force Approach
• Brute Force • Consider 3 items Total Total value
with weights 3 kg, 5 weight
• The naïve way to solve this kg and 7 kg and
problem is to cycle values 4, 3 , 6 and
through all 2n subsets of respectively. The 000 0 0
the n items and pick the capacity of knapsack
subset with a legal weight is 8 kg 001 7 6
that maximizes the value • The possible number 010 5 3
of the knapsack. of combinations of 100 3 4
• We can come up with a these items is 2n = 8
101 10 10
dynamic programming
110 8 7
algorithm that will
USUALLY do better than 011 12 9
this brute force technique. 111 15 13
0-1 Knapsack – Dynamic
Programming
• Divide the problem into stages • Tabular formulation of problem
• At each stage determine a decision • If the bag is capable of holding the current
whether to take an item or not item, add it
• Each stage has a state • So our recursive formula for sub problems:
• Let B[k, w] represent the maximum total value of a
• Capacity of the bag and value subset Sk with weight w.
• Decision • Our goal is to find B[n, W], where n is the total
number of items and W is the maximal weight the
• Whether to add the current item or knapsack can carry.
not B[k, w] = B[k - 1,w], if wk > w
• Optimal policy = max { B[k - 1,w], B[k - 1,w - w k] + vk}, otherwise