-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
208 lines (178 loc) · 6.93 KB
/
main.cc
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
208
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <algorithm>
#include <unordered_map>
#include <utility>
#include <bits/stdc++.h>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
using namespace std;
int t_size;
vector<char> alphabet;
unordered_map<string, pair<vector<int>, vector<int>>> precomputed_values;
boost::unordered_map<tuple<string, string, vector<int>, vector<int>>,
pair<vector<int>, vector<int>>> precomputed_values_alt;
void debugTable(const vector<vector<int>> table) {
for (auto v : table) {
for (auto i : v)
cout << i << " ";
cout << endl;
}
}
void debugVector(const vector<int> vec) {
for (auto i : vec)
cout << i << " ";
}
vector<char> offsetVectorToChars(vector<int> offsetVector) {
vector<char> result;
char temp = 0;
for (int i = 0; i < offsetVector.size(); ++i) {
if (offsetVector[i] == 1) temp |= 1;
else if (offsetVector[i] == -1) temp |= 2;
temp << 2;
if (i % 4 == 3) {
result.push_back(temp);
temp = 0;
}
}
return result;
}
void PrecomputeSingle(
const string &row_string, // length t - 1 string
const string &column_string, // length t - 1 string
const vector<int> &row_offset_vector, // length t - 1 offset vector
const vector<int> &column_offset_vector) { // length t - 1 offset vector
// table initialization
vector<vector<int>> table(t_size, vector<int>(t_size));
table[0][0] = 0;
for (int i = 1; i < t_size; i++) {
table[0][i] = row_offset_vector[i - 1] + table[0][i - 1];
table[i][0] = column_offset_vector[i - 1] + table[i - 1][0];
}
// table calculation
for (int row = 1; row < t_size; row++) {
for (int col = 1; col < t_size; col++) {
int t = (row_string[col - 1] == column_string[row - 1]) ? 0 : 1;
int diagonal = table[row - 1][col - 1] + t;
int vertical = table[row - 1][col] + 1;
int horizontal = table[row][col - 1] + 1;
table[row][col] = min(diagonal, min(vertical, horizontal));
}
}
// returning the result row and column offset vectors
vector<int> row_offset_vector_output(t_size - 1);
vector<int> column_offset_vector_output(t_size - 1);
for (int i = 0; i < t_size - 1; i++) {
row_offset_vector_output[i] = table[t_size - 1][i + 1] - table[t_size - 1][i];
column_offset_vector_output[i] = table[i + 1][t_size - 1] - table[i][t_size - 1];
}
string key = row_string + column_string;
// for (auto c: offsetVectorToChars(row_offset_vector)) key.push_back(c);
// for (auto c: offsetVectorToChars(column_offset_vector)) key.push_back(c);
for (auto v: row_offset_vector) key.append(to_string(v));
for (auto v: column_offset_vector) key.append(to_string(v));
precomputed_values.insert(make_pair(key, make_pair(row_offset_vector_output, column_offset_vector_output)));
// auto keyTuple = make_tuple(row_string, column_string,
// row_offset_vector, column_offset_vector);
// precomputed_values_alt[keyTuple] =
// make_pair(row_offset_vector_output, column_offset_vector_output);
return;
}
void PossibleOffsets(const string &rowstr, const string &colstr,
vector<int> row, vector<int> col,
int kr, int kc) {
if (kr == 0 && kc == 0) {
PrecomputeSingle(rowstr, colstr, row, col);
} else if (kr == 0) {
for (auto i : {-1, 0, 1}) {
col.push_back(i);
PossibleOffsets(rowstr, colstr, row, col, kr, kc - 1);
col.pop_back();
}
} else {
for (auto i : {-1, 0, 1}) {
row.push_back(i);
PossibleOffsets(rowstr, colstr, row, col, kr - 1, kc);
row.pop_back();
}
}
}
void PossibleStringsOffsets(string rowstr, string colstr,
int kr, int kc) {
if (kr == 0 && kc == 0) {
PossibleOffsets(rowstr, colstr, {}, {}, t_size - 1, t_size - 1);
} else if (kr == 0) {
for (auto c : alphabet) PossibleStringsOffsets(rowstr, colstr + c, kr, kc - 1);
} else {
for (auto c : alphabet) PossibleStringsOffsets(rowstr + c, colstr, kr - 1, kc);
}
}
pair<vector<int>, vector<int>> getPair(
const string &row_string, // length t - 1 string
const string &column_string, // length t - 1 string
const vector<int> &row_offset_vector, // length t - 1 offset vector
const vector<int> &column_offset_vector) { // length t - 1 offset vector
string key = row_string + column_string;
// for (auto c: offsetVectorToChars(row_offset_vector)) key.push_back(c);
// for (auto c: offsetVectorToChars(column_offset_vector)) key.push_back(c);
for (auto v : row_offset_vector) key.append(to_string(v));
for (auto v : column_offset_vector) key.append(to_string(v));
return precomputed_values.find(key)->second;
}
// Assumption: n = n'(k - 1), m = m'(k - 1) for positives n' m'.
void overAllTest(int k, const string& T, const string& P) {
int wing = (k - 1) / (t_size - 1) + 1;
cout << "wing: " << wing << endl;
int num_t_block_per_row = 2 * wing + 1;
cout << "number of t blocks per row: " << num_t_block_per_row << endl;
int m = P.length();
int n = T.length();
int row_blocks = m / (t_size - 1);
int col_blocks = n / (t_size - 1);
cout << "row_blocks: " << row_blocks << ", col_blocks: " << col_blocks << endl;
vector<int> temp_col_offset_vec(t_size - 1); // temporary column vector
vector<vector<int>> prev_row_offset_vec(num_t_block_per_row); // previous row
int accumulation = 0;
fill(prev_row_offset_vec.begin(), prev_row_offset_vec.end(), vector<int>(t_size - 1, 1));
for (int row = 0; row < row_blocks; row++) {
fill(temp_col_offset_vec.begin(), temp_col_offset_vec.end(), 1);
for (int col = 0; col < num_t_block_per_row; col++) {
if (row + col < wing || row + col >= wing + col_blocks) continue;
auto pair = getPair(T.substr((row + col - wing) * (t_size - 1), t_size - 1),
P.substr(row * (t_size - 1), t_size - 1),
(col == num_t_block_per_row - 1) ?
vector<int>(t_size - 1, 1) : prev_row_offset_vec[col + 1],
temp_col_offset_vec);
if (col == wing) {
for (auto v : temp_col_offset_vec)
accumulation += v;
for (auto v : get<0>(pair))
accumulation += v;
}
prev_row_offset_vec[col] = get<0>(pair);
temp_col_offset_vec = get<1>(pair);
}
}
cout << "edit distance: " << accumulation << endl;
}
int main(void) {
int alphabet_size;
cout << "alphabet set size: ";
cin >> alphabet_size;
cout << "alphabets: ";
for (int i = 0; i < alphabet_size; ++i) {
char c;
cin >> c;
alphabet.push_back(c);
}
cout << "t-block size: ";
cin >> t_size;
clock_t time_req = clock();
PossibleStringsOffsets("", "", t_size - 1, t_size - 1);
time_req = clock() - time_req;
cout << "Precomputing time: " << (float)time_req/CLOCKS_PER_SEC << endl;
// overAllTest(3, "CCACAGGATTGAATCGAACCATAGTAAATAGGGTGACCAACAGAGTAGACG", "ACAGGATAGGGGATCGAGATAGGAAAACCGATGGAGCGAAGGGTGCGAGCG");
return 0;
}