0-1 Knapsack problem.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Analysis of Algorithms
CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
COMP8620 Lecture 8 Dynamic Programming.
Review: Dynamic Programming
Dynamic Programming and the Knapsack Problem Paul Dohmen Roshnika Fernando.
Dynamic Programming (II)
Greedy vs Dynamic Programming Approach
Dynamic Programming Reading Material: Chapter 7..
0-1 Knapsack Problem A burglar breaks into a museum and finds “n” items Let v_i denote the value of ith item, and let w_i denote the weight of the ith.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
The Knapsack Problem The classic Knapsack problem is typically put forth as: A thief breaks into a store and wants to fill his knapsack with as much value.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Odds and Ends HP ≤ p HC (again) –Turing reductions Strong NP-completeness versus Weak NP-completeness Vertex Cover to Hamiltonian Cycle.
KNAPSACK PROBLEM A dynamic approach. Knapsack Problem  Given a sack, able to hold K kg  Given a list of objects  Each has a weight and a value  Try.
Dynamic Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
1 Dynamic Programming Jose Rolim University of Geneva.
The Knapsack Problem Input –Capacity K –n items with weights w i and values v i Goal –Output a set of items S such that the sum of weights of items in.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Dynamic Programming … Continued 0-1 Knapsack Problem.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Dynamic Programming … Continued
Divide and Conquer. Problem Solution 4 Example.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Introduction to Algorithms: Brute-Force Algorithms.
Dynamic Programming Sequence of decisions. Problem state.
Review: Dynamic Programming
Seminar on Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming and the Knapsack Problem
Topic 25 Dynamic Programming
Algorithm Design Methods
Dynamic Programming.
CS 3343: Analysis of Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Advanced Algorithms Analysis and Design
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
CS6045: Advanced Algorithms
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Longest Common Subsequence
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
CSC 413/513- Intro to Algorithms
Lecture 4 Dynamic Programming
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
This is not an advertisement for the profession
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Knapsack Problem A dynamic approach.
Seminar on Dynamic Programming.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

0-1 Knapsack problem

Knapsack Problem You are a mischievous child camping in the woods. You would like to steal items from other campers, but you can only carry so much mass in your knapsack. You see seven items worth stealing. Each item has a certain mass and monetary value. How do you maximize your profit so you can buy more video games later?

Knapsack Problem Value and mass of each item is given Maximize profit Subject to mass constraint of knapsack: 15 kg Being a smart kid, you apply dynamic programming.

Solution

Sensitivity Analysis Valuables are not very valuable when camping, pick richer people to steal from. Suddenly a valuable item of $15 that weighs 11kg is available, will the optimal solution change? Yes, the maximum profit becomes $23

Knapsack problem Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their values. Item #(i) Weight(wi) Value(bi) 1 1 8 2 3 6 3 5 5

0-1 Knapsack problem wi bi Weight Benefit value Items 3 2 This is a knapsack Max weight: W = 20 4 3 5 4 W = 20 8 5 9 10 5/21/2019

Knapsack problem There are two versions of the problem: Items are indivisible; you either take an item or not. Some special instances can be solved with dynamic programming “Fractional knapsack problem” Items are divisible: you can take any fraction of an item

0-1 Knapsack problem Given a knapsack with maximum capacity W, and a set S consisting of n items Each item i has some weight wi and benefit value bi (all wi, bi and W are integer values) Problem: How to pack the knapsack to achieve maximum total value of packed items?

0-1 Knapsack problem Problem, in other words, is to find The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.

Let’s first solve this problem with a straightforward algorithm Brute-force approach Let’s first solve this problem with a straightforward algorithm Since there are n items, there are 2n possible combinations of items. We go through all combinations and find the one with maximum value and with total weight less or equal to W Running time will be O(2n)

Dynamic programming approach We can do better with an algorithm based on dynamic programming We need to carefully identify the subproblems

Defining a Subproblem This is a reasonable subproblem definition. If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} This is a reasonable subproblem definition. The question is: Can we describe the final solution (SK+1 ) in terms of subproblems (Sk)? Unfortunately, we can’t do that. Basically, the solution to the optimization problem for Sk+1 might NOT contain the optimal solution from problem Sk.

Knapsack 0-1 Problem Let’s illustrate that point with an example: Item Weight Value I0 3 10 I1 8 4 I2 9 9 I3 8 11 The maximum weight the knapsack can hold is 20. The best set of items from {I0, I1, I2} is {I0, I1, I2} BUT the best set of items from {I0, I1, I2, I3} is {I0, I2, I3}. In this example, note that this optimal solution, {I0, I2, I3}, does NOT build upon the previous optimal solution, {I0, I1, I2}. (Instead it builds upon the solution, {I0, I2}, which is really the optimal subset of {I0, I1, I2} with weight 12 or less.) So our definition of a subproblem is flawed and we need another one!

Defining a Subproblem Let’s add another parameter: w, which will represent the maximum weight for each subset of items The subproblem then will be to compute V[k,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w. Assuming knowing V[i, j], where i=0,1, 2, … k-1, j=0,1,2, …w, how to derive V[k,w]?

Recursive Formula The best subset of Si that has the total weight  w, either contains item i or not. First case: wi>w. Item i can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable. Second case: wi  w. Then the item i can be in the solution, and we choose the case with greater value.

0-1 Knapsack Algorithm for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w

Remember that the brute-force algorithm Running Time What is the running time of this algorithm? O(n*W) for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w O(W) O(n) Repeat n times O(W) Remember that the brute-force algorithm takes O(2n)

Example Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)

Example i\W 1 2 3 4 5 1 2 3 4 for w = 0 to W V[0,w] = 0

Example 1 2 3 4 5 i\W for i = 1 to n V[i,0] = 0

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example i\W 1 2 3 4 5 i=1 bi=3 wi=2 w=1 w-wi =-1 1 2 3 4 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w V[1,1] = V[0,1] = 0

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=1 bi=3 wi=2 w=2 w-wi =0 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3+V[0,0] > V[0,2] 3+0 > 0 V[1,2] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=1 bi=3 wi=2 w=3 w-wi =1 3 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3+V[0,1] > V[0,3] 3+0 > 0 V[1,3] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=1 bi=3 wi=2 w=4 w-wi =2 3 3 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3+V[0,2] > V[0,4] 3+0 > 0 V[1,4] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=1 bi=3 wi=2 w=5 w-wi =3 3 3 3 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3+V[0,3] > V[0,5] 3+0 > 0 V[1,5] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=2 bi=4 wi=3 w=1 w-wi =-2 3 3 3 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w V[2,1] = V[1,1] = 0

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=2 bi=4 wi=3 w=2 w-wi =-1 3 3 3 3 3 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w V[2,2] = V[1,2] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=2 bi=4 wi=3 w=3 w-wi =0 3 3 3 3 3 4 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 4+V[1,0] > V[1,3] 4+0 > 3 V[2,3] = 4

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=2 bi=4 wi=3 w=4 w-wi =1 3 3 3 3 3 4 4 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 4+V[1,1] > V[1,4] 4+0 > 3 V[2,4] = 4

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=2 bi=4 wi=3 w=5 w-wi =2 3 3 3 3 3 4 4 7 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 4+V[1,2] > V[1,5] 4+3 > 3 V[2,5] = 7

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=3 bi=5 wi=4 w= 1..3 3 3 3 3 3 4 4 7 3 4 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w V[3,1] = V[2,1] = 0 V[3,2] = V[2,2] = 3 V[3,3] = V[2,3] = 4

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=3 bi=5 wi=4 w= 4 w- wi=0 3 3 3 3 3 4 4 7 3 4 5 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5+V[2,0] > V[2,4] 5+0 > 4 V[3,4] = 5

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=3 bi=5 wi=4 w= 5 w- wi=1 3 3 3 3 3 4 4 7 3 4 5 7 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5+V[2,1] > V[2,5] 5+0 > 7 V[3,4] = 7

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=4 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=4 bi=6 wi=5 w= 1..4 3 3 3 3 3 4 4 7 3 4 5 7 3 4 5 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w V[4,4] = V[3,4] = 5 V[4,3] = V[3,3] = 4 V[4,1] = V[3,1] = 0 V[4,2] = V[3,2] = 3

Example Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=4 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Example 1 2 3 4 5 i\W i=4 bi=6 wi=5 w= 5 w- wi=0 3 3 3 3 3 4 4 7 3 4 5 7 3 4 5 7 for i = 1 to n for w = 1 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 6+V[3,1] > V[3,5] 6+0 > 7 V[4,5] = 7

How to find out which items are in the optimal subset? Exercise How to find out which items are in the optimal subset?

Comments This algorithm only finds the max possible value that can be carried in the knapsack i.e., the value in V[n,W] To know the items that make this maximum value, an addition to this algorithm is necessary

How to find actual Knapsack Items All of the information we need is in the table. V[n,W] is the maximal value of items that can be placed in the Knapsack. Let i=n and k=W if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 // Assume the ith item is not in the knapsack // Could it be in the optimally packed knapsack?

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=4 k= 5 bi=6 wi=5 V[i,k] = V[i1,k] = 3 3 3 3 3 4 4 7 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=4 k= 5 bi=6 wi=5 V[i,k] = V[i1,k] = 3 3 3 3 3 4 4 7 V[i,k] = V[4,5]=7 V[i1,k]=V[3,5] =7 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=3 k= 5 bi=5 wi=4 V[i,k] = V[i1,k] = 3 3 3 3 3 4 4 7 V[i,k] = V[3,5]=7 V[i1,k]=V[2,5] =7 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=2 k= 5 bi=4 wi=3 V[i,k] = V[i1,k] = k  wi= 5-3=2 3 3 3 3 3 4 4 7 7 V[i,k] = V[2,5]=7 V[i1,k]=V[1,5] =3 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5 i =2-1 =1 k =5-3 =2

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=2 k= 5 bi=4 wi=3 V[i,k] = V[2,5]=7 V[i1,k]=V[1,5] =3 k  wi= 5-3=2 3 3 3 3 3 4 4 7 7 2 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5 i =2-1 =1 k =5-3 =2

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=1 k= 2 bi=3 wi=2 V[i,k] = V[i1,k] = k  wi=0 3 3 3 3 3 3 4 4 7 V[i,k] = V[1,2]=3 V[i1,k]=V[0,2] =0 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5 2 I =1-1 =0 K =2-2 =0

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=1 k= 2 bi=3 wi=2 V[i,k] = V[i1,k] = k  wi=0 1 3 3 3 3 3 3 4 4 7 V[i,k] = V[1,2]=3 V[i1,k]=V[0,2] =0 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 W=5 2 I =1-1 =0 K =2-2 =0

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W i=0 k= 0 3 3 3 3 3 4 4 7 The optimal knapsack should contain {1, 2} 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the nth item as in the knapsack i = i1, k = k-wi else i = i1 W=5 2 1: (2, 3) 2: (3, 4) 1

Finding the Items Items: i wi bi 1: (2, 3) 2: (3, 4) 3: (4, 5) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) Finding the Items 1 2 3 4 5 i\W 3 3 3 3 3 3 4 4 7 7 The optimal knapsack should contain {1, 2} 3 4 5 7 3 4 5 7 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the nth item as in the knapsack i = i1, k = k-wi else i = i1 W=5 1: (2, 3) 2: (3, 4)

Memorization (Memory Function Method) Goal: Solve only subproblems that are necessary and solve it only once Memorization is another way to deal with overlapping subproblems in dynamic programming With memorization, we implement the algorithm recursively: If we encounter a new subproblem, we compute and store the solution. If we encounter a subproblem we have seen, we look up the answer Most useful when the algorithm is easiest to implement recursively Especially if we do not need solutions to all subproblems.

Conclusion Dynamic programming is a useful technique of solving certain kind of problems When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them as necessary (memorization) Running time of dynamic programming algorithm vs. naïve algorithm: 0-1 Knapsack problem: O(W*n) vs. O(2n)