编辑问题

发布于 2024-08-16 20:48:43 字数 301 浏览 13 评论 0原文

Levenshtein Distance 算法中,这条线的作用是什么?:

    d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);

虽然它得到了所有距离中的最小值在这些值中,为什么成本添加到末尾,为什么我们在每个数组索引器(前两个参数)的末尾有 + 1?

In the Levenshtein Distance algorithm, what does this line do?:

    d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);

Although it gets the minimum of all of those values, why is cost added to the end, and why do we have + 1 at the end of each array indexer (first two parameters)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-08-23 20:48:47

这是一个公式:

alt text

m(a,b) = if a==b then 0 else 1

带“min”的行是递归公式本身,其他情况是递归导致的非递归情况。

您的线路正在使用数组中的“缓存”结果。尝试将其视为:

 d(i, j) = Minimum (d(i-1, j)+1, d(i, j-1)+1, d(i-1, j-1) + cost);

costm(a,b),如果 a==b 则为零,并且在其他情况

Here is a formula:

alt text

m(a,b) = if a==b then 0 else 1

The line with "min" is the recursive formula itself, the others are non-recursive cases to which the recursion leads.

Your line is using "caching" the results in array. Try to look at it as at:

 d(i, j) = Minimum (d(i-1, j)+1, d(i, j-1)+1, d(i-1, j-1) + cost);

cost is m(a,b), and it's zero if a==b and one in the other case

情深如许 2024-08-23 20:48:47

摘自文章:

                (
                  d[i-1, j] + 1,  // deletion
                  d[i, j-1] + 1,  // insertion
                  d[i-1, j-1] + 1 // substitution
                )

该算法的目的是找到将一个字符串(或列表)转换为另一个字符串的最便宜的路径。任何给定操作的“成本”都在您引用的行中考虑。在你的系统中,“删除”被认为是成本 1,“插入”也是 1,而“替换”则被认为是可变成本。

From the article:

                (
                  d[i-1, j] + 1,  // deletion
                  d[i, j-1] + 1,  // insertion
                  d[i-1, j-1] + 1 // substitution
                )

The algorithm's purpose is to find the cheapest path to convert one string (or list) to another string. The "cost" for any given operation is considered in the line you've referenced. In yours, "deletes" are considered to be a cost of 1, "inserts" also 1, and "substitution" at a variable cost.

江湖彼岸 2024-08-23 20:48:47

如果您进一步阅读,您会知道:我们可以对插入、删除和替换给予不同的惩罚成本。我们还可以根据插入、删除或替换的字符给出惩罚成本。

例如,如果您认为删除字母会导致比字母替换更大的区别

if you readed further, you'd know: We can give different penalty costs to insertion, deletion and substitution. We can also give penalty costs that depend on which characters are inserted, deleted or substituted.

you'd e.g. include some >0 cost value in the deletion part of the formula, if you suppose than a letter deletion makes a bigger difference than a letter replacement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文