TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
minimum_edit_distance.cpp File Reference

Implementation of Minimum Edit Distance using Dynamic Programing. More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for minimum_edit_distance.cpp:

Go to the source code of this file.

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 
namespace  Minimum
 Implementation of Minimum Edit Distance algorithm.
 

Functions

uint64_t dynamic_programming::minimum_edit_distance::min (uint64_t x, uint64_t y, uint64_t z)
 Takes input of the cost of three operations: Insert, Replace and Delete and return the minimum cost among them.
 
uint64_t dynamic_programming::minimum_edit_distance::editDistDP (std::string str1, std::string str2, uint64_t m, uint64_t n)
 Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute the minimum cost of a particular operation if it is already computed and stored in the dp vector.
 
static void test ()
 Self-test implementations.
 
int main (int argc, char *argv[])
 main function
 

Detailed Description

Implementation of Minimum Edit Distance using Dynamic Programing.

Given two strings str1 & str2 and we have to calculate the minimum number of operations (Insert, Remove, Replace) required to convert str1 to str2.

Algorithm

We will solve this problem using Naive recursion. But as we are approaching with a DP solution. So, we will take a DP array to store the solution of all sub-problems so that we don't have to perform recursion again and again. Now to solve the problem, We can traverse all characters from either right side of the strings or left side. Suppose we will do it from the right side. So, there are two possibilities for every pair of characters being traversed.

  1. If the last characters of two strings are the same, Ignore the characters and get the count for the remaining string. So, we get the solution for lengths m-1 and n-1 in a DP array.
  2. Else, (If last characters are not the same), we will consider all three operations (Insert, Remove, Replace) on the last character of the first string and compute the minimum cost for all three operations and take the minimum of three values in the DP array. For Insert: Recur for m and n-1 For Remove: Recur for for m-1 and n For Replace: Recur for for m-1 and n-1
Author
Nirjas Jakilim

Definition in file minimum_edit_distance.cpp.

Function Documentation

◆ editDistDP()

uint64_t dynamic_programming::minimum_edit_distance::editDistDP ( std::string str1,
std::string str2,
uint64_t m,
uint64_t n )

Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute the minimum cost of a particular operation if it is already computed and stored in the dp vector.

Parameters
dpvector to store the computed minimum costs
str1to pass the 1st string
str2to pass the 2nd string
mthe length of str1
nthe length of str2
Returns
dp[m][n] the minimum cost of operations needed to convert str1 to str2

Create a table to store results of subproblems

creasting 2D vector dp to store the results of subproblems

Fill d[][] in bottom up manner

If first string is empty, only option is to insert all characters of second string

Minimum operations = j

If second string is empty, only option is to remove all characters of second string

Minimum operations = i

If last characters are same, ignore last char and recur for remaining string

If the last character is different, consider all possibilities and find the minimum

returning the minimum cost of operations needed to convert str1 to str2

Definition at line 92 of file minimum_edit_distance.cpp.

93 {
95 std::vector<std::vector<uint64_t>> dp(
96 m + 1,
97 std::vector<uint64_t>(
98 n +
99 1));
100
102 for (uint64_t i = 0; i <= m; i++) {
103 for (uint64_t j = 0; j <= n; j++) {
106 if (i == 0) {
107 dp[i][j] = j;
108 }
109
112 else if (j == 0) {
113 dp[i][j] = i;
114 }
115
118 else if (str1[i - 1] == str2[j - 1]) {
119 dp[i][j] = dp[i - 1][j - 1];
120 }
121
124 else {
125 dp[i][j] = 1 + min(dp[i][j - 1], // Insert
126 dp[i - 1][j], // Remove
127 dp[i - 1][j - 1]); // Replace
128 }
129 }
130 }
131
132 return dp[m][n];
134}
for std::vector

◆ main()

int main ( int argc,
char * argv[] )

main function

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit

Definition at line 173 of file minimum_edit_distance.cpp.

173 {
174 test(); // run self-test implementations
175 return 0;
176}
static void test()
Self-test implementations.

◆ min()

uint64_t dynamic_programming::minimum_edit_distance::min ( uint64_t x,
uint64_t y,
uint64_t z )

Takes input of the cost of three operations: Insert, Replace and Delete and return the minimum cost among them.

Parameters
xused to pass minimum cost of Insert operations
yused to pass minimum cost of Replace operations
zused to pass minimum cost of Delete operations
Returns
x if x is the minimum value
y if y is the minimum value
z if z is the minimum value

returns x, if x is the minimum value

returns y, if y is the minimum value

returns z if z is the minimum value

Definition at line 68 of file minimum_edit_distance.cpp.

68 {
69 if (x <= y && x <= z) {
70 return x;
71 }
72 if (y <= x && y <= z) {
73 return y;
74 } else {
75 return z;
76 }
77}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 142 of file minimum_edit_distance.cpp.

142 {
143 // 1st test
144 std::string str1 = "INTENTION"; // Sample input of 1st string
145 std::string str2 = "EXECUTION"; // Sample input of 2nd string
146 uint64_t expected_output1 = 5; // Expected minimum cost
148 str1, str2, str1.length(),
149 str2.length()); // calling the editDistDP function and storing the
150 // result on output1
151 assert(output1 ==
152 expected_output1); // comparing the output with the expected output
153 std::cout << "Minimum Number of Operations Required: " << output1
154 << std::endl;
155
156 // 2nd test
157 std::string str3 = "SATURDAY";
158 std::string str4 = "SUNDAY";
159 uint64_t expected_output2 = 3;
161 str3, str4, str3.length(), str4.length());
162 assert(output2 == expected_output2);
163 std::cout << "Minimum Number of Operations Required: " << output2
164 << std::endl;
165}
uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n)
Calculates and stores the result of all the sub-problems, so that we don't have to recur to compute t...