# 编辑距离

编辑距离是衡量两个字符串差异程度的度量，定义为将一个字符串转换为另一个字符串所需编辑操作的最小总代价。标准版本称为[莱文斯坦距离](https://www.edgepedia.org/zh/levenshtein-distance)（Levenshtein distance），允许的操作包括插入单个字符、删除单个字符以及将一个字符替换为另一个字符，每种操作的代价均为单位代价。<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup><sup> • </sup><sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)</sup> 这一概念起源于弗拉基米尔·I·莱文斯坦（Vladimir I. Levenshtein）1965年关于能够纠正删除、插入和反转的二进制码的论文，该论文以俄文发表于《苏联科学院报告》（*Doklady Akademii Nauk SSSR*）。<sup>[3](https://www.mathnet.ru/php/archive.phtml?jrnid=dan&option_lang=eng&paperid=31411&wshow=paper)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 将一个字符串转换为另一个字符串所需的插入、删除和替换操作的最小数量（或代价）<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup><sup> • </sup><sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)</sup> |
| 起源 | V. I. 莱文斯坦，“能够纠正删除、插入和反转的二进制码”，《苏联科学院报告》163:4 (1965)，845–848<sup>[3](https://www.mathnet.ru/php/archive.phtml?jrnid=dan&option_lang=eng&paperid=31411&wshow=paper)</sup> |
| 标准算法 | 瓦格纳–菲舍尔（Wagner–Fischer）动态规划，1974年，运行时间与两个字符串长度的乘积成正比<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup> |
| 更快计算 | 迈尔斯（Myers）1999年的位向量算法在O(nm/w)时间内计算单位代价编辑距离，其中w为机器字长<sup>[4](https://bishtref.com/articles/10.1145/316542.316550)</sup> |
| 难度 | 精确计算一般需要二次时间，且很可能无法在强次二次时间内计算<sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)</sup> |
| 近似 | 一种随机算法在O~(n<sup>2−2/7</sup>)时间内以常数因子近似编辑距离<sup>[5](https://people.csail.mit.edu/virgi/6.1420/papers/editapprox.pdf)</sup> |
| 应用 | 拼写纠正、生物信息学中的序列比对、患者记录链接、文本处理和信息检索<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup><sup> • </sup><sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8274556/)</sup><sup> • </sup><sup>[7](https://pubmed.ncbi.nlm.nih.gov/15360771/)</sup> |

## 定义与变体

给定字符串x和y，编辑距离d(x, y)是将x转换为y所需的字符插入、删除和替换的最小数量。<sup>[5](https://people.csail.mit.edu/virgi/6.1420/papers/editapprox.pdf)</sup> 瓦格纳和菲舍尔对字符串到字符串纠正问题的表述更为一般：每个操作可以有自己的代价，距离是将一个字符串转换为另一个字符串的任意编辑操作序列的最小代价。<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup> 一种常见约定将替换的代价视为2，相当于一次插入加一次删除；另一种约定将其视为1。<sup>[8](https://web.stanford.edu/~jurafsky/slp3/2.pdf)</sup>

若干相关度量改变了操作集合。达梅劳–莱文斯坦距离（Damerau–Levenshtein distance）增加了两个相邻字符的换位（ab ↔ ba），代价为一个单位，因此莱文斯坦距离是它的特例。<sup>[9](https://cs.uef.fi/sipu/pub/StringSimilarity-ESWA-2019.pdf)</sup> [汉明距离](https://www.edgepedia.org/zh/hamming-distance)（Hamming distance）只允许替换，并要求两个字符串长度相等，计算它们不同的位置数。<sup>[9](https://cs.uef.fi/sipu/pub/StringSimilarity-ESWA-2019.pdf)</sup> 为蛋白质和核苷酸比对而开发的尼德曼–翁施（Needleman–Wunsch）和史密斯–沃特曼（Smith–Waterman）使用可变的插入和删除代价；史密斯–沃特曼执行局部比对，在两个字符串内寻找相似区域，而不是将它们从头到尾完整比对。<sup>[9](https://cs.uef.fi/sipu/pub/StringSimilarity-ESWA-2019.pdf)</sup> 编辑距离也与最长公共子序列（LCS）问题密切相关：瓦格纳和菲舍尔将确定两个字符串共有的最长字符子序列列为他们算法的一个应用，而LCS可以用类似于编辑距离的动态规划来计算。<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup><sup> • </sup><sup>[10](https://pmc.ncbi.nlm.nih.gov/articles/PMC10072857/)</sup> 除称为莱文斯坦距离外，该度量有时也被称为乌拉姆距离（Ulam distance）。<sup>[11](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)</sup>

## 动态规划算法

标准计算填充一个表D，其中D[i, j]保存源字符串前i个字符与目标字符串前j个字符之间的编辑距离。<sup>[8](https://web.stanford.edu/~jurafsky/slp3/2.pdf)</sup> 递推式基于最优子结构：如果完整字符串的最优变换经过一个中间字符串，那么到该点为止的序列部分本身必须是最优的。<sup>[8](https://web.stanford.edu/~jurafsky/slp3/2.pdf)</sup>

瓦格纳和菲舍尔1974年的论文《字符串到字符串纠正问题》（《美国计算机学会杂志》，第21卷，第1期，第168–173页）提出了该算法，并证明它以与两个字符串长度乘积成正比的时间求解该问题。<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup> 该论文从编辑操作出发，经由编辑序列和轨迹，依次推导出最小代价轨迹定理、最优子结构、动态规划递推式、O(mn)算法和回溯，并且它处理的编辑代价比通常作为莱文斯坦距离算法教授的单位代价版本要一般得多。<sup>[12](https://www.levenshtein.net/paper-wagner-fischer-1974)</sup> 一个常见错误是将矩阵算法归因于莱文斯坦1965年的论文本身；瓦格纳–菲舍尔的动态规划表述出现得更晚。<sup>[12](https://www.levenshtein.net/paper-wagner-fischer-1974)</sup>

二次运行时间在实践中可以通过两种经典方式减少。乌科宁（Ukkonen）的带状算法在已知距离上界时缩小搜索空间，因为最多d次编辑的比对必须保持在表对角线两侧宽度为d的带内。<sup>[13](https://pmc.ncbi.nlm.nih.gov/articles/PMC5408825/)</sup> 希尔施贝格（Hirschberg）的算法以空间换速度，将空间复杂度从二次降低到线性。<sup>[13](https://pmc.ncbi.nlm.nih.gov/articles/PMC5408825/)</sup>

## 更快与近似计算

**位并行。** 吉恩·迈尔斯（Gene Myers）1999年的论文《基于动态规划的近似字符串匹配快速位向量算法》（《美国计算机学会杂志》46(3): 395–415）计算查询的k-差异自动机状态集的位表示，运行时间为O(nm/w)，其中w是机器字长，实践中如32或64。<sup>[4](https://bishtref.com/articles/10.1145/316542.316550)</sup> 该方法利用了单位代价编辑距离的特殊性质，即动态规划表的相邻单元最多相差1，这使得每一行可以编码为两个位向量。<sup>[14](https://pmc.ncbi.nlm.nih.gov/articles/PMC6761980/)</sup> 尽管它是最快的确定性比对算法之一，但实现复杂且不支持全局比对，因此历史上很少被实现；Edlib库是作为迈尔斯位向量算法用于精确编辑距离比对的实用实现而创建的。<sup>[13](https://pmc.ncbi.nlm.nih.gov/articles/PMC5408825/)</sup> 现代库建立在相同思想之上：RapidFuzz使用迈尔斯算法的分块实现，当较短字符串在去除公共前后缀后至少为64个字符长时，一次计算64个字符的莱文斯坦距离。<sup>[15](https://rapidfuzz.github.io/rapidfuzz-cpp/group__Distance.html)</sup>

**难度与近似。** [计算](https://www.edgepedia.org/zh/computing)两个序列之间的编辑距离大约需要输入长度的二次时间，这在实践中仍然昂贵，而且很可能无法在强次二次时间内计算，这是巴库尔斯（Backurs）和因迪克（Indyk）的结果。<sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)</sup> 因此，大多数序列比对器依赖启发式方法快速检测可能具有良好比对的序列，在实践中他们使用局部敏感哈希来计算杰卡德相似度（Jaccard similarity）或汉明距离，作为编辑距离的代理，尽管这两种代理都不直接对应于编辑距离。<sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)</sup> 在理论方面，安多尼（Andoni）、克劳特加默（Krauthgamer）和奥纳克（Onak）给出了一种在近线性时间内以多对数因子近似编辑距离的算法，后来的工作提供了一种在O~(n<sup>2−2/7</sup>)时间内以常数因子近似编辑距离的随机算法。<sup>[5](https://people.csail.mit.edu/virgi/6.1420/papers/editapprox.pdf)</sup> 运行时间依赖于输出的算法也可以利用短距离：对于长度为n和m的字符串，编辑距离s可以使用线性内存在O((s − |n − m|) · min(m, n, s) + m + n)时间内计算，其中s表示距离本身。<sup>[16](https://arxiv.org/abs/0807.4368)</sup>

## 应用

**拼写纠正与文本处理。** 瓦格纳和菲舍尔将自动拼写纠正列为字符串到字符串纠正问题的一个动机应用。<sup>[1](https://cir.nii.ac.jp/crid/1363670320103750912)</sup> 基于编辑的度量仍然是噪声文本规范化的标准；在一项对马来语噪声文本的评估中，莱文斯坦、达梅劳–莱文斯坦和拉特克利夫（Ratcliff）度量表现良好，而包含大量换位字符的文本则更适合用拉特克利夫或贾罗–温克勒（Jaro–Winkler）度量处理。<sup>[17](https://jati.um.edu.my/index.php/MJCS/article/download/63760/19773/193095)</sup>

**生物信息学。** 生物序列比较可以重新表述为确定DNA核苷酸或蛋白质氨基酸字符串之间的莱文斯坦距离，莱文斯坦编辑距离在序列比对和生物数据库相似性搜索中发挥了核心作用。<sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8274556/)</sup> 编辑距离是测序读段比对中衡量序列间差异的主要度量，通常通过尼德曼–翁施或史密斯–沃特曼等广义形式实现。<sup>[18](https://pmc.ncbi.nlm.nih.gov/articles/PMC8317111/)</sup> 由于二次时间的精确比对对于现代数据量来说太慢，莱文斯坦编辑距离的数学一般性在大规模工具中已让位于基于k聚体匹配的快速启发式方法。<sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8274556/)</sup> 一种相关的修改，序列-莱文斯坦距离（Sequence-Levenshtein distance），允许嵌入序列之间的移码而不增加额外惩罚，解决了对嵌入较长字符串中的核酸进行测序时引入的插入和删除错误。<sup>[19](https://pmc.ncbi.nlm.nih.gov/articles/PMC10614987/)</sup>

**记录链接。** 在医疗记录链接中，包括莱文斯坦编辑距离、修改的贾罗–温克勒和最长公共子串在内的近似字符串比较器用于比较独立数据源中的对应姓名。在比较器得分阈值为0.8时，贾罗–温克勒比较器在两个医院中实现了最高的链接灵敏度，分别为97.4%和97.7%，并且与精确匹配比较相比，近似比较器将确定性链接灵敏度提高了最多10%。<sup>[7](https://pubmed.ncbi.nlm.nih.gov/15360771/)</sup>

## References

1. [Robert A. Wagner and Michael J. Fischer (1974). The String-to-String Correction Problem. Journal of the ACM 21(1): 168–173.](https://cir.nii.ac.jp/crid/1363670320103750912)
2. [Locality-sensitive hashing for the edit distance, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC6612865/)
3. [V. I. Levenshtein, "Binary codes capable of correcting deletions, insertions, and reversals", Dokl. Akad. Nauk SSSR 163:4 (1965), 845–848](https://www.mathnet.ru/php/archive.phtml?jrnid=dan&option_lang=eng&paperid=31411&wshow=paper)
4. [Gene Myers (1999). A Fast Bit-Vector Algorithm for Approximate String Matching Based on Dynamic Programming. Journal of the ACM 46(3): 395–415.](https://bishtref.com/articles/10.1145/316542.316550)
5. [Approximating Edit Distance Within Constant Factor in Truly Sub-Quadratic Time, arXiv](https://people.csail.mit.edu/virgi/6.1420/papers/editapprox.pdf)
6. [Levenshtein Distance, Sequence Comparison and Biological Database Search, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC8274556/)
7. [Real world performance of approximate string comparators for use in patient matching, PubMed](https://pubmed.ncbi.nlm.nih.gov/15360771/)
8. [Minimum Edit Distance, Speech and Language Processing (Jurafsky & Martin), Stanford](https://web.stanford.edu/~jurafsky/slp3/2.pdf)
9. [N. Gali, R. Mariescu-Istodor and D. Hostettler et al. (2019). Framework for syntactic string similarity measures. Expert Systems With Applications 129: 169–185.](https://cs.uef.fi/sipu/pub/StringSimilarity-ESWA-2019.pdf)
10. [Efficient and Precise Secure Generalized Edit Distance and Beyond, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC10072857/)
11. [Dynamic Programming, 3.7 Edit Distance, Algorithms by Jeff Erickson](https://jeffe.cs.illinois.edu/teaching/algorithms/book/Algorithms-JeffE-2up.pdf)
12. [Wagner–Fischer 1974: The String-to-String Correction Problem, Dynamic Programming, and Edit Distance, levenshtein.net](https://www.levenshtein.net/paper-wagner-fischer-1974)
13. [Edlib: a C/C++ library for fast, exact sequence alignment using edit distance, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC5408825/)
14. [Bit-parallel sequence-to-graph alignment, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC6761980/)
15. [RapidFuzz: Distance Algorithms documentation](https://rapidfuzz.github.io/rapidfuzz-cpp/group__Distance.html)
16. [Improved Algorithms for Approximate String Matching, arXiv](https://arxiv.org/abs/0807.4368)
17. [Evaluating Similarity Measures for Malay Noisy Text, Malaysian Journal of Computer Science](https://jati.um.edu.my/index.php/MJCS/article/download/63760/19773/193095)
18. [Hardware acceleration of genomics data analysis: challenges and opportunities, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC8317111/)
19. [Interpreting Sequence-Levenshtein distance for determining error type and frequency between two embedded sequences of equal length, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC10614987/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Numerical, string, and geometric algorithms › String algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/edit-distance

English original: https://www.edgepedia.org/edit-distance
