1 dynamic programming 2012/11/20. p.2 dynamic programming (dp) dynamic programming dynamic...

43
1 Dynamic Programming 2012/11/20

Upload: shannon-wilcox

Post on 23-Dec-2015

287 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

1

Dynamic Programming

2012/11/20

Page 2: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.2

Dynamic Programming (DP) Dynamic programming is

typically applied to optimization problems.

Problems that can be solved by dynamic programming satisfy the principle of optimality.

Page 3: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

3

Principle of optimality Suppose that in solving a problem, we

have to make a sequence of decisions D1, D2, …, Dn-1, Dn

If this sequence of decisions D1, D2, …, Dn-

1, Dn is optimal, then the last k, 1 k n, decisions must be optimal under the condition caused by the first n-k decisions.

Page 4: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

4

Dynamic method v.s.Greedy method

Comparison: In the greedy method, any decision is locally optimal.

These locally optimal solutions will finally add up to be a globally optimal solution.

Page 5: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

5

The Greedy Method E.g. Find a shortest path from v0 to v3.

The greedy method can solve this problem. The shortest path: 1 + 2 + 4 = 7.

Page 6: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

6

The Greedy Method E.g. Find a shortest path from v0 to v3 in the multi-

stage graph.

Greedy method: v0v1,2v2,1v3 = 23 Optimal: v0v1,1v2,2v3 = 7 The greedy method does not work for this

problem. This is because decisions at different stages

influence one another.

Page 7: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

7

Multistage graph A multistage graph G=(V,E) is a directed

graph in which the vertices are partitioned into k2 disjoint sets Vi, 1i k

In addition, if <u,v> is an edge in E then uVi and vVi+i for some i, 1i<k

The set V1 and Vk are such that V1 =Vk=1

The multistage graph problem is to find a minimum cost path from s in V1 to t in Vk

Each set Vi defines a stage in the graph

Page 8: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

8

Greedy Method vs. Multistage graph E.g.

The greedy method cannot be applied to this case: S A D T 1+4+18 = 23.

The shortest path is: S C F T 5+2+2 = 9.

S T132

B E

9

A D4

C F2

1

5

11

5

16

18

2

Page 9: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

9

Dynamic Programming Dynamic programming approach:

d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}

S T2

B

A

C

1

5d(C, T)

d(B, T)

d(A, T)

Page 10: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

10

Dynamic Programming

d(A, T) = min{4+d(D, T), 11+d(E, T)}

= min{4+18, 11+13} = 22.

A

T

4

E

D

11d(E, T)

d(D, T)S T

132B E

9

A D4

C F2

1

5

11

5

16

18

2

Page 11: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

11

Dynamic Programming d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F,

T)} = min{9+18, 5+13, 16+2} = 18. d(C, T) = min{ 2+d(F, T) } = 2+2 = 4 d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C,

T)} = min{1+22, 2+18, 5+4} = 9.

B T5

E

D

F

9

16d(F, T)

d(E, T)

d(D, T)

S T132

B E

9

A D4

C F2

1

5

11

5

16

18

2

Page 12: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

12

Save computation For example, we never calculate (as a

whole) the length of the path

S B D T( namely, d(S,B)+d(B,D)+d(D,T) ) because we have found d(B, E)+d(E, T)<d(B,D)+d(D,T)

There are some more examples… Compare with the brute-force method…

S T132

B E

9

A D4

C F2

1

5

11

5

16

18

2

Page 13: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

13

The advantages of dynamic programming approach

To avoid exhaustively searching the entire solution space (to eliminate some impossible solutions and save computation).

To solve the problem stage by stage systematically.

To store intermediate solutions in a table (array) so that they can be retrieved from the table in later stages of computation.

Page 14: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Comment If a problem can be described by a

multistage graph then it can be solved by dynamic programming.

14

Page 15: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

15

The longest common subsequence (LCS or LCSS) problem

A sequence of symbols A = b a c a d A subsequence of A: deleting 0 or more

symbols (not necessarily consecutive) from A.

E.g., ad, ac, bac, acad, bacad, bcd. Common subsequences of A = b a c a d and B = a c c b a d c b : ad, ac, bac, acad. The longest common subsequence of A and

B: a c a d.

Page 16: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.16

DNA Matching

DNA = {A|C|G|T}*S1=ACCGGTCGAGTGCGGCCGAAGCCGGC

CGAAS2=GTCGTTCGGAATGCCGTTGCTGTAAA

Are S1 and S2 similar DNAs?

The question can be answered by figuring out the longest common subsequence.

Page 17: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Networked virtual environments (NVEs)

virtual worlds full of numerous virtual objects to simulate a variety of real world scenes

allowing multiple geographically distributed users to assume avatars to concurrently interact with each other via network connections.

E.G., MMOGs: World of Warcraft (WoW), Second Life (SL)

Page 18: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Avatar Path Clustering Because of similar personalities, interests, or

habits, users may possess similar behavior patterns, which in turn lead to similar avatar paths within the virtual world.

We would like to group similar avatar paths as a cluster and find a representative path (RP) for them.

Page 19: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

How similar are two paths in Freebies island of Second Life?

19

Page 20: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

SeqA:C60.C61.C62.C63.C55.C47.C39.C31.C32

LCSS-DC - path transfers sequence

20

Page 21: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

SeqA :C60.C61.C62.C63.C55.C47.C39.C31.C32SeqB :C60.C61.C62.C54.C62.C63.C64

LCSSAB :C60.C61.C62. C63

LCSS-DC - similar path thresholds

21

Page 22: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.22

Longest-common-subsequence problem:

We are given two sequences X = <x1,x2,...,xm> and Y = <y1,y2,...,yn> and wish to find a maximum length common subsequence of X and Y.

We define Xi = < x1,x2,...,xi > and Yj = <y1,y2,...,yj>.

Page 23: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Brute Force Solution

m * 2n = O(2n ) or n * 2m = O(2m)

23

Page 24: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.24

A recursive solution to subproblem

Define c [i, j] is the length of the LCS of Xi and Yj .

ïî

ïí

ì

¹--+--=

ji

ji

yxi,j>jicjic

=yx i,j>jic

j=i=

jic

and 0 if]},1[],1,[max{

and 0if1]1,1[

0or 0 if0

],[

Page 25: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.25

Computing the length of an LCS

LCS_LENGTH(X,Y)1 m length[X]

2 n length[Y]

3 for i 1 to m

4 do c[i, 0] 05 for j 1 to n

6 do c[0, j] 0

Page 26: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.26

7 for i 1 to m8 for j 1 to n9 if xi = yj

10 then c[i, j] c[i-1, j-1]+111 b[i, j] “”12 else if c[i–1, j] c[i, j-1]13 then c[i, j] c[i-1, j]14 b[i, j] “”15 else c[i, j] c[i, j-1]16 b[i, j] “”17 return c and b

Page 27: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.27

Complexity: O(mn) rather than O(2m) or O(2n) of Brute force method

Page 28: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

P.28

PRINT_LCS PRINT_LCS(b, X, i, j )1 if i = 0 or j = 02 then return3 if b[i, j] = “”4 then PRINT_LCS(b, X, i-1, j-1)

5 print xi

6 else if b[i, j] = “”7 then PRINT_LCS(b, X, i-1, j)8 else PRINT_LCS(b, X, i, j-1)

Complexity: O(m+n)

By calling PRINT_LCS(b, X, length[X], length[Y]) to print LCS

Page 29: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.29

Matrix-chain multiplication How to compute where

is a matrix for every i. Example:

A A An1 2 ... Ai

A A A A1 2 3 4

( ( ( ))) ( (( ) ))

(( )( )) (( ( )) )

((( ) ) )

A A A A A A A A

A A A A A A A A

A A A A

1 2 3 4 1 2 3 4

1 2 3 4 1 2 3 4

1 2 3 4

Page 30: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.30

MATRIX MULTIPLY MATRIX MULTIPLY(A,B)1 if columns[A] rows[B]2 then error “incompatible dimensions”

3 else for to rows[A]4 for to columns[B]5 6 for to columns[A]7

8 return C

i 1j 1

0],[ jiCk 1

],[],[],[],[ jkBkiAjiCjiC

Page 31: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.31

Complexity:

Let A be a matrix, and B be

a

matrix. Then the complexity

of

A xB is .

p q

q r

p q r

Page 32: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.32

Example: is a matrix, is a

matrix, and is a matrix. Then

takes time. However takes

time.

A1 10 100 A2 100 5A3 5 50

(( ) )A A A1 2 3 10 100 5 10 5 50 7500 ( ( ))A A A1 2 3

100 5 50 10 100 50 75000

Page 33: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.33

The matrix-chain multiplication problem:

Given a chain of n matrices, where for i=0,1,…,n, matrix Ai has dimension pi-1pi, fully parenthesize the product in a way that minimizes the number of scalar multiplications.

A product of matrices is fully parenthesized if it is either a single matrix, or a product of two fully parenthesized matrix product, surrounded by parentheses.

nAAA ,...,, 21

A A An1 2 ...

Page 34: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.34

Counting the number of parenthesizations:

[Catalan number]

P n

if n

P k p n k if nk

n( )( ) ( )

1 1

21

1

P n C n( ) ( ) 1

11

2 43 2n

n

n n

n( )

/

Page 35: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.35

Step 1: The structure of an optimal parenthesization

(( ... )( ... ))A A A A A Ak k k n1 2 1 2

Optimal

Combine

Page 36: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.36

Step 2: A recursive solution Define m[i, j]= minimum number

of scalar multiplications needed to compute the matrix

goal m[1, n]

A A A Ai j i i j.. .. 1

m i j[ , ]

jipppjkmkim

ji

jkijki }],1[],[{min

0

1

Page 37: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.37

Step 3: Computing the optimal costs Instead of computing the solution

to the recurrence recursively, we compute the optimal cost by using a tabular, bottom-up approach.

The procedure uses an auxiliary table m[1..n, 1..n] for storing the m[i, j] costs and an auxiliary table s[1..n, 1..n] that records which index of k achieved the optimal cost in computing m[i, j].

Page 38: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.38

MATRIX_CHAIN_ORDER MATRIX_CHAIN_ORDER(p)1 n length[p] –1 2 for i 1 to n3 do m[i, i] 04 for l 2 to n5 do for i 1 to n – l + 16 do j i + l – 1 7 m[i, j] 8 for k i to j – 1 9 do q m[i, k] + m[k+1, j]+ pi-1pkpj

10 if q < m[i, j]11 then m[i, j] q12 s[i, j] k13 return m and s

Complexity: O n( )3

Page 39: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.39

Example:

656

545

434

323

212

101

2520

2010

105

515

1535

3530

ppA

ppA

ppA

ppA

ppA

ppA

Page 40: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.40

the m and s table computed by MATRIX-CHAIN-ORDER for n=6

Page 41: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.41

m[2,5]=min{m[2,2]+m[3,5]+p1p2p5=0+2500+351520=130

00,m[2,3]+m[4,5]+p1p3p5=2625+1000+35520=7

125,m[2,4]+m[5,5]+p1p4p5=4375+0+351020=113

74}=7125

Page 42: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Chapter 15P.42

MATRIX_CHAIN_MULTIPLY

PRINT_OPTIMAL_PARENS(s, i, j)1 if i=j2 then print “A”i3 else print “(“4 PRINT_OPTIMAL_PARENS(s, i, s[i,j])5 PRINT_OPTIMAL_PARENS(s, s[i,j]+1,

j)6 print “)” Example:

(( ( ))(( ) ))A A A A A A1 2 3 4 5 6

Page 43: 1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems

Q&A43