From d474ac49c558e604a221f2a23dd4b82bb7c961fb Mon Sep 17 00:00:00 2001 From: Tomas Pecka <peckato1@fit.cvut.cz> Date: Mon, 13 Aug 2018 14:25:43 +0200 Subject: [PATCH] Algo: Language complement on DFA --- .../transform/AutomatonComplement.cpp | 20 ++++++ .../automaton/transform/AutomatonComplement.h | 71 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 alib2algo/src/automaton/transform/AutomatonComplement.cpp create mode 100644 alib2algo/src/automaton/transform/AutomatonComplement.h diff --git a/alib2algo/src/automaton/transform/AutomatonComplement.cpp b/alib2algo/src/automaton/transform/AutomatonComplement.cpp new file mode 100644 index 0000000000..9d956ce2d6 --- /dev/null +++ b/alib2algo/src/automaton/transform/AutomatonComplement.cpp @@ -0,0 +1,20 @@ +/* + * AutomatonComplement.cpp + * + * Created on: 13. 08. 2018 + * Author: Tomas Pecka + */ + +#include "AutomatonComplement.h" +#include <common/createUnique.hpp> +#include <registration/AlgoRegistration.hpp> + +namespace automaton { + +namespace transform { + +auto AutomatonComplementDFA = registration::AbstractRegister < AutomatonComplement, automaton::DFA < >, const automaton::DFA < > & > ( AutomatonComplement::complement ); + +} /* namespace transform */ + +} /* namespace automaton */ diff --git a/alib2algo/src/automaton/transform/AutomatonComplement.h b/alib2algo/src/automaton/transform/AutomatonComplement.h new file mode 100644 index 0000000000..7218adc937 --- /dev/null +++ b/alib2algo/src/automaton/transform/AutomatonComplement.h @@ -0,0 +1,71 @@ +/* + * AutomatonComplement.h + * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * + * Created on: 13. 08. 2018 + * Author: Tomas Pecka + */ + +#ifndef AUTOMATON_COMPLEMENT_H +#define AUTOMATON_COMPLEMENT_H + +#include <automaton/FSM/EpsilonNFA.h> + +namespace automaton { + +namespace transform { + +/** + * Complement of a finite automaton. + * For finite automaton A1, we create automaton A such that L(A) = complement(L(A1)). This is done by reversing the set of final states in the DFA. + */ +class AutomatonComplement { +public: + /** + * Complement of a deterministic finite automaton. + * @tparam SymbolType Type for input symbols. + * @tparam StateType Type for states. + * @param automaton automaton to iterate + * @return nondeterministic FA representing the iteration of @p automaton + */ + template < class SymbolType, class StateType > + static automaton::DFA < SymbolType, StateType > complement ( const automaton::DFA < SymbolType, StateType > & automaton ); +}; + +template < class SymbolType, class StateType > +automaton::DFA < SymbolType, StateType > AutomatonComplement::complement ( const automaton::DFA < SymbolType, StateType > & automaton ) { + // create a complement of automaton's final states + ext::set < StateType > newFinalStates = automaton.getStates ( ); + + // FIXME Not working? + // const ext::set < StateType > & oldFinalStates = automaton.getFinalStates ( ); + // newFinalStates.erase ( oldFinalStates.begin ( ), oldFinalStates.end ( ) ); + + for ( const auto & fin : automaton.getFinalStates ( ) ) + newFinalStates.erase ( fin ); + + automaton::DFA < SymbolType, StateType > res ( automaton ); + res.setFinalStates ( newFinalStates ); + return res; +} + +} /* namespace transform */ + +} /* namespace automaton */ + +#endif /* AUTOMATON_ITERATION_H_ */ -- GitLab