{
  "id": "edit-distance",
  "title": "Edit Distance",
  "lang": "en",
  "source_lang": "en",
  "summary": "Edit distance measures the minimum cost of transforming one sequence into another using specified operations, such as insertions, deletions, and substitutions.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "String edit distance"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/edit-distance/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/edit-distance/",
      "json": "https://aiwiki.page/zh/edit-distance.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Edit distance is a measure of dissimilarity between two sequences, defined as the minimum total cost of a sequence of allowed operations that transforms one into the other. The sequences may consist of characters, words, biological symbols, or other discrete elements. In its most common form, Levenshtein distance, the allowed operations are insertion, deletion, and substitution of a single element, each with unit cost. Other forms change the permitted operations or their costs. Edit distance is widely used in computer science, particularly in approximate text matching, natural language processing, and biological sequence comparison."
    },
    {
      "heading": "Definition",
      "text": "Let \\(x\\) and \\(y\\) be finite sequences over an alphabet \\(\\Sigma\\). An edit script is an ordered sequence of operations that converts \\(x\\) into \\(y\\). If \\(\\mathcal{E}(x,y)\\) is the set of valid scripts and \\(c(e)\\) is the cost of an operation, edit distance is\n\n\\[d(x,y)=\\min_{E\\in\\mathcal{E}(x,y)} \\sum_{e\\in E}c(e).\\]\n\nThis definition assumes that a valid transformation exists and that a minimum is attained, as in the usual finite-sequence models with positive edit costs. With unit costs, distance is simply the smallest number of operations required.\n\nThe standard Levenshtein operations are:\n\n- Insertion: add one element.\n- Deletion: remove one element.\n- Substitution: replace one element with another.\n\nFor example, kitten can be changed into sitting in three edits:\n\n- Substitute s for k: sitten.\n- Substitute i for e: sittin.\n- Insert g at the end: sitting.\n\nNo script using fewer than three such operations exists, so the Levenshtein distance is 3.\n\nThe definition depends on the unit being compared. Character-level and word-level distances can give substantially different results for the same text. In language-processing systems, this choice is closely related to tokenization."
    },
    {
      "heading": "Mathematical properties",
      "text": "For standard unit-cost Levenshtein distance, sequences of lengths \\(m\\) and \\(n\\) satisfy\n\n\\[|m-n|\\leq d(x,y)\\leq \\max(m,n).\\]\n\nThe lower bound follows because only insertion and deletion change length. The upper bound follows by substituting elements in the overlapping positions and then inserting or deleting the remainder.\n\nLevenshtein distance is a metric: for all sequences \\(x,y,z\\),\n\n\\[\\begin{aligned} d(x,y)&\\geq 0,\\\\ d(x,y)&=0 \\iff x=y,\\\\ d(x,y)&=d(y,x),\\\\ d(x,z)&\\leq d(x,y)+d(y,z). \\end{aligned}\\]\n\nThe triangle inequality follows by concatenating a script from \\(x\\) to \\(y\\) with one from \\(y\\) to \\(z\\). The optimal script from \\(x\\) to \\(z\\) cannot cost more than that particular combined script.\n\nNot every measure called an edit distance has these properties. Unequal costs for insertion and deletion can make the result asymmetric. Zero-cost changes between distinct sequences can violate the identity condition, while restrictions on how operations may interact can invalidate the triangle inequality."
    },
    {
      "heading": "Dynamic programming computation",
      "text": "The classical exact algorithm for Levenshtein distance uses dynamic programming. Let \\(D(i,j)\\) be the distance between the first \\(i\\) elements of \\(x\\) and the first \\(j\\) elements of \\(y\\). The boundary conditions are\n\n\\[D(i,0)=i,\\qquad D(0,j)=j.\\]\n\nFor \\(i,j>0\\),\n\n\\[D(i,j)= \\min \\begin{cases} D(i-1,j)+1,\\\\ D(i,j-1)+1,\\\\ D(i-1,j-1)+[x_i\\neq y_j], \\end{cases}\\]\n\nwhere the bracketed expression is 0 when the elements match and 1 otherwise. The three alternatives correspond to deleting \\(x_i\\), inserting \\(y_j\\), and matching or substituting the two elements.\n\nThe answer is \\(D(m,n)\\). An optimal edit script can be recovered by tracing an optimal path backward through the table. Multiple optimal scripts may exist.\n\nThe table can also be interpreted as a shortest-path problem on a directed acyclic grid graph. Each grid position represents a pair of prefixes, and each edge represents an edit or a zero-cost match.\n\nTime and space requirements\n\nFor two nonempty sequences, the full table has time complexity \\(O(mn)\\) and space complexity \\(O(mn)\\). If only the distance is needed, retaining the previous row and the current row reduces auxiliary space to \\(O(\\min(m,n))\\).\n\nRecovering an optimal script with low memory requires additional techniques. A divide-and-conquer reconstruction can avoid storing the entire table while retaining quadratic time.\n\nSeveral methods improve performance for particular settings:\n\n- Thresholded computation: when only distances up to \\(k\\) matter, \\(|m-n|>k\\) immediately rules out a match.\n- Banded dynamic programming: any unit-cost alignment of total cost at most \\(k\\) stays within \\(k\\) diagonals of the main diagonal. Computing only this band can reduce work to \\(O(k\\max(m,n))\\), for positive \\(k\\), when the threshold is small.\n- Bit-parallel methods: encode several dynamic-programming states in machine words and update them using bitwise operations.\n- Similarity-sensitive methods: perform less work when the sequences differ only slightly.\n\nThese methods do not make all sequence comparisons linear-time problems; their advantages depend on the input lengths, edit threshold, operation costs, and machine model."
    },
    {
      "heading": "Major variants",
      "text": "Levenshtein distance\n\nLevenshtein distance permits single-element insertions, deletions, and substitutions. It is often what an unqualified reference to “edit distance” means, although edit distance is the broader family of measures.\n\nWeighted Levenshtein distance assigns different costs to operations or symbol pairs. For example, substituting two nearby keyboard letters may be assigned a lower cost than substituting unrelated letters. The recurrence retains the same structure, with the unit costs replaced by the chosen weights.\n\nWhen substitution costs more than deletion followed by insertion, an optimal script can avoid direct substitution altogether.\n\nInsertion–deletion distance\n\nIf only insertions and deletions are allowed, distance is related to the longest common subsequence. If its length is \\(L\\), then\n\n\\[d_{\\mathrm{indel}}(x,y)=m+n-2L.\\]\n\nAn optimal transformation preserves a longest common subsequence, deletes the remaining elements of \\(x\\), and inserts the remaining elements of \\(y\\).\n\nThis model is closely related to line-oriented comparison tools used in version control, although practical tools may use heuristics and presentation rules rather than directly minimize a character-level edit distance.\n\nHamming distance\n\nHamming distance counts unequal positions in sequences of equal length. It permits no insertions or deletions, so positions cannot shift relative to one another.\n\nFor example, abc and bca have Hamming distance 3 but Levenshtein distance 2: delete the initial a and insert it at the end. Hamming distance is particularly important in error-correcting codes for substitution errors; insertion and deletion errors require models that also account for shifts in alignment.\n\nTransposition-aware distances\n\nDamerau–Levenshtein distance additionally allows the transposition of two adjacent elements. Under unit costs, ab and ba are therefore distance 1 rather than 2.\n\nA commonly confused variant is optimal string alignment distance, which restricts repeated editing of the same substring. It is not identical to unrestricted Damerau–Levenshtein distance and does not generally satisfy the triangle inequality. For example, its distances satisfy\n\n\\[d(\\texttt{CA},\\texttt{AC})=1,\\qquad d(\\texttt{AC},\\texttt{ABC})=1,\\]\n\nbut\n\n\\[d(\\texttt{CA},\\texttt{ABC})=3.\\]\n\nThe distinction matters when software promises metric properties or uses indexing techniques that rely on them.\n\nSequence alignment with gap costs\n\nIn sequence alignment, runs of insertions or deletions are often treated as gaps. An affine gap cost assigns one penalty for opening a gap and another for extending it, commonly written\n\n\\[g(\\ell)=g_{\\mathrm{open}}+(\\ell-1)g_{\\mathrm{extend}}, \\qquad \\ell\\geq 1.\\]\n\nSuch models distinguish a long contiguous gap from many separate gaps. Their dynamic programs require separate states for matches and the different gap types. They are important in comparing DNA and protein sequences, though biological alignment scores are not always distances or metrics."
    },
    {
      "heading": "Applications",
      "text": "Approximate search and correction\n\nEdit distance supports spelling correction, fuzzy lookup, record linkage, and duplicate detection. A system can retrieve entries within a chosen distance of a query or rank candidates by distance.\n\nIn information retrieval, distance is often combined with word frequency, linguistic context, or domain-specific constraints. The closest spelling is not necessarily the intended word.\n\nSpeech and text recognition evaluation\n\nIn speech recognition, word error rate is based on an optimal word-level alignment:\n\n\\[\\mathrm{WER}=\\frac{S+D+I}{N},\\]\n\nwhere \\(S\\), \\(D\\), and \\(I\\) are substitution, deletion, and insertion counts, and \\(N\\) is the number of words in the reference. Character error rate applies the corresponding calculation to characters.\n\nWord error rate can exceed 1 because insertions are not bounded by reference length. It measures transcription disagreement, not the semantic importance of the errors.\n\nBiological comparison\n\nSequence comparison uses edit-like models to identify similarities, propose alignments, and quantify differences between biological sequences. The choice of costs matters: substitutions, gap openings, and gap extensions need not represent equally plausible biological events. A minimum-cost alignment is optimal under its scoring model, not necessarily a reconstruction of the actual evolutionary history."
    },
    {
      "heading": "History",
      "text": "Vladimir Levenshtein's work on codes correcting deletions, insertions, and reversals appeared in Russian in 1965 and in English translation in 1966. His name became attached to the insertion–deletion–substitution distance.\n\nFrederick Damerau's 1964 work on spelling errors treated adjacent transpositions alongside insertion, deletion, and substitution. Robert Wagner and Michael Fischer's 1974 paper, “The String-to-String Correction Problem,” established a widely cited dynamic-programming formulation for string correction. Related alignment methods also developed in biological sequence analysis."
    },
    {
      "heading": "Interpretation and limitations",
      "text": "Edit distance measures changes to a representation, not meaning. Two sentences can have a small distance while expressing opposite claims, or a large distance while conveying nearly the same information.\n\nResults also depend on preprocessing and encoding. A visible character may consist of several Unicode code points, and equivalent text can have different code-point sequences before normalization. Distances computed over bytes, code points, or grapheme clusters therefore need not agree.\n\nCase folding, punctuation removal, and whitespace normalization can improve comparability for some tasks, but they also discard distinctions. These choices are part of the comparison model.\n\nRaw distance generally increases with sequence length. A common normalization divides Levenshtein distance by \\(\\max(m,n)\\), with a separate convention for two empty sequences. This produces values between 0 and 1, but does not necessarily preserve metric properties. Thresholds and reported scores are therefore meaningful only alongside the operation set, costs, sequence units, preprocessing rules, and normalization convention."
    }
  ],
  "links": [
    "computer-science",
    "natural-language-processing",
    "tokenization",
    "metric-space",
    "algorithm",
    "dynamic-programming",
    "shortest-path-problem",
    "time-complexity",
    "space-complexity",
    "divide-and-conquer",
    "version-control",
    "hamming-distance",
    "error-correcting-code",
    "damerau-levenshtein-distance",
    "dna",
    "protein",
    "information-retrieval",
    "speech-recognition",
    "unicode"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}