Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
AutomataLeftQuotientCartesianProduct.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#include <queue>
28
29namespace automaton {
30
31namespace transform {
32
40public:
51 template < class SymbolType, class StateType1, class StateType2 >
53};
54
55template < class SymbolType, class StateType1, class StateType2 >
57 std::queue < ext::pair < StateType1, StateType2 > > queue;
58 queue.push ( ext::pair < StateType1, StateType2 > ( first.getInitialState ( ), second.getInitialState ( ) ) );
59
64
65 while ( !queue.empty ( ) ) {
66 ext::pair < StateType1, StateType2 > state = std::move ( queue.front ( ) );
67 queue.pop ( );
68
69 if ( ! Q.count ( state ) ) {
70 Q.insert ( state );
71
72 if ( first.getFinalStates ( ).count ( state.first ) )
73 Q0.insert ( state );
74 if ( second.getFinalStates ( ).count ( state.second ) )
75 F.insert ( state );
76
77 for(const auto & tp : first.getTransitionsFromState ( state.first ) )
78 for(const auto & tq : second.getTransitionsFromState ( state.second ) )
79 if(tp.first.second == tq.first.second) {
80 if ( ! full || ! first.getFinalStates ( ).count ( tp.second ) )
81 delta [ ext::make_pair ( state, tp.first.second ) ].insert ( ext::make_pair ( tp.second, tq.second ) );
82
83 queue.push ( ext::make_pair ( tp.second, tq.second ) ); // Note: the original paper kept this queue push statement included in body of the above condition. That is not correct.
84 }
85 }
86 }
87
89
90 res.addInputSymbols(first.getInputAlphabet());
91 res.addInputSymbols(second.getInputAlphabet());
92
93 res.setStates ( Q );
94
96 for ( ext::pair < StateType1, StateType2 > to : ext::make_mover ( transitions.second ) )
97 res.addTransition ( transitions.first.first, transitions.first.second, std::move ( to ) );
98 }
99
100 res.setInitialStates ( Q0 );
101 res.setFinalStates ( F );
102
103 return res;
104}
105
106} /* namespace transform */
107
108} /* namespace automaton */
109
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
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
Definition: AutomataLeftQuotientCartesianProduct.h:39
static automaton::MultiInitialStateNFA< SymbolType, ext::pair< StateType1, StateType2 > > quotient(const automaton::NFA< SymbolType, StateType1 > &first, const automaton::NFA< SymbolType, StateType2 > &second, bool full)
Definition: AutomataLeftQuotientCartesianProduct.h:56
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
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
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: AutomataConcatenation.h:102
t first second
Definition: AutomataConcatenation.h:78
Definition: ToGrammar.h:31
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
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