17 #ifndef VCSN_ALGORITHMS_MINIMIZATION_MOORE_HXX
18 # define VCSN_ALGORITHMS_MINIMIZATION_MOORE_HXX
26 # endif // ! VCSN_NDEBUG
28 # include <vaucanson/automata/concept/handlers.hh>
29 # include <vaucanson/automata/concept/delta_kind.hh>
30 # include <vaucanson/misc/usual_macros.hh>
40 # define for_all_groups(I, P) \
41 for (typename groupid_to_group_t::iterator I = ((P).begin()); I != (P).end(); ++I)
45 # define for_all_state_in_groups(I, P) \
46 for (typename group_t::iterator I = ++((P).begin()); I != (P).end(); ++I)
55 template<
typename A,
typename AI,
bool Transposed>
57 do_minimization_moore(
const Element<A, AI>& input, Element<A, AI>& output)
59 BENCH_TASK_SCOPED(
"minimization_moore");
60 typedef Element<A, AI> automaton_t;
61 AUTOMATON_TYPES(automaton_t);
62 AUTOMATON_FREEMONOID_TYPES(automaton_t);
69 const hstate_t NullState;
70 const hstate_t NullGroup;
71 const alphabet_t& alphabet (input.series().monoid().alphabet());
75 typedef int letterid_t;
76 typedef int groupid_t;
78 typedef set<hstate_t> group_t;
79 typedef vector<group_t> groupid_to_group_t;
81 typedef vector<hstate_t> letterid_to_state_t;
83 typedef map<hstate_t, letterid_to_state_t> state_to_letterid_to_state_t;
85 typedef map<hstate_t, groupid_t> state_to_groupid_t;
86 typedef map<letter_t, letterid_t> letter_to_letterid_t;
92 precondition(input.exists());
94 groupid_to_group_t groupid_to_group(input.states().size());
96 state_to_groupid_t state_to_groupid;
97 state_to_groupid[NullState] = NullGroup;
99 letter_to_letterid_t letter_to_letterid;
100 int letter_count = 0;
101 for_all_const_letters(iletter, alphabet)
102 letter_to_letterid[*iletter] = letter_count++;
104 state_to_letterid_to_state_t aut_view;
105 for_all_const_states(istate, input)
107 aut_view[*istate] = letterid_to_state_t(letter_count, NullState);
108 if ((not Transposed and input.is_final(*istate)) or
109 (Transposed and input.is_initial(*istate)))
111 groupid_to_group[0].insert(*istate);
112 state_to_groupid[*istate] = 0;
116 groupid_to_group[1].insert(*istate);
117 state_to_groupid[*istate] = 1;
121 for_all_const_states(istate, input)
123 for_all_const_(letter_to_letterid_t, iletter, letter_to_letterid)
129 for (delta_iterator t(input.value(), *istate);
133 monoid_elt_t w(input.series_of(*t).structure().monoid(), iletter->first);
134 if (input.series_of(*t).get(w) != input.series().semiring().wzero_)
137 first = input.dst_of(*t);
144 for (rdelta_iterator t(input.value(), *istate);
148 monoid_elt_t w(input.series_of(*t).structure().monoid(), iletter->first);
149 if (input.series_of(*t).get(w) != input.series().semiring().wzero_)
152 first = input.src_of(*t);
158 aut_view[*istate][iletter->second] = first;
168 for (
bool group_modified =
true; group_modified;)
170 group_modified =
false;
171 for_all_groups(igroup, groupid_to_group)
176 hstate_t first_state = *(igroup->begin());
177 bool group_created =
false;
179 for_all_state_in_groups(istate, *igroup)
182 for (i = 0; i < letter_count; ++i)
183 if (state_to_groupid[aut_view[first_state][i]] !=
184 state_to_groupid[aut_view[*istate][i]])
186 if (i != letter_count)
188 typename group_t::iterator istate_save = istate;
191 if (group_created ==
false)
194 group_created =
true;
195 group_modified =
true;
197 groupid_to_group[last_group].insert(*istate);
198 state_to_groupid[*istate] = last_group;
199 igroup->erase(*istate);
200 istate = istate_save;
211 std::vector<hstate_t> new_states(last_group + 1);
214 for (
int i = 0; i <= last_group; ++i)
215 new_states[i] = output.add_state();
218 for (
int i = 0; i <= last_group; ++i)
220 hstate_t repres = *(groupid_to_group[i].begin());
222 for_all_const_(letter_to_letterid_t, iletter, letter_to_letterid)
223 if (aut_view[repres][iletter->second] != NullState)
226 output.add_letter_transition(new_states[i],
227 new_states[state_to_groupid
232 output.add_letter_transition(new_states[state_to_groupid
243 output.set_final(new_states[0]);
244 output.set_initial(new_states[state_to_groupid[*input.initial().begin()]]);
248 output.set_initial(new_states[0]);
249 output.set_final(new_states[state_to_groupid[*input.final().begin()]]);
255 template<
typename A,
typename AI>
262 do_minimization_moore<A, AI, false>(a, output);
267 template<
typename A,
typename AI>
274 do_minimization_moore<A, AI, false>(a, output);
278 template<
typename A,
typename AI>
285 do_minimization_moore<A, AI, true>(a, output);
290 template<
typename A,
typename AI>
297 do_minimization_moore<A, AI, true>(a, output);
304 # undef for_all_groups
305 # undef for_all_state_in_groups
308 #endif // ! VCSN_ALGORITHMS_MINIMIZATION_MOORE_HXX