Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
GeneralizedLevenshteinDynamicProgramming.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <algorithm>
9#include <limits.h>
10
11#include <string/LinearString.h>
12
13namespace stringology {
14
15namespace simulations {
16
18public:
19 template <class SymbolType>
21
22 template <class SymbolType>
23 static ext::set<unsigned int> search(const string::LinearString<SymbolType> & text, const string::LinearString<SymbolType> & pattern, unsigned int errors);
24};
25
26template <class SymbolType>
30 pattern.getContent().size() + 1,
31 ext::vector<unsigned int>(text.getContent().size() + 1, 0)
32 );
33
34 for(unsigned int j = 0; j <= pattern.getContent().size(); j++) {
35 table[j][0] = j;
36 }
37
38 for(unsigned int i = 1; i<=text.getContent().size(); i++) {
39 for(unsigned int j = 1; j<=pattern.getContent().size(); j++) {
40 unsigned int value_a;
41 if(pattern.getContent()[j-1] == text.getContent()[i-1]) {
42 value_a = table[j-1][i-1];
43 } else {
44 value_a = table[j-1][i-1] + 1;
45 }
46
47 unsigned int value_b = UINT_MAX;
48 if(j < pattern.getContent().size()) {
49 value_b = table[j][i-1] + 1;
50 }
51
52 value_b = std::min(table[j-1][i] + 1, value_b);
53
54 unsigned int value_c = UINT_MAX;
55 if(j>1 && i>1 && pattern.getContent()[j-2] == text.getContent()[i-1] && pattern.getContent()[j-1] == text.getContent()[i-2]) {
56 value_c = table[j-2][i-2] + 1;
57 }
58
59 table[j][i] = std::min({value_a, value_b, value_c});
60 }
61 }
62
63 return table;
64}
65
66template <class SymbolType>
69
71
72 for(unsigned int i = 0; i<= text.getContent().size(); i++) {
73 if(table[pattern.getContent().size()][i] <= errors) {
74 result.insert(i);
75 }
76 }
77
78 return result;
79}
80
81
82} // namespace simulations
83
84} // namespace stringology
85
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Linear string.
Definition: LinearString.h:57
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
Definition: GeneralizedLevenshteinDynamicProgramming.h:17
static ext::vector< ext::vector< unsigned int > > compute_table(const string::LinearString< SymbolType > &text, const string::LinearString< SymbolType > &pattern)
Definition: GeneralizedLevenshteinDynamicProgramming.h:27
static ext::set< unsigned int > search(const string::LinearString< SymbolType > &text, const string::LinearString< SymbolType > &pattern, unsigned int errors)
Definition: GeneralizedLevenshteinDynamicProgramming.h:67
int i
Definition: AllEpsilonClosure.h:118
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
constexpr const T & min(const T &a)
Definition: algorithm.hpp:310
Definition: ArithmeticCompression.h:18