Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ReachableStates.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 reachable,
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
26#include <ext/algorithm>
27
28#include <alib/deque>
29#include <alib/set>
30#include <alib/map>
31
35#include <automaton/FSM/NFA.h>
36#include <automaton/FSM/DFA.h>
37#include <automaton/TA/DFTA.h>
38#include <automaton/TA/NFTA.h>
41
42namespace automaton {
43
44namespace properties {
45
51public:
60 template < class T >
61 requires isDFA < T > || isNFA < T > || isEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
63
72 template < class T >
73 requires isMultiInitialStateNFA < T > || isMultiInitialStateEpsilonNFA < T >
75
84 template < class T >
85 requires isDFTA < T > || isNFTA < T >
87
88 template < class T >
89 requires isAFDZA < T > || isAFNZA < T >
91};
92
93template < class T >
94requires isDFA < T > || isNFA < T > || isEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
96 using StateType = typename T::StateType;
97
98 // 1a
100 Qi.push_back( ext::set<StateType>( ) );
101 Qi.at( 0 ).insert ( fsm.getInitialState( ) );
102
103 int i = 0;
104
105 // 1bc
106 do {
107 i = i + 1;
108
109 Qi.push_back( Qi.at( i - 1 ) );
110
111 for( const auto & p : Qi.at( i - 1 ) )
112 for( const auto & transition : fsm.getTransitionsFromState( p ) )
113 Qi.at( i ).insert( transition.second );
114
115 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
116
117 return Qi.at( i );
118}
119
120template < class T >
121requires isMultiInitialStateNFA < T > || isMultiInitialStateEpsilonNFA < T >
123 using StateType = typename T::StateType;
124
125 // 1a
127 Qi.push_back( ext::set<StateType>( ) );
128 Qi.at( 0 ) = fsm.getInitialStates( );
129
130 int i = 0;
131
132 // 1bc
133 do {
134 i = i + 1;
135
136 Qi.push_back( Qi.at( i - 1 ) );
137
138 for( const auto & p : Qi.at( i - 1 ) )
139 for( const auto & transition : fsm.getTransitionsFromState( p ) )
140 Qi.at( i ).insert ( transition.second );
141 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
142
143 return Qi.at( i );
144}
145
146template < class T >
147requires isDFTA < T > || isNFTA < T >
149 using StateType = typename T::StateType;
150
151 // 1a
153 Qi.push_back( ext::set<StateType>( ) );
154
155 int i = 0;
156
157 // 1bc
158 do {
159 i = i + 1;
160
161 Qi.push_back( Qi.at( i - 1) );
162
163 for( const auto & transition : fta.getTransitions ( ) )
164 if ( std::all_of ( transition.first.second.begin ( ), transition.first.second.end ( ), [ & ] ( const StateType & state ) { return Qi.at ( i - 1 ).count ( state ); } ) )
165 Qi.at( i ).insert ( transition.second );
166
167 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
168
169 return Qi.at( i );
170}
171
172template < class T >
173requires isAFDZA < T > || isAFNZA < T >
175 using StateType = typename T::StateType;
176 using SymbolType = typename T::SymbolType;
177
178 // 1a
180 Qi.push_back( ext::set<StateType>( ) );
181
182 int i = 0;
183
184 // 1bc
185 do {
186 i = i + 1;
187
188 Qi.push_back( Qi.at( i - 1) );
189
190 for( const auto & transition : afza.getTransitions ( ) ) {
191 if ( transition.first.template is < SymbolType > ( ) ) {
192 Qi.at( i ).insert ( transition.second );
193 } else if ( transition.first.template is < ext::pair < StateType, StateType > > ( ) ) {
194 const auto& [q1, q2] = transition.first.template get < ext::pair < StateType, StateType > > ( );
195 if ( Qi.at ( i - 1 ).contains ( q1 ) && Qi.at ( i - 1 ).contains ( q2 ) ) {
196 Qi.at( i ).insert ( transition.second );
197 }
198 }
199 }
200 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
201
202 return Qi.at( i );
203}
204
205} /* namespace properties */
206
207} /* namespace automaton */
208
Definition: ReachableStates.h:50
static ext::set< typename T::StateType > reachableStates(const T &fta)
static ext::set< typename T::StateType > reachableStates(const T &fsm)
static ext::set< typename T::StateType > reachableStates(const T &fsm)
static ext::set< typename T::StateType > reachableStates(const T &afza)
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
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
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
ext::deque< ext::set< StateType > > Qi
Definition: ReachableStates.h:95
Definition: ToGrammar.h:31
all_of(T &&...) -> all_of< T... >