Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
MinimizeByPartitioning.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/FSM/NFA.h>
31#include <automaton/TA/DFTA.h>
32#include <automaton/TA/NFTA.h>
35
36namespace automaton {
37
38namespace simplify {
39
47 template < class T >
49
50 template < class T >
52
53 template < class T >
55
56public:
68 template < class T >
69 requires isDFA < T > || isNFA < T >
70 static FA < T > minimize ( const T & nfa, const ext::set < ext::set < typename T::StateType > > & partitions );
71
83 template < class T >
84 requires isDFTA < T > || isNFTA < T >
85 static FTA < T > minimize ( const T & dfta, const ext::set < ext::set < typename T::StateType > > & partitions );
86
98 template < class T >
99 requires isUnorderedDFTA < T > || isUnorderedNFTA < T >
100 static UnorderedFTA < T > minimize ( const T & dfta, const ext::set < ext::set < typename T::StateType > > & partitions );
101
102private:
114 template < class StateType >
115 static ext::map < StateType, ext::set < StateType > > partitionMap ( const ext::set < ext::set < StateType > > & partitions );
116};
117
118template < class StateType >
119ext::map < StateType, ext::set < StateType > > MinimizeByPartitioning::partitionMap ( const ext::set < ext::set < StateType > > & partitions ) {
121
122 for ( const auto & partition : partitions )
123 for ( const StateType & state : partition )
124 res [ state ] = partition;
125
126 return res;
127}
128
129template < class T >
130requires isDFA < T > || isNFA < T >
131MinimizeByPartitioning::FA < T > MinimizeByPartitioning::minimize ( const T & nfa, const ext::set < ext::set < typename T::StateType > > & partitions ) {
132 using StateType = typename T::StateType;
133
135
136 MinimizeByPartitioning::FA < T > res ( statePartitionMap.at ( nfa.getInitialState ( ) ) );
137 res.setStates ( partitions );
138 res.addInputSymbols ( nfa.getInputAlphabet ( ) );
139 for ( const StateType & state : nfa.getFinalStates ( ) )
140 res.addFinalState ( statePartitionMap.at ( state ) );
141
142 for ( const auto & transition : nfa.getTransitions ( ) )
143 res.addTransition ( statePartitionMap.at ( transition.first.first ), transition.first.second, statePartitionMap.at ( transition.second ) );
144
145 return res;
146}
147
148template < class T >
149requires isDFTA < T > || isNFTA < T >
150MinimizeByPartitioning::FTA < T > MinimizeByPartitioning::minimize ( const T & dfta, const ext::set < ext::set < typename T::StateType > > & partitions ) {
151 using StateType = typename T::StateType;
152
153 const ext::map < StateType, ext::set < StateType > > statePartitionMap = partitionMap ( partitions );
154
155 MinimizeByPartitioning::FTA < T > res;
156 res.setStates ( partitions );
157 res.addInputSymbols ( dfta.getInputAlphabet ( ) );
158 for ( const StateType & state : dfta.getFinalStates ( ) )
159 res.addFinalState ( statePartitionMap.at ( state ) );
160
161 for ( const auto & transition : dfta.getTransitions ( ) ) {
163 for ( const StateType & from : transition.first.second )
164 minimalFrom.push_back ( statePartitionMap.at ( from ) );
165 res.addTransition ( transition.first.first, minimalFrom, statePartitionMap.at ( transition.second ) );
166 }
167
168 return res;
169}
170
171template < class T >
172requires isUnorderedDFTA < T > || isUnorderedNFTA < T >
173MinimizeByPartitioning::UnorderedFTA < T > MinimizeByPartitioning::minimize ( const T & dfta, const ext::set < ext::set < typename T::StateType > > & partitions ) {
174 using StateType = typename T::StateType;
175
176 const ext::map < StateType, ext::set < StateType > > statePartitionMap = partitionMap ( partitions );
177
178 MinimizeByPartitioning::UnorderedFTA < T > res;
179 res.setStates ( partitions );
180 res.addInputSymbols ( dfta.getInputAlphabet ( ) );
181 for ( const StateType & state : dfta.getFinalStates ( ) )
182 res.addFinalState ( statePartitionMap.at ( state ) );
183
184 for ( const auto & transition : dfta.getTransitions ( ) ) {
186 for ( const StateType & from : transition.first.second )
187 minimalFrom.insert ( statePartitionMap.at ( from ) );
188 res.addTransition ( transition.first.first, minimalFrom, statePartitionMap.at ( transition.second ) );
189 }
190
191 return res;
192}
193
194} /* namespace simplify */
195
196} /* namespace automaton */
197
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
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
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
Definition: MinimizeByPartitioning.h:46
static FA< T > minimize(const T &nfa, const ext::set< ext::set< typename T::StateType > > &partitions)
static UnorderedFTA< T > minimize(const T &dfta, const ext::set< ext::set< typename T::StateType > > &partitions)
static FTA< T > minimize(const T &dfta, const ext::set< ext::set< typename T::StateType > > &partitions)
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: multiset.hpp:44
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
return res
Definition: MinimizeByPartitioning.h:145
const ext::map< StateType, ext::set< StateType > > statePartitionMap
Definition: MinimizeByPartitioning.h:134
Definition: ToGrammar.h:31