code wiki / (root) / sketch_union_find.nx

sketch_union_find.nx

buildroot/runtime/sketch_union_find.nx

4802 B152 linesdepth 4pulls 4 transitivereach 1 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 · 1 importers

syscalls.nx sketch_types.nx sketch_union_find.nx sketch_union_find_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_union_find_test.nx

structs

36struct UnionFind {

consts

33const NX_UF_MIN_N: i64 = 1
34const NX_UF_MAX_N: i64 = 100000000 // 100M

functions

47func nx_uf_alloc(n: i64) -> *UnionFind {
called by 1: main
70func nx_uf_find(uf: *UnionFind, x: i64) -> i64 {
96func nx_uf_union(uf: *UnionFind, x: i64, y: i64) -> i64 {
called by 1: main calls 1: nx_uf_find
118func nx_uf_connected(uf: *UnionFind, x: i64, y: i64) -> i64 {
called by 1: main calls 1: nx_uf_find
129func nx_uf_size_of(uf: *UnionFind, x: i64) -> i64 {
called by 2: nx_uf_query_sizemain calls 1: nx_uf_find
135func nx_uf_n_sets(uf: *UnionFind) -> i64 {
called by 1: main
143func nx_uf_query_size(uf: *UnionFind, x: i64) -> *ApproxI64 {
called by 1: main calls 2: nx_uf_size_ofnx_approx_new
150func nx_uf_memory_bytes(uf: *UnionFind) -> i64 {