Levenshtein distance (editing distance)

Source: Internet
Author: User

Today, I saw the string similarity calculation (vb2005) developed by hichina Yibo. I was interested in studying it. After reading it for a long time, I didn't understand it (I'm ashamed). I Googled the internet, most of them are also implemented. There is no explanation. After several twists and turns, we found detailed information in Wikipedia.AlgorithmC # implementation firstCode:

 C # implementation code  Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. diagnostics; Namespace Wordcompare { Class Program { Static  Void Main ( String [] ARGs) {var fromstring ="English is a West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations. english is a West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations. english is a West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations."; Var tostring ="English is West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations. english is a West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations. english is a West Germanic language that developed in England and during the Anglo-Saxon era. as a result of the military, economy, scientific, political, and cultural influence of the British Empire during the 18th, 19th, and early 20th centuries, and of the United States since the mid 20th century, [7] [8] [9] [10] It has become the lingua franca in our parts of the world. [11] [12] It is used extensively as a second language and as an official language in Commonwealth countries and other international organizations. A"; Stopwatch watch = New Stopwatch (); watch. Start (); var result = comparestrings (fromstring, tostring); watch. Stop (); console. writeline (" The result is {0}, spent {1} milliseconds. ", Result, watch. elapsedmilliseconds );} Private   Static   Int Comparestrings ( String Fromstring, String Tostring) {var flength = fromstring. length; var tlength = tostring. length; // Pre verify the simplest Condition             If (Flength = 0 ){ Return Tlength ;} If (Tlength = 0 ){ Return Flength ;} // Prepare the martix VaR martix = New   Int [Flength + 1, tlength + 1]; For ( Int I = 0; I <= flength; I ++) {martix [I, 0] = I ;} For ( Int J = 0; j <= tlength; j ++) {martix [0, J] = J ;} // Compare the Chars              For ( Int I = 1; I <= flength; I ++) {var tempf = fromstring [I-1]; var cost = 0; For ( Int J = 1; j <= tlength; j ++) {var tempt = tostring [J-1]; If (Tempt = tempf) {cost = 0 ;}Else {Cost = 1;} var valueabve = martix [I-1, J] + 1; var valueleft = martix [I, j-1] + 1; // Left corner VaR valuediag = martix [I-1, J-1] + cost; // Find the minimum from the three vars above VaR cellvalue = valueabove <valueleft? (Valuediag <valueabve? Valuediag: valueabove): (valuediag <valueleft? Valuediag: valueleft); martix [I, j] = cellvalue ;}} var result = martix [flength, tlength]; Return Result ;}}}

In short, it is copied from the C ++ code. Haha ~~

The speed is really good, and the correctness should be without question, but I just cannot understand whyProgramThe correct result is displayed.

The algorithms are as follows:

 

Note that the underlined result is the result of each loop.

Algorithm proof

This algorithm computes s [1... I] to T [1... J] (for example, converting kitten to sitting) The minimum number of operations required (that is, the editing distance), which is saved in D [I, j] (D represents the two-dimensional array shown in.

    • It must be correct in the first row and the first column, which is well understood. For example, we convert kitten to an empty string, the operand we need to perform is the length of kitten (the operation is to discard all characters of kitten ).
    • There are three possible operations on characters:
      • If we can use k operands to convert s [1... I] to T [1... J-1], we just need to add T [J] at the end of the S [1... I] to T [1... J], operand k + 1
      • If we can use k operands to convert s [1... I-1] to T [1... J]. We only need to delete s [I] From the end to complete the conversion. The operand is k + 1.
      • If we can use k operands to convert s [1... I-1] to T [1... J-1], we only need to (s [I] As needed! = T [J]) replace s [I] with T [J], and the required operand is K + cost (cost indicates whether conversion is required, if s [I] = T [J], cost is 0; otherwise, it is 1 ).
    • Set S [1... N] to T [1... M] Of course, all s needs to be converted to all t, so d [n, m] (bottom right corner of the table) is the result we need.

This proof process can only prove that we can get the result, but it does not prove that the result is the smallest (that is, what we get is the least conversion step ). So we introduced another algorithm, that is, d [I, j], which saves the smallest of the three operations above. This ensures that the result we get is the smallest operand (you can use argument by contradiction to prove that it is too far away from the question and ignored ..)

 

Possible improvements
    • The current algorithm complexity is O (Mn), which can be improved to O (m ). This algorithm only needs to store the last row and the current row.
    • If you need to reproduce the conversion steps, we can save the positions and operations of each step for reproduction.
    • If we only need to compare whether the conversion step is smaller than a specific constant K, we can only calculate the rectangle with a high width of 2 k + 1. In this way, the algorithm complexity can be simplifiedO(KL),LRepresents the length of the shortest string to be compared.
    • We can give different values to three operations (add, delete, and replace) (the current algorithm is assumed to be 1, we can set add to 1, delete to 0,) to refine the comparison.
    • If we Initialize all cells in the first line to 0, this algorithm can be used for Fuzzy character query. We can obtain the position (index number) of the last character of the string matching this string. If we need the starting position of this string, We need to store the steps of each operation, then, the algorithm is used to calculate the start position of the string.
    • This algorithm does not support parallel computing and cannot take advantage of parallel computing when processing super-large strings. However, we can also calculate cost values in parallel (whether two characters at the same position are equal), and then use this algorithm for overall calculation.
    • If only the diagonal line is checked instead of the entire line and lazy evaluation is used, the time complexity of this algorithm can be optimized to O (M (1 + D) (D indicates the result ). When the two strings are very similar, the comparison speed can be greatly increased.
Comments

Nothing special, just think you are too stupid. It took a long time to figure out such a simple problem... Sorry .. Shame...

However, I think this algorithm is very useful, especially in fuzzy search. At last, I knew how to deal with fuzzy queries ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.