C Program To Implement Levenshtein Distance Computing Algorithm

  • The method of dynamic programming The technique frequently makes use of dynamic programming, building a matrix to hold the minimal edit distances for every subproblem. The lengths of the two strings under comparison define the size of the matrix. The least amount of edits needed to change the substring up to thaLogic Practice into the other substring is represented by the value at each column in the matrix.
  • First Matrix Configuration The initialization of the matrix's first row and first column indicates how many modifications are needed to change an empty string into the matching substring. The values of adjacent cells are used to fill up each cell, which represents the many edit operations that can be carried out, such as insertion, deletion, or substitution.
  • Calculations and Matrix Traversal The minimal edit distance at each cell is calculated while traversing the matrix, taking into account the values in the adjacent cells and the expense of the relevant edit operations. Each cell represents the many edit operations that can be carried out, such as insertion, deletion, or replacement and is filled in based on the values of nearby cells.
  • Final Result The Levenshtein distance , which shows the bare minimum of modifications needed to change one string into another, is located in the matrix's bottom-right
  • The technique frequently makes use of dynamic programming, building a matrix to hold the minimal edit distances for every subproblem.
  • The lengths of the two strings under comparison define the size of the matrix.
  • The least amount of edits needed to change the substring up to thaLogic Practice into the other substring is represented by the value at each column in the matrix.
  • The initialization of the matrix's first row and first column indicates how many modifications are needed to change an empty string into the matching substring.
  • The values of adjacent cells are used to fill up each cell, which represents the many edit operations that can be carried out, such as insertion, deletion, or substitution.
  • The minimal edit distance at each cell is calculated while traversing the matrix, taking into account the values in the adjacent cells and the expense of the relevant edit operations.
  • Each cell represents the many edit operations that can be carried out, such as insertion, deletion, or replacement and is filled in based on the values of nearby cells.
  • The Levenshtein distance , which shows the bare minimum of modifications needed to change one string into another, is located in the matrix's bottom-right
  • Example:

Let's consider a scenario to demonstrate the implementation of the Levenshtein Distance Calculation Algorithm in the C programming language.

Example

#include <stdio.h>

#include <string.h>

int minimum(int a, int b, int c)

{

 if (a <= b && a <= c) 

 {

 return a;

 } 

 else if (b <= a && b <= c)

 {

 return b;

 }

 else

 {

 return c;

 }

}

int levenshteinDistance(char *str_1, char *str_2) 

{

 int len_1 = strlen(str_1);

 int len_2 = strlen(str_2);

 int distance[len_1 + 1][len_2 + 1];

 for (int i = 0; i <= len_1; i++)

 {

 for (int j = 0; j <= len_2; j++)

 {

 if (i == 0)

 {

 distance[i][j] = j;

 }

 else if (j == 0)

 {

 distance[i][j] = i;

 }

 else if (str_1[i - 1] == str_2[j - 1])

 {

 distance[i][j] = distance[i - 1][j - 1];

 } 

 else

 {

 distance[i][j] = 1 + minimum(distance[i][j - 1], distance[i - 1][j], distance[i - 1][j - 1]);

 }

 }

 }

 return distance[len_1][len_2];

}

int main()

{

 char *str_1 = "kitten";

 char *str_2 = "sitting";

 printf("The Levenshtein distance between '%s' and '%s' is: %d\n", str_1, str_2, levenshteinDistance(str_1, str_2));

 return 0;

}

Output:

Output

The Levenshtein distance between 'kitten' and 'sitting' is: 3

Explanation:

  • Function Declaration and Header Files Two header files are included in the code: one for input/output operations and the other for string manipulation At the start of the code, the minimum function is declared. Its purpose is to determine the lowest value of three numbers.
  • The minimum Function Three integers (a, b, c) are passed into the minimal function, which returns the least of the three. It is applied in the Levenshtein distance computation later on.
  • How the Levinshtein Distance Function Works The Levenshtein distance is computed using the levenshteinDistance function between two input strings, str1 and str2. Initially, it uses strlen to determine the input string lengths. The distances between each substring of the input strings are stored in a 2D array called distance . The distance array is filled using the Levenshtein distance technique through two nested loops that iterate over the substrings. The distance is set to the length of the other string, which indicates the number of insertions or deletions required if one of the strings is empty. If the characters in the two strings match at the respective locations, the distance is set to the value from the preceding substring, and no edit is necessary. If the letters are mismatched, the distance is (1 + the least of the distances from the insertion, deletion, or replacement operations). The Levenshtein distance (which is returned between the two input strings) is represented by the last entry in the distance array.
  • The main Function Two strings are supplied as input for the Levenshtein distance calculation in the main function: str1 ("kitten") and str2 ("sitting") . After that, the Printf function is used to output the result of the levenshteinDistance function to the console after it has been invoked with these strings.
  • Output It shows the Levenshtein distance between "kitten" and "sitting," or the least number of single-character modifications required to change one string into the other.
  • Two header files are included in the code: one for input/output operations and the other for string manipulation
  • At the start of the code, the minimum function is declared. Its purpose is to determine the lowest value of three numbers.
  • Three integers (a, b, c) are passed into the minimal function, which returns the least of the three. It is applied in the Levenshtein distance computation later on.
  • The Levenshtein distance is computed using the levenshteinDistance function between two input strings, str1 and str2.
  • Initially, it uses strlen to determine the input string lengths.
  • The distances between each substring of the input strings are stored in a 2D array called distance .
  • The distance array is filled using the Levenshtein distance technique through two nested loops that iterate over the substrings.
  • The distance is set to the length of the other string, which indicates the number of insertions or deletions required if one of the strings is empty.
  • If the characters in the two strings match at the respective locations, the distance is set to the value from the preceding substring, and no edit is necessary.
  • If the letters are mismatched, the distance is (1 + the least of the distances from the insertion, deletion, or replacement operations).
  • The Levenshtein distance (which is returned between the two input strings) is represented by the last entry in the distance array.
  • Two strings are supplied as input for the Levenshtein distance calculation in the main function: str1 ("kitten") and str2 ("sitting") .
  • After that, the Printf function is used to output the result of the levenshteinDistance function to the console after it has been invoked with these strings.
  • It shows the Levenshtein distance between "kitten" and "sitting," or the least number of single-character modifications required to change one string into the other.

Input Required

This code uses input(). Please provide values below: