1 / 34

Dynamic Programming III

Dynamic Programming III. Michael Tsai 2011/4/1. 複習 : Overlapping Subproblems. 舉個例子 : 連串矩陣問題的遞迴樹. 1..3. 3..3. 1..1. 2..3. 1..2. 2..2. 2..2. 3..3. 1..1. 橘色的是 overlap 的部分 !. 例子 : 有沒有 optimal substructure. 給一個 graph . Edge 沒有 weight. 問題 1: 找出 沒有 loop 最短路徑 .

shaw
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/4/1

  2. 複習: Overlapping Subproblems • 舉個例子: 連串矩陣問題的遞迴樹 1..3 3..3 1..1 2..3 1..2 2..2 2..2 3..3 1..1 橘色的是overlap的部分!

  3. 例子: 有沒有optimal substructure • 給一個graph . Edge沒有weight. • 問題1:找出沒有loop最短路徑. • 問題2:找出沒有loop最長路徑. • 問題1有沒有optimal substructure? • 假設找到的最短路徑p, 則我們可以將其分解為(可以是或). 則其中一定是的最短路徑. • 不然的話, 我們可以找到一個比還短路徑, 那麼和組合起來就變成一條比p更短的路徑 (矛盾)

  4. 例子: 有沒有optimal substructure • 問題2有沒有optimal substructure? • 沒有! 來舉一個反例. • 的最長路徑: • 但是的最長路徑為 • 並不是的最長路徑中間的一部分! • 的最長路徑為 • 也不是的最長路徑中間的一部分! q r t s

  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 Subsequence=LCS • 問: 暴力法有多暴力?

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

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

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

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

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

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

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

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

  17. c紀錄LCS長度, b紀錄選擇結果 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 邊界起始值 填表: 兩層迴圈

  18. Dynamic Programming出招 4. 印出LCS結果 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)

  19. 例題

  20. 翻譯機問題 • 最笨翻譯機: 每個英文單字直接翻成法文單字 • 做法: 建一棵balanced binary search 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 cost最小.

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

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

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

  25. 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 (矛盾)

  26. + 矛盾! +

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

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

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

  30. 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]都需要做次加法 • w[i,i-1]= • w[i,j]=w[i,j-1] j i

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

  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 填表: 兩層迴圈, 對角線順序

  33. 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 填表: 兩層迴圈, 對角線順序

  34. Dynamic Programming出招 4. 印出Optimal Binary Search Tree結果 • 作業! 15.5-1 on p. 403

More Related