Skip to content
Snippets Groups Projects
Commit b1a61660 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify NFA IDPDA determinisation

parent 502e94f7
No related branches found
No related tags found
No related merge requests found
/*
* NFACommon.cpp
*
* Created on: 16. 1. 2014
* Author: Jan Vesely
*/
#include "NFACommon.h"
#include "label/LabelSetLabel.h"
#include <deque>
#include <algorithm>
namespace determinize {
automaton::State createDFAState(const std::set<automaton::State>& nfaStates) {
std::set<label::Label> labelSet;
for(const automaton::State& state : nfaStates) {
labelSet.insert(state.getName());
}
return automaton::State(label::Label(label::LabelSetLabel(labelSet)));
}
std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState) {
std::set<automaton::State> states;
for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) {
states.insert(automaton::State(label));
}
return states;
}
}
/*
* NFACommon.h
*
* Created on: 16. 1. 2014
* Author: Jan Vesely
*/
#ifndef NFA_COMMON_H_
#define NFA_COMMON_H_
#include <automaton/common/State.h>
#include <set>
namespace determinize {
/**
* Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into
* the resulting deterministic automaton.
*
* @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm
* @return state of deterministic fsm
*/
automaton::State createDFAState(const std::set<automaton::State>& nfaStates);
/**
* Finds states from nondeterministic fsm to which at least one state from given set of states have transition
* with given input.
*
* @param fromStates set of states from nondeterministic fsm
* @param input symbol from input alphabet
* @return set of states from nondeterministic fsm
*/
std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState);
} /* namespace determinize */
#endif /* NFA_COMMON_H_ */
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
   
#include "IDPDADeterminizer.h" #include "IDPDADeterminizer.h"
#include "label/LabelSetLabel.h" #include "../common/NFACommon.h"
   
#include <deque> #include <deque>
#include <algorithm> #include <algorithm>
...@@ -14,22 +14,6 @@ ...@@ -14,22 +14,6 @@
namespace determinize { namespace determinize {
   
   
automaton::State IDPDADeterminizer::createDFAState(const std::set<automaton::State>& nfaStates) {
std::set<label::Label> labelSet;
for(const automaton::State& state : nfaStates) {
labelSet.insert(state.getName());
}
return automaton::State(label::Label(label::LabelSetLabel(labelSet)));
}
std::set<automaton::State> IDPDADeterminizer::recreateIDPDAStates(const automaton::State& dfaState) {
std::set<automaton::State> states;
for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) {
states.insert(automaton::State(label));
}
return states;
}
automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& nfa) { automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& nfa) {
// 1, 4 // 1, 4
automaton::State initialState(createDFAState(nfa.getInitialStates())); automaton::State initialState(createDFAState(nfa.getInitialStates()));
...@@ -49,7 +33,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& ...@@ -49,7 +33,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA&
// 3b // 3b
for (const auto& input : nfa.getInputAlphabet()) { for (const auto& input : nfa.getInputAlphabet()) {
std::set<automaton::State> targetIDPDAStates; std::set<automaton::State> targetIDPDAStates;
for(const auto& nfaState : recreateIDPDAStates(state)) { for(const auto& nfaState : recreateNFAStates(state)) {
auto iter = nfa.getTransitions().find(std::make_pair(nfaState, input)); auto iter = nfa.getTransitions().find(std::make_pair(nfaState, input));
if(iter != nfa.getTransitions().end()) { if(iter != nfa.getTransitions().end()) {
targetIDPDAStates.insert(iter->second.begin(), iter->second.end()); targetIDPDAStates.insert(iter->second.begin(), iter->second.end());
...@@ -70,7 +54,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& ...@@ -70,7 +54,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA&
// 5 // 5
for (const auto& dfaState : res.getStates()) { for (const auto& dfaState : res.getStates()) {
std::set<automaton::State> nfaStates = recreateIDPDAStates(dfaState); std::set<automaton::State> nfaStates = recreateNFAStates(dfaState);
if(std::any_of(nfaStates.begin(), nfaStates.end(), [&](const automaton::State& nfaState) { return nfa.getFinalStates().count(nfaState); })) { if(std::any_of(nfaStates.begin(), nfaStates.end(), [&](const automaton::State& nfaState) { return nfa.getFinalStates().count(nfaState); })) {
res.addFinalState(dfaState); res.addFinalState(dfaState);
} }
......
...@@ -20,27 +20,6 @@ namespace determinize { ...@@ -20,27 +20,6 @@ namespace determinize {
* Class for running determinization algorithm on fsm. * Class for running determinization algorithm on fsm.
*/ */
class IDPDADeterminizer { class IDPDADeterminizer {
private:
/**
* Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into
* the resulting deterministic automaton.
*
* @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm
* @return state of deterministic fsm
*/
static automaton::State createDFAState(const std::set<automaton::State>& nfaStates);
/**
* Finds states from nondeterministic fsm to which at least one state from given set of states have transition
* with given input.
*
* @param fromStates set of states from nondeterministic fsm
* @param input symbol from input alphabet
* @return set of states from nondeterministic fsm
*/
static std::set<automaton::State> recreateIDPDAStates(const automaton::State& dfaState);
public: public:
   
/** /**
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
   
#include "NFADeterminizer.h" #include "NFADeterminizer.h"
#include "label/LabelSetLabel.h" #include "../common/NFACommon.h"
   
#include <deque> #include <deque>
#include <algorithm> #include <algorithm>
...@@ -21,22 +21,6 @@ automaton::Automaton NFADeterminizer::determinize(const automaton::Automaton& au ...@@ -21,22 +21,6 @@ automaton::Automaton NFADeterminizer::determinize(const automaton::Automaton& au
return res; return res;
} }
   
automaton::State NFADeterminizer::createDFAState(const std::set<automaton::State>& nfaStates) {
std::set<label::Label> labelSet;
for(const automaton::State& state : nfaStates) {
labelSet.insert(state.getName());
}
return automaton::State(label::Label(label::LabelSetLabel(labelSet)));
}
std::set<automaton::State> NFADeterminizer::recreateNFAStates(const automaton::State& dfaState) {
std::set<automaton::State> states;
for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) {
states.insert(automaton::State(label));
}
return states;
}
automaton::DFA NFADeterminizer::determinize(const automaton::NFA& nfa) { automaton::DFA NFADeterminizer::determinize(const automaton::NFA& nfa) {
// 1, 4 // 1, 4
automaton::State initialState(createDFAState(nfa.getInitialStates())); automaton::State initialState(createDFAState(nfa.getInitialStates()));
......
...@@ -23,28 +23,7 @@ namespace determinize { ...@@ -23,28 +23,7 @@ namespace determinize {
* Class for running determinization algorithm on fsm. * Class for running determinization algorithm on fsm.
*/ */
class NFADeterminizer : public automaton::VisitableAutomatonBase::const_visitor_type { class NFADeterminizer : public automaton::VisitableAutomatonBase::const_visitor_type {
private: private:
/**
* Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into
* the resulting deterministic automaton.
*
* @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm
* @return state of deterministic fsm
*/
static automaton::State createDFAState(const std::set<automaton::State>& nfaStates);
/**
* Finds states from nondeterministic fsm to which at least one state from given set of states have transition
* with given input.
*
* @param fromStates set of states from nondeterministic fsm
* @param input symbol from input alphabet
* @return set of states from nondeterministic fsm
*/
static std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState);
void Visit(void*, const automaton::UnknownAutomaton& automaton) const; void Visit(void*, const automaton::UnknownAutomaton& automaton) const;
void Visit(void*, const automaton::EpsilonNFA& automaton) const; void Visit(void*, const automaton::EpsilonNFA& automaton) const;
void Visit(void*, const automaton::NFA& automaton) const; void Visit(void*, const automaton::NFA& automaton) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment