1 / 29

Dynamic Programming III

Dynamic Programming III. Michael Tsai 2011/10/14. ( 複習 start). DNA 比對問題. DNA 序列可表示為以 {A,C,G,T} 組合而成的 一字串 比較兩者有多 相像 ?? 親屬 關係 ?. 你是我爸 ?!. DNA 比對問題. 多相像  找出兩者中都出現的最長子序列  看 最長 子序列有多長 , 越長越相像 子序列 : 順序 相同 但不一定要 連續 . 簡單的例子 : X=ABCBDAB, Y=BDCABA 子 序列之一 : BCA 最長共同子 序列 : BCBA

hada
Download Presentation

Dynamic Programming III

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Programming III Michael Tsai 2011/10/14

  2. (複習start)

  3. DNA比對問題 • DNA序列可表示為以{A,C,G,T}組合而成的一字串 • 比較兩者有多相像?? • 親屬關係? 你是我爸?!

  4. DNA比對問題 • 多相像找出兩者中都出現的最長子序列看最長子序列有多長, 越長越相像 • 子序列: • 順序相同 • 但不一定要連續. • 簡單的例子: • X=ABCBDAB, Y=BDCABA • 子序列之一: BCA • 最長共同子序列: BCBA • 最長共同子序列=? 答案在課本p.391

  5. DNA比對問題最長共同子序列 • 問題: 給兩字串, 找出最長共同子序列. • 最長共同子序列=Longest Common Subsequence=LCS • 問: 暴力法有多暴力?

  6. 暴力法有多暴力? • 找出所有X之子序列, 與Y比較檢驗看看是不是Y的子序列. • X有幾個子序列? • 個 • Running time:

  7. Dynamic Programming出招 1. 找出Optimal Substructure • 先來個小定義, 對, • 先證明以下三個小定理. 給定兩字串, 及為X和Y的LCS(之一) • If , then and 是及的LCS之一 • If , then 表示是及的LCS之一 • If , then 表示是及的LCS之一

  8. If , then (1) and (2) 是及的LCS之一 (1) Z最後一個字元一定是, 否則可以把加到Z的最後面成為比LCS更長的CS (矛盾) (2)一定是和的LCS. 假設不是, 則可以找到一個長度>k-1的LCS, 但是加上這一個字元, 表示可以找到一個和的LCS長度>k (矛盾)

  9. If , then 表示是及的LCS之一 假設Z不是和的LCS, 則有W為和的LCS, 長度>k, 則W亦為和的LCS, 長度>k (矛盾) • If , then 表示是及的LCS之一 證明類似上面2.的證明.

  10. Optimal Substructure • 給定兩字串, 及為X和Y的LCS(之一) • If , then and是及的LCS之一 • If , then 表示是及的LCS之一 • If , then 表示是及的LCS之一 大問題的解裡面有小問題的解!

  11. Overlapping subproblem • 給定兩字串, 及為X和Y的LCS(之一) • If , then and是及的LCS之一 • If , then 表示是及的LCS之一 • If , then 表示是及的LCS之一 不同問題需要同樣子問題的解!

  12. Dynamic Programming出招 2. 列出遞迴式子 (表示花費) 條件不同, 使用的subproblem不同 if i=0 or j=0 if and if and 兩種選擇

  13. Dynamic Programming出招 3. 計算花費 • 使用dynamic programming填表 • 共有多少個entry? j i 每一格只用到左、左上、上三格的資訊 使用bottom-up方法 兩層迴圈依序填入即可

  14. (複習end)

  15. 翻譯機問題 • 最笨翻譯機: 每個英文單字直接翻成法文單字 • 做法: 建一棵balanced binary search tree (例如紅黑樹), 裡面用英文單字當key, 法文單字當作對應的資料 • 則每個字平均花的時間 • 假設我們知道每個字出現的頻率(或機率), 可以做得更好嗎? • 答: 可以! 把常用的字放離root近一點. the machicolation

  16. 翻譯機問題Optimal Binary Search Tree …… …… • 問題: • 給一個序列共n個排好序的key (). 我們要用這些key建立一棵binary search tree. • 出現的機率為. • 另外我們也有n+1個”假key”代表沒有出現在K中的值, 可用來表示. 代表小於的值, 代表介於和的值,…,代表大於的值. • 假key 出現的機率為. • 則目標是找出一棵binary search tree使得Expected cost最小.

  17. ++ • since • 使得以上E[search cost]最小的binary search tree稱為optimal binary search tree.

  18. 假設給定的key的出現機率為右上表格所顯示, 則左上圖為optimal binary search tree (expected cost=2.75) • 觀察: 機率最大的key不見得在root (不在root)

  19. 暴力法有多暴力? • 上學期也講過(?)…跟上次同樣的 • n個node的binary search tree總共有個 (Catalan number)

  20. Dynamic Programming出招 1. 找出Optimal Substructure • 小觀察: binary search tree的subtree必包含一段連續的key 及. • 小定理: 假設T為之optimal binary search tree. 則T之subtree包含這些key, 也必定是這些key的optimal binary search tree. • 證明: 如果找出的不是optimal binary search tree, 則表示可以找出一個更好的binary search tree , expected cost比更好, 則可以用取代中的, 得到一個比T cost更低的binary search tree (矛盾)

  21. + 矛盾! +

  22. Dynamic Programming出招 • 如何用小問題的答案組出大問題的答案? 選出一r, null

  23. Dynamic Programming出招 2. 列出遞迴式子 (表示花費)

  24. 包含的subtree所發生的機率 條件不同, 使用的subproblem不同 ,if ,if r有多種選擇

  25. Dynamic Programming出招 3. 計算花費 • 填表: 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]都需要做次加法 j i ,if ,if

  26. j • e的填表順序 i 橘色是會用到的subproblem 一次填一條對角線

  27. 大家來練習 ,if ,if ,if ,if

  28. e紀錄expected cost,root紀錄選擇結果 邊界起始值 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 填表: 兩層迴圈, 對角線順序

  29. Dynamic Programming出招 4. 印出Optimal Binary Search Tree結果 • 會出現在hw315.5-1 on p. 403

More Related