17 #ifndef VCSN_ALGORITHMS_SHORTEST_HXX
18 # define VCSN_ALGORITHMS_SHORTEST_HXX
21 #include <vaucanson/automata/concept/automata_base.hh>
22 #include <vaucanson/misc/usual_macros.hh>
25 #include <vaucanson/misc/military_order.hh>
30 template<
typename Automaton,
typename Mono
idElt>
32 do_shortest(
const Automaton& autom, MonoidElt& word)
35 AUTOMATON_TYPES(Automaton);
36 AUTOMATON_FREEMONOID_TYPES (Automaton);
38 monoid_t themonoid = autom.structure().series().monoid();
40 typedef std::map<hstate_t, monoid_elt_t> theword_t;
42 std::queue<hstate_t> thequeue;
44 monoid_elt_t empty_word = themonoid.identity(
SELECT(monoid_elt_value_t));
46 for_all_initial_states(j, autom)
48 theword.insert(std::pair<hstate_t,monoid_elt_t>(*j, empty_word));
49 if (autom.is_final(*j))
57 while (not thequeue.empty())
59 hstate_t i = thequeue.front();
61 for_all_letters(a, themonoid.alphabet())
63 for (
typename Automaton::delta_iterator t(autom.value(), i);
68 monoid_elt_t w(themonoid, *a);
69 if (autom.series_of(*t).get(w)
70 != autom.series().semiring()
71 .zero(
SELECT(
typename semiring_elt_t::value_t)))
73 hstate_t j = autom.dst_of(*t);
74 if (theword.find(j) == theword.end())
79 typename theword_t::const_iterator k = theword.find(i);
80 assert(k != theword.end());
82 theword.insert(std::pair<hstate_t, monoid_elt_t>(j, k->second * (*a)));
83 if (autom.is_final(j))
85 typename theword_t::const_iterator k = theword.find(j);
86 assert(k != theword.end());
100 template<
typename Automaton,
typename Mono
idElt,
typename Alloc>
102 do_enumerate(
const Automaton& autom,
104 std::list<MonoidElt, Alloc>& words)
108 AUTOMATON_TYPES(Automaton);
109 AUTOMATON_FREEMONOID_TYPES (Automaton);
110 monoid_t themonoid = autom.structure().series().monoid();
112 std::map<hstate_t,std::list<monoid_elt_t> > theword;
114 std::list<std::pair<hstate_t,monoid_elt_t> > queue1;
115 std::list<std::pair<hstate_t,monoid_elt_t> > queue2;
117 monoid_elt_t empty_word = themonoid.identity(
SELECT(monoid_elt_value_t));
119 for_all_initial_states(j, autom)
121 theword[*j].push_back(empty_word);
122 queue1.push_back(std::pair<hstate_t,monoid_elt_t>(*j, empty_word));
125 for(
unsigned i = 0; i < max_length && not queue1.empty(); i++)
127 while (not queue1.empty())
129 std::pair<hstate_t,monoid_elt_t> thepair = queue1.front();
131 hstate_t s = thepair.first;
132 monoid_elt_t oldword = thepair.second;
133 for (
typename Automaton::delta_iterator t(autom.value(), s);
137 for_all_letters(a, themonoid.alphabet())
140 monoid_elt_t w(themonoid, *a);
141 if (autom.series_of(*t).get(w) !=
142 autom.series().semiring().zero(
SELECT(
typename semiring_elt_t::value_t)))
144 monoid_elt_t newword = oldword *(*a);
145 theword[autom.dst_of(*t)].push_back(newword);
146 queue2.push_back(std::pair<hstate_t,monoid_elt_t>(autom.dst_of(*t),newword));
154 std::vector<monoid_elt_t> v;
155 for_all_final_states(j, autom)
157 std::list<monoid_elt_t> &l = theword[*j];
158 v.insert(v.end(), l.begin(), l.end());
160 sort(v.begin(), v.end(), MilitaryOrder<monoid_elt_t>());
161 typename std::vector<monoid_elt_t>::iterator v_last
162 = std::unique(v.begin(), v.end());
163 words.insert(words.begin(), v.begin(), v_last);
166 template<
typename Automaton>
167 typename Automaton::monoid_elt_t
170 typename Automaton::monoid_elt_t word(autom.structure().series().monoid());
171 do_shortest(autom, word);
175 template<
typename Automaton,
typename Mono
idElt>
179 return do_shortest(autom, word);
182 template<
typename Automaton,
typename Mono
idElt,
typename Alloc>
185 std::list<MonoidElt, Alloc>& word_list)
187 do_enumerate(autom, max_length, word_list);
191 #endif // ! VCSN_ALGORITHMS_SHORTEST_HXX