5 #include <boost/algorithm/string/predicate.hpp> 6 #include <boost/range/algorithm/mismatch.hpp> 7 #include <boost/range/iterator_range_core.hpp> 41 template <
Automaton Aut,
typename ValueSet,
42 typename Mul,
typename GetValue>
54 using value_t =
typename valueset_t::value_t;
57 Mul mul, GetValue get_value)
61 , get_value_(get_value)
75 return vs_.less(v_, rhs.
v_);
94 template <Automaton AnyAut>
102 for (
auto t = path[dst];
103 t != aut->null_transition();
104 t = path[aut->src_of(t)])
107 if (aut->src_of(t) == src)
110 std::reverse(
res.begin(),
res.end());
120 template <Automaton AnyAut>
127 return algo(src, dst);
133 using boost::starts_with;
134 using boost::make_iterator_range;
135 auto first = compute_lightest_path(aut_, src, dst);
136 auto res =
paths_t{path(aut_, first, src, dst)};
141 for (
auto i = 1
u; i < k; i++)
143 const auto& prev =
res[i - 1];
144 for (
auto j = 0
u; j < prev.size(); j++)
146 auto filter_aut = filter<automaton_t, true>(aut_);
147 auto spur_node = filter_aut->src_of(prev[j]);
148 auto root_path =
path_t(prev.begin(), prev.begin() + j);
150 for (
const auto& selected_path:
res)
151 if (boost::starts_with(selected_path, root_path))
152 filter_aut->hide_transition(selected_path[j]);
154 for (
auto t: root_path)
155 if (t != filter_aut->null_transition()
156 && filter_aut->src_of(t) != spur_node
157 && filter_aut->src_of(t) != aut_->pre())
158 filter_aut->hide_state(filter_aut->src_of(t));
161 = compute_lightest_path(filter_aut, spur_node, dst);
164 root_path.insert(root_path.end(),
165 spur_path.begin(), spur_path.end());
166 if (!root_path.empty()
167 && filter_aut->src_of(root_path.front()) == src
168 && filter_aut->dst_of(root_path.back()) == dst
170 [&root_path](
const auto&
profile)
179 heap.emplace(std::move(root_path), vs_, get_value_(m));
184 res.push_back(heap.top().path_);
196 template <Automaton Aut,
typename ValueSet,
typename Mul,
typename GetValue>
198 make_yen(
const Aut& aut,
const ValueSet& vs, Mul mul, GetValue get_value)
201 (aut, vs, mul, get_value));
206 template <Automaton Aut>
207 std::vector<path_t_of<Aut>>
213 return aut->weightset()->mul(lhs, aut->weight_of(t));
215 auto get_value = [](
auto m) {
return m.second; };
217 return yen(src, dst, k);
226 template <Automaton Aut>
232 if (t != aut->null_transition())
233 res[aut->dst_of(t)] = t;
237 template <Automaton Aut>
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
path_t_of< AnyAut > path(const AnyAut &aut, const predecessors_t_of< AnyAut > &path, state_t_of< AnyAut > src=AnyAut::element_type::pre(), state_t_of< AnyAut > dst=AnyAut::element_type::post())
Transform a map transition_t -> transition_t representing the predecessors of each transition into a ...
std::vector< path_t > paths_t
typename valueset_t::value_t value_t
auto path_monomial(const Aut &aut, const predecessors_t_of< Aut > &path, state_t_of< Aut > src=Aut::element_type::pre(), state_t_of< Aut > dst=Aut::element_type::post()) -> boost::optional< typename detail::word_polynomialset_t< context_t_of< Aut >>::monomial_t >
Given a path (typically computed by lightest_path), the corresponding monomial (label, weight).
weight_t_of< automaton_t > weight_t
auto make_word_polynomialset(const Ctx &ctx) -> word_polynomialset_t< Ctx >
The polynomialset of words of a labelset (not necessarily on words itself).
Yen's algorithm of the K lightest paths.
auto make_yen(const Aut &aut, const ValueSet &vs, Mul mul, GetValue get_value)
state_t_of< automaton_t > state_t
vcsn::min_fibonacci_heap< profile > heap_t
Min heap according to the weight.
context_t_of< automaton_t > context_t
paths_t operator()(state_t src, state_t dst, unsigned k)
bool none_of(const Range &r, Predicate p)
yen_impl(const automaton_t &aut, const ValueSet &vs, Mul mul, GetValue get_value)
profile(const path_t &path, const valueset_t &vs, const value_t &v)
std::vector< transition_t_of< Aut > > lightest_path(const Aut &aut, state_t_of< Aut > source, state_t_of< Aut > dest, a_star_tag)
predecessors_t_of< AnyAut > compute_lightest_path(const AnyAut &aut, state_t_of< AnyAut > src=Aut::element_type::pre(), state_t_of< AnyAut > dst=Aut::element_type::post())
Compute a lightest path on a part of the automaton.
size_t states_size(const Aut &aut)
The largest state number, plus one.
boost::heap::fibonacci_heap< Elt, detail::comparator_t< Elt > > min_fibonacci_heap
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
path_t_of< automaton_t > path_t
std::vector< path_t_of< Aut > > k_lightest_path(const Aut &aut, state_t_of< Aut > src, state_t_of< Aut > dst, unsigned k)
Explicit path representation.
mutable_automaton< Context > u(const Context &ctx, unsigned n)
The Brzozowski universal witness.
transition_t_of< automaton_t > transition_t
bool operator<(const profile &rhs) const
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
auto make_dijkstra_impl(const Aut &aut, const ValueSet &vs, Mul mul)
std::vector< transition_t_of< Aut > > path_t_of
A list of transitions representing a path.
predecessors_t_of< Aut > format_lightest(const Aut &aut, const std::vector< transition_t_of< Aut >> &path)
A state-indexed vector of predecessor transitions from the path path.
std::vector< transition_t_of< Aut > > predecessors_t_of
A state-indexed vector of predecessor transitions from the path path.