Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DeterminizeNFAPart.hxx
Go to the documentation of this file.
1
6#include <ext/algorithm>
7
8#include <alib/deque>
9#include <alib/set>
10
11#include <automaton/FSM/NFA.h>
13
14namespace automaton {
15
16namespace determinize {
17
18template < class SymbolType, class StateType >
20 // 1, 4
21 ext::set < StateType > initialState = nfa.getInitialStates ( );
23
24 res.setInputAlphabet ( nfa.getInputAlphabet ( ) );
25
26 // 2
28 todo.push_back ( std::move ( initialState ) );
29
30 do {
31 // 3a, c
32 ext::set < StateType > state = std::move ( todo.front ( ) );
33 todo.pop_front ( );
34
35 // 3b
36 for ( const SymbolType & input : nfa.getInputAlphabet ( ) ) {
38
39 for ( StateType nfaState : state ) {
40 auto range = nfa.getTransitions ( ).equal_range ( ext::make_pair ( std::move ( nfaState ), input ) );
41
42 for ( auto & transition : range )
43 dfaState.insert ( transition.second );
44 }
45
46 // 4
47 bool existed = !res.addState ( dfaState );
48
49 if ( !existed ) todo.push_back ( dfaState );
50
51 // 3b
52 res.addTransition ( state, input, std::move ( dfaState ) );
53 }
54 } while ( !todo.empty ( ) );
55
56 // 5
57 const ext::set < StateType > & finalLabels = nfa.getFinalStates();
58 for ( const ext::set < StateType > & dfaState : res.getStates ( ) )
59 if ( ! ext::excludes ( finalLabels.begin ( ), finalLabels.end ( ), dfaState.begin ( ), dfaState.end ( ) ) )
60 res.addFinalState ( dfaState );
61
62 return res;
63}
64
65template < class SymbolType, class StateType >
67 // 1, 4
68 ext::set < StateType > initialState;
69 initialState.insert ( nfa.getInitialState ( ) );
71
72 res.setInputAlphabet ( nfa.getInputAlphabet ( ) );
73
74 // 2
76 todo.push_back ( std::move ( initialState ) );
77
78 do {
79 // 3a, c
80 ext::set < StateType > state = std::move ( todo.front ( ) );
81 todo.pop_front ( );
82
83 // 3b
84 for ( const SymbolType & input : nfa.getInputAlphabet ( ) ) {
86
87 for ( StateType nfaState : state ) {
88 auto range = nfa.getTransitions ( ).equal_range ( ext::make_pair ( std::move ( nfaState ), input ) );
89
90 for ( auto & transition : range )
91 dfaState.insert ( transition.second );
92 }
93
94 // 4
95 bool existed = !res.addState ( dfaState );
96
97 if ( !existed ) todo.push_back ( dfaState );
98
99 // 3b
100 res.addTransition ( state, input, std::move ( dfaState ) );
101 }
102 } while ( !todo.empty ( ) );
103
104 // 5
105 const ext::set < StateType > & finalLabels = nfa.getFinalStates();
106 for ( const ext::set < StateType > & dfaState : res.getStates ( ) )
107 if ( ! ext::excludes ( finalLabels.begin ( ), finalLabels.end ( ), dfaState.begin ( ), dfaState.end ( ) ) )
108 res.addFinalState ( dfaState );
109
110 return res;
111}
112
113} /* namespace determinize */
114
115} /* namespace automaton */
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
const ext::set< StateType > & getInitialStates() const &
Definition: MultiInitialStateNFA.h:117
const ext::multimap< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: MultiInitialStateNFA.h:520
const ext::set< StateType > & getFinalStates() const &
Definition: MultiInitialStateNFA.h:215
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: MultiInitialStateNFA.h:264
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::multimap< ext::pair< StateType, SymbolType >, StateType > & getTransitions() const &
Definition: NFA.h:484
const ext::set< StateType > & getFinalStates() const &
Definition: NFA.h:185
static automaton::DFA< SymbolType, StateType > determinize(const automaton::DFA< SymbolType, StateType > &automaton)
Definition: Determinize.h:276
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
auto range(Container &&cont) -> decltype(std::forward< Container >(cont).range())
Definition: range.hpp:247
bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Tests two sorted ranges wheter all elements from the second are not present in the first.
Definition: algorithm.hpp:46
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79