17 #ifndef STTC_ALGOS_FACTOR_HH
18 #define STTC_ALGOS_FACTOR_HH
21 #include <unordered_map>
25 namespace awali {
namespace sttc {
29 template <
typename Aut>
32 using automaton_t = Aut;
39 std::unordered_map<state_t, unsigned int>,
40 std::vector<std::vector<state_t>> >;
45 unsigned int current_root;
51 std::stack<state_t> stack;
52 std::unordered_map<state_t, tarjan_state_t*>
map;
53 std::unordered_map<state_t, unsigned int>
scc_of;
54 std::vector<std::vector<state_t>> states_of;
55 unsigned int cur_num = 0;
56 unsigned int cur_scc = 2;
66 std::vector<state_t> successors (automaton_t aut,
state_t stt) {
67 std::vector<state_t> res;
69 res.push_back(aut->dst_of(tr));
77 tarjan_state_t* cur =
new tarjan_state_t
78 { stt, data.cur_num, data.cur_num,
true };
79 data.map.insert(std::pair<state_t, tarjan_state_t*>{stt,cur});
83 for (
state_t succ : successors(aut, stt)) {
84 auto it= data.map.find(succ);
85 if (it == data.map.end()) {
87 it= data.map.find(succ);
89 if (it->second->onstack)
90 cur->current_root =
utils::min(cur->current_root,
91 it->second->current_root);
95 if (cur->index == cur->current_root) {
97 data.states_of.push_back({});
98 std::vector<state_t>& back = data.states_of.back();
100 top= data.stack.top();
102 data.map[
top]->onstack=
false;
104 data.scc_of[
top]= data.cur_scc;
105 }
while (
top != cur->id);
114 for(
state_t stt : aut->states())
115 if (data.map.find(stt) == data.map.end())
129 tarjan_state_t*
c =
nullptr)
140 if (data.map.find(stt) != data.map.end())
142 std::stack<token_t> tstack;
144 tarjan_state_t* t_stt=
new tarjan_state_t
145 { stt, data.cur_num, data.cur_num,
true };
147 data.map.insert(std::pair<state_t, tarjan_state_t*>(stt, t_stt));
148 tstack.push(
make_token(t_stt, std::move(successors(aut, stt))) );
149 data.stack.push(stt);
151 while (!tstack.empty())
160 auto it = data.map.find(dst);
161 if ( it == data.map.end()) {
163 tarjan_state_t* t_dst=
164 new tarjan_state_t{ dst, data.cur_num, data.cur_num,
true };
166 data.map.insert(std::pair<state_t, tarjan_state_t*>(dst, t_dst));
167 tstack.push(
make_token(t_dst, std::move(successors(aut, dst)), token.
src) );
168 data.stack.push(dst);
169 }
else if ( it->second->onstack ) {
171 it->second->current_root);
177 if (token.
caller !=
nullptr) {
178 token.
caller->current_root =
180 token.
src->current_root );
182 if (token.
src->index == token.
src->current_root)
184 data.states_of.push_back({});
185 std::vector<state_t>& back = data.states_of.back();
189 top= data.stack.top();
191 data.map[
top]->onstack=
false;
193 data.scc_of[
top]= data.cur_scc;
194 }
while (
top != bot);
205 for(
state_t stt : aut->states())
217 auto it = data.states_of.begin();
218 data.states_of.insert(it,{1});
220 it = data.states_of.begin();
221 data.states_of.insert(it,{0});
230 automaton_t out= make_mutable_automaton<context_t>(aut->context());
231 auto history = std::make_shared<partition_history<automaton_t>>(aut);
232 out->set_history(history);
233 unsigned int max= data.states_of.size();
234 for (
unsigned int i=2; i<
max; i++) {
238 for (std::vector<state_t> scc : data.states_of) {
239 std::set<state_t> hist;
240 hist.insert(scc.begin(), scc.end());
241 history->add_state(i, std::move(hist));
245 for (
auto it = data.scc_of.cbegin(); it != data.scc_of.cend() ; it++) {
248 weight_t w= aut->weight_of(t);
249 label_t l= aut->label_of(t);
250 out->add_transition(it->second, data.scc_of[dest], l, w);
262 return { std::move(data.scc_of), std::move(data.states_of) };
269 size_t n=aut->num_states()+2;
271 data.scc_of.reserve(
n);
276 for (
auto it= data.map.cbegin(); it != data.map.cend(); it++)
293 template <
typename Aut>
294 std::pair< std::unordered_map<state_t, unsigned int>,
295 std::vector<std::vector<state_t>> >
311 template <
typename Aut>
312 std::pair< std::unordered_map<state_t, unsigned int>,
313 std::vector<std::vector<state_t>> >
340 template <
typename Aut>
341 std::pair< Aut, std::pair< std::unordered_map<state_t, unsigned int>,
342 std::vector<std::vector<state_t>> > >
359 template <
typename Aut>
366 return std::move(res.second[res.second.size()-1]);
The semiring of complex numbers.
Definition: c.hh:44
~tarjaner_t()
Definition: scc.hh:274
tarjan_state_t * src
Definition: scc.hh:121
automaton_t condensation()
Definition: scc.hh:228
void add_subliminal_sccs()
Definition: scc.hh:212
tarjaner_t(automaton_t a)
Definition: scc.hh:267
void visit_iterative(state_t stt)
Definition: scc.hh:137
long unsigned int remaining
Definition: scc.hh:123
tarjan_state_t * caller
Definition: scc.hh:124
void compute_sccs_iterative()
Definition: scc.hh:203
std::vector< state_t > dst_to_treat
Definition: scc.hh:122
result_t get_result()
Definition: scc.hh:260
token_t make_token(tarjan_state_t *t, std::vector< state_t > v, tarjan_state_t *c=nullptr)
Definition: scc.hh:128
void visit_recursive(const automaton_t &aut, state_t stt)
Definition: scc.hh:75
void compute_sccs_recursive()
Definition: scc.hh:112
The semiring of Natural numbers.
Definition: n.hh:34
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:134
static constexpr TOP< void > top
Definition: priority.hh:93
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
std::pair< std::unordered_map< state_t, unsigned int >, std::vector< std::vector< state_t > > > scc_iterative(Aut aut)
Iterative computation of strongly connected components.
Definition: scc.hh:314
std::vector< state_t > scc_of(Aut aut, state_t s)
Computes the strongly connected component of a state.
Definition: scc.hh:361
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
std::pair< Aut, std::pair< std::unordered_map< state_t, unsigned int >, std::vector< std::vector< state_t > > > > condensation(Aut aut)
Condense each strongly connected component to a state.
Definition: scc.hh:343
std::pair< std::unordered_map< state_t, unsigned int >, std::vector< std::vector< state_t > > > scc_recursive(Aut aut)
Recursive computation of strongly connected components.
Definition: scc.hh:296
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: synchronizing_word.hh:266
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
ATTRIBUTE_CONST int min(int a, int b)
Definition: arith.hh:48
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22