Sunday, September 22, 2024
Google search engine
HomeData Modelling & AIIntroduction to Levenshtein distance

Introduction to Levenshtein distance

Levenshtein distance is a measure of the similarity between two strings, which takes into account the number of insertion, deletion and substitution operations needed to transform one string into the other. 

Operations in Levenshtein distance are:

  • Insertion: Adding a character to string A.
  • Deletion: Removing a character from string A.
  • Replacement: Replacing a character in string A with another character.

Let’s see an example that there is String A: “kitten” which need to be converted in String B: “sitting” so we need to determine the minimum operation required

  • kitten → sitten (substitution of “s” for “k”)
  • sitten → sittin (substitution of “i” for ????”)
  • sittin → sitting (insertion of “g” at the end).

In this case it took three operation do this, so the levenshtein distance will be 3.

  • Upper and lower bounds: If and only if the two strings are identical, the Levenshtein distance is always non-negative and zero. Because it requires completely changing one string into the other through deletions or insertions, the most feasible Levenshtein distance between two strings of length m and n is max(m, n).

Applications of Levenshtein distance:

The Levenshtein distance has various applications in various fields such as:

  • Autocorrect Algorithms: Text editors and messaging applications use the Levenshtein distance in their autocorrect features such as gboard, swift keyboard, etc.
  • Data cleaning: It is widely used in the process of data cleaning and normalization task to reduce redundancy and identify similar records in the data mining process.
  • Data clustering and classification: To identify similar records and cluster them is clustering while identifying similar records and providing them with class labels is classification

Relationship with other edit distance metrics:

Let’s see how Levenshtein distance is different from other distance metrics

  • Damerau-Levenshtein distance: It is similar to the Levenshtein distance, but it just also allows transpositions as an additional operation making it 4 operations.
  • Hamming distance: It can only be applied to strings of equal length, it is used measures the number of positions at which the corresponding characters are different.

Now let’s see its implementation using different approaches in different approaches:

1) Levenshtein distance using a recursive approach

To calculate the Levenshtein distance, In the recursive technique, we will use a simple recursive function. It checks each character in the two strings and performs recursive insertions, removals, and replacements.

Below is the implementation for the above idea:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
int levenshteinRecursive(const string& str1,
                        const string& str2, int m, int n)
{
 
    // str1 is empty
    if (m == 0) {
        return n;
    }
    // str2 is empty
    if (n == 0) {
        return m;
    }
 
    if (str1[m - 1] == str2[n - 1]) {
        return levenshteinRecursive(str1, str2, m - 1,
                                    n - 1);
    }
 
    return 1
        + min(
 
            // Insert
            levenshteinRecursive(str1, str2, m, n - 1),
            min(
 
                // Remove
                levenshteinRecursive(str1, str2, m - 1,
                                        n),
 
                // Replace
                levenshteinRecursive(str1, str2, m - 1,
                                        n - 1)));
}
 
// Drivers code
int main()
{
    string str1 = "kitten";
    string str2 = "sitting";
 
    // Function Call
    int distance = levenshteinRecursive(
        str1, str2, str1.length(), str2.length());
    cout << "Levenshtein Distance: " << distance << endl;
    return 0;
}


Java




import java.io.*;
 
public class Solution {
 
    public static int levenshteinRecursive(String str1,
                                           String str2, int m, int n) {
        // str1 is empty
        if (m == 0) {
            return n;
        }
          
        // str2 is empty
        if (n == 0) {
            return m;
        }
        if (str1.charAt(m - 1) == str2.charAt(n - 1)) {
            return levenshteinRecursive(str1, str2, m - 1, n - 1);
        }
        return 1 + Math.min(
            // Insert
            levenshteinRecursive(str1, str2, m, n - 1),
            Math.min(
                // Remove
                levenshteinRecursive(str1, str2, m - 1, n),
                 
                // Replace
                levenshteinRecursive(str1, str2, m - 1, n - 1)
            )
        );
    }
 
    public static void main(String[] args) {
        String str1 = "kitten";
        String str2 = "sitting";
 
        int distance = levenshteinRecursive(str1, str2, str1.length(), str2.length());
        System.out.println("Levenshtein Distance: " + distance);
    }
}


Python3




def levenshteinRecursive(str1, str2, m, n):
      # str1 is empty
    if m == 0:
        return n
    # str2 is empty
    if n == 0:
        return m
    if str1[m - 1] == str2[n - 1]:
        return levenshteinRecursive(str1, str2, m - 1, n - 1)
    return 1 + min(
          # Insert    
        levenshteinRecursive(str1, str2, m, n - 1),
        min(
              # Remove
            levenshteinRecursive(str1, str2, m - 1, n),
          # Replace
            levenshteinRecursive(str1, str2, m - 1, n - 1))
    )
 
# Drivers code
str1 = "kitten"
str2 = "sitting"
distance = levenshteinRecursive(str1, str2, len(str1), len(str2))
print("Levenshtein Distance:", distance)


C#




using System;
 
class Program
{
    // Recursive function to calculate Levenshtein Distance
    static int LevenshteinRecursive(string str1, string str2, int m, int n)
    {
        // If str1 is empty, the distance is the length of str2
        if (m == 0)
        {
            return n;
        }
         
        // If str2 is empty, the distance is the length of str1
        if (n == 0)
        {
            return m;
        }
 
        // If the last characters of the strings are the same
        if (str1[m - 1] == str2[n - 1])
        {
            return LevenshteinRecursive(str1, str2, m - 1, n - 1);
        }
 
        // Calculate the minimum of three operations:
        // Insert, Remove, and Replace
        return 1 + Math.Min(
            Math.Min(
                // Insert
                LevenshteinRecursive(str1, str2, m, n - 1),
                // Remove
                LevenshteinRecursive(str1, str2, m - 1, n)
            ),
            // Replace
            LevenshteinRecursive(str1, str2, m - 1, n - 1)
        );
    }
 
    static void Main()
    {
        string str1 = "kitten";
        string str2 = "sitting";
 
        // Function Call
        int distance = LevenshteinRecursive(str1, str2, str1.Length, str2.Length);
        Console.WriteLine("Levenshtein Distance: " + distance);
    }
}


Javascript




// JavaScript code for the above approach:
function levenshteinRecursive(str1, str2, m, n) {
    // Base case: str1 is empty
    if (m === 0) {
        return n;
    }
     
    // Base case: str2 is empty
    if (n === 0) {
        return m;
    }
     
    // If the last characters of both
    // strings are the same
    if (str1[m - 1] === str2[n - 1]) {
        return levenshteinRecursive(str1, str2, m - 1, n - 1);
    }
     
    // Calculate the minimum of three possible
    // operations (insert, remove, replace)
    return 1 + Math.min(
        // Insert
        levenshteinRecursive(str1, str2, m, n - 1),
        // Remove
        levenshteinRecursive(str1, str2, m - 1, n),
        // Replace
        levenshteinRecursive(str1, str2, m - 1, n - 1)
    );
}
 
// Driver code
const str1 = "kitten";
const str2 = "sitting";
 
// Function Call
const distance = levenshteinRecursive(str1, str2, str1.length, str2.length);
console.log("Levenshtein Distance: " + distance);


Output

Levenshtein Distance: 3







Time complexity: O(3^(m+n))
Auxiliary complexity: O(m+n)

2) Levenshtein distance using Iterative with the full matrix approach

The iterative technique with a full matrix uses a 2D matrix to hold the intermediate results of the Levenshtein distance calculation. It begins with empty strings and iteratively fills the matrix row by row. It computes the minimum cost of insertions, deletions, and replacements based on the characters of both strings.

Below is the implementation for the above idea:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
int levenshteinFullMatrix(const string& str1,
                        const string& str2)
{
    int m = str1.length();
    int n = str2.length();
 
    vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
 
    for (int i = 0; i <= m; i++) {
        dp[i][0] = i;
    }
 
    for (int j = 0; j <= n; j++) {
        dp[0][j] = j;
    }
 
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (str1[i - 1] == str2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1];
            }
            else {
                dp[i][j] = 1
                        + min(
 
                            // Insert
                            dp[i][j - 1],
                            min(
 
                                // Remove
                                dp[i - 1][j],
 
                                // Replace
                                dp[i - 1][j - 1]));
            }
        }
    }
 
    return dp[m][n];
}
 
// Drivers code
int main()
{
    string str1 = "kitten";
    string str2 = "sitting";
 
    // Function Call
    int distance = levenshteinFullMatrix(str1, str2);
    cout << "Levenshtein Distance: " << distance << endl;
    return 0;
}


Output

Levenshtein Distance: 3







Time complexity: O(m*n)
Auxiliary complexity: O(m*n)

3) Levenshtein distance using Iterative with two matrix rows approach

By simply storing two rows of the matrix at a time, the iterative technique with two matrix rows reduces space complexity. It iterates through the strings row by row, storing the current and past calculations in two rows.

Below is the implementation for the above approach:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
int levenshteinTwoMatrixRows(const string& str1,
                            const string& str2)
{
    int m = str1.length();
    int n = str2.length();
 
    vector<int> prevRow(n + 1, 0);
    vector<int> currRow(n + 1, 0);
 
    for (int j = 0; j <= n; j++) {
        prevRow[j] = j;
    }
 
    for (int i = 1; i <= m; i++) {
        currRow[0] = i;
 
        for (int j = 1; j <= n; j++) {
            if (str1[i - 1] == str2[j - 1]) {
                currRow[j] = prevRow[j - 1];
            }
            else {
                currRow[j] = 1
                            + min(
 
                                // Insert
                                currRow[j - 1],
                                min(
 
                                    // Remove
                                    prevRow[j],
 
                                    // Replace
                                    prevRow[j - 1]));
            }
        }
 
        prevRow = currRow;
    }
 
    return currRow[n];
}
 
// Drivers code
int main()
{
    string str1 = "kitten";
    string str2 = "sitting";
 
    // Function Call
    int distance = levenshteinTwoMatrixRows(str1, str2);
    cout << "Levenshtein Distance: " << distance;
    return 0;
}


Output

Levenshtein Distance: 3






Time complexity: O(m*n)
Auxiliary Space: O(n)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments