6 #include <unordered_map>
7 #include <unordered_set>
9 #include <boost/range/algorithm/permutation.hpp>
10 #include <boost/range/algorithm/sort.hpp>
31 template <Automaton Aut1, Automaton Aut2>
58 using states_t = std::pair<state1_t, state2_t>;
64 template <Automaton Aut>
68 std::unordered_map<label_t_of<Aut>,
69 std::pair<weight_t_of<Aut>, state_t_of<Aut>>,
78 template <Automaton Aut>
84 std::unordered_map<weight_t_of<Aut>,
85 std::vector<state_t_of<Aut>>,
88 vcsn::hash<labelset_t_of<Aut>>,
96 using pair_t = std::pair<state1_t, state2_t>;
101 using s1tos2_t = std::unordered_map<state1_t, state2_t>;
102 using s2tos1_t = std::unordered_map<state2_t, state1_t>;
139 template <Automaton Aut>
145 const state_t_of<Aut>& src = a->src_of(t);
146 const label_t_of<Aut>& l = a->label_of(t);
147 auto& doutsrc = dout[src];
148 if (doutsrc.find(l) == doutsrc.end())
149 dout[src][l] = {a->weight_of(t), a->dst_of(t)};
162 nout1_[a1_->src_of(t1)][a1_->label_of(t1)][a1_->weight_of(t1)]
163 .emplace_back(a1_->dst_of(t1));
165 nout2_[a2_->src_of(t2)][a2_->label_of(t2)][a2_->weight_of(t2)]
166 .emplace_back(a2_->dst_of(t2));
180 if (a1_->num_states() != a2_->num_states())
182 if (a1_->num_transitions() != a2_->num_transitions())
212 "are-isomorphic: lhs automaton must be accessible");
214 "are-isomorphic: rhs automaton must be accessible");
249 template <Automaton Aut>
257 const auto ws = * a->weightset();
258 const auto ls = * a->labelset();
260 using transition_t = std::pair<weight_t_of<Aut>,
264 [&](
const transition_t& t1,
const transition_t& t2)
266 if (ws.less(t1.first, t2.first))
268 else if (ws.less(t2.first, t1.first))
271 return ls.less(t1.second, t2.second);
274 #define HASH_TRANSITIONS(expression, endpoint_getter) \
276 std::unordered_set<state_t_of<Aut>> endpoint_states; \
278 for (auto& t: expression) \
280 tt.emplace_back(transition_t{a->weight_of(t), a->label_of(t)}); \
281 endpoint_states.emplace(a->endpoint_getter(t)); \
283 boost::sort(tt, less); \
284 for (const auto& t: tt) \
286 hash_combine(res, ws.hash(t.first)); \
287 hash_combine(res, ls.hash(t.second)); \
289 hash_combine(res, endpoint_states.size()); \
299 #undef HASH_TRANSITIONS
307 std::unordered_map<class_id, std::pair<states1_t, states2_t>> table;
308 for (
auto s1: a1_->all_states())
310 for (
auto s2: a2_->all_states())
317 for (
const auto& c: table)
318 res.emplace_back(std::move(c.second.first), std::move(c.second.second));
322 return c1.first.size() > c2.first.size();
343 size_t max = 0, min = a1_->num_all_states();
344 long double sum = 0.0;
345 for (
const auto& c: cs)
347 max = std::max(max, c.first.size());
348 min = std::min(min, c.first.size());
349 sum += c.first.size();
351 long state_no = a1_->num_all_states();
352 std::cerr <<
"State no: " << state_no <<
"\n";
353 std::cerr <<
"Class no: " << cs.size() <<
"\n";
354 std::cerr <<
"* min class size: " << min <<
"\n";
355 std::cerr <<
"* avg class size: " << sum / cs.size() <<
"\n";
356 std::cerr <<
"* max class size: " << max <<
"\n";
362 for (
const auto& c: cs)
365 for (
const auto& s1: c.first)
366 std::cerr << s1 <<
" ";
368 for (
const auto& s2: c.second)
369 std::cerr << s2 <<
" ";
380 catch (
const std::out_of_range&)
387 std::unordered_set<state1_t> mss1;
388 std::unordered_set<state2_t> mss2;
389 std::stack<pair_t> worklist;
390 worklist.push({a1_->pre(), a2_->pre()});
391 worklist.push({a1_->post(), a2_->post()});
392 while (! worklist.empty())
394 const auto p = std::move(worklist.top()); worklist.pop();
403 const bool m1 = (mss1.find(s1) != mss1.end());
404 const bool m2 = (mss2.find(s2) != mss2.end());
415 if ((s1 == a1_->pre()) != (s2 == a2_->pre())
416 || (s1 == a1_->post()) != (s2 == a2_->post()))
419 int t1n = 0, t2n = 0;
420 for (
auto t1:
all_out(a1_, s1))
422 auto d1 = a1_->dst_of(t1);
423 const auto& w1 = a1_->weight_of(t1);
424 const auto& l1 = a1_->label_of(t1);
425 const auto& d2s = nout2_.at(s2).at(l1).at(w1);
429 worklist.push({d1, d2});
432 for (
auto t2:
all_out(a2_, s2))
434 auto d2 = a2_->dst_of(t2);
435 const auto& w2 = a2_->weight_of(t2);
436 const auto& l2 = a2_->label_of(t2);
437 const auto& d1s = nout1_.at(s1).at(l2).at(w2);
441 worklist.push({d1, d2});
452 for (
const auto& c: state_classes_)
453 for (
int i = 0; i < int(c.first.size()); ++ i)
465 for (
long i = 1; i <= n; ++ i)
477 class_permutation_max_.clear();
478 class_permutation_generated_.clear();
479 for (
const auto& c: state_classes_) {
480 class_permutation_max_.emplace_back(
factorial(c.second.size()) - 1);
481 class_permutation_generated_.emplace_back(0);
498 const int rightmost = int(state_classes_.size()) - 1;
501 if (boost::next_permutation(state_classes_[rightmost].second))
504 ++ class_permutation_generated_[rightmost];
516 assert(class_permutation_generated_[rightmost]
517 == class_permutation_max_[rightmost]);
518 class_permutation_generated_[rightmost] = 0;
520 for (i = rightmost - 1;
522 && class_permutation_generated_[i] == class_permutation_max_[i];
525 boost::next_permutation(state_classes_[i].second);
526 class_permutation_generated_[i] = 0;
532 boost::next_permutation(state_classes_[i].second);
533 ++ class_permutation_generated_[i];
546 for (
const auto& c: state_classes_)
547 if (c.first.size() != c.second.size())
579 worklist_.push({a1_->pre(), a2_->pre()});
581 while (! worklist_.empty())
583 const states_t states = worklist_.top(); worklist_.pop();
596 if (dout1_[s1].
size() != dout2_[s2].
size())
599 for (
const auto& l1_w1dst1 : dout1_[s1])
601 const label1_t& l1 = l1_w1dst1.first;
602 const weight1_t& w1 = l1_w1dst1.second.first;
603 const state1_t& dst1 = l1_w1dst1.second.second;
605 const auto& s2out = dout2_.find(s2);
606 if (s2out == dout2_.cend())
608 const auto& s2outl = s2out->second.find(l1);
609 if (s2outl == s2out->second.cend())
612 state2_t dst2 = s2outl->second.second;
614 if (! weightset_t::equal(w1, w2))
617 const auto& isomorphics_to_dst1 =
fr_.
s1tos2_.find(dst1);
618 const auto& isomorphics_to_dst2 =
fr_.
s2tos1_.find(dst2);
620 if (isomorphics_to_dst1 ==
fr_.
s1tos2_.cend())
622 if (isomorphics_to_dst2 ==
fr_.
s2tos1_.cend())
626 worklist_.push({dst1, dst2});
631 else if (isomorphics_to_dst1 ==
fr_.
s1tos2_.cend()
632 || isomorphics_to_dst1->second != dst2
633 || isomorphics_to_dst2->second != dst1)
657 __func__,
": isomorphism-check not successfully performed");
660 res[s2s1.first] = s2s1.second;
669 o <<
"/* Origins." << std::endl
670 <<
" node [shape = box, style = rounded]" << std::endl;
674 o <<
" " << p.first - 2
675 <<
" [label = \"" << p.second <<
"\"]" << std::endl;
677 o <<
"*/" << std::endl;
683 template <Automaton Aut1, Automaton Aut2>
698 template <Automaton Aut1, Automaton Aut2>
702 const auto& a1 = aut1->as<Aut1>();
703 const auto& a2 = aut2->as<Aut2>();
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
std::vector< class_pair_t > state_classes_t
std::pair< state1_t, state2_t > states_t
void update_result_isomorphism()
state_t_of< automaton2_t > state2_t
const full_response get_full_response()
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
bool is_isomorphism_valid()
auto sum(const Aut1 &lhs, const Aut2 &rhs, Tag tag={}) -> decltype(join_automata(lhs, rhs))
The sum of two automata.
label_t_of< automaton2_t > label2_t
std::vector< state1_t > states1_t
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
dout_t< automaton2_t > dout2_
context_t_of< automaton1_t > context1_t
std::vector< state2_t > states2_t
pair_t counterexample
Only meaningful if the tag is tag::counterexample.
bool trivially_different()
A datum specifying if two given automata are isomorphic, and why if they are not. ...
dout_t< automaton1_t > dout1_
For the simpler, faster sequential case.
labelset_t_of< context1_t > labelset1_t
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
enum vcsn::are_isomorphicer::full_response::tag response
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
std::size_t class_id
Automaton states partitioned into classes.
Provide a variadic mul on top of a binary mul(), and one().
state_t_of< automaton1_t > state1_t
context_t_of< automaton2_t > context2_t
labelset_t_of< context2_t > labelset2_t
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
nout_t< automaton1_t > nout1_
nout_t< automaton2_t > nout2_
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
void initialize_next_class_combination_state()
transition_t_of< automaton1_t > transition1_t
void hash_combine(std::size_t &seed, const T &v)
void print_classes(const state_classes_t &cs)
Handy debugging method.
std::unordered_map< state_t_of< Aut >, std::unordered_map< label_t_of< Aut >, std::unordered_map< weight_t_of< Aut >, std::vector< state_t_of< Aut >>, vcsn::hash< weightset_t_of< Aut >>, vcsn::equal_to< weightset_t_of< Aut >>>, vcsn::hash< labelset_t_of< Aut >>, vcsn::equal_to< labelset_t_of< Aut >>>> nout_t
For the nonsequential case.
std::unordered_map< state1_t, state2_t > s1tos2_t
The maps associating the states of a1_ and the states of a2_->
Request the unordered_map implementation.
std::unordered_map< state2_t, state1_t > s2tos1_t
are_isomorphicer(const Aut1 &a1, const Aut2 &a2)
std::shared_ptr< detail::automaton_base > automaton
origins_t origins()
Only meaningful if operator() returned true.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
weight_t_of< automaton2_t > weight2_t
struct vcsn::are_isomorphicer::full_response fr_
std::vector< std::pair< string_t, string_t >> transitions_t
const full_response get_full_response_nonsequential()
bool next_class_combination()
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
std::pair< state1_t, state2_t > pair_t
A worklist of pairs of states which are candidate to be isomorphic.
std::stack< pair_t > worklist_t
transition_t_of< automaton2_t > transition2_t
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
const state_classes_t make_state_classes()
bool are_isomorphic(const Aut1 &a1, const Aut2 &a2)
bool is_isomorphism_valid_throwing()
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
weightset_t_of< automaton1_t > weightset2_t
std::map< state2_t, state1_t > origins_t
A map from each a2_ state to the corresponding a1_ state.
auto sort(const Aut &a) -> permutation_automaton< Aut >
weight_t_of< automaton1_t > weight1_t
s1tos2_t s1tos2_
Only meaningful if the tag is tag::isomorphic.
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
std::vector< long > class_permutation_max_
We need to keep some (small) state between a next_class_combination call and the next.
bool are_isomorphic(const automaton &aut1, const automaton &aut2)
Bridge.
std::vector< long > class_permutation_generated_
bool is_sequential_filling(const Aut &a, dout_t< Aut > &dout)
static std::ostream & print(const origins_t &orig, std::ostream &o)
Print origins.
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
class_id state_to_class(state_t_of< Aut > s, Aut &a)
weightset_t_of< automaton1_t > weightset1_t
const full_response get_full_response_sequential()
#define HASH_TRANSITIONS(expression, endpoint_getter)
void print_class_stats(const state_classes_t &cs)
Handy debugging method.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
label_t_of< automaton1_t > label1_t
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
std::unordered_map< state_t_of< Aut >, std::unordered_map< label_t_of< Aut >, std::pair< weight_t_of< Aut >, state_t_of< Aut >>, vcsn::hash< labelset_t_of< Aut >>, vcsn::equal_to< labelset_t_of< Aut >>>> dout_t
See the comment for out_ in minimize.hh.
std::pair< states1_t, states2_t > class_pair_t
Functor to compare Values of ValueSets.
state_classes_t state_classes_
bool is_accessible(const Aut &a)
Whether all its states are accessible.