dynamic programming continued david kauchak cs302 spring 2012

52
Dynamic Programming continued David Kauchak cs302 Spring 2012

Upload: joleen-austin

Post on 04-Jan-2016

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Dynamic Programming continued David Kauchak cs302 Spring 2012

Dynamic Programming

continued

David Kauchak

cs302

Spring 2012

Page 2: Dynamic Programming continued David Kauchak cs302 Spring 2012

Admin

CS lunch Thursday after class

Page 3: Dynamic Programming continued David Kauchak cs302 Spring 2012

Longest common subsequence (LCS)

For a sequence X = x1, x2, …, xn, a subsequence is a subset of the sequence defined by a set of increasing indices (i1, i2, …, ik) where

1 ≤ i1 < i2 < … < ik ≤ n

X = A B A C D A B A B

ABA?

Page 4: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

X = A B C B D A B

Y = B D C A B A

otherwise),(),,(max(

f),(1),(

1...11...1

1...11...1

mn

mnmn

YXLCSYXLCS

yxiYXLCSYXLCS

(for now, let’s just worry about counting the length of the LCS)

Page 5: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: Build the solution from the bottom up

otherwise),(),,(max(

f),(1),(

1...11...1

1...11...1

mn

mnmn

YXLCSYXLCS

yxiYXLCSYXLCS

LCS(X1…j, Y1…k)

otherwise]1,[],,1[max(

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 6: Dynamic Programming continued David Kauchak cs302 Spring 2012

0 xi

1 A2 B3 C4 B5 D6 A7 B

0 1 2 3 4 5 6yj B D C A B Ai

j

0 0 0 0 0 0 00 0 0 0 1 1 10 1 1 1 1 2 20 1 1 2 2 2 20 1 1 2 2 3 30 1 2 2 2 3 30 1 2 2 3 3 40 1 2 2 3 4 4

otherwise]1,[],,1[max(

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 7: Dynamic Programming continued David Kauchak cs302 Spring 2012

LCS Algorithm

Θ(nm)

Page 8: Dynamic Programming continued David Kauchak cs302 Spring 2012

Keeping track of the solutionOur LCS algorithm only calculated the length of the LCS between X and Y

What if we wanted to know the actual sequence?

Keep track of this as well…

Page 9: Dynamic Programming continued David Kauchak cs302 Spring 2012

0 xi

1 A2 B3 C4 B5 D6 A7 B

0 1 2 3 4 5 6yj B D C A B Ai

j

0 0 0 0 0 0 00 0 0 0 1 1 10 1 1 1 1 2 20 1 1 2 2 2 20 1 1 2 2 3 30 1 2 2 2 3 30 1 2 2 3 3 40 1 2 2 3 4 4

otherwise]1,[],,1[max(

f],[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

We can follow the arrows to generate the solution

Page 10: Dynamic Programming continued David Kauchak cs302 Spring 2012

0 xi

1 A2 B3 C4 B5 D6 A7 B

0 1 2 3 4 5 6yj B D C A B Ai

j

0 0 0 0 0 0 00 0 0 0 1 1 10 1 1 1 1 2 20 1 1 2 2 2 20 1 1 2 2 3 30 1 2 2 2 3 30 1 2 2 3 3 40 1 2 2 3 4 4

otherwise]1,[],,1[max(

f],[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

We can follow the arrows to generate the solution

BCBA

Page 11: Dynamic Programming continued David Kauchak cs302 Spring 2012

Longest increasing subsequence

Given a sequence of numbers X = x1, x2, …, xn find the longest increasing subsequence

(i1, i2, …, ik), that is a subsequence where numbers in the sequence increase.

5 2 8 6 3 6 9 7

Page 12: Dynamic Programming continued David Kauchak cs302 Spring 2012

Longest increasing subsequence

5 2 8 6 3 6 9 7

Given a sequence of numbers X = x1, x2, …, xn find the longest increasing subsequence

(i1, i2, …, ik), that is a subsequence where numbers in the sequence increase.

Page 13: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7

Two options: Either 5 is in the LIS or it’s not

Page 14: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7include 5

5 + LIS(8 6 3 6 9 7)

Page 15: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7include 5

5 + LIS(8 6 3 6 9 7)

What is this function exactly?

longest increasing sequence of the numbers

longest increasing sequence of the numbers starting with 8

Page 16: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7include 5

5 + LIS(8 6 3 6 9 7)

What is this function exactly?

longest increasing sequence of the numbers

This would allow for the option of sequences starting with 3 which are NOT valid!

Page 17: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7include 5

5 + LIS’(8 6 3 6 9 7)

longest increasing sequence of the numbers starting with 8

Do we need to consider anything else for subsequences starting at 5?

Page 18: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7

5 + LIS’(6 3 6 9 7)

5 + LIS’(6 9 7)

5 + LIS’(9 7)

5 + LIS’(7)

include 5

5 + LIS’(8 6 3 6 9 7)

Page 19: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

5 2 8 6 3 6 9 7don’t include 5

LIS(2 8 6 3 6 9 7)

Anything else?

Technically, this is fine, but now we have LIS and LIS’ to worry about.

Can we rewrite LIS in terms of LIS’?

Page 20: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

)}('{max)( iLISXLISi

Longest increasing sequence for X is the longest increasing sequence starting at any element

And what is LIS’ defined as (recursively)?

Page 21: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 1: Define the problem with respect to subproblems

)}('{max)( iLISXLISi

Longest increasing sequence for X is the longest increasing sequence starting at any element

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Longest increasing sequence starting at i

Page 22: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’:

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 23: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 24: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 25: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 26: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 27: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 28: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 29: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 2 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 30: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 2 2 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 31: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 4 2 2 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 32: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 3 4 2 2 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 33: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

5 2 8 6 3 6 9 7LIS’: 3 4 2 2 3 2 1 1

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

)}('{max)( iLISXLISi

Page 34: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

Page 35: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

start from the end (bottom)

Page 36: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

)}('1{max)(' ... and 1: 1

nixxii

XLISiLISi

Page 37: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

)}('{max)( iLISXLISi

Page 38: Dynamic Programming continued David Kauchak cs302 Spring 2012

Step 2: build the solution from the bottom up

initialization?

Page 39: Dynamic Programming continued David Kauchak cs302 Spring 2012

Running time?

Θ(n2)

Page 40: Dynamic Programming continued David Kauchak cs302 Spring 2012

Another solution

Can we use LCS to solve this problem?

5 2 8 6 3 6 9 7

2 3 5 6 6 7 8 9LCS

Page 41: Dynamic Programming continued David Kauchak cs302 Spring 2012

Another solution

Can we use LCS to solve this problem?

5 2 8 6 3 6 9 7

2 3 5 6 6 7 8 9LCS

Page 42: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoization

Sometimes it can be a challenge to write the function in a bottom-up fashion

Memoization: Write the recursive function top-down Alter the function to check if we’ve already calculated the value If so, use the pre-calculate value If not, do the recursive call(s)

Page 43: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

Page 44: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

Page 45: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

Use to denote uncalculated

Page 46: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

Use to denote uncalculated

What else could we use besides an array?

Page 47: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

Check if we already calculated the value

Page 48: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

calculate the value

Page 49: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoized fibonacci

store the value

Page 50: Dynamic Programming continued David Kauchak cs302 Spring 2012

Memoization

Pros Can be more intuitive to code/understand Can be memory savings if you don’t need answers to

all subproblems Cons

Depending on implementation, larger overhead because of recursion (though often the functions are tail recursive)

Page 51: Dynamic Programming continued David Kauchak cs302 Spring 2012

Quick summary Step 1: Define the problem with respect to subproblems

We did this for divide and conquer too. What’s the difference? You can identify a candidate for dynamic programming if there is

overlap or repeated work in the subproblems being created

Step 2: build the solution from the bottom up Build the solution such that the subproblems referenced by larger

problems are already solved Memoization is also an alternative

Page 52: Dynamic Programming continued David Kauchak cs302 Spring 2012

0-1 Knapsack problem

0-1 Knapsack – A thief robbing a store finds n items worth v1, v2, .., vn dollars and weightw1, w2, …, wn pounds, where vi and wi are integers. The thief can carry at most W pounds in the knapsack. Which items should the thief take if he/she wants to maximize value?

Repetition is allowed, that is you can take multiple copies of any item

})({max)(:

iiwwi

vwwKwKi