跳到主要内容

東京大学 新領域創成科学研究科 メディカル情報生命専攻 2017年8月実施 問題12

Author

zephyr

Description

For two strings x=x[1]x[n]\mathbf{x} = x[1] \cdots x[n] and y=y[1]y[m]\mathbf{y} = y[1] \cdots y[m] of lengths nn and mm (n0,m0n \geq 0, m \geq 0), we define the set of common subsequences as

S={(i1,,irj1,,jr)r:positive integer,1i1<<irn1j1<<jrm,x[ik]=y[jk],k=1,,r}S = \left\{ \begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} \mid r: \text{positive integer}, \begin{matrix} 1 \leq i_1 < \cdots < i_r \leq n \\ 1 \leq j_1 < \cdots < j_r \leq m \end{matrix}, x[i_k] = y[j_k], k = 1, \cdots, r \right\}

Here, rr represents the length of common subsequence (i1,,irj1,,jr)\begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix}. Let l(x,y)l(x, y) denote the length of the longest common subsequences. If S=S = \emptyset, we define l(x,y)=0l(x, y) = 0.

(1) Let zkpz_k^p and zksz_k^s denote the length-kk prefix and suffix of string zz, respectively. We define α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p) and β[i,j]=l(xni+1s,ymj+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) for 1in,1jm1 \leq i \leq n, 1 \leq j \leq m. Describe a recurrence for computing α[i,j]\alpha[i, j] from α[i1,j1]\alpha[i-1, j-1], α[i1,j]\alpha[i-1, j], and α[i,j1]\alpha[i, j-1] when 1<i,1<j1 < i, 1 < j. Also, describe a recurrence for computing β[i,j]\beta[i, j] from β[i+1,j]\beta[i+1, j], β[i,j+1]\beta[i, j+1], and β[i+1,j+1]\beta[i+1, j+1] when i<n,j<mi < n, j < m.

(2) Compute matrices α\alpha and β\beta for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}.

(3) Suppose matrix α\alpha is given. Write a pseudocode for obtaining one of the longest common subsequences.

(4) Suppose matrices α\alpha and β\beta are given. Write a pseudocode for computing the maximal length of common subsequences that contain the ii-th position of x\mathbf{x} (1in1 \leq i \leq n).


对于长度为 nnmm 的两个字符串 x=x[1]x[n]\mathbf{x} = x[1] \cdots x[n]y=y[1]y[m]\mathbf{y} = y[1] \cdots y[m] (n0,m0n \geq 0, m \geq 0),我们定义公共子序列集为

S={(i1,,irj1,,jr)r:正整数,1i1<<irn1j1<<jrm,x[ik]=y[jk],k=1,,r}S = \left\{ \begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} \mid r: \text{正整数}, \begin{matrix} 1 \leq i_1 < \cdots < i_r \leq n \\ 1 \leq j_1 < \cdots < j_r \leq m \end{matrix}, x[i_k] = y[j_k], k = 1, \cdots, r \right\}

这里,rr 表示公共子序列 (i1,,irj1,,jr)\begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} 的长度。令 l(x,y)l(x, y) 表示最长公共子序列的长度。如果 S=S = \emptyset,我们定义 l(x,y)=0l(x, y) = 0

(1) 令 zkpz_k^pzksz_k^s 分别表示字符串 zz 的长度为 kk 的前缀和后缀。我们定义 α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p)β[i,j]=l(xni+1s,ymj+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) 对于 1in,1jm1 \leq i \leq n, 1 \leq j \leq m。描述从 α[i1,j1]\alpha[i-1, j-1]α[i1,j]\alpha[i-1, j],和 α[i,j1]\alpha[i, j-1] 计算 α[i,j]\alpha[i, j] 的递推关系,当 1<i,1<j1 < i, 1 < j。同时,描述从 β[i+1,j]\beta[i+1, j]β[i,j+1]\beta[i, j+1],和 β[i+1,j+1]\beta[i+1, j+1] 计算 β[i,j]\beta[i, j] 的递推关系,当 i<n,j<mi < n, j < m

(2) 计算 x=ACTGG\mathbf{x} = \text{ACTGG}y=ACACG\mathbf{y} = \text{ACACG} 的矩阵 α\alphaβ\beta

(3) 假设给定矩阵 α\alpha。编写伪代码以获取一个最长的公共子序列。

(4) 假设给定矩阵 α\alphaβ\beta。编写伪代码以计算包含 x\mathbf{x} 的第 ii 个位置的最大长度的公共子序列 (1in1 \leq i \leq n)。

Kai

(1)

Recurrence for α[i,j]\alpha[i, j]

The value of α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p) can be computed as follows:

  • If x[i]=y[j]x[i] = y[j], then α[i,j]=α[i1,j1]+1\alpha[i, j] = \alpha[i-1, j-1] + 1.
  • If x[i]y[j]x[i] \neq y[j], then α[i,j]=max(α[i1,j],α[i,j1])\alpha[i, j] = \max(\alpha[i-1, j], \alpha[i, j-1]).

This can be summarized as:

α[i,j]={α[i1,j1]+1if x[i]=y[j]max(α[i1,j],α[i,j1])if x[i]y[j]\alpha[i, j] = \begin{cases} \alpha[i-1, j-1] + 1 & \text{if } x[i] = y[j] \\ \max(\alpha[i-1, j], \alpha[i, j-1]) & \text{if } x[i] \neq y[j] \end{cases}

Recurrence for β[i,j]\beta[i, j]

The value of β[i,j]=l(xni+1s,ymj+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) can be computed as follows:

  • If x[ni+1]=y[mj+1]x[n-i+1] = y[m-j+1], then β[i,j]=β[i+1,j+1]+1\beta[i, j] = \beta[i+1, j+1] + 1.
  • If x[ni+1]y[mj+1]x[n-i+1] \neq y[m-j+1], then β[i,j]=max(β[i+1,j],β[i,j+1])\beta[i, j] = \max(\beta[i+1, j], \beta[i, j+1]).

This can be summarized as:

β[i,j]={β[i+1,j+1]+1if x[ni+1]=y[mj+1]max(β[i+1,j],β[i,j+1])if x[ni+1]y[mj+1]\beta[i, j] = \begin{cases} \beta[i+1, j+1] + 1 & \text{if } x[n-i+1] = y[m-j+1] \\ \max(\beta[i+1, j], \beta[i, j+1]) & \text{if } x[n-i+1] \neq y[m-j+1] \end{cases}

(2)

Matrix α\alpha

Let's fill the matrix α\alpha for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}:

x\yACACG
A11111
C12222
T12222
G12223
G12223

Matrix β\beta

Let's fill the matrix β\beta for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}:

x\yGCACA
G11111
G11111
T11111
C12222
A12333

(3)

To extract one of the longest common subsequences from the matrix α\alpha, 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 α\alpha.

Explanation

  • Start from the bottom-right corner of the matrix α\alpha, i.e., α[n,m]\alpha[n, m].
  • Compare characters of x\mathbf{x} and y\mathbf{y}:
    • If x[i]=y[j]x[i] = y[j], include x[i]x[i] in the result and move diagonally to α[i1,j1]\alpha[i-1, j-1].
    • If x[i]y[j]x[i] \neq 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 α\alpha and β\beta, we can compute the maximal length of common subsequences that include the ii-th position of x\mathbf{x}. This is done by evaluating the length of subsequences that start from the beginning and end at the ii-th position, combined with subsequences that start at the ii-th position and extend to the end.

Explanation

  • For each position jj in y\mathbf{y}:
    • Combine the length of the prefix up to ii (α[i,j]\alpha[i, j]) with the length of the suffix from ii onwards (β[ni+1,mj+1]\beta[n-i+1, m-j+1]) as the length of the common subsequence.
    • If x[i]=y[j]x[i] = y[j], since x[i]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 动态规划

参考资料

  1. Introduction to Algorithms, 3rd Edition, Cormen et al., Chapter 15.