30 #ifndef OR_TOOLS_ALGORITHMS_DYNAMIC_PARTITION_H_ 31 #define OR_TOOLS_ALGORITHMS_DYNAMIC_PARTITION_H_ 36 #include "ortools/base/logging.h" 61 const int NumParts()
const {
return part_.size(); }
72 int PartOf(
int element)
const;
108 void Refine(
const std::vector<int>& distinguished_subset);
144 std::vector<int> element_;
147 std::vector<int> index_of_;
150 std::vector<int> part_of_;
167 Part() : start_index(0), end_index(0), parent_part(0), fprint(0) {}
168 Part(
int start_index,
int end_index,
int parent_part, uint64 fprint)
169 : start_index(start_index),
170 end_index(end_index),
171 parent_part(parent_part),
174 std::vector<Part> part_;
179 std::vector<int> tmp_counter_of_part_;
180 std::vector<int> tmp_affected_parts_;
185 std::vector<int>::const_iterator
end()
const {
return end_; }
187 std::vector<int>::const_iterator
end_;
193 const std::vector<int>::const_iterator& e)
208 void Reset(
int num_nodes);
265 void SetParentAlongPathToRoot(
int node,
int parent);
267 std::vector<int> parent_;
268 std::vector<int> part_size_;
271 std::vector<bool> tmp_part_bit_;
280 return IterablePart(element_.begin() + part_[i].start_index,
281 element_.begin() + part_[i].end_index);
285 DCHECK_GE(element, 0);
286 DCHECK_LT(element, part_of_.size());
287 return part_of_[element];
292 DCHECK_LT(part, part_.size());
293 const Part& p = part_[part];
294 return p.end_index - p.start_index;
299 DCHECK_LT(part, part_.size());
300 return part_[part].parent_part;
310 DCHECK_LT(part, part_.size());
311 return part_[part].fprint;
319 const int parent = parent_[child];
320 if (parent == child)
return child;
325 inline void MergingPartition::SetParentAlongPathToRoot(
int node,
int parent) {
328 DCHECK_GE(parent, 0);
332 const int old_parent = parent_[child];
333 parent_[child] = parent;
334 if (old_parent == child)
return;
342 parent_[node] = node;
343 part_size_[node] = 1;
348 #endif // OR_TOOLS_ALGORITHMS_DYNAMIC_PARTITION_H_ int SizeOfPart(int part) const
int PartOf(int element) const
std::string DebugString()
Dump all components, with nodes sorted within each part and parts sorted lexicographically.
std::vector< int >::const_iterator begin_
MergingPartition(int num_nodes)
IterablePart ElementsInSamePartAs(int i) const
A handy shortcut to ElementsInPart(PartOf(e)).
int ParentOfPart(int part) const
Partition class that supports incremental merging, using the union-find algorithm (see http://en....
std::string DebugString(DebugStringSorting sorting) const
DynamicPartition(int num_elements)
Creates a DynamicPartition on n elements, numbered 0..n-1.
DebugStringSorting
Dump the partition to a std::string.
void ResetNode(int node)
Advanced usage: sets 'node' to be in its original singleton.
Elements are sorted within parts, and parts are then sorted lexicographically.
const int NumParts() const
int value_type
These typedefs allow this iterator to be used within testing::ElementsAre.
Partition class that supports incremental splitting, with backtracking.
std::vector< int >::const_iterator end() const
IterablePart ElementsInPart(int i) const
*** Implementation of inline methods of the above classes. ***
const std::vector< int > & ElementsInHierarchicalOrder() const
ADVANCED USAGE: All elements (0..n-1) of the partition, sorted in a way that's compatible with the hi...
void KeepOnlyOneNodePerPart(std::vector< int > *nodes)
Specialized reader API: prunes "nodes" to only keep at most one node per part: any node which is in t...
int GetRootAndCompressPath(int node)
Get the representative of "node" (a node in the same equivalence class, which will also be returned f...
std::vector< int >::const_iterator const_iterator
void Reset(int num_nodes)
std::vector< int >::const_iterator begin() const
MergingPartition()
At first, all nodes are in their own singleton part.
IterablePart(const std::vector< int >::const_iterator &b, const std::vector< int >::const_iterator &e)
std::vector< int >::const_iterator end_
Elements are sorted within parts, and parts are kept in order.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
int GetRoot(int node) const
FOR DEBUGGING OR SPECIAL "CONST" ACCESS ONLY: Find the root of the union-find tree with leaf 'node',...
void UndoRefineUntilNumPartsEqual(int original_num_parts)
Undo one or several Refine() operations, until the number of parts becomes equal to "original_num_par...
int FillEquivalenceClasses(std::vector< int > *node_equivalence_classes)
Output the whole partition as node equivalence classes: if there are K parts and N nodes,...
int NumElements() const
Accessors.
void Refine(const std::vector< int > &distinguished_subset)
Refines the partition such that elements that are in distinguished_subset never share the same part a...
uint64 FprintOfPart(int part) const
Returns a fingerprint of the given part.
int NumNodesInSamePartAs(int node)
int MergePartsOf(int node1, int node2)
Complexity: amortized O(Ackermann⁻¹(N)) – which is essentially O(1) – where N is the number of nodes.