Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UnreachableStatesRemover.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
30#include <automaton/FSM/NFA.h>
31#include <automaton/FSM/DFA.h>
34#include <automaton/TA/DFTA.h>
35
37
38namespace automaton {
39
40namespace simplify {
41
54public:
64 template < class T >
65 requires isDFA < T > || isNFA < T > || isEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
66 static T remove ( const T & fsm );
67
78 template < class SymbolType, class StateType >
80
92 template < class T >
93 requires isDFTA < T > || isNFTA < T >
94 static T remove ( const T & fta );
95
106 template < class T >
107 requires isAFDZA < T > || isAFNZA < T >
108 static T remove( const T & afza );
109};
110
111template < class T >
112requires isDFA < T > || isNFA < T > || isEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
114 using StateType = typename T::StateType;
115
116 // 1a
118
119 // 2
120 T M(fsm.getInitialState());
121
122 M.setStates( Qa );
123 M.setInputAlphabet( fsm.getInputAlphabet( ) );
124
125 for( const auto & transition : fsm.getTransitions( ) )
126 if( Qa.count( transition.first.first ) )
127 M.addTransition( transition.first.first, transition.first.second, transition.second );
128
129 ext::set<StateType> finalStates;
130 std::set_intersection( fsm.getFinalStates( ).begin(), fsm.getFinalStates( ).end(), Qa.begin( ), Qa.end( ), std::inserter( finalStates, finalStates.begin( ) ) );
131 M.setFinalStates( finalStates );
132
133 return M;
134}
135
136template < class SymbolType, class StateType >
138 // 1a
140
141 // 2
143
144 M.setStates( Qa );
147
148 for( const auto & transition : fsm.getTransitions( ) )
149 if( Qa.count( transition.first.first ) )
150 M.addTransition( transition.first.first, transition.first.second, transition.second );
151
152 ext::set<StateType> finalStates;
153 std::set_intersection( fsm.getFinalStates( ).begin(), fsm.getFinalStates( ).end(), Qa.begin( ), Qa.end( ), std::inserter( finalStates, finalStates.begin( ) ) );
154 M.setFinalStates( finalStates );
155
156 return M;
157}
158
159template < class T >
160requires isDFTA < T > || isNFTA < T >
161T UnreachableStatesRemover::remove( const T & fta ) {
162 using StateType = typename T::StateType;
163
164 // 1a
166
167 // 2
168 T M;
169
170 M.setStates( Qa );
171 M.setInputAlphabet( fta.getInputAlphabet( ) );
172
173 for( const auto & transition : fta.getTransitions( ) )
174 if( std::all_of ( transition.first.second.begin ( ), transition.first.second.end ( ), [ & ] ( const StateType & state ) { return Qa.count ( state ); } ) )
175 M.addTransition ( transition.first.first, transition.first.second, transition.second );
176
177 ext::set<StateType> finalStates;
178 std::set_intersection( fta.getFinalStates( ).begin(), fta.getFinalStates( ).end(), Qa.begin( ), Qa.end( ), std::inserter( finalStates, finalStates.begin( ) ) );
179 M.setFinalStates( std::move ( finalStates ) );
180
181 return M;
182}
183
184template < class T >
185requires isAFDZA < T > || isAFNZA < T >
186T UnreachableStatesRemover::remove( const T & afza ) {
187 using StateType = typename T::StateType;
188 using SymbolType = typename T::SymbolType;
189
190 // 1a
192
193 // 2
194 T M;
195
196 M.setStates( Qa );
197 M.setInputAlphabet( afza.getInputAlphabet( ) );
198
199 for( const auto & transition : afza.getTransitions( ) ) {
200 if ( transition.first.template is < SymbolType > ( ) && Qa.contains ( transition.second ) ) {
201 M.addTransition ( transition.first, transition.second );
202 } else if ( transition.first.template is < ext::pair < StateType, StateType > > ( ) ) {
203 const auto& [ q1, q2 ] = transition.first.template get < ext::pair < StateType, StateType > > ( );
204 if ( Qa.contains ( q1 ) && Qa.contains ( q2 ) && Qa.contains ( transition.second ) ) {
205 M.addTransition ( transition.first, transition.second );
206 }
207 }
208 }
209
210 ext::set<StateType> finalStates;
211 std::set_intersection( afza.getFinalStates( ).begin(), afza.getFinalStates( ).end(), Qa.begin( ), Qa.end( ), std::inserter( finalStates, finalStates.begin( ) ) );
212 M.setFinalStates( std::move ( finalStates ) );
213
214 return M;
215}
216
217} /* namespace simplify */
218
219} /* namespace automaton */
220
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
void setStates(ext::set< StateType > states)
Definition: MultiInitialStateNFA.h:195
void setInitialStates(ext::set< StateType > states)
Definition: MultiInitialStateNFA.h:146
void setFinalStates(ext::set< StateType > states)
Definition: MultiInitialStateNFA.h:244
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
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: MultiInitialStateNFA.h:302
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
static ext::set< typename T::StateType > reachableStates(const T &fsm)
Definition: UnreachableStatesRemover.h:53
static T remove(const T &fsm)
Definition: UnreachableStatesRemover.h:113
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
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
typename T::SymbolType SymbolType
Definition: SingleInitialStateEpsilonTransition.h:72
Definition: ToGrammar.h:31
all_of(T &&...) -> all_of< T... >