|
ADS
Algorithms and Data Structures
|
The MFSET of section 10.2 with the undo of its merges, beyond the book [see MFSet.h], and on it the connectivity of a graph whose edges come and go, when all the changes and the questions are known in advance (see Undo of the merges and offline connectivity). More...
#include "ADS/Contract.h"#include <cstddef>#include <vector>#include "ADS/mfset/impl/RollbackUnionFind.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RollbackUnionFind |
| the union of disjoint sets of \( [ 0 , n ) \) by rank, with no compression, whose merges can be undone More... | |
| struct | ADS::ConnectivityOp |
| an operation on a graph whose edges come and go More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| std::vector< int > | ADS::offlineConnectivity (int n, const std::vector< ConnectivityOp > &ops) |
| the answers to the questions of ops on \( n \) nodes, in their order: 1 or 0 for CONNECTED, the number of components for COMPONENTS | |
The MFSET of section 10.2 with the undo of its merges, beyond the book [see MFSet.h], and on it the connectivity of a graph whose edges come and go, when all the changes and the questions are known in advance (see Undo of the merges and offline connectivity).
RollbackUnionFind is the forest of the parents with the union by rank and without any compression of the paths, so that find() costs \( O( \log n ) \) in the worst case (Lemma 2 of Unions by size and by rank); each merge() changes at most three words (a parent, a size and a rank), which a record on a stack keeps, undo() puts them back in \( O( 1 ) \), and rollback( t ) undoes the merges back to the checkpoint \( t \), the number of merges done when checkpoint() gave it (a merge of two elements already together leaves a record too, so that the checkpoints count the calls). The space is \( 12 n \) bytes and 12 bytes per record.
offlineConnectivity() answers, for a sequence of \( k \) operations on \( n \) nodes, each the insertion of an edge, its deletion or a question (whether two nodes are connected, or how many components there are), the questions in their order, by the segment tree over the times of Lemma 4 of Undo of the merges and offline connectivity, in \( O( k \log k \log n ) \) expected time (the hash table that pairs each deletion with its insertion) and \( O( k \log k ) \) space.
The contracts [see Contract.h]: \( n \ge 0 \), the elements in \( [ 0 , n ) \); a rollback goes back to a checkpoint not beyond the merges done, and an undo() needs one; a deletion in offlineConnectivity() needs the edge alive (of parallel edges, the last inserted dies first), and a loop \( [ u , u ] \) is allowed and joins nothing. The invariant is that the parents make a forest of count() trees, the rank of each root bounding its height, and that the records undo the merges. The operations are defined in RollbackUnionFind.tpp.