1 / 16

15. Dynamic Programming

15. Dynamic Programming. Heejin Park College of Information and Communications Hanyang University. Contents. Introduction Assembly-line scheduling Matrix-chain multiplication Elements of dynamic programming Longest common subsequence. Longest common subsequence. Definition Character

sitara
Télécharger la présentation

15. Dynamic Programming

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. 15. Dynamic Programming Heejin Park College of Information and Communications Hanyang University

  2. Contents • Introduction • Assembly-line scheduling • Matrix-chain multiplication • Elements of dynamic programming • Longest common subsequence

  3. Longest common subsequence • Definition • Character • Alphabet: A set of characters • English alphabet: {A, B, …Z} • Korean alphabet: { ㄱ,ㄴ,…ㅎ, ㅏ,…ㅣ} • String (or sequence): A list of characters from an alphabet • ex> strings over {0,1}: Binary strings • ex> strings over {A,C,G,T}: DNA sequences

  4. Longest common subsequence • Substring • CBD is a substring of ABCBDAB • Subsequence • BCDB is a subsequence of ABCBDAB • Common subsequence • BCA is a common subsequence of X=ABCBDAB and Y=BDCABA

  5. Longest common subsequence • Longest common subsequence(LCS) • BCBA is the longest common subsequence of X and Y X = A B CB D A B Y = B D C A BA • LCS problem • Given two sequences X=<x1,x2,…,xm> and Y=<y1, y2,..,yn> to find an LCS of X and Y.

  6. Longest common subsequence • Brute force approach • Enumerate all subsequences of X and check each subsequence if it is also a subsequence of Y and find the longest one. • Infeasible! • The number of subsequences of X is 2m.

  7. Longest common subsequence • Dynamic programming • The ith prefixXi of X is Xi=<x1,x2,…,xi>. • If X = <A, B, C, B, D, A, B> • X4=< A, B, C, B> • X0=<>

  8. Longest common subsequence • Optimal substructure of an LCS • Let X = x1, x2, ..., xm and Y = y1, y2, ..., yn be sequences, and let Z = z1, z2, ..., zk be any LCS of X and Y. • If xm=yn, then zk=xm=yn and Zk-1 is an LCS of Xm-1 and Yn-1. • If xm≠yn, then zk≠xm implies Z is an LCS of Xm-1 and Y. • If xm≠yn, then zk≠yn implies Z is an LCS of X and Yn-1.

  9. Longest common subsequence • If xm=yn, then zk=xm=yn and Zk-1 is an LCS of Xm-1 and Yn-1. • Suppose zk ≠ xm. • Then, we could append xm = yn to Z to obtain a common subsequence of X and Y of length k + 1, Thus, zk= xm. X = A B C B D A B Y = B D C A B

  10. Longest common subsequence • If xm=yn, then zk=xm=yn and Zk-1 is an LCS of Xm-1 and Yn-1. • Suppose that there is a common subsequence W of Xm-1 and Yn-1 with length greater than k - 1. • Then, appending xm = yn to W produces a common subsequence of X and Y whose length is greater than k. X = A B C B D A B Y = B D C A B

  11. Longest common subsequence • If xm≠yn, then zk≠xm implies Z is an LCS of Xm-1 and Y. • Since zk≠ xm, Z is an LCS of Xm-1 and Y. X = A B C B D A B Y = B D C A A

  12. Longest common subsequence • If xm≠yn, then zk≠yn implies Z is an LCS of X and Yn-1. • symmetric to the proof of 2. X = A B C B D A B Y = B D C A A

  13. Longest common subsequence • If xm= yn, • Find an LCS of Xm-1 and Yn-1 and append xm= ynto this LCS. • If xm≠ yn • Find the longer between an LCS of Xm-1 and Y and an LCS of X and Yn-1.

  14. Longest common subsequence • c[i, j]: The length of an LCS of the sequences Xiand Yj. • If either i = 0 or j = 0, so the LCS has length = 0.

  15. Longest common subsequence j 0 1 2 3 4 5 6 i 0 1 2 3 4 5 6 7 ↑ 0 ↑ 0 ↑ 0 ↖ 1 ← 1 ↖ 1 0 ↖ 1 ← 1 ← 1 ↑ 1 ↖ 2 ← 2 0 ← 2 ↑ 1 ↑ 1 ↖ 2 ↑ 2 ↑ 2 0 ↖ 1 ↑ 2 ↑ 2 ↑ 1 ↖ 3 ← 3 0 ↑ 1 ↑ 2 ↑ 2 ↑ 3 ↖ 2 ↑ 3 0 ↖ 3 ↖ 4 ↑ 1 ↑ 2 ↑ 2 ↑ 3 0 ↖ 1 ↑ 2 ↑ 2 ↑ 3 ↖ 4 ↑ 4

  16. Longest common subsequence • Computation time: Θ(mn) • Space: Θ(mn) • Space reduction: Θ (min(m,n))

More Related