7 #include <unordered_set>
8 #include <unordered_map>
11 #include <boost/range/algorithm/max_element.hpp>
26 template <Automaton Aut>
27 std::vector<weight_t_of<Aut>>
33 auto ws = *aut->weightset();
34 auto d = std::vector<weight_t>(
states_size(aut), ws.zero());
38 auto todo = std::deque<state_t>{s0};
41 auto s = todo.front();
47 auto dst = aut->dst_of(t);
48 auto w1 = ws.
mul(r1, aut->weight_of(t));
49 auto w = ws.add(d[dst], w1);
50 if (!ws.equal(d[dst], w))
53 r[dst] = ws.add(
r[dst], w1);
55 todo.emplace_back(dst);
68 template <Automaton Aut>
69 std::unordered_map<state_t_of<Aut>,
71 transition_t_of<Aut>>>
74 using automaton_t = Aut;
79 auto marked = std::unordered_set<state_t>{};
81 = std::unordered_map<state_t, std::pair<unsigned, transition_t>>{};
85 state_t p = todo.front();
88 for (
auto t :
all_in(aut, p))
90 auto s = aut->src_of(t);
91 if (marked.find(s) == marked.end())
94 auto cur_p = parent.find(p);
96 = cur_p == parent.end() ? 1
97 : cur_p->second.first + 1;
98 parent[s] = {cur_d, t};
105 template <Automaton Aut>
106 std::vector<std::vector<weight_t_of<Aut>>>
109 using automaton_t = Aut;
112 auto ws = aut->weightset();
114 std::vector<std::vector<weight_t>>
res(
115 n, std::vector<weight_t>(n, ws->zero()));
119 auto i = aut->src_of(t);
120 auto j = aut->dst_of(t);
121 res[i][j] = ws->add(res[i][j], aut->weight_of(t));
123 for (
auto k : aut->states())
125 auto reskk = res[k][k] = ws->star(res[k][k]);
126 for (
auto i : aut->all_states())
127 for (
auto j : aut->all_states())
128 if (i != k && j != k)
131 ws->mul(res[i][k], reskk, res[k][j])
133 for (
auto i : aut->all_states())
136 res[k][i] = ws->mul(reskk, res[k][i]);
137 res[i][k] = ws->mul(res[i][k], reskk);
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.
std::queue< typename Range::value_type > make_queue(const Range &range)
The content of cont as a queue.
value_t mul(const Ts &...ts) const
A variadic multiplication.
Provide a variadic mul on top of a binary mul(), and one().
std::vector< std::vector< weight_t_of< Aut > > > all_distances(const Aut &aut)
size_t states_size(const Aut &aut)
The largest state number, plus one.
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)
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
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
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
std::vector< weight_t_of< Aut > > ss_shortest_distance(const Aut &aut, state_t_of< Aut > s0)
Single source shortest distance.