Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
LevenshteinDynamicProgramming.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 std::vector < unsigned int > values;
41 if(pattern.getContent()[j-1] == text.getContent()[i-1]) {
42 values.push_back ( table[j-1][i-1] );
43 } else {
44 values.push_back ( table[j-1][i-1] + 1 );
45 }
46
47 if(j < pattern.getContent().size()) {
48 values.push_back ( table[j][i-1] + 1 );
49 }
50
51 values.push_back ( table[j-1][i] + 1 );
52
53 table[j][i] = * std::min_element ( values.begin ( ), values.end ( ) );
54 }
55 }
56
57 return table;
58}
59
60template <class SymbolType>
62 auto table = LevenshteinDynamicProgramming::compute_table(text, pattern);
63
65
66 //std::cerr << table << std::endl;
67
68 for ( unsigned int i = 0; i <= text.getContent().size(); i++ ) {
69 if ( table[pattern.getContent().size()][i] <= errors ) {
70 result.insert(i);
71 }
72 }
73
74 return result;
75}
76
77
78
79} // namespace simulations
80
81} // namespace stringology
82
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: LevenshteinDynamicProgramming.h:17
static ext::set< unsigned int > search(const string::LinearString< SymbolType > &text, const string::LinearString< SymbolType > &pattern, unsigned int errors)
Definition: LevenshteinDynamicProgramming.h:61
static ext::vector< ext::vector< unsigned int > > compute_table(const string::LinearString< SymbolType > &text, const string::LinearString< SymbolType > &pattern)
Definition: LevenshteinDynamicProgramming.h:27
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
Definition: ArithmeticCompression.h:18