动态规划求解最短编辑距离

最短编辑距离

The edit distance between two strings is the minimum number of letter insertions(插入), letter
deletions(删除), and letter substitutions(替换) required to transform one string into another. For example, the edit distance between FOOD and MONEY is at most four:

在这里插入图片描述

递归结构

If we remove the last column, the remaining columns must represent the shortest edit sequence for the remaining prefixes.
If the prefixes had a shorter edit sequence, gluing the last column back on would gives us a shorter edit sequence for the original strings. So once we figure out what should happen in the last column, the Recursion Fairy can figure out the rest of the optimal gap representation.
Thus, for any two input strings A[1 … m] and B[1 … n], we can formulate the edit distance problem recursively as follows: For any indices i and j, let Edit(i, j) denote the edit distance between the prefixes A[1 … i] and B[1 … j]. We need to compute Edit(m, n).
We conclude that the Edit function satisfies the following recurrence:
在这里插入图片描述

动态规划

• Subproblems(子问题): Each recursive subproblem is identified by two indices 0 ≤ i ≤ m and
0 ≤ j ≤ n.
• Memoization structure(存储结构): So we can memoize all possible values of Edit(i, j) in a
two-dimensional array Edit[0 … m, 0 … n].
• Dependencies(依赖): Each entry Edit[i, j] depends only on its three neighboring entries
Edit[i − 1, j], Edit[i, j − 1], and Edit[i − 1, j − 1].
• Evaluation order(求值顺序):
So if we fill in our table in the standard row-major order—row by row from top down, each row from left to right—then whenever we reach an entry in the table, the entries it depends on are already available. (This isn’t the only evaluation order we could use, but it works, so let’s go with it.)
• Space and time(空间与时间复杂度): The memoization structure uses O(mn) space. We can compute each entry Edit[i, j] in O(1) time once we know its predecessors, so the overall algorithm runs in O(mn) time.
在这里插入图片描述
Transforming ALGORITHM into ALTRUISTIC. These edit sequences are shown in Figure below.

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gallant Hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值