nx_sketch_union_find.nx
buildroot/runtime/nx_sketch_union_find.nx
about
sketch_union_find.nx -- Disjoint Set Union (Union-Find) primitive.
Foundational data structure for connectivity / clustering. Maintains
a partition of {0, 1, ..., n-1} into disjoint sets. Operations:
find(x): which set is x in? (returns representative element)
union(x, y): merge x's set with y's set
connected(x, y): same set?
size_of(x): cardinality of x's set
OPTIMIZATIONS:
- Path compression: find(x) flattens the parent chain so subsequent
finds are O(1) on the same nodes.
- Union by size: smaller tree attaches under larger; keeps tree
height O(log n).
With both optimizations: nearly-O(1) amortized per operation
(technically O(α(n)) inverse-Ackermann; practically constant).
USE CASES:
- Kruskal's MST (connectivity tracking)
- online clustering (merge clusters as new edges arrive)
- dynamic connectivity (undirected only)
- compilation: equivalence classes for SSA, alias analysis
- image processing: connected components
LOSSLESS-LANGUAGE DISCIPLINE:
Production tier, exact semantics. No envelope returned --
operations are deterministic with O(α(n)) guarantee.
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 42 | struct UnionFind |
consts
| 39 | const NX_UF_MIN_N: i64 = 1 |
| 40 | const NX_UF_MAX_N: i64 = 100000000 // 100M |
functions
| 53 | func nx_uf_alloc(n: i64) -> *UnionFind calls 1: sys_mmap |
| 76 | func nx_uf_find(uf: *UnionFind, x: i64) -> i64 |
| 102 | func nx_uf_union(uf: *UnionFind, x: i64, y: i64) -> i64 calls 1: nx_uf_find |
| 124 | func nx_uf_connected(uf: *UnionFind, x: i64, y: i64) -> i64 calls 1: nx_uf_find |
| 135 | func nx_uf_size_of(uf: *UnionFind, x: i64) -> i64 |
| 141 | func nx_uf_n_sets(uf: *UnionFind) -> i64 |
| 149 | func nx_uf_query_size(uf: *UnionFind, x: i64) -> *ApproxI64 |
| 156 | func nx_uf_memory_bytes(uf: *UnionFind) -> i64 |