Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UselessStatesRemover.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
27
32#include <automaton/FSM/NFA.h>
33#include <automaton/FSM/DFA.h>
36#include <automaton/TA/DFTA.h>
37
38namespace automaton {
39
40namespace simplify {
41
53public:
60 template < class T >
61 static T remove( const T & fsm );
62
68 template < class SymbolType, class StateType >
70
81 template < class SymbolType, class StateType >
83
94 template < class SymbolType, class StateType >
96
106 template < class SymbolType, class StateType >
108
109 template < class SymbolType, class StateType >
111};
112
113template < class T >
115 using StateType = typename T::StateType;
116
117 // 1.
119
120 // 2.
121 T M(fsm.getInitialState());
122
123 for( const auto & a : fsm.getInputAlphabet( ) )
124 M.addInputSymbol( a );
125
126 if(Qu.empty ( )) {
127 return M;
128 }
129
130 for( const auto & q : Qu )
131 M.addState( q );
132
133 for( const auto & t : fsm.getTransitions( ) )
134 if( /* this part is not needed Qu.count( t.first.first ) && */ Qu.count( t.second ) )
135 M.addTransition( t.first.first, t.first.second, t.second );
136
137 for( const auto & q : fsm.getFinalStates( ) )
138 M.addFinalState( q );
139
140 return M;
141}
142
143template < class SymbolType, class StateType >
145 // 1.
147
148 // 2.
150
151 for( const auto & a : fsm.getInputAlphabet( ) )
152 M.addInputSymbol( a );
153
154 if(Qu.empty ( )) {
155 return M;
156 }
157
158 for( const auto & q : Qu )
159 M.addState( q );
160
161 for(const auto& init : fsm.getInitialStates()) {
162 if( Qu.count(init) )
163 M.addInitialState( init );
164 }
165
166 for( const auto & t : fsm.getTransitions( ) )
167 if( Qu.count ( t.second ) )
168 M.addTransition( t.first.first, t.first.second, t.second );
169
170 for( const auto & q : fsm.getFinalStates( ) )
171 M.addFinalState( q );
172
173 return M;
174}
175
176template < class SymbolType, class StateType >
178 // 1.
180
181 // 2.
183
184 for( const auto & a : fta.getInputAlphabet( ) )
185 M.addInputSymbol( a );
186
187 if(Qu.empty ( )) {
188 return M;
189 }
190
191 for( const auto & q : Qu )
192 M.addState( q );
193
194 for( const auto & t : fta.getTransitions( ) )
195 if( Qu.count( t.second ) )
196 M.addTransition( t.first.first, t.first.second, t.second );
197
198 for( const auto & q : fta.getFinalStates( ) )
199 M.addFinalState( q );
200
201 return M;
202}
203
204template < class SymbolType, class StateType >
206 // 1.
208
209 // 2.
211
212 for( const auto & a : fta.getInputAlphabet( ) )
213 M.addInputSymbol( a );
214
215 if(Qu.empty ( )) {
216 return M;
217 }
218
219 for( const auto & q : Qu )
220 M.addState( q );
221
222 for( const auto & t : fta.getTransitions( ) )
223 if( Qu.count( t.second ) )
224 M.addTransition( t.first.first, t.first.second, t.second );
225
226 for( const auto & q : fta.getFinalStates( ) )
227 M.addFinalState( q );
228
229 return M;
230}
231
232template < class SymbolType, class StateType >
235
237 M.setInputAlphabet ( afza.getInputAlphabet ( ) );
238 M.setStates ( Qu );
239
240 if ( M.getStates ( ).empty ( ) ) {
241 return M;
242 }
243
244 for ( const auto & t : afza.getTransitions( ) )
245 if ( Qu.count ( t.second ) )
246 M.addTransition ( t.first, t.second );
247
248 M.setFinalStates ( afza.getFinalStates ( ) );
249 return M;
250}
251
252template < class SymbolType, class StateType >
255
257 M.setInputAlphabet ( afza.getInputAlphabet ( ) );
258 M.setStates ( Qu );
259
260 if ( M.getStates ( ).empty ( ) ) {
261 return M;
262 }
263
264 for ( const auto & t : afza.getTransitions( ) )
265 if ( Qu.count ( t.second ) )
266 M.addTransition ( t.first, t.second );
267
268 M.setFinalStates ( afza.getFinalStates ( ) );
269 return M;
270}
271
272} /* namespace simplify */
273
274} /* namespace automaton */
275
Deterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree la...
Definition: ArcFactoredDeterministicZAutomaton.h:65
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:145
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: ArcFactoredDeterministicZAutomaton.h:232
void setStates(ext::set< StateType > states)
Definition: ArcFactoredDeterministicZAutomaton.h:125
const ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredDeterministicZAutomaton.h:284
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredDeterministicZAutomaton.h:194
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:96
void setFinalStates(ext::set< StateType > states)
Definition: ArcFactoredDeterministicZAutomaton.h:174
bool addTransition(ext::variant< SymbolType, ext::pair< StateType, StateType > > lhs, StateType rhs)
Add a transition to the automaton.
Definition: ArcFactoredDeterministicZAutomaton.h:401
Nondeterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree...
Definition: ArcFactoredNondeterministicZAutomaton.h:67
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:203
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: ArcFactoredNondeterministicZAutomaton.h:241
void setStates(ext::set< StateType > states)
Definition: ArcFactoredNondeterministicZAutomaton.h:134
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:154
void setFinalStates(ext::set< StateType > states)
Definition: ArcFactoredNondeterministicZAutomaton.h:183
const ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:293
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:105
bool addTransition(ext::variant< SymbolType, ext::pair< StateType, StateType > > lhs, StateType rhs)
Add a transition to the automaton.
Definition: ArcFactoredNondeterministicZAutomaton.h:415
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
const ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > & getTransitions() const &
Definition: DFTA.h:289
bool addInputSymbol(common::ranked_symbol< SymbolType > symbol)
Definition: DFTA.h:223
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: DFTA.h:203
bool addFinalState(StateType state)
Definition: DFTA.h:174
bool addTransition(common::ranked_symbol< SymbolType > symbol, ext::vector< StateType > prevStates, StateType next)
Add a transition to the automaton.
Definition: DFTA.h:406
bool addState(StateType state)
Definition: DFTA.h:125
const ext::set< StateType > & getFinalStates() const &
Definition: DFTA.h:154
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
bool addInitialState(StateType state)
Definition: MultiInitialStateNFA.h:137
bool addState(StateType state)
Definition: MultiInitialStateNFA.h:186
bool addFinalState(StateType state)
Definition: MultiInitialStateNFA.h:235
bool addInputSymbol(SymbolType symbol)
Definition: MultiInitialStateNFA.h:284
const ext::set< StateType > & getInitialStates() const &
Definition: MultiInitialStateNFA.h:117
const ext::multimap< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: MultiInitialStateNFA.h:520
const ext::set< StateType > & getFinalStates() const &
Definition: MultiInitialStateNFA.h:215
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: MultiInitialStateNFA.h:264
bool addTransition(StateType from, SymbolType input, StateType to)
Add a transition to the automaton.
Definition: MultiInitialStateNFA.h:486
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
bool addTransition(common::ranked_symbol< SymbolType > symbol, ext::vector< StateType > prevStates, StateType next)
Add a transition to the automaton.
Definition: NFTA.h:425
const ext::set< StateType > & getFinalStates() const &
Definition: NFTA.h:159
bool addState(StateType state)
Definition: NFTA.h:130
const ext::multimap< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > & getTransitions() const &
Definition: NFTA.h:294
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: NFTA.h:208
bool addFinalState(StateType state)
Definition: NFTA.h:179
bool addInputSymbol(common::ranked_symbol< SymbolType > symbol)
Definition: NFTA.h:228
static ext::set< typename T::StateType > usefulStates(const T &fsm)
Definition: UselessStatesRemover.h:52
static T remove(const T &fsm)
Definition: UselessStatesRemover.h:114
Definition: set.hpp:44
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31