• 沒有找到結果。

Dynamic Progr amming III

N/A
N/A
Protected

Academic year: 2022

Share "Dynamic Progr amming III"

Copied!
34
0
0

加載中.... (立即查看全文)

全文

(1)

amming III

Michael Tsai 2011/4/1

(2)

複習 : Overlapping Subproblems

 舉個例子 : 連串矩陣問題的遞迴樹

1..3

1..1 2..3 1..2 3..3

2..2 3..3 1..1 2..2

橘色的是 overlap 的部分 !

(3)

例子 : 有沒有 optimal substructure

 給一個 graph . Edge 沒有 weight.

 問題 1: 找出沒有 loop 最短路徑 .

 問題 2: 找出沒有 loop 最長路徑 .

 問題 1 有沒有 optimal substructure?

 假設找到的最短路徑 p, 則我們可以將其分解為 ( 可 以是或 ). 則其中一定是的最短路徑 .

 不然的話 , 我們可以找到一個比還短路徑 , 那麼和 組合起來就變成一條比 p 更短的路徑 ( 矛盾 )

 

(4)

例子 : 有沒有 optimal substructure

 問題 2 有沒有 optimal substructure?

 沒有 ! 來舉一個反例 .

 的最長路徑 :

 但是的最長路徑為

 並不是的最長路徑中間的一部分 !

 的最長路徑為

 也不是的最長路徑中間的一部分 !

 

q r

s t

(5)

例子 : 有沒有 optimal substructure

為什麼問題 1 和問題 2 相差這麼多 ?

問題 2 缺乏”獨立性” (subproblem 的解互相之間不會影 響 )

出現在的 vertex 就不能出現在 ( 否則就會有 loop 了 ) subproblem 的解互相影響 !

問題 1 有”獨立性”

在最短路徑中 , 出現在的 vertex 本來就不可能出現在

假設中除了 w 以外出現了一個一樣的 vertex x. 則可以將 最短路徑拆解成 .

因為 x 和 w 不同 , 所以 . 則變成比原本更短的 u 到 v 的路 徑 ( 矛盾 )

 

(6)

DNA 比對問題

 DNA 序列可表示為以 {A,C,G,T} 組合而成的 一字串

 比較兩者有多相像 ??

 親屬關係 ?

 

你是我爸 ?!

(7)

DNA 比對問題

多相像找出兩者中都出現的最長子序列看最長子序列有 多長 , 越長越相像

子序列 :

順序相同

但不一定要連續 .

簡單的例子 :

X=ABCBDAB, Y=BDCABA

子序列之一 : BCA

最長共同子序列 : BCBA

最長共同子序列 =?

 

答案在課本 p.391

(8)

DNA 比對問題 最長共同子序列

 問題 : 給兩字串 , 找出最長共同子序列 .

 最長共同子序列 =Longest Common Subsequenc e=LCS

 問 : 暴力法有多暴力 ?

 

(9)

暴力法有多暴力 ?

 找出所有 X 之子序列 , 與 Y 比較檢驗看看是不 是 Y 的子序列 .

 X 有幾個子序列 ?

 個

 Running time:

 

(10)

Dynamic Programming 出招

 先來個小定義 , 對 ,

 先證明以下三個小定理 . 給定兩字串 , 及為 X 和 Y 的 LCS( 之一 )

1. If , then and 是及的 LCS 之一

2. If , then 表示是及的 LCS 之一

3. If , then 表示是及的 LCS 之一

 

1. 找出 Optimal Substructure

(11)

1. If , then (1) and (2) 是及的 LCS 之一

(1) Z 最後一個字元一定是 , 否則可以把加到 Z 的最後面成為比 LCS 更長的 CS ( 矛盾 )

(2) 一定是和的 LCS. 假設不是 , 則可以找到一 個長度 >k-1 的 LCS, 但是加上這一個字元 , 表示可 以找到一個和的 LCS 長度 >k ( 矛盾 )

 

� − 1  

� − 1

 

 

 

� − 1

   

(12)

2. If , then 表示是及的 LCS 之一

假設 Z 不是和的 LCS, 則有 W 為和的 LCS, 長度 >

k, 則 W 亦為和的 LCS, 長度 >k ( 矛盾 )

3. If , then 表示是及的 LCS 之一 證明類似上面 2. 的證明 .

 

� − 1  

 

 

� − 1

   

(13)

Optimal Substructure

 給定兩字串 , 及為 X 和 Y 的 LCS( 之一 )

1. If , then and 是及的 LCS 之一

2. If , then 表示是及的 LCS 之一

3. If , then 表示是及的 LCS 之一

  

大問題的解裡面有小問題的解 !

(14)

Overlapping subproblem

 給定兩字串 , 及為 X 和 Y 的 LCS( 之一 )

1. If , then and 是及的 LCS 之一

2. If , then 表示是及的 LCS 之一

3. If , then 表示是及的 LCS 之一

  

  和�的 ���

  �−1和�的 ���

  和�� −1的 ���

  �−1和��−1的 ���

不同問題需要同樣子問題的解 !

(15)

Dynamic Programming 出招

 

2. 列出遞迴式子 ( 表示花費 )

if i=0 or j=0 if and  

if and  

  兩種選擇

條件不同 , 使用的 subproblem 不同

(16)

Dynamic Programming 出招

 使用 dynamic programming 填表

 共有多少個 entry?

 

3. 計算花費

0 1 2 3

0 1 2 3 i j

每一格只用到左、

左上、上三格的資訊

使用 bottom-up 方法 兩層迴圈依序填入即可

(17)

LCS_Length(X,Y) m=X.length

n=Y.length

let b[1..m,1..n] and c[0..m,0..n] be new tables for i=1 to m

c[i,0]=0

for j=0 to n c[0,j]=0

for i=1 to m for j=1 to n if

c[i,j]=c[i-1,j-1]+1 b[i,j]= 左上

elseif c[i-1,j]c[i,j-1]

c[i,j]=c[i-1,j]

b[i,j]= 上 else

c[i,j]=c[i,j-1]

b[i,j]= 左

return c and b

 

邊界起始值

填表 : 兩層迴 圈

c 紀錄 LCS 長度 , b 紀錄選擇 結果

Θ(��)

 

(18)

Dynamic Programming 出招

Print_LCS(b,X,i,j) if i==0 or j==0 return

if b[i,j]== 左上

Print_LCS(b,X,i-1,j-1) print

elseif b[i,j]== 上 Print_LCS(b,X,i-1,j) else

Print_LCS(b,X,i,j-1)

 

4. 印出 LCS 結果

O(�+�)

 

(19)

0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 0 0 0 1 1 1

2 0 1 1 1 1 2 2

3 0 1 1 2 2 2 2

4 0 1 1 2 2 3 3

5 0 1 2 2 2 3 3

6 0 1 2 2 3 3 4

7 0 1 2 2 3 4 4

例題

0 1 2 3 4 5 6

0 1 2 3 4 5 6 7

B D C A B A

A B C B D A B

(20)

翻譯機問題

 最笨翻譯機 :

每個英文單字直接翻成法文單字

 做法 : 建一棵 balanced binary sea rch tree ( 例如紅黑樹 ), 裡面用英 文單字當 key, 法文單字當作對應的 資料

 則每個字平均花的時間

 假設我們知道每個字出現的頻率 ( 或 機率 ), 可以做得更好嗎 ?

 答 : 可以 ! 把常用的字放離 root 近 一點 .

 

the

machicolation

(21)

翻譯機問題  Optimal Binary Search Tree

 問題 :

給一個序列共 n 個排好序的 key (). 我們要用這些 key 建立一棵 binary search tree.

出現的機率為 .

另外我們也有 n+1 個”假 key” 代表沒有出現在 K 中的值 , 可用來表示 . 代表小於的值 , 代表介於和的值 ,…, 代表大於的值 .

假 key 出現的機率為 .

則目標是找出一棵 binary search tree 使得 Expected co st 最小 .

 

 1  2 

 0

 1

  …2

 

  �− 1

(22)

 + +

 since

 使得以上 E[search cost] 最小的 binary search tree 稱為 optimal binary search tree.

 

 2

 1

 5

 4

 3

  0

  1

  2

  3

  4

5

 

(23)

i 0 1 2 3 4 5 0.1

5 0.1

0 0.0

5 0.1

0 0.2 0 0.0

5

0.1 0

0.0 5

0.0 5

0.0 5

0.1 0

i 0 1 2 3 4 5

0.1

5 0.1

0 0.0

5 0.1

0 0.2 0 0.0

5

0.1 0

0.0 5

0.0 5

0.0 5

0.1 0

 2

 1

 5

 4

 3

0

 

  1

2

 

  3

  4

  5

 假設給定的 key 的出現機率為右上表格所顯示 , 則左上 圖為 optimal binary search tree (expected

cost=2.75)

 觀察 : 機率最大的 key 不見得在 root ( 不在 root)

 

(24)

暴力法有多暴力 ?

 上學期也講過…跟上次同樣的

 n 個 node 的 binary search tree 總共有個 (C atalan number)

 

(25)

 小觀察 : binary search tree 的 subtree 必包含一 段連續的 key 及 .

 小定理 : 假設 T 為之 optimal binary search tree . 則 T 之 subtree 包含這些 key, 也必定是這些 ke y 的 optimal binary search tree.

 證明 : 如果找出的不是 optimal binary search tr ee, 則表示可以找出一個更好的 binary search tre e , expected cost 比更好 , 則可以用取代中的 , 得到一個比 T cost 更低的 binary search tree ( 矛 盾 )

 

Dynamic Programming 出招

1. 找出 Optimal Substructure

(26)

 

′′

 

 

 

,

�+1

,…,

+  

+  

¿

��

+

 

�� <

E

ij

 

�� +

� < �

�� +

 

矛盾 !

(27)

Dynamic Programming 出招

 如何用小問題的答案組出大問題的答案 ?

 

,

�+1

,…,

� −1

 

  � +1

,

�+2

,…,

  �−1

,

,…,

� − 1

 

,

�+ 1

,…,

選出一 r,  

 

,…,

�− 1

 

  �+ 1

,

�+ 2

,…,

  �−1

 null

,…,

�−1

�− 1

 

,

�+1

,…,

(28)

Dynamic Programming 出招

2. 列出遞迴式子 ( 表示花費 )

 

 

,

�+1

,…,

� −1

 

 

  � +1

,

� +2

,…,

  �−1

,

,…,

� − 1

 

,

� + 1

,…,

[ �,�−1 ] =

�=�

�−1

( ����h ( ) +1)∙   +

�=�−1

�−1

( ����h ( ) +1)∙

 

[ �+1,� ] =

�=�+1

( ����h

( ) +1)∙   +

�=�+1

( ����h

( ) +1)∙

 

���� h

 

(∙)= ���� h

(∙ )+1= ���� h

(∙)+1

 

(29)

 

if  

if  

包含的 subtree 所發 生的機率

 

r 有多種選擇

條件不同 , 使用的 subproblem 不同

(30)

Dynamic Programming 出招

 填表 : e & w

 e[i,j]: i=1 to n+1, j=0 to n

 w[i,j]: i=1 to n+1, j=0 to n

 為什麼 w 要填表 ? 不然計算每個 e[i,j] 都需要做次 加法

 w[i,i-1]=

 w[i,j]=w[i,j-1]

 

3. 計算花費

w 0 1 2 3 4 5

1 2 3 4 5 6

w 0 1 2 3 4 5

1 2 3 4 5 6

Θ

  (

2)

i

j

(31)

 e 的填表順序

e 0 1 2 3 4 5

1 2 3 4 5 6

e 0 1 2 3 4 5

1 2 3 4 5 6

橘色是會用到的 subproblem i

j

一次填一條對角線

Θ

  (

3)

(32)

Optimal_BST(p,q,n)

let e[1..n+1,0..n],w[1..n+1,0..n],and root[1..n,1..n]

be new tables for i=1 to n+1 e[i,i-1]=

w[i,i-1]=

for l=1 to n

for i=1 to n-l+1 j=i+l-1

e[i,j]=

w[i,j]=w[i,j-1]++

for r=i to j

t=e[i,r-1]+e[r+1,j]+w[i,j]

if t<e[i,j]

e[i,j]=t

root[i,j]=r

return e and root

 

邊界起始值

填表 : 兩層迴圈 , 對角線 順序

我要請全班喝飲料

Θ(�

  3

)

(33)

Optimal_BST(p,q,n)

let e[1..n+1,0..n],w[1..n+1,0..n],and root[1..n,1..n]

be new tables for i=1 to n+1 e[i,i-1]=

w[i,i-1]=

for l=1 to n

for i=1 to n-l+1 j=i+l-1

e[i,j]=

w[i,j]=w[i,j-1]++

for r=i to j

t=e[i,r-1]+e[r+1,j]+w[i,j]

if t<e[i,j]

e[i,j]=t

root[i,j]=r

return e and root

 

邊界起始值

填表 : 兩層迴圈 , 對角線 順序

e 紀錄 expected cost, root 紀錄選擇 結果

Θ(�

  3

)

(34)

Dynamic Programming 出招

 作業 ! 15.5-1 on p. 403

4. 印出 Optimal Binary Search Tree 結果

參考文獻

相關文件

Conducting binary morphological operations: dilation, erosion, opening, closing and hit-and-miss.. Dynamic gray morphological operation kernel assignment with color depth showing

III Raman Imaging of Raft Model Membrane.. Sphingomyelin and Cholesterol

Since the generalized Fischer-Burmeister function ψ p is quasi-linear, the quadratic penalty for equilibrium constraints will make the convexity of the global smoothing function

大略來說 , 以上兩者決定 dynamic programming a lgorithm 的執行時間. ( 之前說的 Subproblem graphs

 Definition: A problem exhibits optimal subst ructure if an optimal solution to the proble m contains within it optimal solutions to su bproblems..  怎麼尋找 optimal

印出Optimal Binary

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

 Each  node  has  only  a  immediately-­‐right  sibling  (也是廢話).. Data left  child