7 #include <unordered_set>
8 #include <unordered_map>
11 #include <boost/range/algorithm/max_element.hpp>
27 template <Automaton Aut>
28 std::vector<weight_t_of<Aut>>
34 auto ws = *aut->weightset();
35 auto d = std::vector<weight_t>(aut->all_states().back() + 1, ws.zero());
39 auto todo = std::deque<state_t>{s0};
42 auto s = todo.front();
48 auto dst = aut->dst_of(t);
49 auto w1 = ws.
mul(r1, aut->weight_of(t));
50 auto w = ws.add(d[dst], w1);
51 if (!ws.equal(d[dst], w))
54 r[dst] = ws.add(
r[dst], w1);
56 todo.emplace_back(dst);
69 template <Automaton Aut>
70 std::unordered_map<state_t_of<Aut>,
72 transition_t_of<Aut>>>
75 using automaton_t = Aut;
80 auto marked = std::unordered_set<state_t>{};
82 = std::unordered_map<state_t, std::pair<unsigned, transition_t>>{};
86 state_t p = todo.front();
89 for (
auto t :
all_in(aut, p))
91 auto s = aut->src_of(t);
92 if (marked.find(s) == marked.end())
95 auto cur_p = parent.find(p);
97 = cur_p == parent.end() ? 1
98 : cur_p->second.first + 1;
99 parent[s] = {cur_d, t};
106 template <Automaton Aut>
107 std::vector<std::vector<weight_t_of<Aut>>>
110 using automaton_t = Aut;
113 auto ws = aut->weightset();
114 auto n = aut->all_states().back() + 1;
115 std::vector<std::vector<weight_t>> res(
116 n, std::vector<weight_t>(n, ws->zero()));
120 auto i = aut->src_of(t);
121 auto j = aut->dst_of(t);
122 res[i][j] = ws->add(res[i][j], aut->weight_of(t));
124 for (
auto k : aut->states())
126 auto reskk = res[k][k] = ws->star(res[k][k]);
127 for (
auto i : aut->all_states())
128 for (
auto j : aut->all_states())
129 if (i != k && j != k)
132 ws->mul(res[i][k], reskk, res[k][j])
134 for (
auto i : aut->all_states())
137 res[k][i] = ws->mul(reskk, res[k][i]);
138 res[i][k] = ws->mul(res[i][k], reskk);
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
std::vector< std::vector< weight_t_of< Aut > > > all_distances(const Aut &aut)
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
std::queue< typename Range::value_type > make_queue(const Range &range)
The content of cont as a queue.
Provide a variadic mul on top of a binary mul(), and one().
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
std::unordered_map< state_t_of< Aut >, std::pair< unsigned, transition_t_of< Aut > > > paths_ibfs(const Aut &aut, const std::vector< state_t_of< Aut >> &start)
Find the shortest paths from some states to all the states.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
std::vector< weight_t_of< Aut > > ss_shortest_distance(const Aut &aut, state_t_of< Aut > s0)
Single source shortest distance.
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
value_t mul(const Ts &...ts) const
A variadic multiplication.