Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
AutomataUnionCartesianProduct.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
27
28namespace automaton {
29
30namespace transform {
31
38public:
48 template < class SymbolType, class StateType1, class StateType2 >
50
54 template < class SymbolType, class StateType1, class StateType2 >
56};
57
58template < class SymbolType, class StateType1, class StateType2 >
60 if(!first.isTotal() || !second.isTotal())
61 throw exception::CommonException("Automata must be total to unify with cartesian product");
62
65
66 for(const auto& a : first.getInputAlphabet())
67 res.addInputSymbol(a);
68 for(const auto& a : second.getInputAlphabet())
69 res.addInputSymbol(a);
70
71 for(const auto& p : first.getStates())
72 for(const auto& q : second.getStates())
73 res.addState(ext::make_pair(p, q));
74
75 for(const auto& p : first.getFinalStates())
76 for(const auto& q : second.getStates())
77 res.addFinalState(ext::make_pair(p, q));
78
79 for(const auto& p : first.getStates())
80 for(const auto& q : second.getFinalStates())
81 res.addFinalState(ext::make_pair(p, q));
82
83 for(const auto& state : res.getStates())
84 for(const auto& tp : first.getTransitionsFromState(state.first))
85 for(const auto& tq : second.getTransitionsFromState(state.second))
86 if(tp.first.second == tq.first.second)
87 res.addTransition(state, tp.first.second, ext::make_pair(tp.second, tq.second));
88
89
90 return res;
91}
92
93template < class SymbolType, class StateType1, class StateType2 >
95 if(!first.isTotal() || !second.isTotal())
96 throw exception::CommonException("Automata must be total to unify with cartesian product");
97
100
101 for(const auto& a : first.getInputAlphabet())
102 res.addInputSymbol(a);
103 for(const auto& a : second.getInputAlphabet())
104 res.addInputSymbol(a);
105
106 for(const auto& p : first.getStates())
107 for(const auto& q : second.getStates())
108 res.addState(ext::make_pair(p, q));
109
110 for(const auto& p : first.getFinalStates())
111 for(const auto& q : second.getStates())
112 res.addFinalState(ext::make_pair(p, q));
113
114 for(const auto& p : first.getStates())
115 for(const auto& q : second.getFinalStates())
116 res.addFinalState(ext::make_pair(p, q));
117
118 for(const auto& state : res.getStates())
119 for(const auto& tp : first.getTransitionsFromState(state.first))
120 for(const auto& tq : second.getTransitionsFromState(state.second))
121 if(tp.first.second == tq.first.second)
122 res.addTransition(state, tp.first.second, ext::make_pair(tp.second, tq.second));
123
124 return res;
125}
126
127} /* namespace transform */
128
129} /* namespace automaton */
130
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
const ext::set< StateType > & getFinalStates() const &
Definition: DFA.h:183
const StateType & getInitialState() const &
Definition: DFA.h:105
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: DFA.h:232
bool isTotal() const
Determines whether the automaton is total.
Definition: DFA.h:508
const ext::set< StateType > & getStates() const &
Definition: DFA.h:134
ext::iterator_range< typename ext::map< ext::pair< StateType, SymbolType >, StateType >::const_iterator > getTransitionsFromState(const StateType &from) const
Definition: DFA.h:483
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
const ext::set< StateType > & getStates() const &
Definition: NFA.h:136
const StateType & getInitialState() const &
Definition: NFA.h:107
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: NFA.h:234
const ext::set< StateType > & getFinalStates() const &
Definition: NFA.h:185
auto getTransitionsFromState(const StateType &from) const
Definition: NFA.h:494
bool isTotal() const
Determines whether the automaton is total deterministic.
Definition: NFA.h:528
Definition: AutomataUnionCartesianProduct.h:37
static automaton::NFA< SymbolType, ext::pair< StateType1, StateType2 > > unification(const automaton::NFA< SymbolType, StateType1 > &first, const automaton::NFA< SymbolType, StateType2 > &second)
Definition: AutomataUnionCartesianProduct.h:94
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
q
Definition: SingleInitialStateEpsilonTransition.h:85
StateType q0
Definition: SingleInitialState.h:96
return res
Definition: AutomataConcatenation.h:102
t first second
Definition: AutomataConcatenation.h:78
Definition: ToGrammar.h:31
ContainerType< ResType > transform(const ContainerType< InType, Ts ... > &in, Callback transform)
In container tranformation of all elements according to the tranform.
Definition: algorithm.hpp:150
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79