CSC 413/513- Intro to Algorithms

Slides:



Advertisements
Similar presentations
CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Advertisements

Overview What is Dynamic Programming? A Sequence of 4 Steps
COMP8620 Lecture 8 Dynamic Programming.
Dynamic Programming.
Review: Dynamic Programming
Greedy vs Dynamic Programming Approach
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 Dynamic Programming algorithms address problems whose solution is recursive in nature, but has the following property: The direct implementation.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
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.
Fundamental Techniques
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.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & 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.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Dynamic Programming … Continued 0-1 Knapsack Problem.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
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
Dynamic Programming Sequence of decisions. Problem state.
Review: Dynamic Programming
Lecture 5 Dynamic Programming
Weighted Interval Scheduling
Seminar on Dynamic Programming.
Topic 25 Dynamic Programming
CS38 Introduction to Algorithms
Algorithm Design Methods
Dynamic Programming.
Prepared by Chen & Po-Chuan 2016/03/29
CS 3343: Analysis of Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
MCA 301: Design and Analysis of Algorithms
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
Longest Common Subsequence
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Dynamic Programming.
Dynamic programming vs Greedy algo – con’t
Dynamic Programming.
Lecture 4 Dynamic Programming
Longest Common Subsequence
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Lecture 5 Dynamic Programming
Analysis of Algorithms CS 477/677
Lecture 5 Dynamic Programming
This is not an advertisement for the profession
0-1 Knapsack problem.
Longest Common Subsequence
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:

CSC 413/513- Intro to Algorithms Dynamic programming 0-1 Knapsack problem 4/12/2019

Review: Dynamic programming DP is a method for solving certain kind of problems DP can be applied when the solution of a problem includes solutions to subproblems We need to find a recursive formula for the solution We can recursively solve subproblems, starting from the trivial case, and save their solutions in memory In the end we’ll get the solution of the whole problem 4/12/2019

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? 4/12/2019

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

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. Just another version of this problem is the “Fractional Knapsack Problem”, where we can take fractions of items. 4/12/2019

0-1 Knapsack problem: 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 the most total value and with total weight less or equal to W Running time will be O(2n) 4/12/2019

0-1 Knapsack problem: brute-force approach Can we do better? Yes, with an algorithm based on dynamic programming We need to carefully identify the subproblems Let’s try this: If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} 4/12/2019

Defining a Subproblem 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 valid subproblem definition. The question is: can we describe the final solution (Sn ) in terms of subproblems (Sk)? Unfortunately, we can’t do that. Explanation follows…. 4/12/2019

? Defining a Subproblem wi bi Weight Benefit wi bi Item # ? 1 2 3 Max weight: W = 20 For S4: Total weight: 14; total benefit: 20 S4 2 3 4 S5 3 4 5 4 5 8 5 9 10 w1 =2 b1 =3 w3=4 b3 =5 w4 =5 b4 =8 w5 =9 b5 =10 Solution for S4 is not part of the solution for S5!!! For S5: Total weight: 20 total benefit: 26 4/12/2019

Defining a Subproblem (continued) As we have seen, the solution for S4 is not part of the solution for S5 So our definition of a subproblem is flawed and we need another one! Let’s add another parameter: w, which will represent the max weight for each subset of items The subproblem then will be to compute B[k,w] 4/12/2019

Recursive Formula Recursive formula for subproblems: The best subset of Sk that has the total weight w, either contains item k or not. First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable Second case: wk <=w. Then the item k may be in the solution, so consider two sub-cases 4/12/2019

Recursive Formula Recursive formula for subproblems: Second case: the best subset of Sk that has total weight w is the best of the two: 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 4/12/2019

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

Remember that the brute-force algorithm Running time for w = 0 to W B[0,w] = 0 for i = 0 to n B[i,0] = 0 < the rest of the code > O(W) Repeat n times O(W) What is the running time of this algorithm? O(n*W) (remember: sorting by wi not needed) Remember that the brute-force algorithm takes O(2n) 4/12/2019

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) 4/12/2019

Example (2) i 1 2 3 4 W 1 2 3 4 5 for w = 0 to W B[0,w] = 0 4/12/2019

Example (3) i 1 2 3 4 W 1 2 3 4 5 for i = 0 to n B[i,0] = 0 4/12/2019

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

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

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

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

Example (8) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=1 bi=3 wi=2 1 2 3 4 W i=1 bi=3 wi=2 w=5 w-wi=2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (9) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=1 w-wi=-2 1 2 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (10) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=2 w-wi=-1 1 2 3 3 3 3 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (11) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=3 w-wi=0 1 2 3 3 3 3 4 4 3 5 3 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

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

Example (13) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=2 bi=4 wi=3 1 2 3 4 W i=2 bi=4 wi=3 w=5 w-wi=2 1 2 3 3 3 3 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (14) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=1..3 1 2 3 3 3 3 3 4 4 4 3 4 5 3 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

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

Example (15) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=3 bi=5 wi=4 1 2 3 4 W i=3 bi=5 wi=4 w=5 w- wi=1 1 2 3 3 3 3 3 4 4 4 3 4 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (16) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=4 bi=6 wi=5 1 2 3 4 W i=4 bi=6 wi=5 w=1..4 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Example (17) Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i=4 bi=6 wi=5 1 2 3 4 W i=4 bi=6 wi=5 w=5 1 2 3 3 3 3 3 3 4 4 4 4 3 4 5 5 5 3 7 7 7 if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w 4/12/2019

Comments This algorithm only finds the max possible value that can be carried in the knapsack To know the items that make this maximum value, an addition to this algorithm is necessary Please see LCS algorithm from the previous lecture for the example how to extract this data from the table we built 4/12/2019

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 Running time (Dynamic Programming algorithm vs. naïve algorithm): LCS: O(m*n) vs. O(n * 2m) 0-1 Knapsack problem: O(W*n) vs. O(2n) 4/12/2019

Dynamic Programming Runtimes Problem Input size Running time Rod cutting n Q(n2) LCS m+n Q(mn) Matrix chain mult n+1 Q(n3) 0-1 knapsack 2n+lg W Q(nW) Not polynomial time 4/12/2019