25#include "absl/algorithm/container.h"
26#include "absl/container/flat_hash_set.h"
27#include "absl/memory/memory.h"
28#include "absl/status/status.h"
29#include "absl/strings/str_format.h"
30#include "absl/strings/str_join.h"
31#include "absl/time/clock.h"
32#include "absl/time/time.h"
41ABSL_FLAG(
bool, minimize_permutation_support_size,
false,
42 "Tweak the algorithm to try and minimize the support size"
43 " of the generators produced. This may negatively impact the"
44 " performance, but works great on the sat_holeXXX benchmarks"
45 " to reduce the support size.");
53void SwapFrontAndBack(std::vector<int>* v) {
58bool PartitionsAreCompatibleAfterPartIndex(
const DynamicPartition& p1,
59 const DynamicPartition& p2,
61 const int num_parts = p1.NumParts();
62 if (p2.NumParts() != num_parts)
return false;
63 for (
int p = part_index; p < num_parts; ++p) {
64 if (p1.SizeOfPart(p) != p2.SizeOfPart(p) ||
65 p1.ParentOfPart(p) != p2.ParentOfPart(p)) {
81bool ListMapsToList(
const List& l1,
const List& l2,
82 const DynamicPermutation& permutation,
83 std::vector<bool>* tmp_node_mask) {
84 int num_elements_delta = 0;
86 for (
const int mapped_x : l2) {
88 (*tmp_node_mask)[mapped_x] =
true;
90 for (
const int x : l1) {
92 const int mapped_x = permutation.ImageOf(x);
93 if (!(*tmp_node_mask)[mapped_x]) {
97 (*tmp_node_mask)[mapped_x] =
false;
99 if (num_elements_delta != 0) match =
false;
102 for (
const int x : l2) (*tmp_node_mask)[x] =
false;
110 tmp_dynamic_permutation_(NumNodes()),
111 tmp_node_mask_(NumNodes(), false),
112 tmp_degree_(NumNodes(), 0),
113 tmp_nodes_with_degree_(NumNodes() + 1) {
115 time_limit_ = &dummy_time_limit_;
116 tmp_partition_.
Reset(NumNodes());
123 reverse_adj_list_index_.assign(graph.
num_nodes() + 2, 0);
124 for (
const int node : graph.
AllNodes()) {
126 ++reverse_adj_list_index_[graph.
Head(
arc) + 2];
132 std::partial_sum(reverse_adj_list_index_.begin() + 2,
133 reverse_adj_list_index_.end(),
134 reverse_adj_list_index_.begin() + 2);
138 flattened_reverse_adj_lists_.assign(graph.
num_arcs(), -1);
139 for (
const int node : graph.
AllNodes()) {
141 flattened_reverse_adj_lists_[reverse_adj_list_index_[graph.
Head(
arc) +
150 for (
const int i : flattened_reverse_adj_lists_)
DCHECK_NE(i, -1);
158 const int image = permutation.
ImageOf(base);
159 if (image == base)
continue;
160 if (!ListMapsToList(graph_[base], graph_[image], permutation,
165 if (!reverse_adj_list_index_.empty()) {
169 const int image = permutation.
ImageOf(base);
170 if (image == base)
continue;
171 if (!ListMapsToList(TailsOfIncomingArcsTo(base),
172 TailsOfIncomingArcsTo(image), permutation,
185inline void IncrementCounterForNonSingletons(
const T&
nodes,
187 std::vector<int>* node_count,
188 std::vector<int>* nodes_seen,
189 int64_t* num_operations) {
190 *num_operations +=
nodes.end() -
nodes.begin();
191 for (
const int node :
nodes) {
193 const int count = ++(*node_count)[node];
194 if (count == 1) nodes_seen->push_back(node);
202 std::vector<int>& tmp_nodes_with_nonzero_degree = tmp_stack_;
211 int64_t num_operations = 0;
224 std::vector<bool> adjacency_directions(1,
true);
225 if (!reverse_adj_list_index_.empty()) {
226 adjacency_directions.push_back(
false);
228 for (
int part_index = first_unrefined_part_index;
231 for (
const bool outgoing_adjacency : adjacency_directions) {
234 if (outgoing_adjacency) {
236 IncrementCounterForNonSingletons(
237 graph_[node], *partition, &tmp_degree_,
238 &tmp_nodes_with_nonzero_degree, &num_operations);
242 IncrementCounterForNonSingletons(
243 TailsOfIncomingArcsTo(node), *partition, &tmp_degree_,
244 &tmp_nodes_with_nonzero_degree, &num_operations);
249 num_operations += 3 + tmp_nodes_with_nonzero_degree.size();
250 for (
const int node : tmp_nodes_with_nonzero_degree) {
251 const int degree = tmp_degree_[node];
252 tmp_degree_[node] = 0;
253 max_degree =
std::max(max_degree, degree);
254 tmp_nodes_with_degree_[degree].push_back(node);
256 tmp_nodes_with_nonzero_degree.clear();
259 for (
int degree = 1; degree <= max_degree; ++degree) {
262 num_operations += 1 + 3 * tmp_nodes_with_degree_[degree].size();
263 partition->
Refine(tmp_nodes_with_degree_[degree]);
264 tmp_nodes_with_degree_[degree].clear();
273 static_cast<double>(num_operations));
278 const int original_num_parts = partition->
NumParts();
279 partition->
Refine(std::vector<int>(1, node));
283 if (new_singletons !=
nullptr) {
284 new_singletons->clear();
285 for (
int p = original_num_parts; p < partition->
NumParts(); ++p) {
289 if (!tmp_node_mask_[parent] && parent < original_num_parts &&
291 tmp_node_mask_[parent] =
true;
299 for (
int p = original_num_parts; p < partition->
NumParts(); ++p) {
306void MergeNodeEquivalenceClassesAccordingToPermutation(
309 for (
int c = 0; c < perm.
NumCycles(); ++c) {
312 for (
const int e : perm.
Cycle(c)) {
314 const int removed_representative =
316 if (sorted_representatives !=
nullptr && removed_representative != -1) {
317 sorted_representatives->
Remove(removed_representative);
337void GetAllOtherRepresentativesInSamePartAs(
338 int representative_node,
const DynamicPartition& partition,
339 const DenseDoublyLinkedList& representatives_sorted_by_index_in_partition,
340 MergingPartition* node_equivalence_classes,
341 std::vector<int>* pruned_other_nodes) {
342 pruned_other_nodes->clear();
343 const int part_index = partition.PartOf(representative_node);
345 int repr = representative_node;
347 DCHECK_EQ(repr, node_equivalence_classes->GetRoot(repr));
348 repr = representatives_sorted_by_index_in_partition.Prev(repr);
349 if (repr < 0 || partition.PartOf(repr) != part_index)
break;
350 pruned_other_nodes->push_back(repr);
353 repr = representative_node;
355 DCHECK_EQ(repr, node_equivalence_classes->GetRoot(repr));
356 repr = representatives_sorted_by_index_in_partition.Next(repr);
357 if (repr < 0 || partition.PartOf(repr) != part_index)
break;
358 pruned_other_nodes->push_back(repr);
366 std::vector<int> expected_output;
367 for (
const int e : partition.ElementsInPart(part_index)) {
368 if (node_equivalence_classes->GetRoot(e) != representative_node) {
369 expected_output.push_back(e);
372 node_equivalence_classes->KeepOnlyOneNodePerPart(&expected_output);
373 for (
int& x : expected_output) x = node_equivalence_classes->GetRoot(x);
374 std::sort(expected_output.begin(), expected_output.end());
375 std::vector<int> sorted_output = *pruned_other_nodes;
376 std::sort(sorted_output.begin(), sorted_output.end());
377 DCHECK_EQ(absl::StrJoin(expected_output,
" "),
378 absl::StrJoin(sorted_output,
" "));
384 std::vector<int>* node_equivalence_classes_io,
385 std::vector<std::unique_ptr<SparsePermutation>>* generators,
386 std::vector<int>* factorized_automorphism_group_size,
392 factorized_automorphism_group_size->clear();
393 if (node_equivalence_classes_io->size() != NumNodes()) {
394 return absl::Status(absl::StatusCode::kInvalidArgument,
395 "Invalid 'node_equivalence_classes_io'.");
405 return absl::Status(absl::StatusCode::kDeadlineExceeded,
406 "During the initial refinement.");
408 VLOG(4) <<
"Base partition: "
412 std::vector<std::vector<int>> permutations_displacing_node(NumNodes());
413 std::vector<int> potential_root_image_nodes;
436 struct InvariantDiveState {
438 int num_parts_before_refinement;
440 InvariantDiveState(
int node,
int num_parts)
441 : invariant_node(node), num_parts_before_refinement(num_parts) {}
443 std::vector<InvariantDiveState> invariant_dive_stack;
450 for (
int invariant_node = 0; invariant_node < NumNodes(); ++invariant_node) {
454 invariant_dive_stack.push_back(
455 InvariantDiveState(invariant_node, base_partition.
NumParts()));
457 VLOG(4) <<
"Invariant dive: invariant node = " << invariant_node
458 <<
"; partition after: "
461 return absl::Status(absl::StatusCode::kDeadlineExceeded,
462 "During the invariant dive.");
473 while (!invariant_dive_stack.empty()) {
477 const int root_node = invariant_dive_stack.back().invariant_node;
478 const int base_num_parts =
479 invariant_dive_stack.back().num_parts_before_refinement;
480 invariant_dive_stack.pop_back();
483 VLOG(4) <<
"Backtracking invariant dive: root node = " << root_node
508 GetAllOtherRepresentativesInSamePartAs(
509 root_node, base_partition, representatives_sorted_by_index_in_partition,
510 &node_equivalence_classes, &potential_root_image_nodes);
511 DCHECK(!potential_root_image_nodes.empty());
512 IF_STATS_ENABLED(stats_.invariant_unroll_time.StopTimerAndAddElapsedTime());
516 while (!potential_root_image_nodes.empty()) {
518 VLOG(4) <<
"Potential (pruned) images of root node " << root_node
519 <<
" left: [" << absl::StrJoin(potential_root_image_nodes,
" ")
521 const int root_image_node = potential_root_image_nodes.back();
522 VLOG(4) <<
"Trying image of root node: " << root_image_node;
524 std::unique_ptr<SparsePermutation> permutation =
525 FindOneSuitablePermutation(root_node, root_image_node,
526 &base_partition, &image_partition,
527 *generators, permutations_displacing_node);
529 if (permutation !=
nullptr) {
534 MergeNodeEquivalenceClassesAccordingToPermutation(
535 *permutation, &node_equivalence_classes,
536 &representatives_sorted_by_index_in_partition);
541 SwapFrontAndBack(&potential_root_image_nodes);
543 &potential_root_image_nodes);
544 SwapFrontAndBack(&potential_root_image_nodes);
547 const int permutation_index =
static_cast<int>(generators->size());
548 for (
const int node : permutation->Support()) {
549 permutations_displacing_node[node].push_back(permutation_index);
554 generators->push_back(std::move(permutation));
557 potential_root_image_nodes.pop_back();
563 factorized_automorphism_group_size->push_back(
571 return absl::Status(absl::StatusCode::kDeadlineExceeded,
572 "Some automorphisms were found, but probably not all.");
574 return ::absl::OkStatus();
585 int part_index,
int* base_node,
int* image_node) {
599 if (absl::GetFlag(FLAGS_minimize_permutation_support_size)) {
601 for (
const int node : base_partition.
ElementsInPart(part_index)) {
602 if (image_partition.
PartOf(node) == part_index) {
603 *image_node = *base_node = node;
614 if (image_partition.
PartOf(*base_node) == part_index) {
615 *image_node = *base_node;
625std::unique_ptr<SparsePermutation>
626GraphSymmetryFinder::FindOneSuitablePermutation(
627 int root_node,
int root_image_node, DynamicPartition* base_partition,
628 DynamicPartition* image_partition,
629 const std::vector<std::unique_ptr<SparsePermutation>>&
630 generators_found_so_far,
631 const std::vector<std::vector<int>>& permutations_displacing_node) {
637 DCHECK(search_states_.empty());
640 std::vector<int> base_singletons;
641 std::vector<int> image_singletons;
644 int min_potential_mismatching_part_index;
645 std::vector<int> next_potential_image_nodes;
649 search_states_.emplace_back(
651 base_partition->NumParts(),
652 base_partition->NumParts());
654 search_states_.back().remaining_pruned_image_nodes.assign(1, root_image_node);
659 while (!search_states_.empty()) {
671 const SearchState& ss = search_states_.back();
672 const int image_node = ss.first_image_node >= 0
673 ? ss.first_image_node
674 : ss.remaining_pruned_image_nodes.back();
678 DCHECK_EQ(ss.num_parts_before_trying_to_map_base_node,
679 image_partition->NumParts());
688 VLOG(4) << ss.DebugString();
705 bool compatible =
true;
708 compatible = PartitionsAreCompatibleAfterPartIndex(
709 *base_partition, *image_partition,
710 ss.num_parts_before_trying_to_map_base_node);
711 u.AlsoUpdate(compatible ? &stats_.quick_compatibility_success_time
712 : &stats_.quick_compatibility_fail_time);
714 bool partitions_are_full_match =
false;
718 &stats_.dynamic_permutation_refinement_time);
719 tmp_dynamic_permutation_.
AddMappings(base_singletons, image_singletons);
722 min_potential_mismatching_part_index =
723 ss.min_potential_mismatching_part_index;
724 partitions_are_full_match = ConfirmFullMatchOrFindNextMappingDecision(
725 *base_partition, *image_partition, tmp_dynamic_permutation_,
726 &min_potential_mismatching_part_index, &next_base_node,
728 u.AlsoUpdate(partitions_are_full_match
729 ? &stats_.map_election_std_full_match_time
730 : &stats_.map_election_std_mapping_time);
732 if (compatible && partitions_are_full_match) {
733 DCHECK_EQ(min_potential_mismatching_part_index,
734 base_partition->NumParts());
740 bool is_automorphism =
true;
744 u.AlsoUpdate(is_automorphism ? &stats_.automorphism_test_success_time
745 : &stats_.automorphism_test_fail_time);
747 if (is_automorphism) {
751 std::unique_ptr<SparsePermutation> sparse_permutation(
753 VLOG(4) <<
"Automorphism found: " << sparse_permutation->DebugString();
754 const int base_num_parts =
755 search_states_[0].num_parts_before_trying_to_map_base_node;
756 base_partition->UndoRefineUntilNumPartsEqual(base_num_parts);
757 image_partition->UndoRefineUntilNumPartsEqual(base_num_parts);
758 tmp_dynamic_permutation_.
Reset();
759 search_states_.clear();
761 search_time_updater.AlsoUpdate(&stats_.search_time_success);
762 return sparse_permutation;
768 VLOG(4) <<
"Permutation candidate isn't a valid automorphism.";
769 if (base_partition->NumParts() == NumNodes()) {
780 int non_singleton_part = 0;
783 while (base_partition->SizeOfPart(non_singleton_part) == 1) {
784 ++non_singleton_part;
785 DCHECK_LT(non_singleton_part, base_partition->NumParts());
789 1e-9 *
static_cast<double>(non_singleton_part));
793 GetBestMapping(*base_partition, *image_partition, non_singleton_part,
794 &next_base_node, &next_image_node);
809 while (!search_states_.empty()) {
810 SearchState*
const last_ss = &search_states_.back();
811 image_partition->UndoRefineUntilNumPartsEqual(
812 last_ss->num_parts_before_trying_to_map_base_node);
813 if (last_ss->first_image_node >= 0) {
826 const int part = image_partition->PartOf(last_ss->first_image_node);
827 last_ss->remaining_pruned_image_nodes.reserve(
828 image_partition->SizeOfPart(part));
829 last_ss->remaining_pruned_image_nodes.push_back(
830 last_ss->first_image_node);
831 for (
const int e : image_partition->ElementsInPart(part)) {
832 if (e != last_ss->first_image_node) {
833 last_ss->remaining_pruned_image_nodes.push_back(e);
838 PruneOrbitsUnderPermutationsCompatibleWithPartition(
839 *image_partition, generators_found_so_far,
840 permutations_displacing_node[last_ss->first_image_node],
841 &last_ss->remaining_pruned_image_nodes);
843 SwapFrontAndBack(&last_ss->remaining_pruned_image_nodes);
844 DCHECK_EQ(last_ss->remaining_pruned_image_nodes.back(),
845 last_ss->first_image_node);
846 last_ss->first_image_node = -1;
848 last_ss->remaining_pruned_image_nodes.pop_back();
849 if (!last_ss->remaining_pruned_image_nodes.empty())
break;
851 VLOG(4) <<
"Backtracking one level up.";
852 base_partition->UndoRefineUntilNumPartsEqual(
853 last_ss->num_parts_before_trying_to_map_base_node);
858 search_states_.pop_back();
867 VLOG(4) <<
" Deepening the search.";
868 search_states_.emplace_back(
869 next_base_node, next_image_node,
870 base_partition->NumParts(),
871 min_potential_mismatching_part_index);
879 search_time_updater.AlsoUpdate(&stats_.search_time_fail);
884GraphSymmetryFinder::TailsOfIncomingArcsTo(
int node)
const {
886 flattened_reverse_adj_lists_.begin() + reverse_adj_list_index_[node],
887 flattened_reverse_adj_lists_.begin() + reverse_adj_list_index_[node + 1]);
890void GraphSymmetryFinder::PruneOrbitsUnderPermutationsCompatibleWithPartition(
891 const DynamicPartition& partition,
892 const std::vector<std::unique_ptr<SparsePermutation>>& permutations,
893 const std::vector<int>& permutation_indices, std::vector<int>*
nodes) {
894 VLOG(4) <<
" Pruning [" << absl::StrJoin(*
nodes,
", ") <<
"]";
901 if (
nodes->size() <= 1)
return;
906 std::vector<int>& tmp_nodes_on_support =
908 DCHECK(tmp_nodes_on_support.empty());
912 for (
const int p : permutation_indices) {
913 const SparsePermutation& permutation = *permutations[p];
916 bool compatible =
true;
917 for (
int c = 0; c < permutation.NumCycles(); ++c) {
918 const SparsePermutation::Iterator cycle = permutation.Cycle(c);
920 partition.SizeOfPart(partition.PartOf(*cycle.begin()))) {
925 if (!compatible)
continue;
928 for (
int c = 0; c < permutation.NumCycles(); ++c) {
930 for (
const int node : permutation.Cycle(c)) {
931 if (partition.PartOf(node) != part) {
936 part = partition.PartOf(node);
940 if (!compatible)
continue;
943 MergeNodeEquivalenceClassesAccordingToPermutation(permutation,
944 &tmp_partition_,
nullptr);
945 for (
const int node : permutation.Support()) {
946 if (!tmp_node_mask_[node]) {
947 tmp_node_mask_[node] =
true;
948 tmp_nodes_on_support.push_back(node);
957 for (
const int node : tmp_nodes_on_support) {
958 tmp_node_mask_[node] =
false;
961 tmp_nodes_on_support.clear();
962 VLOG(4) <<
" Pruned: [" << absl::StrJoin(*
nodes,
", ") <<
"]";
965bool GraphSymmetryFinder::ConfirmFullMatchOrFindNextMappingDecision(
966 const DynamicPartition& base_partition,
967 const DynamicPartition& image_partition,
968 const DynamicPermutation& current_permutation_candidate,
969 int* min_potential_mismatching_part_index_io,
int* next_base_node,
970 int* next_image_node)
const {
971 *next_base_node = -1;
972 *next_image_node = -1;
976 if (!absl::GetFlag(FLAGS_minimize_permutation_support_size)) {
980 for (
const int loose_node : current_permutation_candidate.LooseEnds()) {
981 DCHECK_GT(base_partition.ElementsInSamePartAs(loose_node).size(), 1);
982 *next_base_node = loose_node;
983 const int root = current_permutation_candidate.RootOf(loose_node);
985 if (image_partition.PartOf(root) == base_partition.PartOf(loose_node)) {
988 *next_image_node = root;
992 if (*next_base_node != -1) {
997 .ElementsInPart(base_partition.PartOf(*next_base_node))
1013 const int initial_min_potential_mismatching_part_index =
1014 *min_potential_mismatching_part_index_io;
1015 for (; *min_potential_mismatching_part_index_io < base_partition.NumParts();
1016 ++*min_potential_mismatching_part_index_io) {
1017 const int p = *min_potential_mismatching_part_index_io;
1018 if (base_partition.SizeOfPart(p) != 1 &&
1019 base_partition.FprintOfPart(p) != image_partition.FprintOfPart(p)) {
1020 GetBestMapping(base_partition, image_partition, p, next_base_node,
1025 const int parent = base_partition.ParentOfPart(p);
1026 if (parent < initial_min_potential_mismatching_part_index &&
1027 base_partition.SizeOfPart(parent) != 1 &&
1028 base_partition.FprintOfPart(parent) !=
1029 image_partition.FprintOfPart(parent)) {
1030 GetBestMapping(base_partition, image_partition, parent, next_base_node,
1039 for (
int p = 0; p < base_partition.NumParts(); ++p) {
1040 if (base_partition.SizeOfPart(p) != 1) {
1041 CHECK_EQ(base_partition.FprintOfPart(p),
1042 image_partition.FprintOfPart(p));
1049std::string GraphSymmetryFinder::SearchState::DebugString()
const {
1050 return absl::StrFormat(
1051 "SearchState{ base_node=%d, first_image_node=%d,"
1052 " remaining_pruned_image_nodes=[%s],"
1053 " num_parts_before_trying_to_map_base_node=%d }",
1054 base_node, first_image_node,
1055 absl::StrJoin(remaining_pruned_image_nodes,
" "),
1056 num_parts_before_trying_to_map_base_node);
#define DCHECK_NE(val1, val2)
#define CHECK_EQ(val1, val2)
#define DCHECK_GT(val1, val2)
#define DCHECK_LT(val1, val2)
#define DCHECK(condition)
#define DCHECK_EQ(val1, val2)
#define VLOG(verboselevel)
IterablePart ElementsInPart(int i) const
void Refine(const std::vector< int > &distinguished_subset)
int SizeOfPart(int part) const
void UndoRefineUntilNumPartsEqual(int original_num_parts)
IterablePart ElementsInSamePartAs(int i) const
int PartOf(int element) const
int ParentOfPart(int part) const
std::string DebugString(DebugStringSorting sorting) const
const std::vector< int > & ElementsInHierarchicalOrder() const
const int NumParts() const
std::unique_ptr< SparsePermutation > CreateSparsePermutation() const
std::string DebugString() const
void UndoLastMappings(std::vector< int > *undone_mapping_src)
void AddMappings(const std::vector< int > &src, const std::vector< int > &dst)
const std::vector< int > & AllMappingsSrc() const
void RecursivelyRefinePartitionByAdjacency(int first_unrefined_part_index, DynamicPartition *partition)
bool IsGraphAutomorphism(const DynamicPermutation &permutation) const
void DistinguishNodeInPartition(int node, DynamicPartition *partition, std::vector< int > *new_singletons_or_null)
absl::Status FindSymmetries(std::vector< int > *node_equivalence_classes_io, std::vector< std::unique_ptr< SparsePermutation > > *generators, std::vector< int > *factorized_automorphism_group_size, TimeLimit *time_limit=nullptr)
GraphSymmetryFinder(const Graph &graph, bool is_undirected)
int NumNodesInSamePartAs(int node)
void Reset(int num_nodes)
int MergePartsOf(int node1, int node2)
int FillEquivalenceClasses(std::vector< int > *node_equivalence_classes)
void KeepOnlyOneNodePerPart(std::vector< int > *nodes)
Iterator Cycle(int i) const
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
bool LimitReached()
Returns true when the external limit is true, or the deterministic time is over the deterministic lim...
void AdvanceDeterministicTime(double deterministic_duration)
Advances the deterministic time.
ArcIndexType num_arcs() const
NodeIndexType num_nodes() const
IntegerRange< NodeIndex > AllNodes() const
NodeIndexType Head(ArcIndexType arc) const
BeginEndWrapper< OutgoingArcIterator > OutgoingArcs(NodeIndexType node) const
ModelSharedTimeLimit * time_limit
ABSL_FLAG(bool, minimize_permutation_support_size, false, "Tweak the algorithm to try and minimize the support size" " of the generators produced. This may negatively impact the" " performance, but works great on the sat_holeXXX benchmarks" " to reduce the support size.")
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Collection of objects used to extend the Constraint Solver library.
DisabledScopedTimeDistributionUpdater ScopedTimeDistributionUpdater
bool GraphIsSymmetric(const Graph &graph)
#define IF_STATS_ENABLED(instructions)
std::vector< int >::const_iterator begin() const