東京大学 新領域創成科学研究科 メディカル情報生命専攻 2017年8月実施 問題12
Author
zephyr
Description
For two strings x=x[1]⋯x[n] and y=y[1]⋯y[m] of lengths n and m (n≥0,m≥0), we define the set of common subsequences as
S={(i1,⋯,irj1,⋯,jr)∣r:positive integer,1≤i1<⋯<ir≤n1≤j1<⋯<jr≤m,x[ik]=y[jk],k=1,⋯,r}
Here, r represents the length of common subsequence (i1,⋯,irj1,⋯,jr). Let l(x,y) denote the length of the longest common subsequences. If S=∅, we define l(x,y)=0.
(1) Let zkp and zks denote the length-k prefix and suffix of string z, respectively. We define α[i,j]=l(xip,yjp) and β[i,j]=l(xn−i+1s,ym−j+1s) for 1≤i≤n,1≤j≤m. Describe a recurrence for computing α[i,j] from α[i−1,j−1], α[i−1,j], and α[i,j−1] when 1<i,1<j. Also, describe a recurrence for computing β[i,j] from β[i+1,j], β[i,j+1], and β[i+1,j+1] when i<n,j<m.
(2) Compute matrices α and β for x=ACTGG and y=ACACG.
(3) Suppose matrix α is given. Write a pseudocode for obtaining one of the longest common subsequences.
(4) Suppose matrices α and β are given. Write a pseudocode for computing the maximal length of common subsequences that contain the i-th position of x (1≤i≤n).
对于长度为 n 和 m 的两个字符串 x=x[1]⋯x[n] 和 y=y[1]⋯y[m] (n≥0,m≥0),我们定义公共子序列集为
S={(i1,⋯,irj1,⋯,jr)∣r:正整数,1≤i1<⋯<ir≤n1≤j1<⋯<jr≤m,x[ik]=y[jk],k=1,⋯,r}
这里,r 表示公共子序列 (i1,⋯,irj1,⋯,jr) 的长度。令 l(x,y) 表示最长公共子序列的长度。如果 S=∅,我们定义 l(x,y)=0。
(1) 令 zkp 和 zks 分别表示字符串 z 的长度为 k 的前缀和后缀。我们定义 α[i,j]=l(xip,yjp) 和 β[i,j]=l(xn−i+1s,ym−j+1s) 对于 1≤i≤n,1≤j≤m。描述从 α[i−1,j−1],α[i−1,j],和 α[i,j−1] 计算 α[i,j] 的递推关系,当 1<i,1<j。同时,描述从 β[i+1,j],β[i,j+1],和 β[i+1,j+1] 计算 β[i,j] 的递推关系,当 i<n,j<m。
(2) 计算 x=ACTGG 和 y=ACACG 的矩阵 α 和 β。
(3) 假设给定矩阵 α。编写伪代码以获取一个最长的公共子序列。
(4) 假设给定矩阵 α 和 β。编写伪代码以计算包含 x 的第 i 个位置的最大长度的公共子序列 (1≤i≤n)。
Kai
(1)
Recurrence for α[i,j]
The value of α[i,j]=l(xip,yjp) can be computed as follows:
- If x[i]=y[j], then α[i,j]=α[i−1,j−1]+1.
- If x[i]=y[j], then α[i,j]=max(α[i−1,j],α[i,j−1]).
This can be summarized as:
α[i,j]={α[i−1,j−1]+1max(α[i−1,j],α[i,j−1])if x[i]=y[j]if x[i]=y[j]
Recurrence for β[i,j]
The value of β[i,j]=l(xn−i+1s,ym−j+1s) can be computed as follows:
- If x[n−i+1]=y[m−j+1], then β[i,j]=β[i+1,j+1]+1.
- If x[n−i+1]=y[m−j+1], then β[i,j]=max(β[i+1,j],β[i,j+1]).
This can be summarized as:
β[i,j]={β[i+1,j+1]+1max(β[i+1,j],β[i,j+1])if x[n−i+1]=y[m−j+1]if x[n−i+1]=y[m−j+1]
(2)
Matrix α
Let's fill the matrix α for x=ACTGG and y=ACACG:
| x\y | A | C | A | C | G |
|---|
| A | 1 | 1 | 1 | 1 | 1 |
| C | 1 | 2 | 2 | 2 | 2 |
| T | 1 | 2 | 2 | 2 | 2 |
| G | 1 | 2 | 2 | 2 | 3 |
| G | 1 | 2 | 2 | 2 | 3 |
Matrix β
Let's fill the matrix β for x=ACTGG and y=ACACG:
| x\y | G | C | A | C | A |
|---|
| G | 1 | 1 | 1 | 1 | 1 |
| G | 1 | 1 | 1 | 1 | 1 |
| T | 1 | 1 | 1 | 1 | 1 |
| C | 1 | 2 | 2 | 2 | 2 |
| A | 1 | 2 | 3 | 3 | 3 |
(3)
To extract one of the longest common subsequences from the matrix α, we use the following algorithm. This algorithm traces back from the bottom-right corner of the matrix to the top-left corner, reconstructing the longest common subsequence by following the path of optimal choices recorded in α.
Explanation
- Start from the bottom-right corner of the matrix α, i.e., α[n,m].
- Compare characters of x and y:
- If x[i]=y[j], include x[i] in the result and move diagonally to α[i−1,j−1].
- If x[i]=y[j], move in the direction that gives the larger value (either up or left).
- Continue this process until reaching the top-left corner of the matrix.
- The result will be one of the longest common subsequences.
Pseudocode
function getLCS(x, y, alpha)
i = length(x)
j = length(y)
lcs = ""
while i > 0 and j > 0
if x[i] == y[j]
lcs = x[i] + lcs
i = i - 1
j = j - 1
else if alpha[i-1, j] >= alpha[i, j-1]
i = i - 1
else
j = j - 1
return lcs
(4)
Given matrices α and β, we can compute the maximal length of common subsequences that include the i-th position of x. This is done by evaluating the length of subsequences that start from the beginning and end at the i-th position, combined with subsequences that start at the i-th position and extend to the end.
Explanation
- For each position j in y:
- Combine the length of the prefix up to i (α[i,j]) with the length of the suffix from i onwards (β[n−i+1,m−j+1]) as the length of the common subsequence.
- If x[i]=y[j], since x[i] is included in both the prefix and suffix, subtract 1 from the total length.
- The maximum value obtained through this process gives the desired length.
Pseudocode
function maxLengthWithPosition(x, y, alpha, beta, i)
maxLength = 0
for j = 1 to length(y)
currentLength = alpha[i, j] + beta[n-i+1, m-j+1]
if x[i] == y[j]
currentLength = currentLength - 1
maxLength = max(maxLength, currentLength)
return maxLength
Knowledge
动态规划 最长公共子序列 递归
难点思路
对于递归关系的理解和矩阵填充的具体实现可能会比较复杂,需要仔细考虑每一步的递推关系。
解题技巧和信息
- 动态规划表格填充方法:先初始化,然后按照递推关系逐步填充。
- 递归关系需要对字符串字符的匹配情况进行详细考虑,以确保递推关系的正确性。
重点词汇
- common subsequence 公共子序列
- recurrence relation 递推关系
- prefix 前缀
- suffix 后缀
- dynamic programming 动态规划
参考资料
- Introduction to Algorithms, 3rd Edition, Cormen et al., Chapter 15.