tags:


1. Edit Distance #

Created Tue Aug 6, 2024 at 11:16 PM

aka Approximate-String-Matching (Skiena page 314)

Edit distance #

We see that a naive solution would take long, since we need to consider all possibilities. We want to minimize edits while considering all possibilities, so DP may be applicable.

We start from the right side. And make ops on P. Ops are M (match), S (substitute), I (Insert) and D (delete). Lets try to figure out a recurrence relation.

diff(P, i, T, j) =

Lets trace how diff(P, 5, T, 4) would look. (5, 4) -> [(4, 4), (4, 4), (5, 3), (4, 4)]. Already 3 times repeat. (4, 4) -> [(3, 3), (3, 3), (4, 3), (3, 4)]. Repeat 2 times.

So we can see there’s a lot of potential recompute in normal recursion.

O(n2), O(n) time and space.

Note:

Stubs of edit distance #

Stubs of edit-distance:

Reusing edit-distance to solve other problems #

The stubs for edit-distance when modified can solve many problems (they happen to be special cases of edit-distance):