Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UndistinguishableStates.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/FSM/DFA.h>
30#include <automaton/TA/DFTA.h>
31
33
34namespace automaton {
35
36namespace properties {
37
44 template < class StateType >
45 static ext::set < ext::pair < StateType, StateType > > initial ( const ext::set < StateType > & states, const ext::set < StateType > & finals ) {
47
48 for ( const StateType & a : states ) {
49 for ( const StateType & b : states ) {
50 if ( finals.count ( a ) == finals.count ( b ) ) {
51 init.insert ( ext::make_pair ( a, b ) );
52 init.insert ( ext::make_pair ( b, a ) );
53 }
54 }
55 }
56
57 return init;
58 }
59
60public:
70 template < class SymbolType, class StateType >
72
83 template < class SymbolType, class StateType >
85};
86
87template < class SymbolType, class StateType >
90
91 do {
93
94 for ( const ext::pair < StateType, StateType > & iter : undistinguishable2 ) {
95 for ( const std::pair < const ext::pair < StateType, SymbolType >, StateType > & transition : dfa.getTransitions ( ).equal_range ( ext::slice_comp ( iter.first ) ) ) {
96 const auto & transition2 = dfa.getTransitions ( ).find ( std::make_pair ( iter.second, transition.first.second ) );
97
98 if ( transition2 == dfa.getTransitions ( ).end ( ) || ! undistinguishable2.count ( ext::make_pair ( transition.second, transition2->second ) ) ) {
99 // end up in dead state - dead state is be distinguishable from every other state OR
100 // end up in distinguishable states
101 undistinguishable.erase ( ext::make_pair ( iter.first, iter.second ) );
102 undistinguishable.erase ( ext::make_pair ( iter.second, iter.first ) );
103 }
104 }
105 }
106 if ( undistinguishable == undistinguishable2 )
107 break;
108
109 } while ( true );
110
111 return undistinguishable;
112}
113
114template < class SymbolType, class StateType >
117
118 do {
120
121 for ( const ext::pair < StateType, StateType > & iter : undistinguishable2 ) {
122 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < StateType > >, StateType > & transition : fta.getTransitions ( ) ) {
123 for ( size_t i = 0; i < transition.first.second.size ( ); ++ i ) {
124 if ( transition.first.second [ i ] == iter . first ) {
125 ext::vector < StateType > copy = transition.first.second;
126 copy [ i ] = iter.second;
127
128 const auto & transition2 = fta.getTransitions ( ).find ( std::make_pair ( transition.first.first, std::move ( copy ) ) );
129
130 if ( transition2 == fta.getTransitions ( ).end ( ) || ! undistinguishable2.count ( ext::make_pair ( transition.second, transition2->second ) ) ) {
131 // end up in dead state - dead state is be distinguishable from every other state OR
132 // end up in distinguishable states
133 undistinguishable.erase ( ext::make_pair ( iter.first, iter.second ) );
134 undistinguishable.erase ( ext::make_pair ( iter.second, iter.first ) );
135 }
136 }
137 }
138 }
139 }
140 if ( undistinguishable == undistinguishable2 )
141 break;
142
143 } while ( true );
144
145 return undistinguishable;
146}
147
148} /* namespace properties */
149
150} /* namespace automaton */
151
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
const ext::set< StateType > & getFinalStates() const &
Definition: DFA.h:183
const ext::map< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: DFA.h:473
const ext::set< StateType > & getStates() const &
Definition: DFA.h:134
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
const ext::set< StateType > & getFinalStates() const &
Definition: DFTA.h:154
const ext::set< StateType > & getStates() const &
Definition: DFTA.h:105
Definition: UndistinguishableStates.h:43
static ext::set< ext::pair< StateType, StateType > > undistinguishable(const automaton::DFA< SymbolType, StateType > &dfa)
Definition: UndistinguishableStates.h:88
Definition: ranked_symbol.hpp:20
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
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
Definition: ToGrammar.h:31
SliceComp< Ts ... > slice_comp(const Ts &... inst)
Definition: functional.hpp:95
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79