sketch_union_find.nx
buildroot/runtime/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 · 1 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_union_find_test.nx
structs
| 36 | struct UnionFind { |
consts
| 33 | const NX_UF_MIN_N: i64 = 1 |
| 34 | const NX_UF_MAX_N: i64 = 100000000 // 100M |
functions
| 47 | func nx_uf_alloc(n: i64) -> *UnionFind {
called by 1: main |
| 70 | func nx_uf_find(uf: *UnionFind, x: i64) -> i64 { |
| 96 | func nx_uf_union(uf: *UnionFind, x: i64, y: i64) -> i64 { |
| 118 | func nx_uf_connected(uf: *UnionFind, x: i64, y: i64) -> i64 { |
| 129 | func nx_uf_size_of(uf: *UnionFind, x: i64) -> i64 { |
| 135 | func nx_uf_n_sets(uf: *UnionFind) -> i64 {
called by 1: main |
| 143 | func nx_uf_query_size(uf: *UnionFind, x: i64) -> *ApproxI64 { |
| 150 | func nx_uf_memory_bytes(uf: *UnionFind) -> i64 { |