Skip to content

Edit distance

planning ahead

Level 2 of 3

Also called Levenshtein distance and Wagner-Fischer algorithm.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many cells does the method fill to turn plank into blank?

the answer, and why

the answer is B

5 rows of 5 cells, a row for each letter of plank. The first row and column are written at the start.

if you chose another

  • That counts the first row and column too, which are written at the start, not filled.
  • That is the fewest edits, replace p with b.
  • That adds the letters, but each row fills a cell for each letter of blank.

question 2

How few edits turn lion into loin?

the answer, and why

the answer is C

Keep l, replace i with o, replace o with i, keep n. Swapping two letters is not one edit, so it costs 2.

if you chose another

  • That counts a swap as one edit, which it is not.
  • That replaces every letter, but l and n can stay.
  • 0 is only for equal words. These have the same letters in a different order.

question 3

Which pair makes the method fill the most cells?

the answer, and why

the answer is B

5 times 5, which is 25.

if you chose another

  • 3 times 4, which is 12.
  • Equal words still fill every cell, but 4 times 4 is 16.
  • 1 times 7, which is 7.

question 4

A cell's two last letters are the same. What does the method write in it?

the answer, and why

the answer is C

The matching letters need no edit of their own, so the cell copies up and to the left. Question 4 of the learn page's section 4 shows that this is the cheapest.

this tests how it works, on the learn page

if you chose another

  • Keeping a letter costs nothing.
  • The letters before it may still need edits.
  • That is the rule for two different letters.

pen and paper

question 5about 8 minutes

Run the method by hand to turn sale into seal.

  1. Fill the table.
  2. How many edits, and how many cells did you fill?
  3. Read back one line-up of the fewest edits.
a hint

The first row and column hold 0, 1, 2, 3, 4.

a worked answer

The table is drawn below. The last cell says 2 edits, and you fill 16 cells, 4 rows of 4. Reading back: keep s, insert e, keep a, keep l, delete e.

start-sseseaseal
-01234
s10123
sa21112
sal32221
sale43232
Sale into seal. The last cell holds 2.

this tests how it works, on the learn page

what a full answer includes
  • The table as drawn.
  • 2 edits and 16 cells.
  • Keep s, insert e, keep a, keep l, delete e.

question 6about 6 minutes

Two texts, each 100,000 letters long, are compared with this method.

  1. How many cells does it fill?
  2. How long does that take at a billion cells a second?
  3. How many numbers does the table hold, and how many do two rows hold?
a hint

Questions 1 to 3 of the learn page's section 4 count all three.

a worked answer

100,000 times 100,000, which is 10,000,000,000 cells, about 10 seconds. The table holds 100,001 times 100,001, which is 10,000,200,001 numbers, but two rows hold only 200,002.

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

this tests where the formula comes from, on the learn page

what a full answer includes
  • 10,000,000,000 cells.
  • About 10 seconds.
  • 10,000,200,001 numbers, against 200,002.