1
1
/* *
2
2
* @file
3
- * @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic Programing
3
+ * @brief Implementation of [Minimum Edit
4
+ * Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic
5
+ * Programing
4
6
*
5
7
* @details
6
8
*
32
34
* @author [Nirjas Jakilim](github.com/nirzak)
33
35
*/
34
36
35
- #include < cassert> // / for assert
36
- #include < iostream> // / for IO operations
37
+ #include < cassert> // / for assert
38
+ #include < cstdint> // / for std::uint64_t
39
+ #include < iostream> // / for IO operations
37
40
#include < vector> // / for std::vector
41
+
38
42
/* *
39
43
* @namespace dynamic_programming
40
44
* @brief Dynamic Programming algorithms
@@ -44,7 +48,8 @@ namespace dynamic_programming {
44
48
45
49
/* *
46
50
* @namespace Minimum Edit Distance
47
- * @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
51
+ * @brief Implementation of [Minimum Edit
52
+ * Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
48
53
*/
49
54
50
55
namespace minimum_edit_distance {
@@ -61,15 +66,14 @@ namespace minimum_edit_distance {
61
66
* @returns z if `z` is the minimum value
62
67
*/
63
68
uint64_t min (uint64_t x, uint64_t y, uint64_t z) {
64
- if (x <= y && x <= z) {
65
- return x; // / returns x, if x is the minimum value
66
- }
67
- if (y <= x && y <= z) {
68
- return y; // / returns y, if y is the minimum value
69
- }
70
- else {
71
- return z; // / returns z if z is the minimum value
72
- }
69
+ if (x <= y && x <= z) {
70
+ return x; // / returns x, if x is the minimum value
71
+ }
72
+ if (y <= x && y <= z) {
73
+ return y; // / returns y, if y is the minimum value
74
+ } else {
75
+ return z; // / returns z if z is the minimum value
76
+ }
73
77
}
74
78
75
79
/* *
@@ -85,42 +89,48 @@ uint64_t min(uint64_t x, uint64_t y, uint64_t z) {
85
89
* @returns dp[m][n] the minimum cost of operations
86
90
* needed to convert str1 to str2
87
91
*/
88
- uint64_t editDistDP (std::string str1, std::string str2, uint64_t m, uint64_t n) {
89
- // / Create a table to store results of subproblems
90
- std::vector<std::vector<uint64_t >>dp (m+1 , std::vector<uint64_t >(n+1 )); // / creasting 2D vector dp to store the results of subproblems
92
+ uint64_t editDistDP (std::string str1, std::string str2, uint64_t m,
93
+ uint64_t n) {
94
+ // / Create a table to store results of subproblems
95
+ std::vector<std::vector<uint64_t >> dp (
96
+ m + 1 ,
97
+ std::vector<uint64_t >(
98
+ n +
99
+ 1 )); // / creasting 2D vector dp to store the results of subproblems
91
100
92
- // / Fill d[][] in bottom up manner
93
- for (uint64_t i = 0 ; i <= m; i++) {
94
- for (uint64_t j = 0 ; j <= n; j++) {
95
- // / If first string is empty, only option is to
96
- // / insert all characters of second string
97
- if (i == 0 ) {
98
- dp[i][j] = j; // / Minimum operations = j
99
- }
101
+ // / Fill d[][] in bottom up manner
102
+ for (uint64_t i = 0 ; i <= m; i++) {
103
+ for (uint64_t j = 0 ; j <= n; j++) {
104
+ // / If first string is empty, only option is to
105
+ // / insert all characters of second string
106
+ if (i == 0 ) {
107
+ dp[i][j] = j; // / Minimum operations = j
108
+ }
100
109
101
- // / If second string is empty, only option is to
102
- // / remove all characters of second string
103
- else if (j == 0 ) {
104
- dp[i][j] = i; // / Minimum operations = i
105
- }
110
+ // / If second string is empty, only option is to
111
+ // / remove all characters of second string
112
+ else if (j == 0 ) {
113
+ dp[i][j] = i; // / Minimum operations = i
114
+ }
106
115
107
- // / If last characters are same, ignore last char
108
- // / and recur for remaining string
109
- else if (str1[i - 1 ] == str2[j - 1 ]) {
110
- dp[i][j] = dp[i - 1 ][j - 1 ];
111
- }
116
+ // / If last characters are same, ignore last char
117
+ // / and recur for remaining string
118
+ else if (str1[i - 1 ] == str2[j - 1 ]) {
119
+ dp[i][j] = dp[i - 1 ][j - 1 ];
120
+ }
112
121
113
- // / If the last character is different, consider all
114
- // / possibilities and find the minimum
115
- else {
116
- dp[i][j] = 1 + min (dp[i][j - 1 ], // Insert
117
- dp[i - 1 ][j], // Remove
118
- dp[i - 1 ][j - 1 ]); // Replace
119
- }
122
+ // / If the last character is different, consider all
123
+ // / possibilities and find the minimum
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
+ }
120
130
}
121
- }
122
131
123
- return dp[m][n]; // / returning the minimum cost of operations needed to convert str1 to str2
132
+ return dp[m][n]; // / returning the minimum cost of operations needed to
133
+ // / convert str1 to str2
124
134
}
125
135
} // namespace minimum_edit_distance
126
136
} // namespace dynamic_programming
@@ -130,25 +140,28 @@ uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n)
130
140
* @returns void
131
141
*/
132
142
static void test () {
133
- // 1st test
134
- std::string str1 = " INTENTION" ; // Sample input of 1st string
135
- std::string str2 = " EXECUTION" ; // Sample input of 2nd string
136
- uint64_t expected_output1 = 5 ; // Expected minimum cost
137
- uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP (
138
- str1, str2, str1.length (), str2.length ()); // calling the editDistDP function and storing the result on output1
139
- assert (output1 == expected_output1); // comparing the output with the expected output
140
- std::cout << " Minimum Number of Operations Required: " << output1
141
- << std::endl;
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
147
+ uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP (
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;
142
155
143
- // 2nd test
144
- std::string str3 = " SATURDAY" ;
145
- std::string str4 = " SUNDAY" ;
146
- uint64_t expected_output2 = 3 ;
147
- uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP (
148
- str3, str4, str3.length (), str4.length ());
149
- assert (output2 == expected_output2);
150
- std::cout << " Minimum Number of Operations Required: " << output2
151
- << std::endl;
156
+ // 2nd test
157
+ std::string str3 = " SATURDAY" ;
158
+ std::string str4 = " SUNDAY" ;
159
+ uint64_t expected_output2 = 3 ;
160
+ uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP (
161
+ str3, str4, str3.length (), str4.length ());
162
+ assert (output2 == expected_output2);
163
+ std::cout << " Minimum Number of Operations Required: " << output2
164
+ << std::endl;
152
165
}
153
166
154
167
/* *
@@ -158,6 +171,6 @@ static void test() {
158
171
* @returns 0 on exit
159
172
*/
160
173
int main (int argc, char *argv[]) {
161
- test (); // run self-test implementations
162
- return 0 ;
174
+ test (); // run self-test implementations
175
+ return 0 ;
163
176
}
0 commit comments