Lecture 8: Dynamic Programming

Post on 05-Jan-2016

18 views 1 download

Tags:

description

Lecture 8: Dynamic Programming. Shang-Hua Teng. First Example: n choose k. Many combinatorial problems require the calculation of the binomial coefficient. This is equivalent to given n objects how many different ways can you choose k of them. Mathematically we have or,. - PowerPoint PPT Presentation

transcript

Lecture 8:Dynamic Programming

Shang-Hua Teng

First Example: n choose k

• Many combinatorial problems require the calculation of the binomial coefficient.

• This is equivalent to given n objects how many different ways can you choose k of them.

• Mathematically we have

• or,

! !

!

knk

n

k

n

nkk

nk

k

n

k

n

k

n

0

0

1

1

1

1

int choose(n,k) if (n = = k) return 1;if (n < k) or (k < 0) return 0;return choose(n-1, k-1) + choose(n - k, k);

Consider Divide and Conquer

How long does this take?

T(n,k) = T(n-1, k-1) + T(n-k, k) )( kn

Remember Pascal’s Trianglefunction choose (n,k)

1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1

By adding two numbers from previous row we can calculate the triangle quickly.

Consider triangle shape

11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1

012345

0 1 2 3 4 5

• Looks like a two dimensional array.

• Create the triangle in this array and output A(i,j) for

j

i

Do it algorithmically

Create an 2-d array A of size (n+1) by (k +1 ).

Set A[i, 0] 1 for i = 0,1,2,.., nSet A[i, i ] 1 for i = 0,1,2,...., n

for i = 1 to nfor j = 1 to k

A[ i, j ] = A[ i-1, j ] + A[ i –1, j-1 ];

Output A[n,k]

Runtime is O(nk)

Matrix Chain Multiplication - Review

• Recall how to multiply matrices.

• Given two matrices

• The product is

• Time is

AB

CX =

)( edA )( feB

d

e

e

f

1

0

],[],[],[e

k

jkkiji BAC

d

f

)(defO

Matrix Chaining contd.• What about multiplying multiple matrices?

• We know matrix multiplication is associative. Can we use this to help minimize calculations?

• Sure, consider

• If we multiply we do 4000 operations

• But instead if we try we do 1575 operations

54321 AAAAAA

)35( A )1003( B )5100( C

CBA

)( CBA

How do we parenthesize

• Brute force - try all ways to parenthesize

• Find out which has smallest number of operations by doing multiplication, then pick the best one.

• But how many ways can we parenthesize these?

• Equivalent to the number of ways to make a binary tree with n nodes. This is see Catalan numbers.

• See homework Problem 12-4, ex15.2-3 in the book.

nAAAAA 321

)2( n

Be greedy

• We just learned that a greedy algorithm can sometimes work, let’s try.

• Why not try starting with the product with the most operations.

• Consider

• If we do our greedy method we would compute • This is 2000 operations.• Try instead, which takes 1000

operations.

)105()510()105()510( DCBA

DCBA

DCBA

Another Greedy way

• Select the product with the fewest operations.

• Consider

• Here the new greedy method would compute

• This is 109989+9900+108900=228789 operations

• Try

• Here we only need 9999+89991+89100=189090

)99100()1009()911()11101( DCBA

DCBA

DCBA

Optimality of Subproblems

• Consider general question again, we want to parenthesize

• Now suppose somehow we new that the last multiplication we need to do was at the ith position.

• Then the problem is the same as knowing the best way to parenthesize and

nAAAA 321

)()( 121 nii AAAAA

)( 21 iAAA )( 1 ni AA

Overlapping Subproblems

• We know how to break problem into smaller pieces, why doesn’t divide and conquer work?

• We need to optimize each piece why doesn’t greedy work.

• Again suppose we know where the final multiply is and we want to generalize the cost with a recurrence, consider

• But notice these subproblems overlap.

11,1,, min jkijkki

jkiji dddNNN

Dynamic Programming

• Since subproblems overlap we can not use divide and conquer method.

• Instead work from the “bottom up.”

• We know how to parenthesize one matrix, two matrices, we can build from there…

Matrix Chain Order

sm

kjis

qjim

jimq

pppjkmkimq

jik

jim

lij

lni

nl

iim

ni

plengthn

jki

and return

],[

].[ then

],[ if

],1[],[ do

1 tofor

],[

1 do

1 to1for do

to2for

0, do

to1for

1

1

What have we noticed

• Optimal solution depends on optimal subproblems

• Subproblems overlap.

• So a “top down” divide and conquer doesn’t seem to work.

• Somehow we need to build up from bottom by remembering solution from subproblem.

Another Example

• Biologists need to measure how similar strands of DNA are to determine how closely related an organism is to another.

• They do this by considering DNA as strings of letters A,C,G,T and then comparing similarities in the strings.

• Formally they look at common subsequences in the strings.

• Example X = AGTCAACGTT, Y=GTTCGACTGTG

• Both S = AGTG and S’=GTCACGT are subsequences

• How to do find these efficiently?

Brute Force

• if |X| = m, |Y| = n, then there are 2m subsequences of x; we must compare each with Y (n comparisons)

• So the running time of the brute-force algorithm is O(n 2m)

• Notice that the LCS problem has optimal substructure: solutions of subproblems are parts of the final solution.

• Subproblems: “find LCS of pairs of prefixes of X and Y”

111 and of LCSan is and then , If nmknmknm YXZyxzyx

1 and of LCSan is that implies

then , If

n

nknm

YXZ

yzyx

Some observations

. and

of LCSany be ,,,let and sequences

be ,,, and ,,,Let

21

2121

YX

zzzZ

yyyYxxxX

k

nm

YXZ

xzyx

m

mknm

and of LCSan is that implies

then , If

1

Setup

• First we’ll find the length of LCS, along the way we will leave “clues” on finding subsequence.

• Define Xi, Yj to be the prefixes of X and Y of length i and j respectively.

• Define c[i,j] to be the length of LCS of Xi and Yj

• Then the length of LCS of X and Y will be c[m,n]

The recurrence

• The subproblems overlap, to find LCS we need to find LCS of c[i, j-1] and of c[i-1, j]

ji

ji

yxjijicjic

yxjijic

ji

jic

and 0, if ]),1[],1,[max(

, and 0, if 1]1,1[

0or 0 if 0

],[

LCS-Length(X, Y)m = length(X), n = length(Y)for i = 1 to m do c[i, 0] = 0 for j = 0 to n do c[0, j] = 0 for i = 1 to m do for j = 1 to n

do if ( xi = = yj ) then c[i, j] = c[i - 1, j - 1] + 1

else if c[i - 1, j]>=c[i, j - 1] then c[i, j] = c[i - 1, j]

else c[i, j] = c[i, j - 1] return c and b

"" , jib

"" , jib

"" , jib