Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Dynamic Programming

Similar presentations


Presentation on theme: "Lecture 5 Dynamic Programming"— Presentation transcript:

1 Lecture 5 Dynamic Programming

2 Outline Knapsack revisited: How to output the optimal solution, and how to prove correctness? Longest Common Subsequence Maximum Independent Set on Trees.

3 Example 2 Knapsack Problem
There 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 knapsack 1. 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 function
Example: 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 - 1, j - wi] + vi (item i in knapsack) a[i, j] = max a[i - 1, j] (item i not in knapsack)

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

6 Outputting the Solution
Remember the choice (arrows) in the DP table. Solution = {1, 3}, value = 6 1 2 3 4 5 6

7 Outputting the Solution
Another Example (capacity = 3) Solution = {1, 2}, value = 5 1 2 3 4 5 6

8 Pseudo-code Knapsack Initialize 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 RETURN IF 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 Subsequence
Input: 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 Trees
Input: a tree Independent Set: Set of nodes that are not connected by any edges Goal: Find an independent set of maximum size.

11 Max Independent Set on Trees
Input: a tree Independent Set: Set of nodes that are not connected by any edges Goal: Find an independent set of maximum size.


Download ppt "Lecture 5 Dynamic Programming"

Similar presentations


Ads by Google