# [Essay] a preliminary understanding of Levenshtein Distance (Edit Distance) Editing Distance and character similarity Algorithms

Source: Internet
Author: User

A few days ago, I saw a post on the CSDN forum that needed to determine the number of characters in the difference between two strings. I have learned some algorithms to calculate the difference, but I am not familiar with it. let's take a moment to understand the Levenshtein Distance (Edit Distance) Editing Distance and character similarity algorithm.

From Wikipedia:

"Edit distance, Also knownLevenshtein distanceThe minimum number of edits required to convert a string from one to another. Licensed editing operations include replacing one character with another, inserting one character, and deleting one character.

For example, convert kitten to sitting:

1. Sitten (k → s)
2. Sittin (e → I)
3. Sitting (→ g)

Russian scientist Vladimir Levenshtein proposed this concept in 1965. "

English: http://zh.wikipedia.org/wiki/%E7%B7%A8%E8%BC%AF%E8%B7%9D%E9%9B%A2

Http://en.wikipedia.org/wiki/Levenshtein_distance.

Here is the pseudocode provided by Wikipedia:

"

Dynamic Planning is often used as a solution to this problem.

Levenshtein is an integer away from bytes (character str1 [1 .. lenStr1], character str2 [1 .. lenStr2])

Declare int d [0... lenstr1, 0... lenstr2]

Declare int I, j, cost

I equals from 0 to lenstr1

D [I, 0]: = I

J equals from 0 to lenstr2

D [0, J]: = J

I equals from 1 to lenstr1

J is equal to 1 to lenstr2.

If str1 [I] = str2 [J], cost: = 0

Otherwise, cost: = 1

D [I, j]: = Minimum value (

D [I-1, J] + 1, // distinct

D [I, J-1] + 1, // insert

D [I-1, J-1] + cost // replace counter

)

Returns d [lenStr1, lenStr2].

"

The Demo is as follows:

Using System; <br/> using System. collections. generic; <br/> using System. componentModel; <br/> using System. data; <br/> using System. drawing; <br/> using System. linq; <br/> using System. text; <br/> using System. windows. forms; <br/> namespace WindowsFormsApplication1 <br/>{< br/> public partial class Form1: Form <br/>{< br/> public Form1 () <br/>{< br/> InitializeComponent (); <br/>}< br/> private void button#click (object sender, EventArgs e) <br/>{</p> <p> char [] c1 = richTextBox1.Text. trim (). toCharArray (); <br/> char [] c2 = richTextBox2.Text. trim (). toCharArray (); <br/> int [,] result = Calculate (c1, c2); </p> <p> DataTable dt = new DataTable (); <br/> for (int I = 0; I <c1.Length + 2; I ++) <br/> {<br/> dt. columns. add (I. toString (); <br/>}< br/> for (int I = 0; I <c2.Length + 2; I ++) <br/> {<br/> DataRow row = dt. newRow (); <br/> dt. rows. add (row); <br/>}< br/> for (int I = 0; I <c2.Length + 2; I ++) <br/>{< br/> if (I <2) <br/>{< br/> dt. rows [I] [0] = string. empty; <br/>}< br/> else <br/> {<br/> dt. rows [I] [0] = c2 [I-2]; <br/>}< br/> for (int I = 0; I <c1.Length + 2; I ++) <br/>{< br/> if (I <2) <br/>{< br/> dt. rows [0] [I] = string. empty; <br/>}< br/> else <br/> {<br/> dt. rows [0] [I] = c1 [I-2]; <br/>}< br/> for (int I = 0; I <result. getLength (0); I ++) <br/>{< br/> for (int j = 0; j <result. getLength (1); j ++) <br/>{< br/> dt. rows [j + 1] [I + 1] = result [I, j]; <br/>}< br/> if (checkBox1.Checked = true) <br/>{< br/> maid = dt; <br/> maid = false; <br/> maid = autosizecolumnsmode. allCells; <br/>}< br/> label1.Text = string. format ("character similarity: {0}" <br/>, dt. rows [result. getLength (1)] [result. getLength (0)]. toString (); <br/>}< br/> public static int [,] Calculate (char [] First, char [] Second) <br/>{< br/> int lenFirst = First. length + 1; <br/> int lenSecond = Second. length + 1; <br/> int [,] matrixResult = new int [lenFirst, lenSecond]; <br/> for (int I = 0; I <lenFirst; I ++) matrixResult [I, 0] = I; <br/> for (int j = 0; j <lenSecond; j ++) matrixResult [0, j] = j; <br/> for (int I = 1; I <lenFirst; I ++) <br/>{< br/> for (int j = 1; j <lenSecond; j ++) <br/>{< br/> if (First [I-1] = Second [j-1]) <br/>{< br/> matrixResult [I, j] = matrixResult [I-1, j-1]; <br/>}< br/> else <br/> {<br/> matrixResult [I, j] = new int [] {matrixResult [I-1, j] + 1 <br/>, matrixResult [I, J-1] + 1 <br/>, matrixResult [I-1, J-1] + 1 <br/> }. min (); <br/>}< br/> return matrixResult; <br/>}< br/>

Related Keywords:

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.