code wiki / (root) / nx_sketch_union_find.nx

nx_sketch_union_find.nx

buildroot/runtime/nx_sketch_union_find.nx

4907 B158 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_sketch_types.nx nx_sketch_union_find.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

42struct UnionFind

consts

39const NX_UF_MIN_N: i64 = 1
40const NX_UF_MAX_N: i64 = 100000000 // 100M

functions

53func nx_uf_alloc(n: i64) -> *UnionFind
calls 1: sys_mmap
76func nx_uf_find(uf: *UnionFind, x: i64) -> i64
102func nx_uf_union(uf: *UnionFind, x: i64, y: i64) -> i64
calls 1: nx_uf_find
124func nx_uf_connected(uf: *UnionFind, x: i64, y: i64) -> i64
calls 1: nx_uf_find
135func nx_uf_size_of(uf: *UnionFind, x: i64) -> i64
called by 1: nx_uf_query_size calls 1: nx_uf_find
141func nx_uf_n_sets(uf: *UnionFind) -> i64
149func nx_uf_query_size(uf: *UnionFind, x: i64) -> *ApproxI64
156func nx_uf_memory_bytes(uf: *UnionFind) -> i64