TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
minimum_edit_distance.cpp
Go to the documentation of this file.
1
36
37
#include <cassert>
38
#include <cstdint>
39
#include <iostream>
40
#include <vector>
41
46
47
namespace
dynamic_programming
{
48
54
55
namespace
minimum_edit_distance {
56
68
uint64_t
min
(uint64_t x, uint64_t y, uint64_t z) {
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
}
78
92
uint64_t
editDistDP
(std::string str1, std::string str2, uint64_t m,
93
uint64_t n) {
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
}
135
}
// namespace minimum_edit_distance
136
}
// namespace dynamic_programming
137
142
static
void
test
() {
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;
155
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;
165
}
166
171
int
main
() {
172
test
();
// run self-test implementations
173
return
0;
174
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
dynamic_programming::minimum_edit_distance::min
uint64_t 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 a...
Definition
minimum_edit_distance.cpp:68
dynamic_programming::minimum_edit_distance::editDistDP
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...
Definition
minimum_edit_distance.cpp:92
dp
for std::vector
Definition
partition_problem.cpp:39
dynamic_programming
Dynamic Programming algorithms.
dynamic_programming
minimum_edit_distance.cpp
Generated by
1.18.0