Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ForwardBisimulation.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 <alib/set>
27#include <alib/map>
28
29#include <automaton/TA/DFTA.h>
30#include <automaton/TA/NFTA.h>
31
34
35#include <automaton/FSM/DFA.h>
36#include <automaton/FSM/NFA.h>
37
40
41namespace automaton {
42
43namespace properties {
44
50 template < class StateType >
51 static ext::set < ext::pair < StateType, StateType > > initial ( const ext::set < StateType > & states, const ext::set < StateType > & finals ) {
53
54 for ( const StateType & a : states ) {
55 for ( const StateType & b : states ) {
56 if ( finals.count ( a ) == finals.count ( b ) ) {
57 init.insert ( ext::make_pair ( a, b ) );
58 init.insert ( ext::make_pair ( b, a ) );
59 }
60 }
61 }
62
63 return init;
64 }
65
66public:
67
75 template < class SymbolType, class StateType >
77
85 template < class SymbolType, class StateType >
87
88 template < class SymbolType, class StateType >
90
91 template < class SymbolType, class StateType >
93
101 template < class SymbolType, class StateType >
103
111 template < class SymbolType, class StateType >
113
121 template < class SymbolType, class StateType >
123
131 template < class SymbolType, class StateType >
133};
134
135template < class SymbolType, class StateType >
138}
139
140template < class SymbolType, class StateType >
143
144 bool changed;
145 do {
146 changed = false;
147
148 for ( const StateType & p : fta.getStates ( ) ) {
149 for ( const StateType & q : fta.getStates ( ) ) {
150 if ( ! forwardBisimulation.contains ( ext::make_pair ( p, q ) ) )
151 continue;
152
153 for ( const std::pair < const ext::pair < StateType, SymbolType >, StateType > & pTransition : fta.getTransitions ( ) ) {
154 if ( pTransition.first.first == p ) {
155 bool exists = false;
156 for ( const std::pair < const ext::pair < StateType, SymbolType >, StateType > & qTransition : fta.getTransitions ( ).equal_range ( std::make_pair ( q, pTransition.first.second ) ) ) {
157 if ( forwardBisimulation.contains ( ext::make_pair ( pTransition.second, qTransition.second ) ) ) {
158 exists = true;
159 break;
160 }
161 }
162
163 if ( ! exists ) {
164 forwardBisimulation.erase ( ext::make_pair ( p, q ) );
165 forwardBisimulation.erase ( ext::make_pair ( q, p ) );
166 changed = true;
167 }
168 }
169 }
170 }
171 }
172 } while ( changed );
173
174 return forwardBisimulation;
175}
176
177template < class SymbolType, class StateType >
180}
181
182template < class SymbolType, class StateType >
185
186 bool changed;
187 do {
188 changed = false;
189
190 for ( const StateType & p : afnza.getStates ( ) ) {
191 for ( const StateType & q : afnza.getStates ( ) ) {
192 if ( ! forwardBisimulation.contains ( ext::make_pair ( p, q ) ) )
193 continue;
194
195 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & pTransition : afnza.getTransitions ( ) ) {
196 if ( pTransition.first.template is < ext::pair < StateType, StateType > > ( ) && pTransition.first.template get < ext::pair < StateType, StateType > > ( ).first == p ) {
197 bool exists = false;
198 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & qTransition : afnza.getTransitions ( ) ) {
199 if ( qTransition.first.template is < ext::pair < StateType, StateType > > ( ) && qTransition.first.template get < ext::pair < StateType, StateType > > ( ).first == q && pTransition.first.template get < ext::pair < StateType, StateType > > ( ).second == qTransition.first.template get < ext::pair < StateType, StateType > > ( ).second ) {
200 if ( forwardBisimulation.contains ( ext::make_pair ( pTransition.second, qTransition.second ) ) ) {
201 exists = true;
202 break;
203 }
204 }
205 }
206
207 if ( ! exists ) {
208 forwardBisimulation.erase ( ext::make_pair ( p, q ) );
209 forwardBisimulation.erase ( ext::make_pair ( q, p ) );
210 changed = true;
211 }
212 }
213 if ( pTransition.first.template is < ext::pair < StateType, StateType > > ( ) && pTransition.first.template get < ext::pair < StateType, StateType > > ( ).second == p ) {
214 bool exists = false;
215 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & qTransition : afnza.getTransitions ( ) ) {
216 if ( qTransition.first.template is < ext::pair < StateType, StateType > > ( ) && qTransition.first.template get < ext::pair < StateType, StateType > > ( ).second == q && pTransition.first.template get < ext::pair < StateType, StateType > > ( ).first == qTransition.first.template get < ext::pair < StateType, StateType > > ( ).first ) {
217 if ( forwardBisimulation.contains ( ext::make_pair ( pTransition.second, qTransition.second ) ) ) {
218 exists = true;
219 break;
220 }
221 }
222 }
223
224 if ( ! exists ) {
225 forwardBisimulation.erase ( ext::make_pair ( p, q ) );
226 forwardBisimulation.erase ( ext::make_pair ( q, p ) );
227 changed = true;
228 }
229 }
230 }
231 }
232 }
233 } while ( changed );
234
235 return forwardBisimulation;
236}
237
238template < class SymbolType, class StateType >
241}
242
243template < class SymbolType, class StateType >
246
247 bool changed;
248 do {
249 changed = false;
250
251 for ( const StateType & p : fta.getStates ( ) ) {
252 for ( const StateType & q : fta.getStates ( ) ) {
253 if ( ! forwardBisimulation.contains ( ext::make_pair ( p, q ) ) )
254 continue;
255
256 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < StateType > >, StateType > & pTransition : fta.getTransitions ( ) ) {
257 for ( size_t i = 0; i < pTransition.first.second.size ( ); ++ i ) {
258 if ( pTransition.first.second [ i ] == p ) {
259 ext::vector < StateType > copy = pTransition.first.second;
260 copy [ i ] = q;
261
262 bool exists = false;
263 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < StateType > >, StateType > & qTransition : fta.getTransitions ( ).equal_range ( std::make_pair ( pTransition.first.first, std::move ( copy ) ) ) ) {
264 if ( forwardBisimulation.contains ( ext::make_pair ( pTransition.second, qTransition.second ) ) ) {
265 exists = true;
266 break;
267 }
268 }
269
270 if ( ! exists ) {
271 forwardBisimulation.erase ( ext::make_pair ( p, q ) );
272 forwardBisimulation.erase ( ext::make_pair ( q, p ) );
273 changed = true;
274 }
275 }
276 }
277 }
278 }
279 }
280 } while ( changed );
281
282 return forwardBisimulation;
283}
284
285template < class SymbolType, class StateType >
288}
289
290template < class SymbolType, class StateType >
293
294 bool changed;
295 do {
296 changed = false;
297
298 for ( const StateType & p : fta.getStates ( ) ) {
299 for ( const StateType & q : fta.getStates ( ) ) {
300 if ( ! forwardBisimulation.contains ( ext::make_pair ( p, q ) ) )
301 continue;
302
303 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::multiset < StateType > >, StateType > & pTransition : fta.getTransitions ( ) ) {
304 for ( const StateType & state : pTransition.first.second ) {
305 if ( state == p ) {
306 ext::multiset < StateType > copy = pTransition.first.second;
307 copy.erase ( copy.find ( state ) );
308 copy.insert ( q );
309
310 bool exists = false;
311 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::multiset < StateType > >, StateType > & qTransition : fta.getTransitions ( ).equal_range ( std::make_pair ( pTransition.first.first, std::move ( copy ) ) ) ) {
312 if ( forwardBisimulation.contains ( ext::make_pair ( pTransition.second, qTransition.second ) ) ) {
313 exists = true;
314 break;
315 }
316 }
317
318 if ( ! exists ) {
319 forwardBisimulation.erase ( ext::make_pair ( p, q ) );
320 forwardBisimulation.erase ( ext::make_pair ( q, p ) );
321 changed = true;
322 }
323 }
324 }
325 }
326 }
327 }
328 } while ( changed );
329
330 return forwardBisimulation;
331}
332
333} /* namespace properties */
334
335} /* namespace automaton */
336
Deterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree la...
Definition: ArcFactoredDeterministicZAutomaton.h:65
Nondeterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree...
Definition: ArcFactoredNondeterministicZAutomaton.h:67
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:154
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
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
const ext::set< StateType > & getStates() const &
Definition: NFA.h:136
const ext::multimap< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: NFA.h:484
const ext::set< StateType > & getFinalStates() const &
Definition: NFA.h:185
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
const ext::set< StateType > & getFinalStates() const &
Definition: NFTA.h:159
const ext::multimap< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > & getTransitions() const &
Definition: NFTA.h:294
const ext::set< StateType > & getStates() const &
Definition: NFTA.h:110
Deterministic unordered finite tree automaton without epsilon transitions. Accepts regular tree langu...
Definition: UnorderedDFTA.h:72
Nondeterministic unordered finite tree automaton without epsilon transitions. Accepts regular tree la...
Definition: UnorderedNFTA.h:72
const ext::set< StateType > & getStates() const &
Definition: UnorderedNFTA.h:110
const ext::set< StateType > & getFinalStates() const &
Definition: UnorderedNFTA.h:159
const ext::multimap< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > & getTransitions() const &
Definition: UnorderedNFTA.h:294
Definition: ForwardBisimulation.h:49
static ext::set< ext::pair< StateType, StateType > > forwardBisimulation(const automaton::DFA< SymbolType, StateType > &fta)
Definition: ForwardBisimulation.h:136
Definition: ranked_symbol.hpp:20
Definition: multiset.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
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693