|
ADS
Algorithms and Data Structures
|
The link-cut trees of Sleator and Tarjan, beyond the book: a forest of rooted trees on the nodes \( 0 , \ldots , n - 1 \) that changes by link() and cut() of an edge, and answers connected(), findRoot(), the lowest common ancestor lca() and the aggregate of the values of the nodes on the path between two nodes, aggregate(), each in \( O( \log n ) \) amortized time (see Dynamic trees). More...
#include "ADS/Contract.h"#include "ADS/Modification.h"#include "ADS/Portable.h"#include <concepts>#include <cstddef>#include <cstdint>#include <functional>#include <utility>#include <vector>#include "ADS/tree/impl/LinkCutTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::LinkCutTree< T, Op, Policy > |
| a forest of dynamic trees whose nodes have values of the type T, aggregated on the paths by the commutative monoid Op, its changes told through Policy More... | |
Namespaces | |
| namespace | ADS |
The link-cut trees of Sleator and Tarjan, beyond the book: a forest of rooted trees on the nodes \( 0 , \ldots , n - 1 \) that changes by link() and cut() of an edge, and answers connected(), findRoot(), the lowest common ancestor lca() and the aggregate of the values of the nodes on the path between two nodes, aggregate(), each in \( O( \log n ) \) amortized time (see Dynamic trees).
The forest is split into preferred paths, each kept by a splay tree [see SplayTree.h] in the order of the depths, whose root points to the parent of the top of the path; the operation access( v ) makes the path from the root of its tree to \( v \) preferred, and every other operation is a few calls of it. evert( v ) makes \( v \) the root of its tree by reversing that path, with a flag pushed down lazily, so that the trees are unrooted for link(), cut() and aggregate().
As long as the paths are short the forest is kept as a vector of parents instead, on which an operation walks the path (and an evert reverses it), the path of the last walk from a node kept for an evert of that node that follows; the experiments of Tarjan and Werneck find such walks the fastest on shallow trees. The mean steps of the recent walks are counted, and once they exceed a bound (about where an operation on the splay trees costs as much) the vector becomes splay trees in \( O( n ) \), each tree a path-parent pointer per node, and stays so; useSplay() makes the change at once.
The values are of the type T, combined by the operation Op, which must be associative and commutative with the identity given to the constructor (e.g., the maximum, the minimum or the sum): commutative, since an evert reverses the order of the nodes of a path. The weights of the edges are kept by a node in the middle of each edge, as the dynamic minimum spanning forest of SpanningForestObserver does [see Reoptimization.h]. The nodes are kept in vectors indexed by their numbers, with no allocation per operation.
The forest tells its changes through the subject policy Policy [see Modification.h], NoObservers by default, with which every operation compiles to the code of the plain forest. What is told is the forest without its roots and the values of its nodes, since evert(), aggregate() and lca() move the root of a tree and leave its edges as they were: addNode() sends Inserted< int , int , T > of the node, its number as handle and key, with its value, setValue() sends Assigned< int , T > of the node, link( u , v ) sends Inserted< std::uint64_t , std::pair< int , int > > of the edge, whose handle is edgeOf( u , v ) (the same for \( [ u , v ] \) and \( [ v , u ] \)) and whose key is the pair of its ends, the least first, and cut( u , v ) and cutParent( v ) send the Erased of the edge, that of \( v \) and of its parent before the cut for cutParent(); the insertions and the changes are told after the change and undone if an observer throws, the erasures before it. An observer that applies these events to a graph keeps the edges and the values of the forest, and thus its trees.
The libraries of graphs do not have the dynamic trees (neither the Boost Graph Library nor LEMON), and those of competitive programming have them for fixed numbers of nodes; the batch-parallel ones cited by JoinAlgorithms.h answer batches of links and cuts instead.
The contracts [see Contract.h]: the nodes are in \( [ 0 , n ) \); link() joins two nodes of two distinct trees, cut() removes an edge that is there, aggregate() and lca() need two nodes of the same tree, checks left out of the count of the walks [see joined()]; at the level of the audits each operation checks the parents, the children and the aggregates of the splay trees it touched. The operations are defined in LinkCutTree.tpp.