13/40
Part 3 of 7 · Two sequencesLearn

Fill row by row from a base row and a base column, for O(nm)

Here is the table it fills for tails and train.

for row in edit_distance("tails", "train"):
    print(row)
[0, 1, 2, 3, 4, 5]
[1, 0, 1, 2, 3, 4]
[2, 1, 1, 1, 2, 3]
[3, 2, 2, 2, 1, 2]
[4, 3, 3, 3, 2, 2]
[5, 4, 4, 4, 3, 3]

The function returns the whole table. With a's prefixes down the side and b's along the top, it reads like this:

The edit distance table for tails and train
d
""train""012345t101234a211123i322212l433322s544433

Row a, column a: the letters match, so the cell copies its diagonal, 1. The corner says tails becomes train in 3 edits.

What it costs. There are (n+1)(m+1)(n + 1)(m + 1) cells for strings of lengths nn and mm, and each one is a comparison and a min of three. That's O(nm)O(nm) time and memory. Measured on random strings of the letters A, C, G and T, best of 3 runs:

Predict

Double both string lengths. About how much longer does the table take?

Pick an answer above to see the rest