|
|
Line 1: |
Line 1: |
| {{merge from|Levenshtein distance|discuss=Talk:Edit distance#Proposed merge with Levenshtein distance|date=January 2014}}
| | Hi there. The author's name is [http://Photo.net/gallery/tag-search/search?query_string=Eusebio+regrettably Eusebio regrettably] he never really liked that name. The beloved hobby for him then his kids is to finally drive and he's been doing it for quite some time. Auditing is how he supports my family. Massachusetts is where he and his wife stay. He's been working about his website for several time now. Check it out here: http://prometeu.net<br><br>My web-site ... [http://prometeu.net clash of clans hack tool no survey or password] |
| In [[computer science]], '''edit distance''' is a way of quantifying how dissimilar two [[String (computing)|strings]] (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other. Edit distances find applications in [[natural language processing]], where automatic [[Spell checker|spelling correction]] can determine candidate corrections for a misspelled word by selecting words from a dictionary that have a low distance to the word in question. In [[bioinformatics]], it can be used to quantify the similarity of [[macromolecule]]s such as [[DNA]], which can be viewed as strings of the letters A, C, G and T.
| |
| | |
| Several definitions of edit distance exist, using different sets of string operations. One of the most common variants is called '''Levenshtein distance''', named after the Soviet Russian computer scientist [[Vladimir Levenshtein]]. In this version, the allowed operations are the removal or insertion of a single character, or the substitution of one character for another. Levenshtein distance may also simply be called "edit distance", although several variants exist.<ref name="navarro">{{Cite doi/10.1145.2F375360.375365}}</ref>{{rp|32}}
| |
| | |
| ==Formal definition and properties==
| |
| Given two strings {{mvar|a}} and {{mvar|b}} on an alphabet {{mvar|Σ}} (e.g. the set of [[ASCII]] characters, the set of [[byte]]s [0..255], etc.), the edit distance d({{mvar|a}}, {{mvar|b}}) is the minimum-weight series of edit operations that transforms {{mvar|a}} into {{mvar|b}}. One of the simplest sets of edit operations is that defined by Levenshtein in 1966:<ref name="slp"/>
| |
| | |
| :'''Insertion''' of a single symbol. If {{mvar|a}} = {{mvar|u}}{{mvar|v}}, then inserting the symbol {{mvar|x}} produces {{mvar|u}}{{mvar|x}}{{mvar|v}}. This can also be denoted ε→{{mvar|x}}, using ε to denote the empty string.
| |
| :'''Deletion''' of a single symbol changes {{mvar|u}}{{mvar|x}}{{mvar|v}} to {{mvar|u}}{{mvar|v}} ({{mvar|x}}→ε).
| |
| :'''Substitution''' of a single symbol {{mvar|x}} for a symbol {{mvar|y}} ≠ {{mvar|x}} changes {{mvar|u}}{{mvar|x}}{{mvar|v}} to {{mvar|u}}{{mvar|y}}{{mvar|v}} ({{mvar|x}}→{{mvar|y}}).
| |
| | |
| In Levenshtein's original definition, each of these operations has unit cost (except that substitution of a character by itself has zero cost), so the Levenshtein distance is equal to the minimum ''number'' of operations required to transform {{mvar|a}} to {{mvar|b}}. A more general definition associates non-negative weight functions {{mvar|w}}<sub>ins</sub>({{mvar|x}}), {{mvar|w}}<sub>del</sub>({{mvar|x}}) and {{mvar|w}}<sub>sub</sub>({{mvar|x}} {{mvar|y}}) with the operations.<ref name="slp">{{cite book |author1=Daniel Jurafsky |author2=James H. Martin |title=Speech and Language Processing |publisher=Pearson Education International |pages=107–111}}</ref>
| |
| | |
| Additional primitive operations have been suggested. A common mistake when typing text is '''transposition''' of two adjacent characters commonly occur, formally characterized by an operation that changes {{mvar|u}}{{mvar|x}}{{mvar|y}}{{mvar|v}} into {{mvar|u}}{{mvar|y}}{{mvar|x}}{{mvar|v}} where {{mvar|x}}, {{mvar|y}} ∈ {{mvar|Σ}}.<ref name="ukkonen83">{{cite conference |author=Esko Ukkonen |title=On approximate string matching |conference=Foundations of Computation Theory |year=1983 |pages=487–495 |publisher=Springer}}</ref><ref name="ssm"/>
| |
| For the task of correcting [[Optical character recognition|OCR]] output, '''merge''' and '''split''' operations have been used which replace a single character into a pair of them or vice-versa.<ref name="ssm">{{cite journal |first1=Klaus U. |last1=Schulz |first2=Stoyan |last2=Mihov |year=2002 |id={{citeseerx|10.1.1.16.652}} |title=Fast string correction with Levenshtein automata |journal=International Journal of Document Analysis and Recognition |volume=5 |issue=1 |pages=67–85 |doi=10.1007/s10032-002-0082-8}}</ref>
| |
| | |
| Other variants of edit distance are obtained by restricting the set of operations. [[Longest common subsequence]] (LCS) distance is edit distance with insertion and deletion as the only two edit operations, both at unit cost.<ref name="navarro"/>{{rp|37}} Similarly, by only allowing substitutions (again at unit cost, [[Hamming distance]] is obtained; this must be restricted to equal-length strings.<ref name="navarro"/>
| |
| [[Jaro–Winkler distance]] can be obtained from an edit distance where only transpositions are allowed.
| |
| | |
| ===Example===
| |
| The Levenshtein distance between "kitten" and "sitting" is 3. The minimal edit script that transforms the former into the latter is: | |
| | |
| # '''k'''itten → '''s'''itten (substitution of "s" for "k")
| |
| # sitt'''e'''n → sitt'''i'''n (substitution of "i" for "e")
| |
| # sittin → sittin'''g''' (insertion of "g" at the end).
| |
| | |
| LCS distance (insertions and deletions only) gives a different distance and minimal edit script:
| |
| | |
| # delete '''k''' at 0
| |
| # insert '''s''' at 0
| |
| # delete '''e''' at 4
| |
| # insert '''i''' at 4
| |
| # insert '''g''' at 6
| |
| | |
| for a total cost/distance of 6 operations. | |
| | |
| ===Properties===
| |
| Edit distance with non-negative cost satisfies the axioms of a [[Metric (mathematics)|metric]], giving rise to a [[metric space]] of strings, when the following conditions are met:<ref name="navarro"/>{{rp|37}}
| |
| | |
| * Every edit operation has positive cost;
| |
| * for every operation, there is an inverse operation with equal cost.
| |
| | |
| With these properties, the metric axioms are satisfied as follows:
| |
| | |
| :{{mvar|d}}({{mvar|a}}, {{mvar|a}}) = 0, since each string can be trivially transformed to itself using exactly zero operations.
| |
| :{{mvar|d}}({{mvar|a}}, {{mvar|b}}) > 0 when {{mvar|a}} ≠ {{mvar|b}}, since this would require at least one operation at non-zero cost.
| |
| :{{mvar|d}}({{mvar|a}}, {{mvar|b}}) = {{mvar|d}}({{mvar|b}}, {{mvar|a}}) by equality of the cost of each operation and its inverse.
| |
| :Triangle inequality: {{mvar|d}}({{mvar|a}}, {{mvar|c}}) ≤ {{mvar|d}}({{mvar|a}}, {{mvar|b}}) + {{mvar|d}}({{mvar|b}}, {{mvar|c}}).<ref>{{cite conference |author1=Lei Chen |author2=Raymond Ng |title=On the marriage of Lₚ-norms and edit distance |conference=Proc. 30th Int'l Conf. on Very Large Databases (VLDB) |volume=30 |year=2004}}</ref>
| |
| | |
| Levenshtein distance and LCS distance with unit cost satisfy the above conditions, and therefore the metric axioms. Variants of edit distance that are not proper metrics have also been considered in the literature.<ref name="navarro"/>
| |
| | |
| Other useful properties of unit-cost edit distances include:
| |
| | |
| * LCS distance is bounded above by the sum of lengths of a pair of strings.<ref name="navarro"/>{{rp|37}}
| |
| * LCS distance is an upper bound on Levenshtein distance.
| |
| * For strings of the same length, Hamming distance is an upper bound on Levenshtein distance.<ref name="navarro"/>
| |
| | |
| Regardless of cost/weights, the following property holds of all edit distances:
| |
| | |
| * When {{mvar|a}} and {{mvar|b}} share a common prefix, this prefix has no effect on the distance. Formally, when {{mvar|a}} = {{mvar|uv}} and {{mvar|b}} = {{mvar|uw}}, then {{mvar|d}}({{mvar|a}}, {{mvar|b}}) = {{mvar|d}}({{mvar|v}}, {{mvar|w}}).<ref name="ssm"/> This allows speeding up many computations involving edit distance and edit scripts, since common prefixes and suffixes can be skipped in linear time.
| |
| | |
| ==Algorithm==
| |
| ===Basic algorithm===
| |
| {{main|Wagner–Fischer algorithm}}
| |
| Using Levenshtein's original operations, the edit distance between <math>a = a_1\ldots a_n</math> and <math>b = b_1\ldots b_m</math> is given by <math>d_{mn}</math>, defined by the recurrence<ref name="slp"/>
| |
| | |
| :<math>d_{i0} = i</math> for 0 ≤ {{mvar|i}} ≤ {{mvar|m}}, | |
| :<math>d_{0j} = j</math> for 0 ≤ {{mvar|j}} ≤ {{mvar|m}}, | |
| :<math>d_{ij} = \min \begin{cases} d_{i-1, j} + w_\mathrm{ins}(b_{i-1}) \\ d_{i,j-1} + w_\mathrm{del}(a_{j-1}) \\ d_{i-1,j-1} + w_\mathrm{sub}(a_{j-1}, b_{i-1}) \end{cases}</math> for 1 ≤ {{mvar|i}} ≤ {{mvar|m}}, 1 ≤ {{mvar|j}} ≤ {{mvar|n}}.
| |
| | |
| This algorithm can be generalized to handle transpositions by adding an additional term in the recursive clause's minimization.<ref name="ukkonen83"/>
| |
| | |
| The straightforward, [[Recursion (computer science)|recursive]] way of evaluating this recurrence takes [[exponential time]]. Therefore, it is usually computed using a [[dynamic programming]] algorithm that is commonly credited to [[Wagner–Fischer algorithm|Wagner and Fisher]],<ref>{{cite journal |author1=R. Wagner |author2=M. Fisher |title=The string-to-string correction problem |journal=J. ACM |volume=21 |year=1974 |pages=168–178}}</ref> although it has a history of multiple invention.<ref name="slp"/><ref name="ukkonen83"/>
| |
| After completion of the Wagner–Fischer algorithm, a minimal sequence of edit operations can be read off as a backtrace of the operations used during the dynamic programming algorithm starting at <math>d_{mn}</math>.
| |
| | |
| This algorithm has a [[time complexity]] of Θ({{mvar|m}}{{mvar|n}}). When the full dynamic programming table is constructed, its [[space complexity]] is also Θ({{mvar|m}}{{mvar|n}}); this can be improved to Θ(min({{mvar|m}},{{mvar|n}})) by observing that at any instant, the algorithm only requires two rows (or two columns) in memory. However, this optimization makes it impossible to read off the minimal series of edit operations.<ref name="ukkonen83"/>
| |
| | |
| ===Improved algorithm===
| |
| Improving on the Wagner–Fisher algorithm described above, Ukkonen describes a variant that takes two strings and a maximum edit distance {{mvar|s}}, and returns min({{mvar|s}}, {{mvar|d}}). It achieves this by only computing and storing a part of the dynamic programming table around its diagonal. This algorithm takes time O({{mvar|s}}×min({{mvar|m}},{{mvar|n}})), where {{mvar|m}} and {{mvar|n}} are the lengths of the strings. Space complexity is O({{mvar|s}}²) or O({{mvar|s}}), depending on whether the edit sequence needs to be read off.<ref name="ukkonen83"/>
| |
| | |
| ==Applications==
| |
| Edit distance finds applications in [[computational biology]] and natural language processing, e.g. the correction of spelling mistakes or OCR errors, and [[approximate string matching]], where the objective is to find matches for short strings in many longer texts, in situations where a small number of differences is to be expected.
| |
| | |
| Various algorithms exist that solve problems beside the computation of distance between a pair of strings, to solve related types of problems.
| |
| | |
| * [[Hirschberg's algorithm]] computes the optimal [[Sequence alignment|alignment]] of two strings, where optimality is defined as minimizing edit distance.
| |
| * [[Approximate string matching]] can be formulated in terms of edit distance. Ukkonen's 1985 algorithm takes a string {{mvar|p}}, called the pattern, and a constant {{mvar|k}}; it then builds a [[deterministic finite state automaton]] that finds, in an arbitrary string {{mvar|s}}, a substring whose edit distance to {{mvar|p}} is at most {{mvar|k}}<ref>{{cite journal |author=Esko Ukkonen |title=Finding approximate patterns in strings |journal=J. Algorithms |volume=6 |pages=132–137 |year=1985}}</ref> (cf. the [[Aho–Corasick string matching algorithm|Aho–Corasick algorithm]], which similarly constructs an automaton to search for any of a number of patterns, but without allowing edit operations). A similar algorithm for approximate string matching is the [[bitap algorithm]], also defined in terms of edit distance.
| |
| * [[Levenshtein automaton|Levenshtein automata]] are finite-state machines that recognize a set of strings within bounded edit distance of a fixed reference string.<ref name="ssm"/>
| |
| | |
| ==References==
| |
| {{reflist|30em}}
| |
| | |
| [[Category:String similarity measures]]
| |