dynamic programming david kauchak cs302 spring 2013

73
Dynamic Programming David Kauchak cs302 Spring 2013

Upload: gwenda-french

Post on 05-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Programming David Kauchak cs302 Spring 2013

Dynamic Programming

David Kauchak

cs302

Spring 2013

Page 2: Dynamic Programming David Kauchak cs302 Spring 2013

Administative

Page 3: Dynamic Programming David Kauchak cs302 Spring 2013

Dynamic programming

One of the most important algorithm tools!

Very common interview question

Method for solving problems where optimal solutions can be defined in terms of optimal solutions to sub-problems

AND

the sub-problems are overlapping

Page 4: Dynamic Programming David Kauchak cs302 Spring 2013

Fibonacci numbers

1, 1, 2, 3, 5, 8, 13, 21, 34, …What is the recurrence for the nth Fibonacci number?

F(n) = F(n-1) + F(n-2)

The solution for n is defined with respect to the solution to smaller problems (n-1 and n-2)

Page 5: Dynamic Programming David Kauchak cs302 Spring 2013

Fibonacci: a first attempt

Page 6: Dynamic Programming David Kauchak cs302 Spring 2013

Is it correct?

F(n) = F(n-1) + F(n-2)

Page 7: Dynamic Programming David Kauchak cs302 Spring 2013

Running time

Each call creates two recursive calls

Each call reduces the size of the problem by 1 or 2

Creates a full binary of depth n

O(2n)

Page 8: Dynamic Programming David Kauchak cs302 Spring 2013

Can we do better?Fib(n)

Fib(n-1) Fib(n-2)

Fib(n-2) Fib(n-3)

Fib(n-3) Fib(n-4) Fib(n-4) Fib(n-5)

Fib(n-3) Fib(n-4)

Fib(n-4) Fib(n-5) Fib(n-5) Fib(n-6)

Page 9: Dynamic Programming David Kauchak cs302 Spring 2013

A lot of repeated work!Fib(n)

Fib(n-1) Fib(n-2)

Fib(n-2) Fib(n-3)

Fib(n-3) Fib(n-4) Fib(n-4) Fib(n-5)

Fib(n-3) Fib(n-4)

Fib(n-4) Fib(n-5) Fib(n-5) Fib(n-6)

Page 10: Dynamic Programming David Kauchak cs302 Spring 2013

Identifying a dynamic programming problem

The solution can be defined with respect to solutions to subproblems

The subproblems created are overlapping, that is we see the same subproblems repeated

Page 11: Dynamic Programming David Kauchak cs302 Spring 2013

Overlapping sub-problems

divide andconquer

dynamic programming

Page 12: Dynamic Programming David Kauchak cs302 Spring 2013

Creating a dynamic programming solution

Step 1: Identify a solution to the problem with respect to smaller subproblems (pretend like you have a solver, but it only works on smaller problems):

F(n) = F(n-1) + F(n-2)

Step 2: bottom up - start with solutions to the smallest problems and build solutions to the larger problems

use an array to store solutions to subproblems

Page 13: Dynamic Programming David Kauchak cs302 Spring 2013

Is it correct?

F(n) = F(n-1) + F(n-2)

Page 14: Dynamic Programming David Kauchak cs302 Spring 2013

Running time?

Θ(n)

Page 15: Dynamic Programming David Kauchak cs302 Spring 2013

Counting binary search trees

How many unique binary search trees can be created using the numbers 1 through n?

4

2

1 3 6

5

Page 16: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: What is the subproblem?Assume we have some black box solver (call it T) that can give us the answer to smaller subproblems

How can we use the answer from this to answer our question?

How many options for the root are there?

1 2 3 n

Page 17: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

How many trees have i as the root?

Page 18: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

1, 2, …, i-1 i+1, i+2, …, n

?

Page 19: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

1, 2, …, i-1 i+1, i+2, …, n

T(i-1)

subproblem of size i-1

?

Page 20: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

1, 2, …, i-1 i+1, i+2, …, n

T(i-1) Number of trees for i+1, i+2, …, i+n is the same as the number of trees from 1, 2, …, n-i

Page 21: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

1, 2, …, i-1 i+1, i+2, …, n

T(i-1) T(n-i)

Given solutions for T(i-1) and T(n-i) how many trees are there with i as the root?

Page 22: Dynamic Programming David Kauchak cs302 Spring 2013

Subproblems i

1, 2, …, i-1 i+1, i+2, …, n

T(i-1) T(n-i)

T(i) = T(i-1) * T(n-i)

Page 23: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: define the answer with respect to subproblems

T(i) = T(i-1) * T(n-i)

n

iinTiTnT

1)(*)1()(

Page 24: Dynamic Programming David Kauchak cs302 Spring 2013

Is there a problem?

As with Fibonacci, we’re repeating a lot of work

Page 25: Dynamic Programming David Kauchak cs302 Spring 2013

Step 2: Generate a solution from the bottom-up

Page 26: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

Page 27: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1

Page 28: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1

c[0]*c[1] + c[1]*c[0]

Page 29: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1

2

1

1

2

c[0]*c[1] + c[1]*c[0]

Page 30: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1 2

Page 31: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1 2

c[0]*c[2] + c[1]*c[1] + c[2]*c[0]

1 2 3

Page 32: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1 2 5

Page 33: Dynamic Programming David Kauchak cs302 Spring 2013

0 1 2 3 4 5 … n

1 1 2 5 …

Page 34: Dynamic Programming David Kauchak cs302 Spring 2013

Running time?

Θ(n2)

Page 35: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

ABA?

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

Page 36: Dynamic Programming David Kauchak cs302 Spring 2013

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 37: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

ACA?

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

Page 38: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

ACA

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

Page 39: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

DCA?

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

Page 40: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

DCA

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

Page 41: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

AADAA?

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

Page 42: Dynamic Programming David Kauchak cs302 Spring 2013

Longest common subsequence (LCS)

X = A B A C D A B A B

AADAA

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

Page 43: Dynamic Programming David Kauchak cs302 Spring 2013

LCS problem

Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y

Given two sequences X = x1, x2, …, xn and Y = y1, y2, …, yn,

What is the longest common subsequence?

X = A B C B D A B

Y = B D C A B A

Page 44: Dynamic Programming David Kauchak cs302 Spring 2013

LCS problem

Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y

Given two sequences X = x1, x2, …, xn and Y = y1, y2, …, yn,

What is the longest common subsequence?

X = A B C B D A B

Y = B D C A B A

Page 45: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A B

Y = B D C A B A

Assume you have a solver for smaller problems

Page 46: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A ?

Y = B D C A B ?

Is the last character part of the LCS?

Page 47: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A ?

Y = B D C A B ?

Two cases: either the characters are the same or they’re different

Page 48: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A A

Y = B D C A B A

If they’re the same

The characters are part of the LCS

nmn xYXLCSYXLCS ),(),( 1...11...1

LCS

What is the recursive relationship?

Page 49: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A B

Y = B D C A B A

If they’re different

LCS

),(),( 1...1 YXLCSYXLCS n

Page 50: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A B

Y = B D C A B A

If they’re different

LCS

),(),( 1...1 mYXLCSYXLCS

Page 51: Dynamic Programming David Kauchak cs302 Spring 2013

Step 1: Define the problem with respect to subproblems

X = A B C B D A B

Y = B D C A B A

If they’re different

X = A B C B D A B

Y = B D C A B A

?

Page 52: Dynamic Programming David Kauchak cs302 Spring 2013

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 53: Dynamic Programming David Kauchak cs302 Spring 2013

Step 2: Build the solution from the bottom up

otherwise),(),,(max(

f),(1),(

1...11...1

1...11...1

mn

mnmn

YXLCSYXLCS

yxiYXLCSYXLCS

What types of subproblem

solutions do we need to store?

LCS(X1…j, Y1…k)

two different indices

Page 54: Dynamic Programming David Kauchak cs302 Spring 2013

Step 2: Build the solution from the bottom up

otherwise),(),,(max(

f),(1),(

1...11...1

1...11...1

mn

mnmn

YXLCSYXLCS

yxiYXLCSYXLCS

What types of subproblem

solutions do we need to store?

LCS(X1…j, Y1…k)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 55: Dynamic Programming David Kauchak cs302 Spring 2013

0 xi

1 A2 B3 C4 B5 D6 A7 B

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

j

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

For Fibonacci and tree counting, we had to initialize some entries in the array. Any here?

Page 56: Dynamic Programming David Kauchak cs302 Spring 2013

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 00000000

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Need to initialize values within 1 smaller in either dimension.

Page 57: Dynamic Programming David Kauchak cs302 Spring 2013

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 ?000000

LCS(A, B)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 58: Dynamic Programming David Kauchak cs302 Spring 2013

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 0000000

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 59: Dynamic Programming David Kauchak cs302 Spring 2013

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 ?000000

LCS(A, BDCA)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 60: Dynamic Programming David Kauchak cs302 Spring 2013

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 1000000

LCS(A, BDCA)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 61: Dynamic Programming David Kauchak cs302 Spring 2013

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 ?000

LCS(ABCB, BDCAB)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 62: Dynamic Programming David Kauchak cs302 Spring 2013

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 3000

LCS(ABCB, BDCAB)

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

f]1,1[1],[

jiLCSjiLCS

yxijiLCSjiLCS ji

Page 63: Dynamic Programming David Kauchak cs302 Spring 2013

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

Where’s the final answer?

Page 64: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Page 65: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Base case initialization

Page 66: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Fill in the matrix

Page 67: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Page 68: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Page 69: Dynamic Programming David Kauchak cs302 Spring 2013

The algorithm

Page 70: Dynamic Programming David Kauchak cs302 Spring 2013

Running time?

Θ(nm)

Page 71: Dynamic Programming David Kauchak cs302 Spring 2013

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 72: Dynamic Programming David Kauchak cs302 Spring 2013

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 73: Dynamic Programming David Kauchak cs302 Spring 2013

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