Edit Distance Problem – Dynamic Programming Solutions

This is a C++ Program that Solves Edit Distance Problem using Dynamic Programming technique.

Problem Description

There are two strings src and dest. The goal of the problem is to convert src to dest by applying minimum edit operations on string str1. The edit operations are following –

1) insert a character
2) delete a character
3) replace a character

Problem Solution

This problem can be solved optimally using bottom up DP. We will first calculate minimum operations required to transform a prefix of the string src to a prefix of string dest. Then we will use this information to calculate minimum no. of operations required to transform a bigger prefix of src to a bigger prefix of dest.

The time complexity of this solution will be O(len1*len2).

Expected Input and Output

Case-1:

advertisement
advertisement
str1="vish"
str2="vishal"
 
Minimum number of edit operations=2
inserting 2 chracters 'a' and 'l'

Case-2:

str1="vshkl"
str2="vishal"
 
Minimum number of edit operations=2
inserting 'i' and replacing 'k' with 'a'

Case-3:

str1="apiskal"
str2="vishal"
 
Minimum number of edit operations=3
deleting 'a', replacing 'p' with 'v' and replacing 'k' with 'h'
Program/Source Code for Brute Force Solution

Here is source code of the C++ Program to Solve Edit Distance Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. //we will be processing the src and dest string from the rear end. 
  7. //If the current character matches in both string, we proceed for next character.
  8. //else we will apply all the 3 operations one by one, and select the one which takes 
  9. //minimum number of overall edit operations to convert src to dest
  10. int minEditDistanceBrute(string src, string dest, int len1, int len2)
  11. {
  12.     //if we have already attained the dest string, we can simply remove the left out characters in src string
  13.     if(len2==0)
  14.     {
  15.         return len1;
  16.     }
  17.  
  18.     //if the entire src string is process, we can simply insert all the remaining characters of dest string
  19.     if(len1==0)
  20.     {
  21.         return len2;
  22.     }
  23.  
  24.     //if current character of the src and dest is same, no operation is required to be done
  25.     if(src[len1-1]==dest[len2-1])
  26.     {
  27.         return minEditDistanceBrute(src, dest, len1-1, len2-1);
  28.     }
  29.  
  30.  
  31.     //if current character is different for src and dest string
  32.     else
  33.     {
  34.         //if the required character of dest string is inserted in src string
  35.         int x=1+minEditDistanceBrute(src,dest,len1,len2-1);
  36.  
  37.         //if the current character of src is removed
  38.         int y=1+minEditDistanceBrute(src,dest,len1-1,len2);
  39.  
  40.         //if the current character of the src string is replaced with the current character of the dest string
  41.         int z=1+minEditDistanceBrute(src,dest,len1-1,len2-1);
  42.  
  43.  
  44.         //now, choose the operation which require minimum number of overall operations out of these
  45.         return min(x,min(y,z));
  46.  
  47.     }
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53.     string src,dest;
  54.  
  55.     cout<<"Enter source string   ";
  56.     getline(cin, src);
  57.  
  58.     cout<<"Enter destination string   ";
  59.     getline(cin, dest);
  60.  
  61.     int len1=src.length();  //length of src string
  62.     int len2=dest.length(); //length of dest string
  63.  
  64.     cout<<"Minimum number of edit operations required for transforming \nsource string to destination string is "<<minEditDistanceBrute(src,dest,len1,len2);
  65.  
  66.     cout<<endl;
  67.     return 0;
  68. }
Program/Source Code for DP solution

Here is source code of the C++ Program to Solve Edit Distance Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int minEditDistance(string src, string dest, int len1, int len2)
  7. {
  8.     int i, j;
  9.  
  10.     //create a matrix of order (len1+1)*(len2+1) to memoize values
  11.     int edit[len1+1][len2+1];
  12.  
  13.     //edit[i][j]=minimum number of edit operations required to transform src[0....(i-1)] to dest[0...(j-1)]
  14.  
  15.     //initializing
  16.     for(i=0;i<=len1;i++)
  17.         edit[i][0]=i;    //min operations required to transform src[0...i-1] to empty dest string
  18.  
  19.     for(j=0;j<=len2;j++)
  20.         edit[0][j]=j;   //min operations required to transform empty src to dest[0...j-1]
  21.  
  22.     //now, start filling the matrix row wise
  23.     for(i=1;i<=len1;i++)
  24.     {
  25.         for(j=1;j<=len2;j++)
  26.         {
  27.             //if current character of both strings match
  28.             if(src[i-1]==dest[j-1])
  29.             {
  30.                 edit[i][j]=edit[i-1][j-1];
  31.             }
  32.  
  33.             //mismatch
  34.             else
  35.             {
  36.                 //try applying all operations and choose the one which costs minimum
  37.                 int x=1+edit[i-1][j];    //delete 
  38.                 int y=1+edit[i][j-1];    //insert
  39.                 int z=1+edit[i-1][j-1];  //replace
  40.  
  41.                 edit[i][j]=min(x,min(y,z));
  42.  
  43.             }
  44.         }
  45.     }
  46.  
  47.     //now, return the final value 
  48.     return edit[len1][len2];
  49.  
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.     string src,dest;
  56.  
  57.     cout<<"Enter source string   ";
  58.     getline(cin, src);
  59.  
  60.     cout<<"Enter destination string   ";
  61.     getline(cin, dest);
  62.  
  63.     int len1=src.length();  //length of src string
  64.     int len2=dest.length(); //length of dest string
  65.  
  66.     cout<<"Minimum number of edit operations required for transforming \nsource string to destination string is "<<minEditDistance(src,dest,len1,len2);
  67.  
  68.     cout<<endl;
  69.     return 0;
  70. }
Program Explanation

In the main function, we ask the user to input the source and destination strings. We pass these values to the function minEditDistance as parameters. This function will calculate the expected result and return it. The returned value will be displayed.

Runtime Test Cases
 
Case-1:
$ g++ edit_distance_DP.cpp
$ ./a.out
Enter source string   vish
Enter destination string   vishal
Minimum number of edit operations required for transforming 
source string to destination string is 3

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.