160 likes | 286 Views
This guide explores the concept of the Longest Common Subsequence (LCS) using dynamic programming techniques. It outlines the definition of LCS, the difference between substrings and subsequences, and presents a structured approach for solving LCS problems. The document also discusses optimal substructure and provides a brute force comparison to highlight efficiency. Additionally, it covers basic principles of dynamic programming, including space and time complexities associated with the LCS algorithm.
E N D
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 • 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
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
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.
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.
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=<>
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.
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
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
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
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
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.
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.
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
Longest common subsequence • Computation time: Θ(mn) • Space: Θ(mn) • Space reduction: Θ (min(m,n))