Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
AllEpsilonClosure.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <set>
9#include <map>
10
15#include <automaton/FSM/NFA.h>
16#include <automaton/FSM/DFA.h>
17
19
20namespace automaton {
21
22namespace properties {
23
24namespace efficient {
25
27 template < class StateType >
28 static void process(const ext::multimap<StateType, StateType>& epsilonTransitions, const StateType & state, ext::map<StateType, ext::set<StateType>>& closures, ext::set<StateType>& nonvisited);
29public:
33 template < class SymbolType, class StateType >
35 template < class SymbolType, class StateType >
37 template < class SymbolType, class StateType >
39 template < class SymbolType, class StateType >
41 template < class SymbolType, class StateType >
43 template < class SymbolType, class StateType >
45};
46
47template < class StateType >
48void AllEpsilonClosure::process(const ext::multimap<StateType, StateType>& epsilonTransitions, const StateType & state, ext::map<StateType, ext::set<StateType>>& closures, ext::set<StateType>& nonvisited) {
49 auto node = nonvisited.extract ( state );
50 if ( ! static_cast < bool > ( node ) )
51 return;
52
53 for ( const auto & transition : epsilonTransitions.equal_range ( node.value ( ) ) ) {
54 process ( epsilonTransitions, transition.second, closures, nonvisited );
55 const auto & closure = closures [ transition.second ];
56 closures [ node.value ( ) ].insert ( closure.begin ( ), closure.end ( ) );
57 }
58}
59
60template < class SymbolType, class StateType >
63
64 for(const StateType& state : fsm.getStates())
65 res[state].insert(state);
66
67 ext::set<StateType> nonvisited = fsm.getStates();
68
70
71 while(!nonvisited.empty ( )) {
72 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
73 }
74
75 nonvisited = fsm.getStates();
76
77 while(!nonvisited.empty ( )) {
78 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
79 }
80
81 return res;
82}
83
84template < class SymbolType, class StateType >
87 for(const StateType& state : fsm.getStates())
88 closure[state].insert(state);
89 return closure;
90}
91
92template < class SymbolType, class StateType >
95 for(const StateType& state : fsm.getStates())
96 closure[state].insert(state);
97 return closure;
98}
99
100template < class SymbolType, class StateType >
103 for(const StateType& state : fsm.getStates())
104 closure[state].insert(state);
105 return closure;
106}
107
108template < class SymbolType, class StateType >
111
112 for(const StateType& state : fsm.getStates())
113 res[state].insert(state);
114
115 ext::set<StateType> nonvisited = fsm.getStates();
116
117 ext::multimap<StateType, StateType> epsilonTransitions;
118 for(const std::pair<const ext::pair<StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType >& transition : fsm.getTransitions() )
120 epsilonTransitions.insert ( transition.first.first, transition.second );
121
122 while(!nonvisited.empty ( )) {
123 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
124 }
125
126 nonvisited = fsm.getStates();
127
128 while(!nonvisited.empty ( )) {
129 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
130 }
131
132 return res;
133}
134
135template < class SymbolType, class StateType >
138
139 for(const StateType& state : fsm.getStates())
140 res[state].insert(state);
141
142 ext::set<StateType> nonvisited = fsm.getStates();
143
144 ext::multimap<StateType, StateType > epsilonTransitions;
145 for(const std::pair<const ext::pair<StateType, ext::vector < SymbolType > >, StateType >& transition : fsm.getTransitions() )
146 if( transition.first.second.empty ( ) )
147 epsilonTransitions.insert ( transition.first.first, transition.second );
148
149 while(!nonvisited.empty ( )) {
150 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
151 }
152
153 nonvisited = fsm.getStates();
154
155 while(!nonvisited.empty ( )) {
156 process(epsilonTransitions, *nonvisited.begin(), res, nonvisited);
157 }
158
159 return res;
160}
161
162} /* namespace efficient */
163
164} /* namespace properties */
165
166} /* namespace automaton */
167
Compact nondeterministic finite automaton. Accepts regular languages. The automaton has a list of sym...
Definition: CompactNFA.h:78
const ext::multimap< ext::pair< StateType, ext::vector< SymbolType > >, StateType > & getTransitions() const &
Definition: CompactNFA.h:555
const ext::set< StateType > & getStates() const &
Definition: CompactNFA.h:183
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
const ext::set< StateType > & getStates() const &
Definition: DFA.h:134
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
const ext::set< StateType > & getStates() const &
Definition: EpsilonNFA.h:158
ext::multimap< StateType, StateType > getEpsilonTransitions() const
Definition: EpsilonNFA.h:676
Extended nondeterministic finite automaton. Accepts regular languages. The automaton has a regular ex...
Definition: ExtendedNFA.h:80
const ext::multimap< ext::pair< StateType, regexp::UnboundedRegExpStructure< SymbolType > >, StateType > & getTransitions() const &
Definition: ExtendedNFA.h:581
const ext::set< StateType > & getStates() const &
Definition: ExtendedNFA.h:185
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
const ext::set< StateType > & getStates() const &
Definition: MultiInitialStateNFA.h:166
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
const ext::set< StateType > & getStates() const &
Definition: NFA.h:136
Definition: AllEpsilonClosure.h:26
static ext::map< StateType, ext::set< StateType > > allEpsilonClosure(const automaton::EpsilonNFA< SymbolType, StateType > &fsm)
Definition: AllEpsilonClosure.h:61
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
iterator insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:118
auto equal_range(K &&key) const &
Make range of elements with key equal to the key.
Definition: multimap.hpp:268
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: UnboundedRegExpStructure.h:47
static bool languageContainsEpsilon(const regexp::FormalRegExpElement< SymbolType > &regexp)
Definition: RegExpEpsilon.h:91
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
return closure
Definition: AllEpsilonClosure.h:142
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
Definition: Node.cpp:11