Clustering
C++20 header-only: DBSCAN, HDBSCAN, k-means.
Loading...
Searching...
No Matches
clustering::UnionFind< Idx > Class Template Reference

Disjoint-set-union with iterative path compression and union-by-rank. More...

#include <clustering/math/dsu.h>

Public Member Functions

 UnionFind (std::size_t n)
 Construct n singleton components numbered [0, n).
Idx find (Idx x) noexcept
 Root of the component containing x, with path compression applied.
bool unite (Idx a, Idx b) noexcept
 Merge the components containing a and b.
bool sameComponent (Idx a, Idx b) noexcept
 Whether a and b share a component.
std::size_t countComponents () const noexcept
 Current number of distinct components.
std::size_t size () const noexcept
 Total number of elements under management (fixed at construction).
std::size_t componentSize (Idx root) const noexcept
 Population of the component whose root is root.

Detailed Description

template<class Idx = std::uint32_t>
class clustering::UnionFind< Idx >

Disjoint-set-union with iterative path compression and union-by-rank.

Stores n elements initially as singleton components. find compacts the path from x to its root in two passes so recursion depth never grows with n – the recursive one-pass form blows the default stack at n = 1M. unite merges components by attaching the shorter tree under the taller, keeping worst-case tree depth at O(log n) even without compression.

Template Parameters
IdxUnsigned integer index type; defaults to uint32_t. Use uint64_t only when n may exceed 2^32.

Definition at line 22 of file dsu.h.

Constructor & Destructor Documentation

◆ UnionFind()

template<class Idx = std::uint32_t>
clustering::UnionFind< Idx >::UnionFind ( std::size_t n)
inlineexplicit

Construct n singleton components numbered [0, n).

Parameters
nElement count; each element starts as its own parent with rank 0 and size 1.

Definition at line 31 of file dsu.h.

Member Function Documentation

◆ componentSize()

template<class Idx = std::uint32_t>
std::size_t clustering::UnionFind< Idx >::componentSize ( Idx root) const
inlinenodiscardnoexcept

Population of the component whose root is root.

The caller must pass a root index (typically obtained from find); passing a non-root is undefined by contract. Size is maintained at the root on every unite – when trees merge, the winning root accumulates the losing root's size so the figure stays accurate without a tree walk at query time.

Parameters
rootRoot index as returned by find.
Returns
Number of elements under root's component.

Definition at line 132 of file dsu.h.

◆ countComponents()

template<class Idx = std::uint32_t>
std::size_t clustering::UnionFind< Idx >::countComponents ( ) const
inlinenodiscardnoexcept

Current number of distinct components.

Starts at n and decreases by 1 on each successful unite.

Returns
Component count.

Definition at line 112 of file dsu.h.

◆ find()

template<class Idx = std::uint32_t>
Idx clustering::UnionFind< Idx >::find ( Idx x)
inlinenoexcept

Root of the component containing x, with path compression applied.

Two-pass iterative compression: first walk to the root, then rewalk from x setting each visited node's parent to the root. The iterative form survives n = 1M inputs where the recursive form would overflow the stack.

Parameters
xElement index; must satisfy x < size().
Returns
Root index of x's component.

Definition at line 47 of file dsu.h.

◆ sameComponent()

template<class Idx = std::uint32_t>
bool clustering::UnionFind< Idx >::sameComponent ( Idx a,
Idx b )
inlinenoexcept

Whether a and b share a component.

Convenience wrapper over find(a) == find(b); applies path compression on both paths.

Parameters
aFirst element index.
bSecond element index.
Returns
true iff a and b have the same root.

Definition at line 103 of file dsu.h.

◆ size()

template<class Idx = std::uint32_t>
std::size_t clustering::UnionFind< Idx >::size ( ) const
inlinenodiscardnoexcept

Total number of elements under management (fixed at construction).

Returns
The n passed to the constructor.

Definition at line 119 of file dsu.h.

◆ unite()

template<class Idx = std::uint32_t>
bool clustering::UnionFind< Idx >::unite ( Idx a,
Idx b )
inlinenoexcept

Merge the components containing a and b.

Attaches the shorter tree under the taller; when the two ranks tie, a's root becomes the new root and its rank is bumped. No-op if a and b are already in the same component.

Parameters
aFirst element index.
bSecond element index.
Returns
true if the call actually merged two distinct components, false if they were already joined.

Definition at line 73 of file dsu.h.


The documentation for this class was generated from the following file:
  • include/clustering/math/dsu.h