Skip to content

Edit distance

planning ahead

Level 2 of 3

Also called Levenshtein distance and Wagner-Fischer algorithm.

how it works

The idea on crate into cart, the example of the books this site follows (volume three, chapter 40). Then the whole method on the watch page's first example, seat into eats.

An edit changes a word by one letter: delete a letter, insert one, or replace one with another. How few edits turn crate into cart? Keep c, replace r with a, replace a with r, keep t and delete e: 3 edits. You might say swap r and a, then delete e. But a swap is not one of the edits, so it costs 2. To be sure nothing takes fewer, fill a table, as the books do. Each row stands for a start of crate, the empty start first, and each column for a start of cart. A cell holds the fewest edits that turn its row's start into its column's start. The first row and column take one edit a letter: nothing into car is 3 inserts, and cra into nothing is 3 deletes. Fill the other cells row by row. Compare the last letters of the cell's two starts. If they are the same, copy the cell up and to the left. If not, take 1 more than the cheapest of three cells. Above is for a delete, to the left for an insert, and up and to the left for a replace. For cr into ca: delete r, then c into ca, the cell above, holds 1, so 2. Or cr into c, to the left, holds 1, then insert a, so 2. Or c into c, up and to the left, holds 0, then replace r with a, so 1. The cell holds the cheapest, 1. The last cell is the answer. That fewest is the edit distance: 3 for crate into cart. To see which edits, read back: from the last cell, step to a cell it was built from. That is up and to the left when the letters match, or else one holding 1 fewer. Repeat up to the first cell. Written one above the other, the two words then make a line-up of the edits. This page counts cells, one for each cell the rule fills, not the first row and column: 20 for crate into cart, as the watch page counts. The code below, in the language Python, does the same.

start-ccacarcart
-01234
c10123
cr21112
cra32122
crat43222
crate54333
Crate into cart. Each row is a start of crate and each column a start of cart, and a dash is the empty start. The last cell holds 3, the answer.
cellholdsstep toedit
crate into cart3abovedelete e
crat into cart2up-leftkeep t
cra into car2up-leftreplace a with r
cr into ca1up-leftreplace r with a
c into c0up-leftkeep c
Reading back from the last cell. A step up and to the left keeps or replaces, a step up deletes, and a step left inserts. Read from the bottom, these are the line-up below.
cratecrate
cartcart-
editkeepreplacereplacekeepdelete
One line-up of the 3 edits, read back from the table. A dash is a gap. Crate into cart has 4 such line-ups, each of 3 edits.
Seat into eats: 8 moments of its 18 steps, the steps between them left for the watch page
  1. step 1 of 18

    eatsseat012341234

    Turn seat into eats. An empty start takes one edit a letter, so the first row counts inserts and the first column counts deletes.

    what it remembers to know where it is
    letters compared
    not yet
    edits in this cell
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 2 of 18

    eatsseat0123411234

    s is not e, so take one edit more than the cheapest of above, 1, for a delete, to the left, 1, for an insert, and up and to the left, 0, for a replace: 1.

    what it remembers to know where it is
    letters compared
    s e
    edits in this cell
    1

    watch from here

  3. step 6 of 18

    eatsseat01234112332134

    Both letters are e, so this cell needs no edit of its own. Copy 1 from the cell up and to the left.

    what it remembers to know where it is
    letters compared
    e e
    edits in this cell
    1

    watch from here

  4. step 9 of 18

    eatsseat01234112332123434

    e is not s, so take one edit more than the cheapest of above, 3, for a delete, to the left, 3, for an insert, and up and to the left, 3, for a replace: 4.

    what it remembers to know where it is
    letters compared
    e s
    edits in this cell
    4

    watch from here

  5. step 11 of 18

    eatsseat0123411233212343214

    Both letters are a, so this cell needs no edit of its own. Copy 1 from the cell up and to the left.

    what it remembers to know where it is
    letters compared
    a a
    edits in this cell
    1

    watch from here

  6. step 16 of 18

    eatsseat012341123321234321234321

    Both letters are t, so this cell needs no edit of its own. Copy 1 from the cell up and to the left.

    what it remembers to know where it is
    letters compared
    t t
    edits in this cell
    1

    watch from here

  7. step 17 of 18

    eatsseat0123411233212343212343212

    t is not s, so take one edit more than the cheapest of above, 3, for a delete, to the left, 1, for an insert, and up and to the left, 2, for a replace: 2.

    what it remembers to know where it is
    letters compared
    t s
    edits in this cell
    2

    watch from here

  8. step 18 of 18

    eatsseat0123411233212343212343212

    Done: turning seat into eats takes 2 edits at the fewest.

    what it remembers to know where it is
    letters compared
    not yet
    edits in this cell
    2

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. the first row and column: an empty start takes one edit a letterPython lines 9, 10, 11, 12 and 13
  2. for each other cell, row by row, compare the two letters it stands forPython lines 14, 15 and 16
  3. the same letter, so no more edits than the cell up and to the leftPython line 17
  4. different: one more than the cheapest of above, to the left and up and to the leftPython lines 18 and 19
  5. the last cell is the fewest editsPython line 20
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

def edit_distance(a, b):
    """The fewest one-letter edits that turn word a into word b.

    An edit deletes a letter, inserts a letter or replaces one with another.
    a and b are words of any length. Returns the fewest edits.
    """
    m, n = len(a), len(b)
    # cell[i][j]: the fewest edits from a's first i letters to b's first j
    cell = [[0] * (n + 1) for _ in range(m + 1)]
    for j in range(n + 1):
        cell[0][j] = j  # nothing into j letters: j inserts
    for i in range(m + 1):
        cell[i][0] = i  # i letters into nothing: i deletes
    for i in range(1, m + 1):  # each other cell, row by row
        for j in range(1, n + 1):
            if a[i - 1] == b[j - 1]:  # the same last letter: no edit of its own
                cell[i][j] = cell[i - 1][j - 1]  # copy up and to the left
            else:  # one more than the cheapest of above, left and up-left
                cell[i][j] = 1 + min(cell[i - 1][j], cell[i][j - 1], cell[i - 1][j - 1])
    return cell[m][n]  # the last cell

how it scales

The plot counts the cells the method fills on two words of nn letters each, measured from 10 to 100,000 letters.

Each mark across or up is ten times the one before. The line climbs two marks up for each mark across: 10,000 cells at 100 and 1,000,000 at 1,000: ten times the letters, 100 times the cells. Here the cost is the cells, and a slope of 2 means they grow as n2n^2, as section 4's question 1 derives. Twice the letters make 4 times the cells.

cells filled, ten times more at each mark up

a seconda minutean houra dayslope 2102104106108101010121014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107

nn, how many letters the second word has, ten times more at each mark to the right

  • two words of nn letters each, whatever their letterssolid where measured by running it; dashed where its formula carries it on to ten millionslope 2: ten times n, 100 times the work
how long filling the cells takes, at a billion a second
nncells filledtime
1,0001,000,000about 1 millisecond
100,00010,000,000,000about 10 seconds
10,000,000100,000,000,000,000about 1.2 days

The cells filled are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count edit distance's cells yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each n from 10 to 100,000 it prints
the cells the method fills on two words of n letters each. The counts are
the same on any computer, whatever the letters; only the time differs.
"""


def edit_distance(a, b):
    """The fewest one-letter edits that turn word a into word b.

    An edit deletes a letter, inserts a letter or replaces one with another.
    a and b are words of any length. Returns the fewest edits.
    """
    m, n = len(a), len(b)
    # cell[i][j]: the fewest edits from a's first i letters to b's first j
    cell = [[0] * (n + 1) for _ in range(m + 1)]
    for j in range(n + 1):
        cell[0][j] = j  # nothing into j letters: j inserts
    for i in range(m + 1):
        cell[i][0] = i  # i letters into nothing: i deletes
    for i in range(1, m + 1):  # each other cell, row by row
        for j in range(1, n + 1):
            if a[i - 1] == b[j - 1]:  # the same last letter: no edit of its own
                cell[i][j] = cell[i - 1][j - 1]  # copy up and to the left
            else:  # one more than the cheapest of above, left and up-left
                cell[i][j] = 1 + min(cell[i - 1][j], cell[i][j - 1], cell[i - 1][j - 1])
    return cell[m][n]  # the last cell


def count(a, b):
    """How many cells the method fills: each letter of a makes a row, with
    one cell for each letter of b, whatever the letters."""
    cells = 0
    for _ in a:  # a row for each letter of a
        cells += len(b)  # a cell in it for each letter of b
    return cells


LETTERS = "abcdefghijklmnopqrstuvwxyz"


def plot_words(n):
    """The plot's two words of n letters: the first from a, the second
    from n, round the alphabet."""
    first = "".join(LETTERS[k % 26] for k in range(n))
    second = "".join(LETTERS[(13 + k) % 26] for k in range(n))
    return first, second


if __name__ == "__main__":
    print(f"{'n':>7} {'cells':>12}")
    for n in (10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000, 50_000, 100_000):
        print(f"{n:>7} {count(*plot_words(n)):>12}")

best, worst and average

Best, worst and average usually name different inputs of one size. Here the size is nn, the second word's length, and this page gives the first word nn letters too. Then every pair fills the same cells, whatever its letters. So the table has one column, under the name worst, and no average is worked out. In the grid below, from 1 up means for any nn of 1 or more.

A case is one kind of input: here, which letters. A bound says how fast its cost grows as the words grow.

The counts are in the grid below. A count is often made of pieces that grow at different speeds, and the fastest-growing piece swamps the rest. With its first row and column, the table holds n2+2n+1n^2 + 2n + 1 numbers, as question 2 works out: at 1,000 letters each, 1,000,000 plus 2,001, and the 2,001 hardly matters. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
worsttwo words of nn letters each, whatever their letters
cells filledn2n^2, from 1 up
how the cells filled grow in each case
tight, Θ\ThetaΘ(n2)\Theta(n^2)
ceiling, OOO(n2)O(n^2)
floor, Ω\OmegaΩ(n2)\Omega(n^2)

extra memory: A table of one number for each pair of starts, or two rows for the answer alone.

Extra memory is the room it needs on top of the input itself, while it runs.

first wordsecond wordcellsedits
apeape90
apepea92
apedog93
Three pairs of 3 letters, counted by the run. The edits change with the letters, and the cells do not, 3 times 3.
ncellsnumbers held
114
249
3916
74964
10010,00010,201
1,0001,000,0001,002,001
Two words of nn letters each. The table also holds its first row and column, as question 2 counts.

where the formula comes from

Five questions, each worked line by line. Question 1 is the count the plot draws.

Question 1: how many cells, CC, does the method fill on two words of nn letters, from 1 up?
  1. the move: a row for each letter

    the line:
    m letters into n letters:m×n cells\begin{gathered} m \text{ letters into } n \text{ letters:} \\ m \times n \text{ cells} \end{gathered}
    order-ppepea
    -givengivengivengiven
    agiven123
    apgiven456
    apegiven789
    Ape into pea, each cell numbered in the order it is filled, row by row. Given cells are written first, not counted. 3 rows of 3, 9 cells.

    why it is allowed: Each letter of the first word has a row, and each row fills one cell for each letter of the second. Crate into cart fills 5 times 4, 20 cells.

  2. the move: both of n letters

    the line:
    C=n×nC = n \times n

    why it is allowed: Here the first word has nn letters too, so mm is nn. For 3, 3 times 3, which is 9.

  3. the move: write it shorter

    the line:
    C=n2C = n^2

    why it is allowed: nn times nn is written n2n^2, read nn squared. For 3, 9.

  4. the move: a ceiling

    the line:
    C≤n2C \le n^2

    why it is allowed: By line 3, as a number is at most itself.

  5. the move: read the bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 4, with the fixed multiple 1. ∈\in reads is in, and O(n2)O(n^2) means at most a fixed multiple of n2n^2, from some nn on.

  6. the move: a floor

    the line:
    C≥n2C \ge n^2

    why it is allowed: By line 3, as a number is at least itself.

  7. the move: read the bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)

    why it is allowed: By line 6, with the fixed multiple 1. Ω(n2)\Omega(n^2) means at least a fixed multiple of n2n^2, above 0, from some nn on.

  8. the move: both together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 5 and 7 bound CC by the same growth. Θ(n2)\Theta(n^2) means both.

try it with a small nn: at n=3n = 3, line 2 gives C=9C = 9, which you can work out by hand

Question 2: how many numbers, TT, does the table hold on two words of nn letters, from 1 up?
  1. the move: count the rows and columns

    the line:
    T=(n+1)×(n+1)T = (n + 1) \times (n + 1)
    start-ppepea
    -0123
    a1122
    ap2123
    ape3212
    Ape into pea, 4 rows of 4 numbers, 16 in all. The first row and column count, though no step fills them.

    why it is allowed: A row for the empty start, then one for each letter of the first word, so n+1n + 1 rows. The columns are the same for the second word. For 3, 4 times 4, which is 16.

  2. the move: multiply out

    the line:
    T=n2+2n+1T = n^2 + 2n + 1

    why it is allowed: nn times nn, then nn twice, then 1 times 1. For 3, 9 + 6 + 1, which is 16.

  3. the move: a ceiling

    the line:
    T≤4n2T \le 4n^2

    why it is allowed: From 1 up, nn is at most n2n^2, and so is 1, so 2n+12n + 1 is at most 3 lots of n2n^2, and line 2's sum at most 4 lots. For 3, 16 is at most 36.

  4. the move: read the bound

    the line:
    T∈O(n2)T \in O(n^2)

    why it is allowed: By line 3, with the fixed multiple 4.

  5. the move: a floor

    the line:
    T≥n2T \ge n^2

    why it is allowed: By line 2: take 2n+12n + 1 away from TT, and n2n^2 is left, which is less. For 3, 16 is at least 9.

  6. the move: read the bound

    the line:
    T∈Ω(n2)T \in \Omega(n^2)

    why it is allowed: By line 5, with the fixed multiple 1.

  7. the move: both together

    the line:
    T∈Θ(n2)T \in \Theta(n^2)

    why it is allowed: Lines 4 and 6 bound TT by the same growth.

try it with a small nn: at n=3n = 3, line 1 gives T=16T = 16, which you can work out by hand

Question 3: if you want only how many edits, how many numbers, RR, do two rows keep, from 1 up?
  1. the move: what a cell reads

    the line:
    a cell reads only its own rowand the row above\begin{gathered} \text{a cell reads only its own row} \\ \text{and the row above} \end{gathered}
    row-ppepea
    a1122
    ap212?
    Ape into pea, filling the last cell of row ap. It reads 2 above, 2 to the left and 2 up and to the left, all in these two rows, and writes 3.

    why it is allowed: Above and up and to the left are in the row above, and to the left is in the cell's own row. So once a row is full, the rows above it are never read again.

  2. the move: keep two rows

    the line:
    R=2(n+1)R = 2(n + 1)

    why it is allowed: So keep two rows of n+1n + 1 numbers, the one above and the one being filled, and reuse them. For 3, 8 numbers, against the table's 16.

  3. the move: but not the edits

    the line:
    reading back needsevery row\begin{gathered} \text{reading back needs} \\ \text{every row} \end{gathered}

    why it is allowed: Each step back climbs at most one row, so the walk passes every row, and from 3 letters on, two rows have thrown away a row it needs. So two rows give how many edits, not which ones, as the books note (volume three, chapter 40).

try it with a small nn: at n=3n = 3, line 2 gives R=8R = 8, which you can work out by hand

Question 4: why is every number in the table right, and how many is that? Let KK be how many, for two words of nn letters.
  1. the move: the promise

    the line:
    each cell holds the fewest editsfrom its row’s start to its column’s\begin{gathered} \text{each cell holds the fewest edits} \\ \text{from its row's start to its column's} \end{gathered}

    why it is allowed: This is what to prove. Lines 2 to 10 prove it, cell by cell, in the order the cells are filled.

  2. the move: it starts right

    the line:
    the first row and column are right\text{the first row and column are right}

    why it is allowed: Each edit adds or removes at most one letter. So nothing into a start takes at least one edit a letter, and one insert a letter does it: nothing into car, 3. A start into nothing is the same, with deletes.

  3. the move: suppose so far

    the line:
    suppose every cell filledbefore this one is right\begin{gathered} \text{suppose every cell filled} \\ \text{before this one is right} \end{gathered}

    why it is allowed: Cells fill row by row, so the three a cell reads, above, to the left, and up and to the left, are written or filled before it. Suppose, for now, that each holds the fewest. This is fair, as cells are checked in the order they are filled, from line 2's first row and column, so the three it reads are checked already.

  4. the move: a list is a line-up

    the line:
    any list of edits can be drawnas a line-up, no dearer\begin{gathered} \text{any list of edits can be drawn} \\ \text{as a line-up, no dearer} \end{gathered}
    salesealedit
    sskeep
    -einsert
    aakeep
    llkeep
    e-delete
    Sale into seal as a line-up, read down, 2 edits. A dash is a gap.

    why it is allowed: Follow each letter of the first start through the list. It is deleted, or it ends as a letter of the second start, maybe changed. Edits never reorder letters, so these pairs stay in order, and the rest of the second start was inserted. Each delete, insert and changed pair took an edit of its own.

  5. the move: the line-up's last column

    the line:
    the line-up’s last columndeletes, inserts, keeps orreplaces a last letter\begin{gathered} \text{the line-up's last column} \\ \text{deletes, inserts, keeps or} \\ \text{replaces a last letter} \end{gathered}

    why it is allowed: It holds the first start's last letter over a gap, a gap over the second's last letter, or the two last letters. Take it away, and the rest lines up the starts of the cell above, to the left, or up and to the left. Sale into seal ends e over a gap, a delete, and the rest lines up sal into seal, the cell above.

  6. the move: so at least

    the line:
    the fewest is at leastthe cheapest of: above+1,left+1, up-left+0 or 1\begin{gathered} \text{the fewest is at least} \\ \text{the cheapest of: above} + 1, \\ \text{left} + 1, \text{ up-left} + 0 \text{ or } 1 \end{gathered}

    why it is allowed: By line 4, any list costs at least its line-up, and by line 5 that is the line-up's last column plus a line-up of a smaller cell's starts. Done as edits one by one, that smaller line-up is a list for the smaller cell, so by line 3 it costs at least that cell. Up and to the left adds 0 when the last letters match, else 1.

  7. the move: each can be done

    the line:
    each of the threecan be done\begin{gathered} \text{each of the three} \\ \text{can be done} \end{gathered}

    why it is allowed: Take a fewest list for that smaller cell, by line 3. End it with the edit line 5 pairs with that cell: a delete for above, an insert for the left, a keep or replace for up-left. For cr into ca, up and to the left holds 0, c into c, and replacing r with a makes 1.

  8. the move: so the cell is right

    the line:
    the cheapest of the threeis the fewest\begin{gathered} \text{the cheapest of the three} \\ \text{is the fewest} \end{gathered}

    why it is allowed: By lines 6 and 7. When the last letters differ, that is what the method writes.

  9. the move: a match copies

    the line:
    when they match, up-leftis the cheapest\begin{gathered} \text{when they match, up-left} \\ \text{is the cheapest} \end{gathered}

    why it is allowed: From a fewest list for above, delete the second start's last letter at the end, and you reach up-left's starts. So up-left costs at most above + 1. From a fewest list for the left, first insert the first start's last letter, and the same holds. So by line 8, copying up-left is right. For crat into cart, t matches t: up-left holds 2, and above + 1 and left + 1 are both 3.

  10. the move: every cell

    the line:
    every cell is right\text{every cell is right}

    why it is allowed: By line 2, the first row and column are. By lines 3 to 9, each later cell is right once those before it are. So, filled in order, every cell is, the last included.

  11. the move: count them

    the line:
    K=(n+1)×(n+1)K = (n + 1) \times (n + 1)

    why it is allowed: Every number the table holds, each right by line 10: the same count as question 2's TT. For 3, 16.

try it with a small nn: at n=3n = 3, line 11 gives K=16K = 16, which you can work out by hand

Question 5: how fast must the plain method's calls grow? It asks for the same three cells as calls, with no table. Take two words of nn letters with no letter in common, and let LL be a floor.
  1. the move: count the calls

    the line:
    calls for n letters each: 1 + the calls of the threecells it asks for\begin{gathered} \text{calls for } n \text{ letters each: } 1 \\ \text{ + the calls of the three} \\ \text{cells it asks for} \end{gathered}
    callasks for
    a into b- into b, a into -, - into -
    - into bnothing
    a into -nothing
    - into -nothing
    Turning a into b takes 4 calls: the first asks for three cells, and each of those, with an empty word, asks for nothing.
    ncalls3 times the row above
    01-
    143
    21912
    39457
    4481282
    52,5241,443
    Calls counted by running the plain method. Each row is more than 3 times the row above.

    why it is allowed: A call is one ask for a cell's answer. The first call counts 1. Its last letters differ, so it asks for above, to the left and up and to the left. Each makes all its own calls, as nothing is written down. A pair with an empty word asks for nothing.

  2. the move: more letters, more calls

    the line:
    above and to the left each makeat least the calls of up-left\begin{gathered} \text{above and to the left each make} \\ \text{at least the calls of up-left} \end{gathered}

    why it is allowed: Above asks for up-left as its own left, and to the left asks for it as its own above. With an empty word, each makes 1 call, as up-left does.

  3. the move: each at least up-left

    the line:
    calls for n are at least 1+3×the calls for n−1\begin{gathered} \text{calls for } n \text{ are at least } 1 \\ + 3 \times \text{the calls for } n - 1 \end{gathered}

    why it is allowed: By line 2, each of line 1's three cells makes at least up-left's calls, and up-left has n−1n - 1 letters each. For 3, 94 is at least 1 + 3 times 19, which is 58.

  4. the move: leave out the 1

    the line:
    calls for n are more than 3times the calls for n−1\begin{gathered} \text{calls for } n \text{ are more than } 3 \\ \text{times the calls for } n - 1 \end{gathered}

    why it is allowed: Line 3 without the first call's 1. For 3, 94 is more than 57.

  5. the move: repeat from 0

    the line:
    L=3nL = 3^n

    why it is allowed: 0 letters make 1 call, and by line 4, each letter more at least triples the calls. So nn letters make at least nn 3s multiplied together. Name that LL: the calls are at least LL. For 10, at least 59,049, and counted, 12,146,179, against the table's 100 cells.

try it with a small nn: at n=3n = 3, line 5 gives L=27L = 27, which you can work out by hand

count it yourself

Ape into pea, step by step. Each cell filled adds 1 to the count on its own step.

ape, pea, every step written down
stepwhat happenscells filled
1Turn ape into pea. An empty start takes one edit a letter, so the first row counts inserts and the first column counts deletes.0
2a is not p, so take one edit more than the cheapest of above, 1, for a delete, to the left, 1, for an insert, and up and to the left, 0, for a replace: 1.1
3a is not e, so take one edit more than the cheapest of above, 2, for a delete, to the left, 1, for an insert, and up and to the left, 1, for a replace: 2.2
4Both letters are a, so this cell needs no edit of its own. Copy 2 from the cell up and to the left.3
5Both letters are p, so this cell needs no edit of its own. Copy 1 from the cell up and to the left.4
6p is not e, so take one edit more than the cheapest of above, 2, for a delete, to the left, 1, for an insert, and up and to the left, 1, for a replace: 2.5
7p is not a, so take one edit more than the cheapest of above, 2, for a delete, to the left, 2, for an insert, and up and to the left, 2, for a replace: 3.6
8e is not p, so take one edit more than the cheapest of above, 1, for a delete, to the left, 3, for an insert, and up and to the left, 2, for a replace: 2.7
9Both letters are e, so this cell needs no edit of its own. Copy 1 from the cell up and to the left.8
10e is not a, so take one edit more than the cheapest of above, 3, for a delete, to the left, 1, for an insert, and up and to the left, 2, for a replace: 2.9
11Done: turning ape into pea takes 2 edits at the fewest.9

In all, 9 cells filled. The formula from section 4, n2n^2, gives C=9C = 9 at n=3n = 3: the same.

where you meet it

Vladimir Levenshtein defined the distance in 1965, as the books note (volume three, chapter 40). Robert Wagner and Michael Fischer published this table for it in 1974, in the Journal of the ACM.

  • Near matches in a database

    The database PostgreSQL has a levenshtein function in its fuzzystrmatch add-on. Given two words or names of up to 255 characters each, it returns the fewest edits between them, so a search can find names spelt almost right.

    source: the PostgreSQL documentation, fuzzystrmatch (opens in a new tab)

  • Typing slips

    Fred Damerau found in 1964 that over 80 per cent of the spelling errors he studied were a single slip: one letter deleted, inserted or replaced, or two neighbours swapped. So spelling checkers often count a swap as one edit, a variant named after him and Levenshtein.

    source: F. J. Damerau, A technique for computer detection and correction of spelling errors (opens in a new tab), Communications of the ACM 7, 1964, and the books' volume three, chapter 40

  • Proteins

    In 1970, Saul Needleman and Christian Wunsch published the same kind of table to compare proteins, letter by letter. Biologists still line sequences up this way, scoring how alike they are instead of counting edits.

    source: S. B. Needleman and C. D. Wunsch, their paper (opens in a new tab), Journal of Molecular Biology 48, 1970, and the books' volume three, chapter 40

what is used instead

For whole genomes, the table's cells are far too many. Tools such as BLAST find good line-ups quickly instead, though they can miss the best, as the books note (volume three, chapter 40).

how it compares

The plain method asks for the same three cells as calls, with no table, so it works the same starts out again and again. A memo keeps each answer once worked out, as the page on solving each smaller problem once shows. The table fills every cell once, row by row, with no calls, and two rows are enough for the answer alone. Longest common subsequence is the most letters both words share in the same order: c, a and t in crate and cart. It fills the same kind of table, counting shared letters instead of edits, as the books note (volume three, chapter 40).

waywork, 8 letters eachnumbers kept
plain method398,593 callsnone
with a memo193 calls81
the table64 cells81
two rows64 cells18
Two words of 8 letters with no letter in common, each row counted by running the method. A call and a cell are different steps, so read this for the size of each number.