Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DeterminizeNFTAPart.hxx
Go to the documentation of this file.
1
6#include <ext/algorithm>
7
8#include <alib/deque>
9
10#include <automaton/TA/NFTA.h>
11
12namespace automaton {
13
14namespace determinize {
15
16template < class SymbolType, class StateType >
18 if ( state == end ) {
19 resultTransition [ ext::make_pair ( symbol, transitionLHS ) ].insert ( rhs );
20 } else {
21 for ( const std::pair < const StateType, ext::set < StateType > > & dftaState : nftaStateToDftaStates.equal_range ( * state ) ) {
22 transitionLHS.push_back ( dftaState.second );
23 constructTransitions ( symbol, std::next ( state ), end, rhs, nftaStateToDftaStates, transitionLHS, resultTransition );
24 transitionLHS.pop_back ( );
25 }
26 }
27}
28
29template < class SymbolType, class StateType >
32
33 res.setInputAlphabet ( nfta.getInputAlphabet ( ) );
34
35 ext::multimap < StateType, ext::set < StateType > > nftaStateToDftaStates; // each dfta state must be inserted only once to each nfta d-subset state
36
37 for ( const auto & symbol : nfta.getInputAlphabet ( ) ) {
38 if ( symbol.getRank ( ) != 0 )
39 continue;
40
41 ext::set < StateType > dftaState;
43 for ( const auto & transition : nfta.getTransitions ( ).equal_range ( ext::tie ( symbol, source ) ) )
44 dftaState.insert ( transition.second );
45
46 for ( const auto & state : dftaState )
47 nftaStateToDftaStates.insert ( state, dftaState );
48
49 res.addState ( dftaState );
50
51 res.addTransition ( symbol, ext::vector < ext::set < StateType > > { }, dftaState );
52 }
53
54 bool added = true;
55 while ( added ) {
56 added = false;
57
59
60 for ( const auto & transition : nfta.getTransitions ( ) ) {
62 constructTransitions ( transition.first.first, transition.first.second.begin ( ), transition.first.second.end ( ), transition.second, nftaStateToDftaStates, transitionLHS, transitions );
63 }
64
65 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < ext::set < StateType > > >, ext::set < StateType > > & transition : transitions ) {
66 if ( res.addState ( transition.second ) ) {
67 added = true;
68
69 for ( const auto & state : transition.second )
70 nftaStateToDftaStates.insert ( state, transition.second );
71 }
72 res.addTransition ( transition.first.first, transition.first.second, transition.second );
73 }
74
75 }
76
77 const ext::set < StateType > & finalLabels = nfta.getFinalStates();
78 for ( const ext::set < StateType > & dfaState : res.getStates ( ) )
79 if ( ! ext::excludes ( finalLabels.begin ( ), finalLabels.end ( ), dfaState.begin ( ), dfaState.end ( ) ) )
80 res.addFinalState ( dfaState );
81
82 return res;
83}
84
85
86template < class SymbolType, class StateType >
88 if ( state == end ) {
89 resultTransition [ ext::make_pair ( symbol, transitionLHS ) ].insert ( rhs );
90 } else {
91 for ( const std::pair < const StateType, ext::set < StateType > > & dftaState : nftaStateToDftaStates.equal_range ( * state ) ) {
92 transitionLHS.insert ( dftaState.second );
93 constructTransitions ( symbol, std::next ( state ), end, rhs, nftaStateToDftaStates, transitionLHS, resultTransition );
94 transitionLHS.erase ( transitionLHS.find ( dftaState.second ) );
95 }
96 }
97}
98
99template < class SymbolType, class StateType >
102
103 res.setInputAlphabet ( nfta.getInputAlphabet ( ) );
104
105 ext::multimap < StateType, ext::set < StateType > > nftaStateToDftaStates; // each dfta state must be inserted only once to each nfta d-subset state
106
107 for ( const auto & symbol : nfta.getInputAlphabet ( ) ) {
108 if ( symbol.getRank ( ) != 0 )
109 continue;
110
111 ext::set < StateType > dftaState;
113 for ( const auto & transition : nfta.getTransitions ( ).equal_range ( ext::tie ( symbol, source ) ) )
114 dftaState.insert ( transition.second );
115
116 for ( const auto & state : dftaState )
117 nftaStateToDftaStates.insert ( state, dftaState );
118
119 res.addState ( dftaState );
120
121 res.addTransition ( symbol, ext::multiset < ext::set < StateType > > { }, dftaState );
122 }
123
124 bool added = true;
125 while ( added ) {
126 added = false;
127
129
130 for ( const auto & transition : nfta.getTransitions ( ) ) {
132 constructTransitions ( transition.first.first, transition.first.second.begin ( ), transition.first.second.end ( ), transition.second, nftaStateToDftaStates, transitionLHS, transitions );
133 }
134
135 for ( const std::pair < const ext::pair < common::ranked_symbol < SymbolType >, ext::multiset < ext::set < StateType > > >, ext::set < StateType > > & transition : transitions ) {
136 if ( res.addState ( transition.second ) ) {
137 added = true;
138
139 for ( const auto & state : transition.second )
140 nftaStateToDftaStates.insert ( state, transition.second );
141 }
142 res.addTransition ( transition.first.first, transition.first.second, transition.second );
143 }
144
145 }
146
147 const ext::set < StateType > & finalLabels = nfta.getFinalStates();
148 for ( const ext::set < StateType > & dfaState : res.getStates ( ) )
149 if ( ! ext::excludes ( finalLabels.begin ( ), finalLabels.end ( ), dfaState.begin ( ), dfaState.end ( ) ) )
150 res.addFinalState ( dfaState );
151
152 return res;
153}
154
155} /* namespace determinize */
156
157} /* namespace automaton */
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
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< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: NFTA.h:208
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 > & 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
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: UnorderedNFTA.h:208
static automaton::DFA< SymbolType, StateType > determinize(const automaton::DFA< SymbolType, StateType > &automaton)
Definition: Determinize.h:276
Definition: ranked_symbol.hpp:20
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
iterator insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:118
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
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
auto equal_range(K &&key) const &
Make range of elements with key equal to the key.
Definition: set.hpp:200
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
typename std::vector< T, Alloc >::const_iterator const_iterator
The type of constant values iterator.
Definition: vector.hpp:67
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
void constructTransitions(const common::ranked_symbol< SymbolType > &symbol, typename ext::vector< StateType >::const_iterator state, typename ext::vector< StateType >::const_iterator end, const StateType &rhs, const ext::multimap< StateType, ext::set< StateType > > &nftaStateToDftaStates, ext::vector< ext::set< StateType > > &transitionLHS, ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< ext::set< StateType > > >, ext::set< StateType > > &resultTransition)
Definition: DeterminizeNFTAPart.hxx:17
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Tests two sorted ranges wheter all elements from the second are not present in the first.
Definition: algorithm.hpp:46
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
void end()
Definition: measurements.cpp:19