- Fast and Easy Levensthein distance using a Trie
- SO: Most efficient way to calculate Levenshtein distance
- SO: Levenshtein Distance Algoirthm better than $O(n m)$
- An extension of Ukkonen’s enhanced dynamic programming ASM algorithm
- Damn Cool Algorithms: Levenshtein Automata
I’ve also heard some talk about using some type of distance metric to optimize search (such as a BK-tree?) but I know little about this area and how it applies to this problem.
Asked By : user834
Answered By : Raphael
- Initialise the first row/column with $0$ (instead of $i$/$j$) if free deletions/insertions are allowed at the beginning.
- Select the minimum value from the last row/column as result if free deletions/insertions are allowed at the end.
Different combinations are possible. In your case, assume that $r$ is the horizontal word, you initialise the first row with $0$ (the first column with $i$, no change here) and the result is the minimum of the last row. The result is then the smallest Levenshtein distance between $s$ and any substring of $r$, computed in time and space $O(nm)$. Now, in order to get scores for all starting positions, note that the computation of semi-global alignments with allowed deletions/insertions at the end conveniently provides the wanted array in the last row/column — well, almost: that gives you the best matches against prefixes up to position $k$. So in order to get best matches against suffixes from position $k$, reverse both $r$ and $s$.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/2519