# Fast String Comparator

Faster Computation of Levenshtein Distance for Spell Checker.

2015-06-15
Now the development moved to GitHub! This page is a historical archive. Please refer to the GitHub page for the latest information.

## 1.Abstract

Fast String Comparator is an efficient tool to determine whether two strings are within two edit distance or not. Given two strings of length m and n (m <= n), the computation requires O(1) space and O(n) time, which is much smaller and faster than when using well-known algorithm with O(mn) space and time for the same purpose. It is mainly targeted at the use in spell checker, where considering words within two edit distance suffices.

2014-02-01
Here is a live demo you can actually try! Also, javascript implementation of this algorithm (used in the demo) is available here.

## 2.Usage

The fastcomp.py has a function named compare(). It takes two strings as arguments and returns an integer, which is...

• the exact distance between two strings (if they are within two edit distance)
• -1 (if they are over two edit distance away)

Therefore, the return value should be any one of 0, 1, 2 or -1.

Example

> from fastcomp import compare
> compare("than", "then")
> 1
> compare("meeting", "eating")
> 2
> compare("book", "talk") # distance 3
> -1

Changelog

• 2012-3-31: Refactoring the whole script. The code gets much more readable.
• 2012-3-17: First release.

## 4.FAQ

#### 4.1.Reducing the time complexity from O(mn) to O(n)? Who cares?

Imagine correcting spells of thousand-word text with thousand-word dictionary. If you try to list all words in dictionary within edit distance 2 for each word in the text, you may end up computing 1,000*1,000=1,000,000 pairs. In this situation, even a small difference easily adds up. See the performance test for the effect of this.

#### 4.2.Why only measure edit distance up to two?

Actually, same technique is applicable to any finite bound k. Moreover, we can construct a general function which take the upper bound k as an argument. Such a general function, however, needs much more complicated code, thus produces non-trivial cost. So I choose two as bound, because, anyway, distance 2 seems enough to me.

#### 4.3.Is the technique same as Ukkonen's optimization?

No. Although Ukkonen algorithm produces the same result with O(n) time and space, Fast String Comparator is still faster and uses less space than that.

#### 4.4.Is it your original algorithm?

The answer is both "Yes" and "No". The problem of edit distance computation has a long long history of re-inventions, partly because this problem has been studied independently in various fields (for example, Wagner-Fischer algorithm has been rediscovered independently at least 10 times in the past). Although I conceived this algorithm by myself, I'm certain that there is someone who had invented the same stuff already.

## 5.Performance

Compare randomly generated 1000 string pairs for 1000 times (= 1 million comparison). Each string has length between [N/2] to N. Here's the source code used in this test.

(Test Machine - CPU: Celeron T3000 1.80GHz / Memory: 2GB)

FunctionN=3N=7N=10
fastcomp.py7.75sec10.68sec12.94sec
editdist.py*26.00sec77.23sec140.96sec

* An (slightly modified) implementation of Wagnerâ€“Fischer algorithm in Python. Here is the source code.