Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Week 11 - Dynamic Programming KnapSack

Uploaded by

eshaasif005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Week 11 - Dynamic Programming KnapSack

Uploaded by

eshaasif005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Dynamic Programming

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

• The if previous stages have been


solved optimally, the current decision • Simply put, this means that the best subset of Sk
moves towards global optimal that has total weight w is:
1) The best subset of Sk-1 that has total weight w, or
2) The best subset of Sk-1 that has total weight w-wk plus
the item k
0-1 Knapsack - Example
• Consider 4 items with values = [10, 40, 30,60]
• And weights = [5, 4, 6, 3]
• The total bag capacity is 10.
At any stage the bag will have a capacity available from 0-10 units
At each stage we need to take a decision whether to add the current
item to bag or not
If the current item can be adjusted in the available capacity then we
can add and see if it increases the value
If the item cannot be added, we move to next
0-1 Knapsack
Available
Capacity
Items Weight Value 0 1 2 3 4 5 6 7 8 9 10
. 0 0 0 0 0 0 0 0 0 0 0
Item 1 5 10 0 0 0 0 0 10 10 10 10 10 10
Item 2 4 40 0 0 0 0 40 40 40 40 40 50 50
Item 3 6 30 0 0 0 0 40 40 40 40 40 50 50
Item 4 3 60 0 0 0 60 40 40 40 100 100 100 100

B[k, w] = B[k - 1,w], if wk > w B[1,0] =


= max { B[k - 1,w], B[k - 1,w - wk] + vk}, otherwise

You might also like