Calculate the Levenshtein distance between two sequences ‘intention’ and ‘execution’

The Levenshtein distance, also known as the edit distance, is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another. To calculate the Levenshtein distance between the sequences 'intention' and 'execution', we can use dynamic programming. Here's how it's done:

  1. Initialize a matrix M with dimensions (m+1) x (n+1), where m is the length of the first sequence and n is the length of the second sequence.
  2. Initialize the first row and first column of the matrix with values 0 to m and 0 to n respectively, representing the number of insertions and deletions required to convert an empty string to each sequence.
  3. Iterate over each cell (i, j) of the matrix, where i corresponds to characters from the first sequence and j corresponds to characters from the second sequence.
  4. For each cell (i, j), calculate the minimum of the following three operations:
    • If the characters at positions i and j are equal, the cost of the operation is the same as the cost of the previous diagonal cell (M[i-1][j-1]).
    • If the characters are different, the cost of the operation is the cost of the previous diagonal cell plus 1.
    • Add 1 to the minimum of the above three values to account for the current operation (insertion, deletion, or substitution).
  5. The value in the bottom-right cell of the matrix (M[m][n]) represents the Levenshtein distance between the two sequences.

Let's calculate the Levenshtein distance between 'intention' and 'execution':


 

luaCopy code

'' e x e c u t i o n -------------------------------------- '' | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------- i | 1 | 1 | 2 | 3 | 4 | 5 | 6 | 5 | 6 | -------------------------------------- n | 2 | 2 | 2 | 3 | 4 | 5 | 6 | 6 | 5 | -------------------------------------- t | 3 | 3 | 3 | 3 | 4 | 5 | 6 | 7 | 6 | -------------------------------------- e | 4 | 3 | 4 | 4 | 3 | 4 | 5 | 6 | 7 | -------------------------------------- n | 5 | 4 | 4 | 5 | 4 | 4 | 5 | 6 | 7 | -------------------------------------- t | 6 | 5 | 5 | 5 | 5 | 5 | 5 | 6 | 7 | -------------------------------------- i | 7 | 6 | 6 | 6 | 6 | 6 | 6 | 5 | 6 | -------------------------------------- o | 8 | 7 | 7 | 7 | 7 | 7 | 7 | 6 | 5 | -------------------------------------- n | 9 | 8 | 8 | 8 | 8 | 8 | 8 | 7 | 6 | --------------------------------------

The Levenshtein distance between 'intention' and 'execution' is 6.

Top Questions From Calculate the Levenshtein distance between two sequences ‘intention’ and ‘execution’

Top Countries For Calculate the Levenshtein distance between two sequences ‘intention’ and ‘execution’

Top Services From Calculate the Levenshtein distance between two sequences ‘intention’ and ‘execution’

Top Keywords From Calculate the Levenshtein distance between two sequences ‘intention’ and ‘execution’