Awali
Another Weighted Automata library
is_congruence.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_ISCONGRUENT_HH
18 # define AWALI_ALGOS_ISCONGRUENT_HH
19 
20 #include <vector>
21 #include <queue>
22 
23 #include <awali/sttc/weightset/fwd.hh> // b
25 #include <awali/sttc/misc/pair.hh>
26 # include <limits>
27 
28 namespace awali {
29  namespace sttc {
30 
31  /*Test whether all states in partition[i] have the same transition function modulo partition.
32 
33  */
34 
35  template <typename Aut>
36  bool is_congruence(const Aut& aut, const std::vector<std::vector<state_t>>& partition) {
37 
38  using automaton_t = Aut;
39 
42 
43  //part_of[s] is the class of state s
44  std::vector<unsigned> part_of(aut->max_state()+1);
45  for(unsigned i=0; i<partition.size(); ++i)
46  for(auto s : partition[i]){
47  part_of[s]=i;
48  }
49 
50  for(auto part : partition) {
52  for(auto s : part) {
53  //signature[{i,a}] is the sum of weights of transitions
54  //with label a from s to states in i
56  for(auto tr : aut->all_out(s)) {
57  std::pair<unsigned, label_t> key{part_of[aut->dst_of(tr)], aut->label_of(tr)};
58  weight_t w=aut->weight_of(tr);
59 
60  if(signature.count(key))
61  signature[key] = aut->weightset()->add(signature[key], w);
62  else
63  signature[key] = w;
64  }
65  for(auto p: signature.domain()) {
66  std::pair<label_t, weight_t> plw{p.second, signature[p]};
67  if(successors.count(p.first) && successors[p.first].count(plw))
68  ++successors[p.first][plw];
69  else
70  successors[p.first][plw]=1;
71  }
72  }
73  unsigned n=part.size();
74  for(auto a:successors.domain())
75  for(auto b: successors[a].domain())
76  if(successors[a][b]!=n)
77  return false;
78  }
79  return true;
80  }
81  }
82 }//end of ns awali::stc
83 
84 #endif // !AWALI_ALGOS_ISCONGRUENT_HH
The Boolean semring.
Definition: b.hh:38
The semiring of Natural numbers.
Definition: n.hh:34
automaton_t domain(transducer_t tdc)
Returns the automaton corresponding to the second tape of the transducer.
any_t label_t
Type for (transition) labels; it is an alias to any_t since its precise type depends on the weightset...
Definition: typedefs.hh:48
any_t weight_t
Type for (transition) weights; it is an alias to any_t since the its precise type depends on the weig...
Definition: typedefs.hh:61
bool is_congruence(const Aut &aut, const std::vector< std::vector< state_t >> &partition)
Definition: is_congruence.hh:36
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
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
Main namespace of Awali.
Definition: ato.hh:22
Definition: linkedhashmap.hh:31
unsigned count(const K &k) const
Definition: linkedhashmap.hh:46
const std::vector< K > & domain() const
Definition: linkedhashmap.hh:36