Programming III
Michael Tsai 2011/10/14
(複習start)
DNA比對問題
你是我爸?!
答案在課本p.391
DNA比對問題最長共同子序列
暴力法有多暴力?
Dynamic Programming出招
1. 找出Optimal Substructure
Optimal Substructure
大問題的解裡面有小問題的解!
Overlapping subproblem
不同問題需要同樣子問題的解!
Dynamic Programming出招
2. 列出遞迴式子 (表示花費)
if i=0 or j=0
兩種選擇
條件不同, 使用的subproblem不同
Dynamic Programming出招
3. 計算花費
0 1 2 3
0 1 2 3 i j
每一格只用到左、
左上、上三格的資訊
使用bottom-up方法 兩層迴圈依序填入即可
(複習end)
翻譯機問題
the
machicolation
Tree
……
……
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
暴力法有多暴力?
1. 找出Optimal Substructure
矛盾!
如何用小問題的答案組出大問題的答案?
null
Dynamic Programming出招
2. 列出遞迴式子 (表示花費)
r有多種選擇
條件不同, 使用的subproblem不同
Dynamic Programming出招
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
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
一次填一條對角線
大家來練習
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
邊界起始值
填表: 兩層迴圈, 對角線順序
e紀錄expected cost, root紀錄選擇結果
Dynamic Programming出招
會出現在hw3 15.5-1 on p. 403
4. 印出Optimal Binary Search Tree結果