/
Lecture 5 Dynamic Programming Lecture 5 Dynamic Programming

Lecture 5 Dynamic Programming - PowerPoint Presentation

brianna
brianna . @brianna
Follow
66 views
Uploaded On 2023-10-31

Lecture 5 Dynamic Programming - PPT Presentation

Outline Knapsack revisited How to output the optimal solution and how to prove correctness Longest Common Subsequence Maximum Independent Set on Trees Example 2 Knapsack Problem There is a knapsack that can hold items of total weight at most ID: 1027578

items set knapsack case set items case knapsack item independent output capacity weight maximum subsequence total find common longest

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture 5 Dynamic Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Lecture 5 Dynamic Programming

2. OutlineKnapsack revisited: How to output the optimal solution, and how to prove correctness?Longest Common SubsequenceMaximum Independent Set on Trees.

3. Example 2 Knapsack ProblemThere is a knapsack that can hold items of total weight at most W. There are now n items with weights w1,w2,…, wn. Each item also has a value v1,v2,…,vn.Goal: Select some items to put into knapsack1. Total weight is at most W.2. Total value is as large as possible.Output: the set of items to put into the Knapsack

4. Recall: States and Transition functionExample: Capacity W = 4, 3 items with (weight, value) = (1, 2), (2, 3), (3, 4). States (sub-problems): What is the maximum value for a Knapsack with capacity j (= 0, 1, 2, 3, 4) and the first i (= 0, 1, 2, 3) items?Use a[i, j] to denote this maximum value, we have a[i, j] = a[i - 1, j - wi] + vi (item i in knapsack) a[i - 1, j] (item i not in knapsack) max

5. Dynamic Programming TableExample: Capacity W = 4, 3 items with (weight, value) = (1, 2), (2, 3), (3, 4). 01234000000102222202355302356+4

6. Outputting the SolutionRemember the choice (arrows) in the DP table.Solution = {1, 3}, value = 601234000000102222202355302356

7. Outputting the SolutionAnother Example (capacity = 3)Solution = {1, 2}, value = 501234000000102222202355302356

8. Pseudo-codeKnapsackInitialize a[i, 0] = 0, a[0, j] = 0 (Base Case)FOR i = 1 to n (Enumerate #items) FOR j = 1 to W (Enumerate capacity) a[i, j] = a[i-1, j] (Case 1: not using item i) IF j >= w[i] and a[i-1, j-w[i]] + v[i] > a[i,j] (if Case 2 is better) a[i, j] = a[i-1, j-w[i]] + v[i] (Case 2: using item i)Output(n, W)IF n = 0 or W = 0 THEN RETURNIF a[n, W] = a[n-1, W] THEN (Case 1) Output(n-1, W)ELSE (Case 2) Output(n-1, W-w[n]) Print(n)

9. Longest Common SubsequenceInput: two strings a[] = ‘ababcde’ and b[] = ‘abbecd’Subsequence: same definition as in LIS (can skip characters)E.g. ‘abac’ is a subsequence of a[], but not b[]‘abed’ is a subsequence of b[] but not a[]Goal: Find the length of the longest common subsequence (LCS)In this example: LCS = ‘abbcd’, length = 5.

10. Max Independent Set on TreesInput: a treeIndependent Set: Set of nodes that are not connected by any edgesGoal: Find an independent set of maximum size.

11. Max Independent Set on TreesInput: a treeIndependent Set: Set of nodes that are not connected by any edgesGoal: Find an independent set of maximum size.