Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToRTEStateElimination.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 <algorithm>
27#include <numeric>
28
30
31#include <automaton/Automaton.h>
32#include <automaton/TA/DFTA.h>
34#include <automaton/TA/NFTA.h>
35
37
39
41
43
44namespace automaton {
45
46namespace convert {
47
53public:
62 template < class T, class SymbolType = DefaultSymbolType, class StateType = DefaultStateType >
64
65private:
72 template < class SymbolType, class StateType >
73 static void extendExtendedNFTA ( automaton::ExtendedNFTA< SymbolType, StateType >& automaton );
74
83 template < class SymbolType, class StateType >
85
93 template < class SymbolType, class StateType >
95};
96
97template < class T, class SymbolType, class StateType >
99 if ( automaton.getFinalStates ( ).empty ( ) )
101
102 // create an automaton, extend it with a new final state
104 extendExtendedNFTA ( extendedAutomaton );
105
106 // eliminate all non-final states
107 ext::set< StateType > statesToEliminate;
108 std::set_difference ( extendedAutomaton.getStates ( ).begin ( ), extendedAutomaton.getStates ( ).end ( ),
109 extendedAutomaton.getFinalStates ( ).begin ( ), extendedAutomaton.getFinalStates ( ).end ( ),
110 std::inserter ( statesToEliminate, statesToEliminate.begin ( ) ) );
111
112 for ( const StateType& state : statesToEliminate ) {
113 extendedAutomaton = eliminateState ( extendedAutomaton, state );
114 }
115
116 // step 4
118 for ( const auto& tr : extendedAutomaton.getTransitions ( ) )
119 for ( const StateType& targetState : tr.second )
120 alt.push_back ( ext::make_pair ( tr.first, targetState ) );
121
123}
124
125template < class SymbolType, class StateType >
127 if ( transitions.empty ( ) )
129 if ( transitions.size ( ) == 1 )
130 return transitions.at ( 0 ).first.first;
131
134 };
135
136 return std::accumulate ( transitions.begin ( ), transitions.end ( ), rte::FormalRTEStructure< ext::variant< SymbolType, StateType > > ( ), alt );
137}
138
139template < class SymbolType, class StateType >
141 // create state symbol in RTE's K alphabet
143
144 // create new automaton, without state Q
145 automaton::ExtendedNFTA< SymbolType, StateType > newAutomaton ( automaton.getStates ( ), automaton.getInputAlphabet ( ), automaton.getFinalStates ( ) );
146 newAutomaton.removeState ( state ); // preserve all states but state (the one to eliminate)
147
148 // divide transitions into the following groups:
149 // - loop(Q) - Q is in sources AND Q is a target
150 // - incoming(Q) - Q is NOT in sources AND Q is a target
151 // - outgoing(Q) - Q is in sources AND Q is NOT a target
152 // - not_take_part(Q) - Q is NOT in sources AND Q is NOT a target
153 //
154 // also identify prev(incoming(Q)), loop(incoming(Q))
156 transitionType loop;
157 transitionType incoming;
158 transitionType outgoing;
159
160 ext::set < StateType > prev_loop;
161 ext::set < StateType > prev_incoming;
162
163 for ( const auto& transition : automaton.getTransitions ( ) ) {
164 for ( const StateType& target : transition.second ) {
165 const ext::vector< StateType >& src_states = transition.first.second;
166
167 const bool is_source = std::find ( src_states.begin ( ), src_states.end ( ), state ) != src_states.end ( );
168 const bool is_target = target == state;
169
170 if ( is_source && is_target ) { // loop
171 loop.push_back ( ext::make_pair ( transition.first, target ) );
172 prev_loop.insert ( transition.first.second.begin ( ), transition.first.second.end ( ) );
173 } else if ( ! is_source && is_target ) { // incoming
174 incoming.push_back ( ext::make_pair ( transition.first, target ) );
175 prev_incoming.insert ( transition.first.second.begin ( ), transition.first.second.end ( ) );
176 } else if ( is_source && ! is_target ) { //outgoing
177 outgoing.push_back ( ext::make_pair ( transition.first, target ) );
178 } else /* ( ! is_source && ! is_target ) */ { // not_take_part
179 newAutomaton.addTransition ( transition.first.first, transition.first.second, target );
180 }
181 }
182 }
183
185 const edgeLabelType rte_loop = createAlternation ( loop );
186 const edgeLabelType rte_incoming = createAlternation ( incoming );
187
188 // eliminate all incoming and outgoing transitions as follows:
189 // for every outgoing transition:
190 // - do the SUBST(outgoing, SUBST(iter(loops), incoming)) operation
191 for ( const auto& transition : outgoing ) {
192 const rte::FormalRTEStructure< ext::variant< SymbolType, StateType > >& tr_rte = transition.first.first;
193 const ext::vector< StateType >& tr_src_states = transition.first.second;
194 const StateType& tr_target = transition.second;
195
196 // by eliminating Q, the new transition T will be in the from ( merge(prev_loop, prev_incoming, prev_outgoing) ) -> outgoing.target
197 // The order of prev_states is not important as the order is present in the RTE structure since the first conversion to ExtendedNFTA
198 ext::set< StateType > prevStates;
199 prevStates.insert ( prev_loop.begin ( ), prev_loop.end ( ) );
200 prevStates.insert ( prev_incoming.begin ( ), prev_incoming.end ( ) );
201 prevStates.insert ( tr_src_states.begin ( ), tr_src_states.end ( ) );
202 prevStates.erase ( state );
203
206 tr_rte.getStructure ( ), // outgoing rte
208 rte::FormalRTEIteration< ext::variant< SymbolType, StateType > > ( rte_loop.getStructure ( ), stateSymbol ),
209 rte_incoming.getStructure ( ), stateSymbol ),
210 stateSymbol ) );
211
212 // TATA pattern
213 /*
214 rte::FormalRTEStructure < ext::variant < SymbolType, StateType > > rte (
215 rte::FormalRTESubstitution < ext::variant < SymbolType, StateType > > (
216 rte::FormalRTESubstitution < ext::variant < SymbolType, StateType > > (
217 tr_rte.getStructure ( ), // outgoing rte
218 rte::FormalRTEIteration < ext::variant < SymbolType, StateType > > ( rte_loop.getStructure ( ), stateSymbol ),
219 stateSymbol ),
220 rte_incoming.getStructure ( ),
221 stateSymbol ) );
222 */
223
224 newAutomaton.addTransition ( rte::simplify::RTEOptimize::optimize ( rte ), ext::vector< StateType > ( prevStates.begin ( ), prevStates.end ( ) ), tr_target );
225 }
226
227 return newAutomaton;
228}
229
230template < class SymbolType, class StateType >
231void ToRTEStateElimination::extendExtendedNFTA ( automaton::ExtendedNFTA< SymbolType, StateType >& automaton ) {
232 // create new state with final label
233 const StateType f = common::createUnique ( label::FinalStateLabel::instance< StateType > ( ), automaton.getStates ( ) );
234 automaton.addState ( f );
235
236 // create transitions from all final states to the new state, the transition contains only reference (RTE's K symbol) to the previous final state
237 for ( const StateType& state : automaton.getFinalStates ( ) ) {
239 automaton.addTransition ( expr, { state }, f );
240 }
241
242 // new state is the only final
243 automaton.setFinalStates ( { f } );
244}
245
246} /* namespace convert */
247
248} /* namespace automaton */
249
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: ExtendedNFTA.h:75
Definition: ToRTEStateElimination.h:52
static rte::FormalRTE< ext::variant< SymbolType, StateType > > convert(const T &automaton)
Definition: ToRTEStateElimination.h:98
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Represents the alternation operator in the regular tree expression. The node must have exactly two ch...
Definition: FormalRTEAlternation.h:44
Represents the empty expression in the regular tree expression. The node can't have any children.
Definition: FormalRTEEmpty.h:40
Represents the iteration operator in the regular tree expression. The node has exactly one child.
Definition: FormalRTEIteration.h:45
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: FormalRTEStructure.h:41
const FormalRTEElement< SymbolType > & getStructure() const
Definition: FormalRTEStructure.h:155
Represents the concatenation operator in the regular tree expression. The node must have exactly two ...
Definition: FormalRTESubstitution.h:44
Represents the substitution symbol in the regular tree expression. The node can't have any children.
Definition: FormalRTESymbolSubst.h:43
Formal regular tree expression represents regular tree expression. It describes regular tree language...
Definition: FormalRTE.h:71
static rte::FormalRTE< SymbolType > optimize(const rte::FormalRTE< SymbolType > &rte)
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
Definition: ToGrammar.h:31
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
Definition: converterCommon.hpp:8
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToFTAGlushkov.h:22