-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequentialAlgorithm.cpp
More file actions
207 lines (183 loc) · 5.3 KB
/
SequentialAlgorithm.cpp
File metadata and controls
207 lines (183 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "SequentialAlgorithm.h"
#include <iostream>
#include <chrono>
using namespace std::chrono;
SequentialAlgorithm::SequentialAlgorithm(bool time)
{
this->time = time;
}
/// <summary>
/// Calculates the minimum of three integers and assigns the corresponding transformation.
/// </summary>
/// <param name="a">First integer.</param>
/// <param name="b">Second integer.</param>
/// <param name="c">Third integer.</param>
/// <param name="t">Pointer to a character where the corresponding transformation will be stored.</param>
/// <returns>The minimum of the three integers.</returns>
int minimum(int a, int b, int c, char* t)
{
int min = a;
*t = 's';
if (b < min)
{
min = b;
*t = 'i';
}
if (c < min)
{
min = c;
*t = 'd';
}
return min;
}
// <summary>
/// Retrieves the transformation path from a 2D array of characters.
/// </summary>
/// <param name="transformations">2D array of characters representing the transformations.</param>
/// <param name="m">Size of the first dimension of the array.</param>
/// <param name="n">Size of the second dimension of the array.</param>
/// <returns>The transformation path as a string.</returns>
string RetrieveTransformations(char** transformations, int m, int n)
{
string path = "";
int i = m, j = n;
while (i != 0 || j != 0)
{
string k(1, transformations[i][j]);
path.append(k);
switch (transformations[i][j])
{
case 'd':
i--;
break;
case 'i':
j--;
break;
default:
i--; j--;
break;
}
}
reverse(path.begin(), path.end());
return path;
}
/// <summary>
/// Prints a 2D integer array with row and column labels for visualization, used while debugging to print distance array.
/// </summary>
/// <param name="array">Pointer to the 2D integer array.</param>
/// <param name="m">Number of rows in the array.</param>
/// <param name="n">Number of columns in the array.</param>
/// <param name="s">String representing row labels.</param>
/// <param name="t">String representing column labels.</param>
void SeqPrintArray(int** array, int m, int n, string s, string t)
{
cout << " ";
for (int j = 0; j < n; j++)
cout << t[j] << " ";
cout << endl;
for (int i = 0; i <= m; i++)
{
if (i == 0)
cout << " ";
else
cout << s[i - 1] << " ";
for (int j = 0; j <= n; j++)
cout << array[i][j] << " ";
cout << endl;
}
cout << endl << endl;
}
/// <summary>
/// Prints a 2D char array with row and column labels for visualization, used while debugging to print transformations array.
/// </summary>
/// <param name="array">Pointer to the 2D char array.</param>
/// <param name="m">Number of rows in the array.</param>
/// <param name="n">Number of columns in the array.</param>
/// <param name="s">String representing row labels.</param>
/// <param name="t">String representing column labels.</param>
void SeqPrintArray(char** array, int m, int n, string s, string t)
{
cout << " ";
for (int j = 0; j < n; j++)
cout << t[j] << " ";
cout << endl;
for (int i = 0; i <= m; i++)
{
if (i == 0)
cout << " ";
else
cout << s[i - 1] << " ";
for (int j = 0; j <= n; j++)
cout << array[i][j] << " ";
cout << endl;
}
cout << endl << endl;
}
/// <summary>
/// Calculates the Levenshtein distance between two words and determines the transformation path.
/// </summary>
/// <param name="s">The source word.</param>
/// <param name="t">The target word.</param>
/// <param name="transformPath">Pointer to a string where the transformation path will be stored.</param>
int SequentialAlgorithm::LevenstheinDistance(string s, string t, string* transformPath)
{
char transform;
int d;
int m = s.size(), n = t.size();
// Create 2D arrays for distance and transformations
int** distance = new int* [m + 1];
char** transformations = new char* [m + 1];
for (int i = 0; i <= m; i++)
{
distance[i] = new int[n + 1];
transformations[i] = new char[n + 1];
}
steady_clock::time_point start, stop;
milliseconds duration;
start = high_resolution_clock::now();
// Initialize base cases for the dynamic programming approach
for (int i = 0; i <= m; i++)
{
distance[i][0] = i;
transformations[i][0] = 'd';
}
for (int j = 1; j <= n; j++)
{
distance[0][j] = j;
transformations[0][j] = 'i';
}
// Populate the distance and transformations matrices
for (int j = 1; j <= n; j++)
{
for (int i = 1; i <= m; i++)
{
int notMatching = s[i - 1] == t[j - 1] ? 0 : 1;
distance[i][j] = minimum(distance[i - 1][j - 1] + notMatching, // substitution
distance[i][j - 1] + 1, // insertion
distance[i - 1][j] + 1, // deletion
&transform);
if (transform == 's' && notMatching == 0)
transform = '-';
transformations[i][j] = transform;
}
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
if (time)
cout << "Computing distance and transformations arrays duration: " << duration.count() << " ms" << endl << endl;
// Print distance and transformations arrays
//SeqPrintArray(distance, m, n, s, t);
//SeqPrintArray(transformations, m, n, s, t);
// Retrieve the transformation path and Levenshtein distance
*transformPath = RetrieveTransformations(transformations, m, n);
d = distance[m][n];
// Clean up memory
for (int i = 0; i < m; i++)
{
delete[] distance[i];
delete[] transformations[i];
}
delete[] distance;
delete[] transformations;
return d;
}