00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGORITHMS_MINIMIZATION_HOPCROFT_HXX
00018 # define VCSN_ALGORITHMS_MINIMIZATION_HOPCROFT_HXX
00019
00020 # include <algorithm>
00021 # include <list>
00022 # include <queue>
00023 # include <set>
00024 # include <vector>
00025
00026 # include <vaucanson/algebra/implementation/semiring/numerical_semiring.hh>
00027 # include <vaucanson/algorithms/minimization_hopcroft.hh>
00028 # include <vaucanson/algorithms/is_deterministic.hh>
00029 # include <vaucanson/algorithms/complete.hh>
00030 # include <vaucanson/automata/concept/automata_base.hh>
00031 # include <vaucanson/misc/usual_macros.hh>
00032
00033 # ifndef VCSN_NDEBUG
00034 # include <vaucanson/algorithms/is_deterministic.hh>
00035 # endif // ! VCSN_NDEBUG
00036
00037 namespace vcsn
00038 {
00039
00040 namespace internal
00041 {
00042 namespace hopcroft_minimization_det
00043 {
00044
00045 # define HOPCROFT_TYPES() \
00046 typedef std::set<hstate_t> hstates_t; \
00047 typedef std::vector<hstates_t> partition_t; \
00048 typedef std::vector<unsigned> class_of_t; \
00049 typedef std::queue<std::pair<hstates_t*, unsigned> > to_treat_t;
00050
00054 template <typename input_t>
00055 struct splitter_functor
00056 {
00057 AUTOMATON_TYPES (input_t);
00058 AUTOMATON_FREEMONOID_TYPES (input_t);
00059 HOPCROFT_TYPES ();
00060
00061 const input_t& input_;
00062 hstates_t going_in_;
00063 class_of_t& class_of_;
00064 std::list<unsigned> maybe_splittable_;
00065 std::vector<unsigned> count_for_;
00066
00067 splitter_functor (const input_t& input, unsigned int max_state,
00068 class_of_t& class_of)
00069 : input_ (input), going_in_ (), class_of_(class_of),
00070 count_for_ (max_state)
00071 {}
00072
00074 bool compute_states_going_in (const hstates_t& ss, letter_t l)
00075 {
00076 going_in_.clear ();
00077 maybe_splittable_.clear ();
00078 for_all_const_ (hstates_t, i, ss)
00079 {
00080 for (rdelta_iterator t(input_.value(), *i); ! t.done(); t.next())
00081 {
00082 monoid_elt_t w(input_.series_of(*t).structure().monoid(), l);
00083 if (input_.series_of(*t).get(w) != input_.series().semiring().wzero_)
00084 {
00085 hstate_t s = input_.src_of(*t);
00086 unsigned class_of_state = class_of_[s];
00087
00088 if (count_for_[class_of_state] == 0)
00089 maybe_splittable_.push_back (class_of_state);
00090 count_for_[class_of_state]++;
00091 going_in_.insert (s);
00092 }
00093 }
00094 }
00095 return not going_in_.empty ();
00096 }
00097
00099 void execute (partition_t& partition, to_treat_t& to_treat,
00100 unsigned& n_partition)
00101 {
00102 for_all (std::list<unsigned>, inpartition, maybe_splittable_)
00103 {
00104 hstates_t& states = partition[*inpartition];
00105 if (states.size () == count_for_[*inpartition])
00106 {
00107 count_for_[*inpartition] = 0;
00108 continue;
00109 }
00110 count_for_[*inpartition] = 0;
00111 hstates_t states_inter_going_in;
00112 hstates_t& states_minus_going_in = partition[n_partition];
00113
00114 set_difference
00115 (states.begin (), states.end (),
00116 going_in_.begin (), going_in_.end (),
00117 std::insert_iterator<hstates_t> (states_minus_going_in,
00118 states_minus_going_in.begin ()));
00119
00120 set_intersection
00121 (states.begin(), states.end (),
00122 going_in_.begin (), going_in_.end (),
00123 std::insert_iterator<hstates_t> (states_inter_going_in,
00124 states_inter_going_in.begin ()));
00125
00126 assertion (not (states_inter_going_in.empty ()
00127 or states_minus_going_in.empty ()));
00128
00129 if (states_minus_going_in.size () > states_inter_going_in.size ())
00130 {
00131 states.swap (states_minus_going_in);
00132 states_minus_going_in.swap (states_inter_going_in);
00133 }
00134 else
00135 states.swap (states_inter_going_in);
00136 for_all_const_ (hstates_t, istate, states_minus_going_in)
00137 class_of_[*istate] = n_partition;
00138 to_treat.push (std::make_pair (&states_minus_going_in,
00139 n_partition++));
00140 }
00141 }
00142 };
00143
00145 template <typename input_t, typename output_t>
00146 struct transition_adder_functor
00147 {
00148 AUTOMATON_TYPES (input_t);
00149 HOPCROFT_TYPES ();
00150
00151 const input_t& input_;
00152 output_t& output_;
00153 const class_of_t& class_of_;
00154
00155 unsigned src_;
00156
00157 transition_adder_functor (const input_t& input, output_t& output,
00158 const class_of_t& class_of)
00159 : input_ (input), output_ (output), class_of_ (class_of)
00160 {}
00161
00163 void execute (hstate_t representative)
00164 {
00165 src_ = class_of_[representative];
00166 for (delta_iterator t(input_.value(), representative); ! t.done(); t.next())
00167 {
00168 output_.add_series_transition (src_, class_of_[input_.dst_of (*t)],
00169 input_.series_of (*t));
00170 }
00171 }
00172 };
00173 }
00174 }
00175
00176
00177 template <typename A, typename AI1, typename AI2>
00178 void
00179 do_hopcroft_minimization_det(const AutomataBase<A>&,
00180 Element<A, AI2>& output,
00181 const Element<A, AI1>& input)
00182 {
00183 typedef Element<A, AI1> input_t;
00184 typedef Element<A, AI2> output_t;
00185 AUTOMATON_TYPES (input_t);
00186 AUTOMATON_FREEMONOID_TYPES (input_t);
00187 HOPCROFT_TYPES ();
00188
00189 using namespace internal::hopcroft_minimization_det;
00190
00191 precondition(is_deterministic(input));
00192
00193 unsigned max_state = input.states ().back () + 1;
00194 partition_t partition (max_state);
00195 class_of_t class_of (max_state);
00196 to_treat_t to_treat;
00197 unsigned n_partition = 0;
00198 const alphabet_t& alphabet =
00199 input.structure ().series ().monoid ().alphabet ();
00200
00201 {
00202
00203 hstates_t* finals = 0, * others = 0;
00204 int n_finals = -1, n_others = -1,
00205 count_finals = 0, count_others = 0;
00206
00207 # define add_to_class(Name) \
00208 do { \
00209 if (not Name) \
00210 { \
00211 Name = &(partition[n_partition]); \
00212 n_ ## Name = n_partition++; \
00213 } \
00214 count_ ## Name ++; \
00215 (*Name).insert (*state); \
00216 class_of[*state] = n_ ## Name; \
00217 } while (0)
00218
00219 for_all_const_states (state, input)
00220 if (input.is_final (*state))
00221 add_to_class (finals);
00222 else
00223 add_to_class (others);
00224 # undef add_to_class
00225
00226 if (n_partition == 0)
00227 return;
00228 if (n_partition == 1)
00229 {
00230 output = input;
00231 return;
00232 }
00233
00234 if (count_finals > count_others)
00235 to_treat.push (std::make_pair (others, n_others));
00236 else
00237 to_treat.push (std::make_pair (finals, n_finals));
00238 }
00239
00240 {
00241 splitter_functor<input_t> splitter (input, max_state, class_of);
00242
00243
00244 while (not to_treat.empty () && n_partition < max_state)
00245 {
00246
00247 hstates_t& states = *(to_treat.front ().first);
00248 to_treat.pop ();
00249
00250
00251 for_all_const_letters (letter, alphabet)
00252 {
00253 if (not splitter.compute_states_going_in (states, *letter))
00254 continue;
00255 splitter.execute (partition, to_treat, n_partition);
00256 if (n_partition == max_state)
00257 break;
00258 }
00259 }
00260 }
00261
00262
00263
00264 for (unsigned i = 0; i < n_partition; ++i)
00265 output.add_state ();
00266
00267 transition_adder_functor<input_t, output_t>
00268 transition_adder (input, output, class_of);
00269
00270 typename partition_t::iterator istates = partition.begin ();
00271 for (unsigned i = 0; i < n_partition; ++i, ++istates)
00272 {
00273 hstate_t representative = *(*istates).begin();
00274
00275 if (input.is_final (representative))
00276 output.set_final (class_of[representative]);
00277 transition_adder.execute (representative);
00278 }
00279
00280 for_all_const_initial_states (state, input)
00281 output.set_initial (class_of[*state]);
00282 }
00283
00284 # undef HOPCROFT_TYPES
00285
00295 template<typename A, typename AI>
00296 Element<A, AI>
00297 minimization_hopcroft(const Element<A, AI>& a)
00298 {
00299 TIMER_SCOPED ("minimization_hopcroft");
00300 precondition(is_deterministic(a));
00301 precondition(is_complete(a));
00302 Element<A, AI> output(a.structure());
00303 do_hopcroft_minimization_det(a.structure(), output, a);
00304 return output;
00305 }
00306
00307
00308
00309
00310
00311 namespace internal
00312 {
00313 namespace hopcroft_minimization_undet
00314 {
00315
00316 # define QUOTIENT_TYPES() \
00317 typedef std::list<hstate_t> partition_t; \
00318 typedef std::vector<partition_t> partition_set_t; \
00319 typedef typename partition_t::iterator partition_iterator; \
00320 typedef std::vector<unsigned> class_of_t; \
00321 typedef std::pair<unsigned, letter_t> pair_t; \
00322 typedef std::list<pair_t> to_treat_t;
00323
00324 template <typename input_t>
00325 class quotient_splitter
00326 {
00327 public:
00328 AUTOMATON_TYPES(input_t);
00329 AUTOMATON_FREEMONOID_TYPES(input_t);
00330 QUOTIENT_TYPES();
00331
00332 typedef std::vector<bool> going_in_t;
00333
00334 quotient_splitter (const automaton_t& input, class_of_t& class_of,
00335 unsigned max_states)
00336 : input_(input),
00337 alphabet_(input.series().monoid().alphabet()),
00338 class_(class_of),
00339 count_for_(max_states, 0),
00340 twin_(max_states, 0),
00341 going_in_(max_states, false)
00342 { }
00343
00345 bool compute_going_in_states (partition_t& p, letter_t a)
00346 {
00347 for_all_(going_in_t, s, going_in_)
00348 *s = false;
00349
00350 for_all_(partition_t, s, p)
00351 {
00352 for (rdelta_iterator t(input_.value(), *s); ! t.done(); t.next())
00353 {
00354 monoid_elt_t w(input_.series_of(*t).structure().monoid(), a);
00355 if (input_.series_of(*t).get(w) != input_.series().semiring().wzero_)
00356 {
00357 hstate_t s = input_.src_of(*t);
00358 if (!going_in_[s])
00359 {
00361 going_in_[s] = true;
00362 const unsigned i = class_[s];
00363 if (count_for_[i] == 0)
00364 met_class_.push_back(i);
00365 count_for_[i]++;
00366 }
00367 }
00368 }
00369 }
00370 return !met_class_.empty();
00371 }
00372
00374 void split (partition_set_t& parts, unsigned& max_partitions)
00375 {
00376 for_all_(std::list<unsigned>, klass, met_class_)
00377 {
00378
00379 if (count_for_[*klass] == parts[*klass].size())
00380 continue;
00381
00382 if (twin_[*klass] == 0)
00383 twin_[*klass] = max_partitions++;
00384 unsigned new_klass = twin_[*klass];
00385
00386 typename partition_t::iterator q;
00387 for (typename partition_t::iterator next = parts[*klass].begin();
00388 next != parts[*klass].end();)
00389 {
00390 q = next;
00391 ++next;
00392 if (going_in_[*q])
00393 {
00394 parts[new_klass].insert(parts[new_klass].end(), *q);
00395 class_[*q] = new_klass;
00396 parts[*klass].erase(q);
00397 }
00398 }
00399 }
00400 }
00401
00402 void add_new_partitions(to_treat_t& to_treat,
00403 const partition_set_t& part)
00404 {
00405 for_all_(std::list<unsigned>, klass, met_class_)
00406 {
00407 if (twin_[*klass] != 0)
00408 {
00409 for_all_const_letters(e, alphabet_)
00410 {
00411 if (find(to_treat.begin(), to_treat.end(), pair_t(*klass, *e)) !=
00412 to_treat.end())
00413 to_treat.push_back(pair_t(twin_[*klass], *e));
00414 else
00415 if (part[*klass].size() < part[twin_[*klass]].size())
00416 to_treat.push_back(pair_t(*klass, *e));
00417 else
00418 to_treat.push_back(pair_t(twin_[*klass], *e));
00419 }
00420 }
00421 }
00422
00423 for_all_(std::list<unsigned>, klass, met_class_)
00424 {
00425 count_for_[*klass] = 0;
00426 twin_[*klass] = 0;
00427 }
00428 met_class_.clear();
00429 }
00430
00431 private:
00432 const automaton_t& input_;
00433 const alphabet_t& alphabet_;
00434 class_of_t& class_;
00435 std::vector<unsigned> count_for_;
00436 std::vector<unsigned> twin_;
00437 going_in_t going_in_;
00438 std::list<unsigned> met_class_;
00439 };
00440 }
00441 }
00442
00443 template <typename A, typename AI1, typename AI2>
00444 void
00445 do_quotient(const AutomataBase<A>&,
00446 const algebra::NumericalSemiring&,
00447 SELECTOR(bool),
00448 Element<A, AI2>& output,
00449 const Element<A, AI1>& input)
00450 {
00451 typedef Element<A, AI1> input_t;
00452 typedef Element<A, AI2> output_t;
00453 AUTOMATON_TYPES(input_t);
00454 AUTOMATON_FREEMONOID_TYPES(input_t);
00455 QUOTIENT_TYPES();
00456
00457 using namespace internal::hopcroft_minimization_undet;
00458
00459 const alphabet_t& alphabet_(input.series().monoid().alphabet());
00460 unsigned max_states = 0;
00461
00462 for_all_const_states(i, input)
00463 max_states = std::max(unsigned(*i), max_states);
00464 ++max_states;
00465
00466
00467
00468
00469 unsigned max_partitions = 0;
00470
00471
00472
00473
00474 class_of_t class_(max_states);
00475 partition_set_t part(max_states);
00476
00477
00478
00479
00480 to_treat_t to_treat;
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 unsigned final = 1;
00493
00494 for_all_const_states (p, input)
00495 {
00496 unsigned c;
00497 if (max_partitions == 0)
00498 {
00499 c = 0;
00500 final = !input.is_final(*p);
00501 }
00502 else
00503 {
00504 c = input.is_final(*p) ? final : (1 - final);
00505 }
00506 class_[*p] = c;
00507 part[c].insert(part[c].end(), *p);
00508 max_partitions = std::max(max_partitions, c + 1);
00509 }
00510
00511
00512
00513
00514
00515 if (max_partitions > 0)
00516 for_all_const_letters (e, alphabet_)
00517 to_treat.push_back(pair_t(0, *e));
00518
00519 if (max_partitions > 1)
00520 for_all_const_letters (e, alphabet_)
00521 to_treat.push_back(pair_t(1, *e));
00522
00523
00524
00525
00526 {
00527 quotient_splitter<input_t> splitter(input, class_, max_states);
00528 while (!to_treat.empty())
00529 {
00530 pair_t c = to_treat.front();
00531 to_treat.pop_front();
00532 unsigned p = c.first;
00533 letter_t a = c.second;
00534
00535 if (!splitter.compute_going_in_states(part[p], a))
00536 continue;
00537 splitter.split(part, max_partitions);
00538
00539 splitter.add_new_partitions(to_treat, part);
00540 }
00541 }
00542
00543
00544
00545
00546
00547 for (unsigned i = 0; i < max_partitions; ++i)
00548 output.add_state();
00549
00550 std::set<unsigned> already_linked;
00551 for (unsigned i = 0; i < max_partitions; ++i)
00552 {
00553
00554
00555 hstate_t s = part[i].front();
00556
00557 if (input.is_final(s))
00558 output.set_final(i);
00559
00560
00561 for_all_const_letters (e, alphabet_)
00562 {
00563 already_linked.clear();
00564
00565 for (delta_iterator t(input.value(), s); ! t.done(); t.next())
00566 {
00567 monoid_elt_t w(input.series_of(*t).structure().monoid(), *e);
00568 if (input.series_of(*t).get(w) != input.series().semiring().wzero_)
00569 {
00570 hstate_t out = input.dst_of(*t);
00571 unsigned c = class_[out];
00572 if (already_linked.find(c) == already_linked.end())
00573 {
00574 already_linked.insert(c);
00575 output.add_letter_transition(i, c, *e);
00576 }
00577 }
00578 }
00579 }
00580 }
00581
00582
00583 for_all_const_initial_states(i, input)
00584 output.set_initial(class_[*i]);
00585 }
00586
00587 # undef QUOTIENT_TYPES
00588
00589
00590
00591
00592
00593
00594 template <class S, class T,
00595 typename A, typename input_t, typename output_t>
00596 void
00597 do_quotient(const AutomataBase<A>& ,
00598 const S& ,
00599 const T& ,
00600 output_t& output,
00601 const input_t& input)
00602 {
00603 AUTOMATON_TYPES(input_t);
00604 AUTOMATON_FREEMONOID_TYPES(input_t);
00605 using namespace std;
00606
00607
00608
00609
00610
00611 typedef set<htransition_t> set_transitions_t;
00612 typedef set<hstate_t> set_states_t;
00613 typedef set<semiring_elt_t> set_semiring_elt_t;
00614 typedef vector<semiring_elt_t> vector_semiring_elt_t;
00615 typedef pair<unsigned, letter_t> pair_class_letter_t;
00616 typedef pair<hstate_t, semiring_elt_t> pair_state_semiring_elt_t;
00617 typedef set<pair_state_semiring_elt_t> set_pair_state_semiring_elt_t;
00618 typedef map<semiring_elt_t, unsigned> map_semiring_elt_t;
00619
00620 series_set_elt_t null_series = input.series().zero_;
00621 semiring_elt_t weight_zero = input.series().semiring().wzero_;
00622 monoid_elt_t monoid_identity = input.series().monoid().VCSN_EMPTY_;
00623 const alphabet_t& alphabet (input.series().monoid().alphabet());
00624
00625 queue<pair_class_letter_t> the_queue;
00626
00627 set<unsigned> met_classes;
00628
00629 unsigned max_partition = 0;
00630 unsigned max_states = 0;
00631
00632 for_all_const_states(q, input)
00633 max_states = std::max(unsigned (*q), max_states);
00634 ++max_states;
00635
00636 max_states = std::max(max_states, 2u);
00637
00638 vector< vector<set_pair_state_semiring_elt_t> > inverse (max_states);
00639
00640 map<letter_t, unsigned> pos_of_letter;
00641
00642 unsigned max_pos = 0;
00643
00644 for_all_const_letters(a, alphabet)
00645 pos_of_letter[*a] = max_pos++;
00646
00647 set_states_t states_visited;
00648 set_semiring_elt_t semiring_had_class;
00649 vector<set_states_t> classes (max_states);
00650 vector<unsigned> class_of_state (max_states);
00651 vector_semiring_elt_t old_weight (max_states);
00652 map_semiring_elt_t class_of_weight;
00653
00654 for (unsigned i = 0; i < max_states; ++i)
00655 inverse[i].resize(max_pos);
00656
00657 for_all_const_states(q, input)
00658 for_all_const_letters(a, alphabet)
00659 {
00660
00661 for_all_const_(set_states_t, r, states_visited)
00662 old_weight[*r] = weight_zero;
00663 states_visited.clear();
00664
00665 for (rdelta_iterator e(input.value(), *q); ! e.done(); e.next())
00666 {
00667 monoid_elt_t w(input.series_of(*e).structure().monoid(), *a);
00668 if (input.series_of(*e).get(w) != input.series().semiring().wzero_)
00669 {
00670 hstate_t p = input.src_of(*e);
00671 if (states_visited.find(p) != states_visited.end())
00672 inverse[*q][pos_of_letter[*a]].
00673 erase(pair_state_semiring_elt_t (p, old_weight[p]));
00674 else
00675 states_visited.insert(p);
00676 series_set_elt_t sd = input.series_of(*e);
00677 monoid_elt_t md (input.structure().series().monoid(), *a);
00678 semiring_elt_t wsd = sd.get(md);
00679 old_weight[p] += wsd;
00680 inverse[*q][pos_of_letter[*a]].
00681 insert(pair_state_semiring_elt_t (p, old_weight[p]));
00682 }
00683 }
00684 }
00685
00686
00687
00688
00689
00690
00691 bool empty = true;
00692 unsigned class_non_final (0);
00693
00694 for_all_const_states(q, input)
00695 {
00696 if (not input.is_final(*q))
00697 {
00698 if (empty == true)
00699 {
00700 empty = false;
00701 class_non_final = max_partition;
00702 max_partition++;
00703 }
00704 classes[class_non_final].insert(*q);
00705 class_of_state[*q] = class_non_final;
00706 }
00707 else
00708 {
00709 semiring_elt_t w = input.get_final(*q).get(monoid_identity);
00710 if (semiring_had_class.find(w) == semiring_had_class.end())
00711 {
00712 semiring_had_class.insert(w);
00713 classes[max_partition].insert(*q);
00714 class_of_weight[w] = max_partition;
00715 class_of_state[*q] = max_partition;
00716 max_partition++;
00717 }
00718 else
00719 {
00720 classes[class_of_weight[w]].insert(*q);
00721 class_of_state[*q] = class_of_weight[w];
00722 }
00723 }
00724 }
00725
00726
00727
00728
00729
00730 for (unsigned i = 0; i < max_partition; i++)
00731 for_all_const_letters(a, alphabet)
00732 the_queue.push(pair_class_letter_t (i, *a));
00733
00734
00735
00736
00737
00738 unsigned old_max_partition = max_partition;
00739
00740 while(not the_queue.empty())
00741 {
00742 pair_class_letter_t pair = the_queue.front();
00743 the_queue.pop();
00744 met_classes.clear();
00745 vector_semiring_elt_t val (max_states);
00746
00747
00748 for_all_const_states(q, input)
00749 val[*q] = 0;
00750
00751
00752 for_all_const_(set_states_t, q, classes[pair.first])
00753 for_all_const_(set_pair_state_semiring_elt_t, pair_,
00754 inverse[*q][pos_of_letter[pair.second]])
00755 {
00756 unsigned state = pair_->first;
00757 if (met_classes.find(class_of_state[state]) ==
00758 met_classes.end())
00759 met_classes.insert(class_of_state[state]);
00760 val[state] += pair_->second;
00761 }
00762
00763
00764 for_all_const_(set<unsigned>, class_id, met_classes)
00765 {
00766 if (classes[*class_id].size() == 1)
00767 continue ;
00768
00769 queue<hstate_t> to_erase;
00770 semiring_elt_t next_val;
00771 semiring_elt_t first_val = val[*(classes[*class_id].begin())];
00772 class_of_weight.clear();
00773 semiring_had_class.clear();
00774
00775 for_all_const_(set_states_t, p, classes[*class_id])
00776 {
00777 next_val = val[*p];
00778
00779 if (next_val != first_val)
00780 {
00781 if (semiring_had_class.find(next_val) ==
00782 semiring_had_class.end())
00783 {
00784 classes[max_partition].insert(*p);
00785 class_of_state[*p] = max_partition;
00786 semiring_had_class.insert(next_val);
00787 class_of_weight[next_val] = max_partition;
00788 max_partition++;
00789 }
00790 else
00791 {
00792 classes[class_of_weight[next_val]].insert(*p);
00793 class_of_state[*p] = class_of_weight[next_val];
00794 }
00795 to_erase.push(*p);
00796 }
00797 }
00798
00799 while(not to_erase.empty())
00800 {
00801 hstate_t s = to_erase.front();
00802 to_erase.pop();
00803 classes[*class_id].erase(s);
00804 }
00805
00806
00807 for (unsigned i = old_max_partition; i < max_partition; i++)
00808 for_all_const_letters(b, alphabet)
00809 the_queue.push(pair_class_letter_t(i, *b));
00810 old_max_partition = max_partition;
00811 }
00812 }
00813
00814
00815
00816
00817
00818 typedef vector<series_set_elt_t> vector_series_set_elt_t;
00819
00820 std::vector<hstate_t> out_states (max_partition);
00821
00822
00823 for(unsigned i = 0; i < max_partition; i++)
00824 {
00825 out_states[i] = output.add_state();
00826 hstate_t a_state = *classes[i].begin();
00827 series_set_elt_t a_serie = null_series;
00828
00829 for_all_const_(set_states_t, state, classes[i])
00830 if(input.is_initial(*state))
00831 a_serie += input.get_initial(*state);
00832
00833 output.set_initial(out_states[i] , a_serie);
00834
00835 if (input.is_final(a_state))
00836 output.set_final(out_states[i] , input.get_final(a_state));
00837 }
00838
00839
00840 vector_series_set_elt_t seriesof (max_partition, null_series);
00841
00842 for(unsigned i = 0; i < max_partition; i++)
00843 {
00844 met_classes.clear();
00845
00846 for (delta_iterator e(input.value(), *classes[i].begin()); ! e.done(); e.next())
00847 {
00848 series_set_elt_t se = input.series_of(*e);
00849 unsigned cs = class_of_state[input.dst_of(*e)];
00850
00851 if (met_classes.find(cs) == met_classes.end())
00852 {
00853 met_classes.insert(cs);
00854 seriesof[cs] = se;
00855 }
00856 else
00857 seriesof[cs] += se;
00858 }
00859
00860 for_all_const_(set<unsigned>, cs, met_classes)
00861 output.add_series_transition(out_states[i],
00862 out_states[*cs],
00863 seriesof[*cs]);
00864 }
00865 }
00866
00867 template<typename A, typename AI>
00868 Element<A, AI>
00869 quotient(const Element<A, AI>& a)
00870 {
00871 TIMER_SCOPED ("quotient");
00872 precondition(is_realtime(a));
00873 typedef Element<A, AI> automaton_t;
00874 AUTOMATON_TYPES(automaton_t);
00875 automaton_t output(a.structure());
00876 do_quotient(a.structure(), a.structure().series().semiring(),
00877 SELECT(semiring_elt_value_t), output, a);
00878 return output;
00879 }
00880
00881 }
00882
00883 #endif // ! VCSN_ALGORITHMS_MINIMIZATION_HOPCROFT_HXX