東京大学 情報理工学系研究科 創造情報学専攻 2005年8月実施 筆記試験 第1問
Author
祭音Myyura, itsuitsuki
Description
日本語
ある二つの文字列 の編集距離はつぎの 3 つの操作を行うことにより を に変換するのに要する操作の最低回数である.
- 1 文字挿入する.
- 1 文字削除する.
- 1 文字を他の 1 文字に置き換える. たとえば, は に,文字 ‘p’ を削除することにより に変換できるため,編集距離は 1 である.
(1) , の編集距離を求めなさい.
(2) を文字列 の頭から 番目までの部分列とし, を と の編集距離を表すものとする. の間に成り立つ再帰式を記述しなさい.
(3) (2) の再帰式に基づき効率良く編集距離を計算するアルゴリズムを示し,そのアルゴリズムの時間,空間計算量について述べなさい.
(4) (3) のアルゴリズムに基づき, と の編集距離を求めなさい.
(5) 編集距離の応用として考えられるものを 3 つ挙げなさい.
English
The edit distance between two strings and is defined as the minimum number of the following operations required to transform into .
- insert one character
- delete one character
- substitute one character by another character For example, is trasnformed into by deleting the character ‘p’; therefore the edit distance is 1.
(1) Answer the edit distance between and .
(2) Let’s denote as the prefix of length of a given , as the edit distance between and . Write down the recursive formula that holds between and .
(3) Describe an algorithm for calculating the edit distance between two given strings based on the recursive function described in (2). Show its complexity in both space and time.
(4) Calculate the edit distance between and .
(5) Describe three applications of the edit distance.
题目描述
两个字符串 与 的编辑距离,是用以下三种操作把 变成 所需的最少操作次数:插入一个字符、删除一个字符、把一个字符替换为另一个字符。例如,从 sport 删除字符 p 即可得到 sort,所以二者编辑距离为 1。
- 求
commuter与computers的编辑距离。 - 记 为字符串 的长度为 的前缀, 为 与 的编辑距离。写出 与 、、 之间的递推关系。
- 根据第 2 问的递推式,给出高效计算两个字符串编辑距离的算法,并说明其时间复杂度与空间复杂度。
- 用上述算法求
abrabr与arbarb的编辑距离。 - 举出编辑距离的三种应用。
Kai
以下、、 とする。
(1)
次の二回の操作で変換できる。
commuter
↓ 4文字目の m を p に置換
computer
↓ 末尾に s を挿入
computers
一方、二文字列の長さは異なるため、一回だけで変換できるなら、その操作は挿入でなければならない。しかし、commuter は computers の部分列ではないので、一回の挿入だけでは変換できない。
したがって、編集距離は
である。
(2)
末尾の文字に対する置換コストを
と定める。境界条件は
である。 に対して、求める漸化式は
である。
(3)
の表 を用意し、短い接頭辞から順に値を計算する。
EditDistance(x, y):
n <- length(x)
k <- length(y)
D[0..n][0..k] を用意する
for i <- 0 to n:
D[i][0] <- i
for j <- 0 to k:
D[0][j] <- j
for i <- 1 to n:
for j <- 1 to k:
if x[i] = y[j]:
cost <- 0
else:
cost <- 1
D[i][j] <- min(
D[i-1][j] + 1,
D[i][j-1] + 1,
D[i-1][j-1] + cost
)
return D[n][k]
各 を一度ずつ、定数時間で計算するので、時間計算量は
である。表全体を保存する場合の空間計算量も
である。
ただし、ある行の計算に必要なのは現在の行と直前の行だけである。短い方の文字列を列方向に取って二行だけを保持すれば、空間計算量は
まで削減できる。この場合も時間計算量は のままである。
(4)
行を abrabr の接頭辞、列を arbarb の接頭辞に対応させると、動的計画法の表は次のようになる。
| a | r | b | a | r | b | ||
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
| a | 1 | 0 | 1 | 2 | 3 | 4 | 5 |
| b | 2 | 1 | 1 | 1 | 2 | 3 | 4 |
| r | 3 | 2 | 1 | 2 | 2 | 2 | 3 |
| a | 4 | 3 | 2 | 2 | 2 | 3 | 3 |
| b | 5 | 4 | 3 | 2 | 3 | 3 | 3 |
| r | 6 | 5 | 4 | 3 | 3 | 3 | 4 |
したがって、
である。実際、次の四回の置換で変換できる。
abrabr
→ arrabr (2文字目: b → r)
→ arbabr (3文字目: r → b)
→ arbarr (5文字目: b → r)
→ arbarb (6文字目: r → b)
(5)
編集距離の代表的な応用として、次の三つが挙げられる。
- スペルチェックや入力ミスの自動訂正における候補語の順位付け。
- DNA・RNA・タンパク質配列の類似度評価や配列比較。
- 検索システム、氏名照合、重複データ検出などにおける近似文字列照合。