ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
UnionFind.h File Reference

The MFSET of section 10.2 engineered, beyond the book [see MFSet.h]: the forest of the parents in one vector of integers, where a root keeps minus the size of its tree instead of a parent, so that the union by size needs no second vector and a find() touches one array; find() halves the path (every other node on the way is made a child of its grandparent), which needs a single pass, no recursion and no stack, and keeps the bound of the compression with the union by size (Tarjan and van Leeuwen): a sequence of \( m \geq n \) operations on \( n \) elements takes. More...

#include "ADS/Contract.h"
#include "ADS/Modification.h"
#include "ADS/Portable.h"
#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>
#include "ADS/mfset/impl/UnionFind.tpp"

Go to the source code of this file.

Classes

class  ADS::DisjointSets< L, C, Policy >
 the union of disjoint sets of \( [ 0 , n ) \), the roots joined by L, the paths shortened by C, the links of the roots told through Policy More...

Namespaces

namespace  ADS

Typedefs

using ADS::UnionFind = DisjointSets<>
 the union by size with path halving

Enumerations

enum class  ADS::Union { ADS::Union::SIZE , ADS::Union::RANK }
 how the roots of two trees are joined: the smaller tree under the root of the larger, or the lower rank under the higher More...
enum class  ADS::Compression { ADS::Compression::HALVING , ADS::Compression::FULL }
 how find() shortens the path: halving it (every other node on the way made a child of its grandparent, one pass), or compressing it (all the nodes on the way made children of the root, two passes) More...

Detailed description

The MFSET of section 10.2 engineered, beyond the book [see MFSet.h]: the forest of the parents in one vector of integers, where a root keeps minus the size of its tree instead of a parent, so that the union by size needs no second vector and a find() touches one array; find() halves the path (every other node on the way is made a child of its grandparent), which needs a single pass, no recursion and no stack, and keeps the bound of the compression with the union by size (Tarjan and van Leeuwen): a sequence of \( m \geq n \) operations on \( n \) elements takes.

\[ O( n + m \, \alpha( m , n ) ) , \]

\( \alpha \) the inverse of the function of Ackermann, at most 4 for any \( n \) that fits a computer; a tree of root \( r \) has at least \( 2^h \) nodes, \( h \) its height, so that a single find() is \( O( \log n ) \) in the worst case. The space is \( 4 n \) bytes. Patwary, Blair and Manne found the union by size or rank with halving among the fastest variants, close to the splicing of Rem, which the benchmark of this library finds slower up to \( 10^6 \) elements. It is the union-find of the observer of the spanning forests [see Reoptimization.h], while kruskal() takes any MFSET with merge().

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: merge() of two components sends Changed< Parent , int , int > with the handle \( x \) of the root made a child, from \( -1 \) to the root \( y \) of the other tree (after the link, undone if an observer throws), and reset() sends Rebuilt. Only the links of two roots are told: the shortening of the paths by find() is internal maintenance, which leaves every component as it was, so that an observer that keeps a parent for each element from these events alone holds a forest with the components of the structure, although not its shape.

The contracts [see Contract.h]: \( n \ge 0 \), the elements are in \( [ 0 , n ) \); the invariant is that the parents make a forest of count() trees whose roots hold minus their sizes. The operations are defined in UnionFind.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 10.2, https://algoritmicamente.it/materiali/libro/capitoli/cap10.pdf
R. E. Tarjan, J. van Leeuwen, Worst-case analysis of set union algorithms, Journal of the ACM 31(2), 245-281, 1984, doi:10.1145/62.2160
M. M. A. Patwary, J. Blair, F. Manne, Experiments on union-find algorithms for the disjoint-set data structure, Proceedings of SEA, LNCS 6049, 411-423, 2010, doi:10.1007/978-3-642-13193-6_35
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa