1 #ifndef VCSN_ALGOS_DISTANCE_HH
2 # define VCSN_ALGOS_DISTANCE_HH
7 # include <unordered_set>
8 # include <unordered_map>
21 template <
typename Aut>
22 std::unordered_map<state_t_of<Aut>, weight_t_of<Aut>>
28 std::unordered_map<state_t, weight_t> d;
29 std::unordered_map<state_t, weight_t>
r;
30 auto ws = *aut->weightset();
31 for (
auto s : aut->all_states())
33 d.emplace(s, ws.zero());
34 r.emplace(s, ws.zero());
37 std::queue<state_t> todos;
38 std::unordered_set<state_t> marked;
43 while (!todos.empty())
45 auto s = todos.front();
49 for (
auto t : aut->all_out(s))
51 auto dst = aut->dst_of(t);
52 auto w1 = ws.mul(r1, aut->weight_of(t));
53 auto w = ws.add(d[dst], w1);
54 if (!ws.equals(d[dst], w))
57 r[dst] = ws.add(r[dst], w1);
58 if (!
has(marked, dst))
70 template<
typename Aut>
71 std::unordered_map<state_t_of<Aut>,
73 transition_t_of<Aut>>>
81 std::queue<state_t> todo;
82 std::unordered_set<state_t> marked;
83 std::unordered_map<state_t, std::pair<state_t, transition_t>> parent;
90 state_t p = todo.front();
93 for (
auto t : aut->all_in(p))
95 auto s = aut->src_of(t);
96 if (marked.find(s) == marked.end())
99 auto cur_p = parent.find(p);
101 if (cur_p == parent.end())
104 cur_d = cur_p->second.first + 1;
105 parent[s] = {cur_d, t};
117 template<
typename Aut>
118 std::vector<transition_t_of<Aut>>
127 std::queue<state_t> todo;
128 std::unordered_set<state_t> marked;
129 std::unordered_map<state_t, std::pair<state_t, transition_t>> parent;
132 while (!todo.empty())
134 state_t p = todo.front();
139 std::vector<transition_t> rpath;
140 state_t bt_curr = end;
141 while (bt_curr != start)
144 std::tie(bt_curr, t) = parent.find(bt_curr)->second;
147 std::reverse(rpath.begin(), rpath.end());
151 for (
auto t : aut->all_out(p))
153 auto s = aut->dst_of(t);
154 if (marked.find(s) == marked.end())
162 return std::vector<transition_t>();
166 #endif // !VCSN_ALGOS_DISTANCE_HH
std::vector< transition_t_of< Aut > > path_bfs(const Aut &aut, state_t_of< Aut > start, state_t_of< Aut > end)
A shortest path between two states.
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
std::unordered_map< state_t_of< Aut >, weight_t_of< Aut > > ss_shortest_distance(Aut aut, state_t_of< Aut > s0)
Single source shortest distance.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
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
std::unordered_map< state_t_of< Aut >, std::pair< unsigned, transition_t_of< Aut > > > paths_ibfs(const Aut &aut, std::vector< state_t_of< Aut >> start)
variadic_mul_mixin< detail::r_impl > r
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)