// C Program to Solve Edit Distance Using Recursion
#include <stdio.h>
#include <string.h>
// Function to return the minimum of three integers
int min(int a, int b, int c)
{
return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
}
// Recursive function to find the edit distance between two
// strings
int editDistanceRecursive(char* X, char* Y, int m, int n)
{
// If first string is empty, only option is to insert
// all characters of second string
if (m == 0)
return n;
// If second string is empty, only option is to remove
// all characters of first string
if (n == 0)
return m;
// If last characters are same, ignore last characters
// and recur for remaining strings
if (X[m - 1] == Y[n - 1])
return editDistanceRecursive(X, Y, m - 1, n - 1);
// If last characters are different, consider all three
// operations on last character of first string,
// recursively compute minimum cost for all three
// operations and take minimum of three values.
return 1
+ min(editDistanceRecursive(X, Y, m,
n - 1), // Insert
editDistanceRecursive(X, Y, m - 1,
n), // Remove
editDistanceRecursive(X, Y, m - 1,
n - 1) // Replace
);
}
int main()
{
// Define the two strings to compare
char X[] = "GEEXSFRGEEKKS";
char Y[] = "GEEKSFORGEEKS";
// Calculate the lengths of the strings
int m = strlen(X);
int n = strlen(Y);
// Print the minimum edit distance between the two
// strings
printf("Minimum edit distance is %d\n",
editDistanceRecursive(X, Y, m, n));
return 0;
}