Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UsefulStates.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
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>
39#include <automaton/TA/DFTA.h>
40#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 > || isMultiInitialStateNFA < T > || isMultiInitialStateEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
63
72 template < class T >
73 requires isDFTA < T > || isNFTA < T >
75
84 template < class T >
85 requires isAFDZA < T > || isAFNZA < T >
87};
88
89template < class T >
90requires isDFA < T > || isNFA < T > || isEpsilonNFA < T > || isMultiInitialStateNFA < T > || isMultiInitialStateEpsilonNFA < T > || isCompactNFA < T > || isExtendedNFA < T >
92 using StateType = typename T::StateType;
93
94 // 1a
96 Qi.push_back( ext::set<StateType>( ) );
97 Qi.at( 0 ) = fsm.getFinalStates( );
98
99 int i = 1;
100
101 // 1bc
102 while( true ) {
103 Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
104 for( const auto & p : Qi.at( i - 1 ) )
105 for( const auto & t : fsm.getTransitionsToState( p ) )
106 Qi.at( i ).insert( t.first.first );
107
108 if( Qi.at( i ) == Qi.at( i - 1 ) )
109 break;
110
111 i = i + 1;
112 }
113 return Qi.at( i );
114}
115
116template < class T >
117requires isDFTA < T > || isNFTA < T >
119 using StateType = typename T::StateType;
120
121 // 1a
123 Qi.push_back( ext::set<StateType>( ) );
124 Qi.at( 0 ) = fta.getFinalStates( );
125
126 int i = 0;
127
128 // 1bc
129 do {
130 i = i + 1;
131
132 Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
133 for( const auto & p : Qi.at( i - 1 ) )
134 for( const auto & t : fta.getTransitionsToState ( p ) )
135 Qi.at( i ).insert( t.first.second.begin ( ), t.first.second.end ( ) );
136
137 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
138
139 return Qi.at( i );
140}
141
142template < class T >
143requires isAFDZA < T > || isAFNZA < T >
145 using StateType = typename T::StateType;
146
147 // 1a
149 Qi.push_back( ext::set<StateType>( ) );
150 Qi.at( 0 ) = afza.getFinalStates( );
151
152 int i = 0;
153
154 // 1bc
155 do {
156 i = i + 1;
157
158 Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
159 for( const auto & p : Qi.at( i - 1 ) )
160 for( const auto & t : afza.getTransitionsToState ( p ) ) {
161 if ( t.first.template is < ext::pair < StateType, StateType > > ( ) ) {
162 const auto& lhs = t.first.template get < ext::pair < StateType, StateType > > ( );
163 Qi.at( i ).insert( lhs.first );
164 Qi.at( i ).insert( lhs.second );
165 }
166 }
167
168 } while ( Qi.at( i ) != Qi.at( i - 1 ) );
169
170 return Qi.at( i );
171}
172
173} /* namespace properties */
174
175} /* namespace automaton */
176
Definition: UsefulStates.h:50
static ext::set< typename T::StateType > usefulStates(const T &fsm)
static ext::set< typename T::StateType > usefulStates(const T &fta)
static ext::set< typename T::StateType > usefulStates(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
ext::deque< ext::set< StateType > > Qi
Definition: ReachableStates.h:95
Definition: ToGrammar.h:31