• 沒有找到結果。

# Dynamic Programming

N/A
N/A
Protected

Share "Dynamic Programming"

Copied!
60
0
0

(1)

## Chapter 15

### Dynamic Programming

Lee, Hsiu-Hui

Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as

(2)

## Introduction

(3)

### The development of a dynamic programming algorithm can be broken into a sequence of four steps:

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.

(4)

(5)

### to find the fast way through a factory

a : the assembly time required at station S

(6)
(7)

An instance of the assembly-line problem with costs

(8)

### fastest way through the factory

(9)

Optimal substructure

An optimal solution to a problem (fastest way through S1, j ) contains within it an optimal solution to subproblems.

(fastest way through S1, j−1 or S 2, j−1).

Use optimal substructure to construct optimal solution to problem from optimal solutions to subproblems.

To solve problems of finding a fastest way through S1, j and S2, j , solve subproblems of finding a fastest way through S and S

(10)

1 1 2 2

*

### f = + +

2 if

, 1 if

2 if

, 1 if

) ]

1 [

, ]

1 [

] min(

[

) ]

1 [

, ]

1 [

] min(

[

, 2 1

, 1 1

, 2 2

1 , 2 2

2

, 1 1

, 2 2

, 1 1

1 , 1 1

1

=

=

⎪⎩

⎪⎨

+ +

− +

= +

⎪⎩

⎪⎨

+ +

− +

= +

j j j j

a t

j f a

j f a j e

f

a t

j f a

j f a j e

f

j j

j

j j

j

fi[j]: the fastest possible time to get a chassis from the starting point through station Si, j

f*: the fastest time to get a chassis all the way through the factory.

(11)

li [ j ] = line # (1 or 2) whose station j − 1 is used in fastest way through Si, j .

• Sli [ j ], j−1 precedes Si, j .

• Defined for i = 1,2 and j = 2, . . . , n.

• l = line # whose station n is used.

(12)

### step 3: computing an optimal solution

Let ri(j)be the number of references made to fi[j] in a recursive algorithm.

r1(n)=r2(n)=1

r1(j) = r2(j)=r1(j+1)+r2(j+1)

The total number of references to all fi[j] values is Θ(2n).

We can do much better if we compute the fi[j] values in different order from the recursive way. Observe that for j ≥ 2, each value of fi[j] depends only on the values of f1[j-1] and f2[j-1].

(13)

### FASTEST-WAYprocedure

FASTEST-WAY(a, t, e, x, n) 1 f1 ← e1 + a1,1

2 f2 ← e2 + a2,1 3 for j ← 2 to n

4 do if f1[j-1] + a1,j ≤ f2[j-1] + t2,j-1 +a1,j 5 then f1[j] ← f1[j-1] + a1,j

6 l1[j] ← 1

7 else f1[j] ← f2[j-1] + t2,j-1 +a1,j

8 l [j] ← 2

(14)

10 then f2[j] ← f2[j – 1] + a2,j

11 l2[j] ← 2

12 else f2[j] ← f1[j – 1] + t1,j-1 + a2,j

13 l2[j] ← 1

14 if f1[n] + x1 ≤ f2[n] + x2 15 then f* = f1[n] + x1

16 l* = 1

17 else f* = f2[n] + x2

18 l* = 2

(15)

### fastest way through the factory

PRINT-STATIONS(l, l*, n) 1 i l*

2 print “line” i “,station” n 3 for j ← n downto 2

4 do i ← li[j]

5 print “line” i “,station” j – 1

output

line 1, station 6 line 2, station 5 line 2, station 4 line 1, station 3 line 2, station 2 line 1, station 1

(16)

(17)

1 2 3 4

1 2 3 4 1 2 3 4

1 2 3 4 1 2 3 4

1 2 3 4

(18)

(19)

(20)

A2

A3

(21)

i

i-1

i

1

2

n

(22)

=

1

1

n

k

3 2

n

/

(23)

(24)

i j..

i i+1

j

<

j k i

j k

i

1

(25)

(26)

## MATRIX_CHAIN_ORDER

MATRIX_CHAIN_ORDER(p) 1 n ← length[p] –1

2 for i ← 1 to n 3 do m[i, i] ← 0 4 for l ← 2 to n

5 do for i ← 1 to n – l + 1

6 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] ← q

12 s[i, j] ← k

13 return m and s

(27)

## Example:

(28)

### MATRIX-CHAIN-ORDER for n=6

(29)

m[2,5]=

min{

m[2,2]+m[3,5]+p1p2p5=0+2500+35×15×20=13000, m[2,3]+m[4,5]+p1p3p5=2625+1000+35×5×20=7125, m[2,4]+m[5,5]+p1p4p5=4375+0+35×10×20=11374 }

=7125

(30)

(31)

i

(32)

### example:

PRINT_OPTIMAL_PARENS(s, 1, 6) Output:

5

4

2

3

6

1

### [2,2][3,3][4,4][5,5]

(( A A A1( 2 3))(( A A4 5 )A6 ))

(33)

(34)

## Optimal substructure

### problem

(35)

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

(36)

(37)

(38)

i-1

k

j

(39)

(40)

n

⎪⎩

>

+

+

+

= 1

1

1 )

1 ) (

) ( ( 1

) (

1 ) 1 (

n

k

n for k

n T k

T n

T

T

T n T i n

i n

( ) ≥ ∑ ( ) +

=

2

1 1

(41)

1 1

2 0 1

1

1 0

=

=

n n

n

n i n i

i

i

(42)

(43)

## LOOKUP_CHAIN

LOOKUP_CHAIN(p, i, j) 1 if m[i, j] < ∞

2 then return m[i, j]

3 if i = j

4 then m[i, j] ← 0

5 else for k ← i to j – 1

6 do q ← LC(p, i, k) +LC(p, k+1, j)+pi-1pkpj 7 if q < m[i, j]

8 then m[i, j] ← q

(44)

(45)

### • Given two sequences

X = <x1,x2,...,xm> Y = <y1,y2,...,yn>

i

### : the ithprefixof X

Xi = < x1,x2,...,xi >

(46)

1

2

m

1

2

n

1

2

k

### > 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.

(47)

i

j

j i

j i

(48)

(49)

i

j

### 15 else c[i, j]← c[i, j-1]

(50)

Complexity: O(mn)

(51)

## PRINT_LCS

i

(52)

### 15.5 Optimal Binary search trees

cost:2.80 cost:2.75

optimal!!

(53)

= =

=

n +

i

n

i

i

i q

p

1 0

1

= =

⋅ +

⋅ +

=

⋅ + +

⋅ +

=

n n

i n

i

n

i

i T i

i T

q d

p k

q d

p k

1 0

) ( depth )

( depth 1

) 1 ) ( depth (

) 1 ) ( depth (

T]

in cost E[search

(54)

(55)

i

j

i

j

i-1

j

i

j

(56)

⎪⎩

⎪⎨

= +

+ +

= −

+

=

=

=

### ∑

. if

, 1 if

)}

, ( ]

, 1 [

] 1 ,

[ { ] min

, [

) , (

1 1

j i

i j j

i w j

r e r

i e

q j

i e

q p

j i w

j r i

i j

i l

l j

i l

l

e[i,j]: the expected cost of searching an optimal binary search tree containing the keys ki, … kj

(57)

i-1

i-1

(58)

### 14 return e and root

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

(59)

PTIMAL

(60)

2

### )time.

 In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second one is called

The execution of a comparison-based algorithm can be described by a comparison tree, and the tree depth is the greatest number of comparisons, i.e., the worst-case

• An algorithm for such a problem whose running time is a polynomial of the input length and the value (not length) of the largest integer parameter is a..

Describe and analyze a recursive algorithm to reconstruct an arbitrary full binary tree, given its preorder and postorder node sequences as

• Goal is to construct a no-arbitrage interest rate tree consistent with the yields and/or yield volatilities of zero-coupon bonds of all maturities.. – This procedure is

Primal-dual approach for the mixed domination problem in trees Although we have presented Algorithm 3 for ﬁnding a minimum mixed dominating set in a tree, it is still desire to

In this section, we consider a solution of the Ricci flow starting from a compact manifold of dimension n 12 with positive isotropic curvature.. Our goal is to establish an analogue

Given a shift κ, if we want to compute the eigenvalue λ of A which is closest to κ, then we need to compute the eigenvalue δ of (11) such that |δ| is the smallest value of all of

 A genre is more dynamic than a text type and is always changing and evolving; however, for our practical purposes here, we can take genre to mean text type. Materials developed

• Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any). • Definition: A max heap is a

Establish the start node of the state graph as the root of the search tree and record its heuristic value.. while (the goal node has not

The min-max and the max-min k-split problem are defined similarly except that the objectives are to minimize the maximum subgraph, and to maximize the minimum subgraph respectively..

Given a graph and a set of p sources, the problem of finding the minimum routing cost spanning tree (MRCT) is NP-hard for any constant p &gt; 1 .. When p = 1, i.e., there is only

Our main goal is to give a much simpler and completely self-contained proof of the decidability of satisfiability of the two-variable logic over data words.. We do it for the case

• Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and t he right subtree2.

- Greedy Best-First Search (or Greedy Search) Minimizing estimated cost from the node to reach a goal Expanding the node that appears to be closest to goal - A* Search.. Minimizing

Such analysis enables valuable applications including social entity search en- gines and substitution recommendation systems. For a social entity search engine, existing work such

To decide the correspondence between different sets of fea- ture points and to consider the binary relationships of point pairs at the same time, we construct a graph for each set

To convert a string containing floating-point digits to its floating-point value, use the static parseDouble method of the Double class..

• We need to make each barrier coincide with a layer of the binomial tree for better convergence.. • The idea is to choose a Δt such

To illustrate how LINDO can be used to solve a preemptive goal programming problem, let’s look at the Priceler example with our original set of priorities (HIM followed by LIP

The dynamic feature points are roughly clustered by the C-means algorithm and then a spatial-temporal shortest spanning tree is proposed to segment each