Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Edit Distance

Also Levenshtein Distance · ED

The edit distance ED(x, y) (Levenshtein distance) is the minimal number of single-character insert, delete and replace operations that transform the string x into the string y.

Formula#

Recurrence (for n,m>0n, m > 0):

ED(x,y)=min⁡{ED(x[1..n], y[1..m−1])+1(1) insert y[m]ED(x[1..n−1], y[1..m])+1(2) delete x[n]ED(x[1..n−1], y[1..m−1])+1(3a) replace, if x[n]≠y[m]ED(x[1..n−1], y[1..m−1])(3b) keep, if x[n]=y[m]\mathrm{ED}(x, y) = \min \begin{cases} \mathrm{ED}(x[1..n],\ y[1..m-1]) + 1 & \text{(1) insert } y[m] \\ \mathrm{ED}(x[1..n-1],\ y[1..m]) + 1 & \text{(2) delete } x[n] \\ \mathrm{ED}(x[1..n-1],\ y[1..m-1]) + 1 & \text{(3a) replace, if } x[n] \ne y[m] \\ \mathrm{ED}(x[1..n-1],\ y[1..m-1]) & \text{(3b) keep, if } x[n] = y[m] \end{cases}

with ED(ε,y)=∣y∣\mathrm{ED}(\varepsilon, y) = |y| and ED(x,ε)=∣x∣\mathrm{ED}(x, \varepsilon) = |x|.

Dynamic programming. Fill an (n+1)×(m+1)(n+1) \times (m+1) table MM with M[i][j]=ED(x[1..i],y[1..j])M[i][j] = \mathrm{ED}(x[1..i], y[1..j]) (row/column 0 = ε\varepsilon):

  • M[i][0]=iM[i][0] = i, M[0][j]=jM[0][j] = j,
  • M[i][j]=min⁡(M[i−1][j−1]+s, M[i−1][j]+1, M[i][j−1]+1)M[i][j] = \min\bigl(M[i-1][j-1] + s,\ M[i-1][j] + 1,\ M[i][j-1] + 1\bigr) with s=0s = 0 if x[i]=y[j]x[i] = y[j], else s=1s = 1,
  • result ED(x,y)=M[n][m]\mathrm{ED}(x, y) = M[n][m] (bottom-right entry).

Each entry depends only on its left, upper and upper-left neighbour. Time and space Θ(n⋅m)\Theta(n \cdot m); space O(min⁡{n,m})O(\min\{n, m\}) suffices, since only the previous row is needed.

Properties#

ProofProof sketches (recommended as exam preparation)
  1. Every operation can be undone by one operation (insert ↔ delete, replace ↔ replace); reversing an optimal sequence x→yx \to y gives one y→xy \to x of the same length.
  2. ∣x∣|x| deletions suffice; each operation shortens by at most 1, so at least ∣x∣|x| are needed.
  3. Each operation changes the length by at most 1, and the length must change from ∣x∣|x| to ∣y∣|y|.
  4. Transform x[1..n−1]x[1..n-1] into y[1..m−1]y[1..m-1] optimally, then replace x[n]x[n] by y[m]y[m] (or do nothing if they are equal).
  • A single operation sequence only proves an upper bound on ED; the exact value needs the DP table or a lower-bound argument (e.g. property 3, or the Q-Gram Lemma read backwards).
  • ED is the relevance model for fuzzy search on whole words; for search-as-you-type it is replaced by the Prefix Edit Distance.

Example#

DOOF → BOOF (replace(1,B)) → BLOF (replace(2,L)) → BLOE (replace(4,E)) → BLOED (insert(5,D)) proves ED(DOOF,BLOED)≤4\mathrm{ED}(\text{DOOF}, \text{BLOED}) \le 4. The table for ED(BLOED,DOOF)\mathrm{ED}(\text{BLOED}, \text{DOOF}) shows equality:

ε\varepsilonDOOF
ε\varepsilon01234
B11234
L22234
O33223
E44333
D54444

Every entry is the ED of two prefixes, e.g. ED(BLO,DOO)=2\mathrm{ED}(\text{BLO}, \text{DOO}) = 2; the result is ED(BLOED,DOOF)=4\mathrm{ED}(\text{BLOED}, \text{DOOF}) = 4.

Exam WS 22/23, Task 2.1: ED(alfa,alpha)=2\mathrm{ED}(\text{alfa}, \text{alpha}) = 2 via alfa → alpa (replace f by p) → alpha (insert h); at least 2 are needed, since by property 3 one operation would have to be an insert, but deleting any one character of alpha (lpha, apha, alha, alpa, alph) never gives alfa.

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only