• 沒有找到結果。

例子: 有沒有optimal substructure

N/A
N/A
Protected

Academic year: 2022

Share "例子: 有沒有optimal substructure"

Copied!
34
0
0

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

全文

(1)

Programming  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到 的最長路徑中間的一部分!

q r

s t

(5)

例子: 有沒有optimal substructure

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

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

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

問題1有”獨立性”

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

假設 , 中除了w以外出現了一個一樣的vertex x. 則可以將

最短路徑拆解成

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

(6)

DNA比對問題

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

 比較兩者有多相像??

 親屬關係?

你是我爸?!

(7)

DNA比對問題

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

 子序列:

順序相同

但不一定要連續.

 簡單的例子:

 X=ABCBDAB, Y=BDCABA

 子序列之一: BCA

 最長共同子序列: BCBA

 最長共同子序列=?  答案在課本p.391

(8)

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

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

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

 問: 暴力法有多暴力?

(9)

暴力法有多暴力?

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

 X有幾個子序列? 

 2 個

 Running time: Ω 2

(10)

Dynamic Programming出招

 先來個小定義, 對 , , … , ,  , , … , , 0

 先證明以下三個小定理. 給定兩字串

, , … , , , , … , , 及 , , … , 為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 (矛盾)

(12)

假設Z不是 和 的LCS, 則有W為 和 的LCS, 長度>k, 則W亦為 和 的LCS, 長度>k (矛盾)

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

證明類似上面2.的證明.

(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之一

和 的 和 的

和 的

和 的

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

(15)

Dynamic Programming出招

 ,

0

1, 1 1 max , 1 , 1,

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

if i=0 or j=0 if  , 0 and  if  , 0 and 

and Y 的長度 兩種選擇

條件不同, 使用的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 search  tree (例如紅黑樹), 裡面用英文單字當 key, 法文單字當作對應的資料

 則每個字平均花 log 的時間

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

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

the

machicolation

(21)

翻譯機問題Optimal Binary Search  Tree

 問題: 

給一個序列 , , … , 共n個排好序的key 

( ). 我們要用這些key建立一棵binary  search tree.

出現的機率為 .

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

假key  出現的機率為 .

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

……

……

(22)

∑ 1 ∙ +∑ 1 ∙

1 ∑ ∙ +∑ ∙

 since ∑ ∑ 1

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

(23)

i 0 1 2 3 4 5 0.15 0.10 0.05 0.10 0.20 0.05 0.10 0.05 0.05 0.05 0.10

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

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

(24)

暴力法有多暴力?

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

 n個node的binary search tree總共有Ω 個 (Catalan number)

(25)

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

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

 證明: 如果 ′找出的不是optimal binary search tree, 則 表示可以找出一個更好的binary search tree  , 

expected cost比 更好, 則可以用 取代T中的 , 得 到一個比T cost更低的binary search tree (矛盾)

Dynamic Programming出招

1. 找出Optimal Substructure

(26)

, , … ,

∑ 1 ∙

+∑ 1 ∙

∑ 1 ∙

+∑ 1 ∙

E

矛盾!

(27)

Dynamic Programming出招

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

, , … , , , … , , , … , , , … ,

選出一r, 

, … , , , … ,

, … , , , … , null

(28)

Dynamic Programming出招

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

, , … , , , … , , , … , , , … , , 1

1

∙   

+ 1 ∙

1,

1

∙   

+ 1 ∙

∙ ∙ 1 ∙ 1

(29)

 , ∑ ∑

 ,

, 1 , 1

1, 1,

 , min , 1 1, ,

if  1 if  1 包含 , … , 的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

Θ 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

一次填一條對角線 Θ

(32)

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

邊界起始值

填表: 兩層迴圈, 對角線順序 我要請全班喝飲料

Θ

(33)

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紀錄選擇結果

Θ

(34)

Dynamic Programming出招

 作業! 15.5‐1 on p. 403

4. 印出Optimal Binary Search Tree結果

參考文獻

相關文件

把一所「好」的房間,改為:一所幽靜的,不是 面對高樓大廈,或者煩囂街道,最好還看得到海

星雲大師尤其強調工作的重要 性,對個人而言工作中才有生 命、有人緣、有財富:「一個 人如果沒有工作,等於行屍走

 Definition: A problem exhibits  optimal substructure if an ..

● 應用 Greedy choice,將問題劃分成子問題,根據 optimal. substructure,可以持續應用

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

[r]

[r]

[r]