Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DistinguishableStates.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
43 template < class StateType >
44 static ext::set < ext::pair < StateType, StateType > > initial ( const ext::set < StateType > & states, const ext::set < StateType > & finals ) {
46
47 for ( const StateType & a : states ) {
48 for ( const StateType & b : states ) {
49 if ( finals.count ( a ) != finals.count ( b ) ) {
50 init.insert ( ext::make_pair ( a, b ) );
51 init.insert ( ext::make_pair ( b, a ) );
52 }
53 }
54 }
55
56 return init;
57 }
58
59public:
68 template < class SymbolType, class StateType >
70
71 template < class SymbolType, class StateType >
73
74 template < class SymbolType, class StateType >
76};
77
78template < class SymbolType, class StateType >
81
82 bool changed;
83 do {
84 changed = false;
85
86 for ( const StateType & a : fsm.getStates ( ) ) {
87 for ( const StateType & b : fsm.getStates ( ) ) {
88 if ( distinguishable.count ( ext::make_pair ( a, b ) ) ) // we have not yet found this
89 continue;
90
91 for ( const SymbolType & symb : fsm.getInputAlphabet ( ) ) {
92 auto trA = fsm.getTransitions ( ).find ( ext::make_pair ( a, symb ) );
93 auto trB = fsm.getTransitions ( ).find ( ext::make_pair ( b, symb ) );
94
95 if ( trA == fsm.getTransitions ( ).end ( ) && trB == fsm.getTransitions ( ).end ( ) ) {
96 // end up in the same target state (dead state) -> not distinguishable
97 } else if ( trA == fsm.getTransitions ( ).end ( ) || trB == fsm.getTransitions ( ).end ( ) || distinguishable.count ( ext::make_pair ( trA -> second, trB -> second ) ) ) {
98 // end up in dead state - dead state is be distinguishable from every other state OR
99 // end up in distinguishable states
100 distinguishable.insert ( ext::make_pair ( a, b ) );
101 distinguishable.insert ( ext::make_pair ( b, a ) );
102 changed = true;
103 }
104 }
105 }
106 }
107
108 } while ( changed );
109
110 return distinguishable;
111}
112
113template < class SymbolType, class StateType >
116
117 bool changed;
118 do {
119 changed = false;
120
121 for ( const StateType & a : fta.getStates ( ) ) {
122 for ( const StateType & b : fta.getStates ( ) ) {
123 if ( distinguishable.count ( ext::make_pair ( a, b ) ) )
124 continue;
125
126 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < StateType > >, StateType > & transition : fta.getTransitions ( ) ) {
127 for ( size_t i = 0; i < transition.first.second.size ( ); ++ i ) {
128 if ( transition.first.second [ i ] == a ) {
129 ext::vector < StateType > copy = transition.first.second;
130 copy [ i ] = b;
131
132 const auto & transition2 = fta.getTransitions ( ).find ( std::make_pair ( transition.first.first, std::move ( copy ) ) );
133
134 if ( transition2 == fta.getTransitions ( ).end ( ) || distinguishable.count ( ext::make_pair ( transition.second, transition2->second ) ) ) {
135 // end up in dead state - dead state is be distinguishable from every other state OR
136 // end up in distinguishable states
137 distinguishable.insert ( ext::make_pair ( a, b ) );
138 distinguishable.insert ( ext::make_pair ( b, a ) );
139 changed = true;
140 }
141 }
142 }
143 }
144 }
145 }
146 } while ( changed );
147
148 return distinguishable;
149}
150
151template < class SymbolType, class StateType >
154
155 bool changed;
156 do {
157 changed = false;
158
159 for ( const StateType & a : fta.getStates ( ) ) {
160 for ( const StateType & b : fta.getStates ( ) ) {
161 if ( distinguishable.count ( ext::make_pair ( a, b ) ) )
162 continue;
163
164 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::multiset < StateType > >, StateType > & transition : fta.getTransitions ( ) ) {
165 for ( const StateType & state : transition.first.second ) {
166 if ( state == a ) {
167 ext::multiset < StateType > copy = transition.first.second;
168 copy.erase ( copy.find ( state ) );
169 copy.insert ( b );
170
171 const auto & transition2 = fta.getTransitions ( ).find ( std::make_pair ( transition.first.first, std::move ( copy ) ) );
172
173 if ( transition2 == fta.getTransitions ( ).end ( ) || distinguishable.count ( ext::make_pair ( transition.second, transition2->second ) ) ) {
174 // end up in dead state - dead state is be distinguishable from every other state OR
175 // end up in distinguishable states
176 distinguishable.insert ( ext::make_pair ( a, b ) );
177 distinguishable.insert ( ext::make_pair ( b, a ) );
178 changed = true;
179 }
180 }
181 }
182 }
183 }
184 }
185 } while ( changed );
186
187 return distinguishable;
188}
189
190} /* namespace properties */
191
192} /* namespace automaton */
193
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< SymbolType > & getInputAlphabet() const &
Definition: DFA.h:232
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
Deterministic unordered finite tree automaton without epsilon transitions. Accepts regular tree langu...
Definition: UnorderedDFTA.h:72
const ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > & getTransitions() const &
Definition: UnorderedDFTA.h:287
const ext::set< StateType > & getFinalStates() const &
Definition: UnorderedDFTA.h:152
const ext::set< StateType > & getStates() const &
Definition: UnorderedDFTA.h:103
Definition: DistinguishableStates.h:42
static ext::set< ext::pair< StateType, StateType > > distinguishable(const automaton::DFA< SymbolType, StateType > &fsm)
Definition: DistinguishableStates.h:79
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
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
p second
Definition: ToRegExpAlgebraic.h:126
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: ToGrammar.h:31
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79