ch15 dynamic programming

58
Ch15 Dynamic Programming 1 Algorithms Ch. 15: Dynamic Programming Ming-Te Chi

Upload: kumar-siva

Post on 18-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Dynamic Programming

TRANSCRIPT

Page 1: Ch15 Dynamic Programming

Ch15 Dynamic Programming 1

Algorithms

Ch. 15: Dynamic Programming

Ming-Te Chi

Page 2: Ch15 Dynamic Programming

Dynamic programmingDynamic programming is typically applied to optimization problems. In such problem there can be many solutions. Each solution has a value, and we wish to find a solution with the optimal value.

Ch. 15 Dynamic Programming

Page 3: Ch15 Dynamic Programming

The development of a dynamic programming

1. Characterize the structure of an optimal solution.

2. Recursively define the value of an optimal solution.

3. Compute the value of an optimal solution in a bottom up fashion.

4. Construct an optimal solution from computed information.

Ch. 15 Dynamic Programming

Page 4: Ch15 Dynamic Programming

15.1 Rod cutting

Input: A length n and table of prices pi , for i = 1, 2, …, n.

Output: The maximum revenue obtainable for rods whose lengths sum to n, computed as the sum of the prices for the individual rods.

Ch. 15 Dynamic Programming

Page 5: Ch15 Dynamic Programming

Ex: a rod of length 4

Ch. 15 Dynamic Programming

Page 6: Ch15 Dynamic Programming

Recursive top-down solution

Ch. 15 Dynamic Programming

Page 7: Ch15 Dynamic Programming

Ch. 15 Dynamic Programming

Page 8: Ch15 Dynamic Programming

Dynamic-programming solution

Store, don’t recompute time-memory trade-off

Turn a exponential-time solution into a polynomial-time solution

Ch. 15 Dynamic Programming

Page 9: Ch15 Dynamic Programming

Top-down with memoization

Ch. 15 Dynamic Programming

Page 10: Ch15 Dynamic Programming

Bottom-up

Ch. 15 Dynamic Programming

Page 11: Ch15 Dynamic Programming

Ch. 15 Dynamic Programming

Page 12: Ch15 Dynamic Programming

Subproblem graphs For rod-cutting problem with n = 4

Directed graph One vertex for each distinct subproblem. Has a directed edge (x, y). if computing an optimal

solution to subproblem x directly requires knowing an optimal solution to subproblem y.

Ch. 15 Dynamic Programming

Page 13: Ch15 Dynamic Programming

Reconstructing a solution

Ch. 15 Dynamic Programming

Page 14: Ch15 Dynamic Programming

Ch. 15 Dynamic Programming

Page 15: Ch15 Dynamic Programming

15.2 Matrix-chain multiplication

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.

Ch. 15 Dynamic Programming

Page 16: Ch15 Dynamic Programming

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

Ch. 15 Dynamic Programming

Page 17: Ch15 Dynamic Programming

MATRIX MULTIPLY

Ch. 15 Dynamic Programming

Page 18: Ch15 Dynamic Programming

Complexity:

Ch. 15 Dynamic Programming

Page 19: Ch15 Dynamic Programming

Example:

Ch. 15 Dynamic Programming

Page 20: Ch15 Dynamic Programming

The matrix-chain multiplication problem:

nAAA ,...,, 21

A A An1 2 ...

Ch. 15 Dynamic Programming

Page 21: Ch15 Dynamic Programming

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

)4

(2

1

12/3nn

n

n

n

Ch. 15 Dynamic Programming

Page 22: Ch15 Dynamic Programming

Step 1: The structure of an optimal parenthesization

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

Optimal

Combine

Ch. 15 Dynamic Programming

Page 23: Ch15 Dynamic Programming

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

Ch. 15 Dynamic Programming

Page 24: Ch15 Dynamic Programming

Step 3: Computing the optimal costs

Complexity: O n( )3

Ch. 15 Dynamic Programming

Page 25: Ch15 Dynamic Programming

Example:

656

545

434

323

212

101

2520

2010

105

515

1535

3530

ppA

ppA

ppA

ppA

ppA

ppA

Ch. 15 Dynamic Programming

Page 26: Ch15 Dynamic Programming

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

Ch. 15 Dynamic Programming

30 35 15p 5 10 20 25

Page 27: Ch15 Dynamic Programming

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

Ch. 15 Dynamic Programming

Page 28: Ch15 Dynamic Programming

Step 4: Constructing an optimal solution

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

Ch. 15 Dynamic Programming

Page 29: Ch15 Dynamic Programming

16.3 Elements of dynamic programming

Optimal substructure

Overlapping subproblems

How memoization might help

Page 30: Ch15 Dynamic Programming

Optimal substructure:

We say that a problem exhibits optimal substructure if an optimal solution to the problem contains within its optimal solution to subproblems.

Example: Matrix-multiplication problem

Ch. 15 Dynamic Programming

Page 31: Ch15 Dynamic Programming

Common pattern 1. You show that a solution to the problem consists

of making a choice. Making this choice leaves one or more subproblems to be solved.

2. You suppose that for a given problem, you are given the choice that leads to an optimal solution.

3. Given this choice, you determine which subproblems ensue and how to best characterize the resulting space of subproblems.

4. You show that the solutions to the subproblems used within the optimal solution to the problem must themselves be optimal by using a “cut-and-paste” technique.

Ch. 15 Dynamic Programming

Page 32: Ch15 Dynamic Programming

Optimal substructure1. How many subproblems are used in an

optimal solution to the original problem

2. How many choices we have in determining which subproblem(s) to use in an optimal solution.

Ch. 15 Dynamic Programming

Page 33: Ch15 Dynamic Programming

Subtleties One should be careful not to assume that

optimal substructure applies when it does not. consider the following two problems in which we are given a directed graph G = (V, E) and vertices u, v V. Unweighted shortest path:

Find a path from u to v consisting of the fewest edges. Good for Dynamic programming.

Unweighted longest simple path: Find a simple path from u to v consisting of the

most edges. Not good for Dynamic programming.

Ch. 15 Dynamic Programming

Page 34: Ch15 Dynamic Programming

Overlapping subproblems

example: MAXTRIX_CHAIN_ORDER

Page 35: Ch15 Dynamic Programming

RECURSIVE_MATRIX_CHAIN

Ch. 15 Dynamic Programming

Page 36: Ch15 Dynamic Programming

The recursion tree for the computation of RECURSUVE-MATRIX-CHAIN(P, 1, 4)

Ch. 15 Dynamic Programming

Page 37: Ch15 Dynamic Programming

We can prove that T(n) =(2n) using substitution method.

1

11)1)()((1)(

1)1(n

knforknTkTnT

T

T n T i ni

n( ) ( )

2

1

1

Ch. 15 Dynamic Programming

Page 38: Ch15 Dynamic Programming

Solution: 1. bottom up2. memorization (memorize the natural,

but inefficient)

11

2

0

1

1

1

0

2)22()12(2

2222)(

21)1(

nnn

n

i

in

i

i

nn

nnnT

T

Ch. 15 Dynamic Programming

Page 39: Ch15 Dynamic Programming

MEMORIZED_MATRIX_CHAIN

Ch. 15 Dynamic Programming

Page 40: Ch15 Dynamic Programming

LOOKUP_CHAIN

Time Complexity: )( 3nOCh. 15 Dynamic Programming

Page 41: Ch15 Dynamic Programming

16.4 Longest Common Subsequence

X = < A, B, C, B, D, A, B >Y = < B, D, C, A, B, A >

Ch. 15 Dynamic Programming

• < B, C, A > is a common subsequence of both X and Y.

• < B, C, B, A > or < B, C, A, B > is the longest common subsequence of X and Y.

Page 42: Ch15 Dynamic Programming

DNA

Ch. 15 Dynamic Programming

Page 43: Ch15 Dynamic Programming

Longest-common-subsequence problem:

We are given two sequences X = <x1, x2, ..., xm> Y = <y1, y2, ..., yn>

Find a maximum length common subsequence of X and Y.

We define ith prefix of X, Xi = < x1, x2, ..., xi >.

Ch. 15 Dynamic Programming

Page 44: Ch15 Dynamic Programming

Theorem 16.1. (Optimal substructure of LCS)

Let X = <x1, x2, ..., xm> and Y = <y1, y2, ..., yn> be the sequences, and let Z = <z1, z2, ..., zk> be any LCS of X and Y.

1. If xm = yn

then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-

1.

2. If xm yn

then zk xm implies Z is an LCS of Xm-1 and Y.

3. If xm yn

then zk yn implies Z is an LCS of X and Yn-1.Ch. 15 Dynamic Programming

Page 45: Ch15 Dynamic Programming

A recursive solution to subproblem

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

Ch. 15 Dynamic Programming

Page 46: Ch15 Dynamic Programming

Computing the length of an LCS

Ch. 15 Dynamic Programming

Page 47: Ch15 Dynamic Programming

Ch. 15 Dynamic Programming

Page 48: Ch15 Dynamic Programming

Complexity: O(mn) Ch. 15 Dynamic Programming

Page 49: Ch15 Dynamic Programming

Complexity: O(Complexity: O(m+nm+n))

Ch. 15 Dynamic Programming

Page 50: Ch15 Dynamic Programming

15.5 Optimal Binary search trees

cost:2.80 cost:2.75optimal!!

Ch. 15 Dynamic Programming

Page 51: Ch15 Dynamic Programming

Expected cost

the expected cost of a search in T is

n

i

n

iii qp

1 0

1

n

ii

n

iiTiiT

i

n

i

n

iiTiiT

qdpk

qdpk

1 0

1 0

)(depth)(depth1

)1)(depth()1)(depth(T]in cost E[search

Ch. 15 Dynamic Programming

Page 52: Ch15 Dynamic Programming

For a given set of probabilities, our goal is to construct a binary search tree whose expected search is smallest. We call such a tree an optimal binary search tree.

Ch. 15 Dynamic Programming

Page 53: Ch15 Dynamic Programming

Step 1: The structure of an optimal binary search tree

Consider any subtree of a binary search tree. It must contain keys in a contiguous range ki, ...,kj, for some 1 i j n. In addition, a subtree that contains keys ki, ..., kj must also have as its leaves the dummy keys di-1, ..., dj.

If an optimal binary search tree T has a subtree T' containing keys ki, ..., kj, then this subtree T' must be optimal as well for the subproblem with keys ki, ..., kj and dummy keys di-1, ..., dj.

Ch. 15 Dynamic Programming

Page 54: Ch15 Dynamic Programming

Step 2: A recursive solution

Ch. 15 Dynamic Programming

Page 55: Ch15 Dynamic Programming

Step 3:computing the expected search cost of an optimal binary search tree

Ch. 15 Dynamic Programming

Page 56: Ch15 Dynamic Programming

the OPTIMAL-BST procedure takes (n3), just like MATRIX-CHAIN-ORDER

Ch. 15 Dynamic Programming

Page 57: Ch15 Dynamic Programming

The table e[i,j], w[i,j], and root[i,j] computer by OPTIMAL-BST on the key distribution.

Ch. 15 Dynamic Programming

Page 58: Ch15 Dynamic Programming

Knuth has shown that there are always roots of optimal subtrees such that root[i, j –1] root[i+1, j] for all 1 i j n. We can use this fact to modify Optimal-BST procedure to run in (n2) time.

Ch. 15 Dynamic Programming