Representation-independent disjoint-set specification #
This module specifies a dynamic partition (disjoint-set / union-find abstract state machine) over
a finite universe Fin n in the Cslib.DataStructure framework.
Mathematical states are equivalence relations Partition n with decidable Boolean membership,
starting from the discrete partition Partition.discrete n of n singleton classes. The
interface (Op, spec) exposes:
Op.same x y: test whetherxandybelong to the same equivalence class;Op.union x y: merge the classes ofxandy, returningtrueiff two distinct classes merged;Op.numComponents: return the current number of equivalence classes.
Unlike concrete union-find forests, spec intentionally does not expose raw tree-root indices:
under union by rank, two histories reaching the same mathematical partition can choose different
canonical roots, so exposing root indices would leak internal forest history rather than abstract
partition state. Returning the effective-merge Boolean from Op.union gives single-pass edge
filters such as spanningForestEdges (the unweighted Kruskal / cycle-filtering loop, whose
returned edge list depends on observed answers while its issued operation sequence is fixed at one
Op.union per input edge), while clients such as connectIfNew branch on same so that the
issued operation sequence itself adapts to the current partition.
Main definitions #
Cslib.DataStructure.DisjointSet.Partition: decidable equivalence relations onFin n.Partition.toSetoid,Partition.discrete,Partition.classOf,Partition.components,Partition.numComponents,Partition.merge: setoid view, singleton partition, equivalence classes, quotient component set, component count, and binary class merge.Cslib.DataStructure.DisjointSet.Op,Cslib.DataStructure.DisjointSet.spec: typed disjoint-set operations andCslib.DataStructure.StateTransitionSpec.same,union,getNumComponents,connectIfNew,spanningForestEdges: primitive client operations, history-adaptive conditional union, and the single-pass spanning-forest edge filter.
Main results #
Partition.rel_merge_iff_eqvGen:p.merge x yis the exact equivalence closureRelation.EqvGengenerated bypand the pair(x, y).Partition.numComponents_discrete,Partition.numComponents_merge_of_rel,Partition.numComponents_merge_of_not_rel: exact component counts initially and across redundant versus effective merges.runClient_connectIfNew,runClient_spanningForestEdges_history: exact operation histories for history-adaptive conditional union and single-pass spanning-forest filtering.spanningForestEdges_sublist,spanningForestEdges_rel_iff_eqvGen,spanningForestEdges_numComponents_add_length,spanningForestEdges_idempotent_partition: correctness, connectivity preservation, and component-count conservation forspanningForestEdges.
References #
The discrete partition of Fin n into n singleton classes.
Instances For
The number of equivalence classes of p.
Instances For
Every partition of Fin n has at most n components.
Every partition of a nonempty Fin n has at least one component.
The discrete partition on Fin n has n components.
p.merge x y is the exact equivalence closure Relation.EqvGen of p together with
(x, y).
Typed operations on a disjoint-set partition over Fin n.
- same {n : ℕ} (x y : Fin n) : Op n
Test whether
xandybelong to the same equivalence class. - union {n : ℕ} (x y : Fin n) : Op n
Merge the classes of
xandy, returningtrueiff two distinct classes merged. - numComponents {n : ℕ} : Op n
Query the current number of equivalence classes.
Instances For
Instances For
Exact semantics and state-dependent operation history of connectIfNew.
Single-pass spanning-forest client (the cycle-filtering loop shared with Kruskal's algorithm)
parameterized over an arbitrary edge type E with endpoint map endpts : E → Fin n × Fin n: scan
edges in order, retaining an edge e iff union (endpts e).1 (endpts e).2 reports that its
endpoints lay in distinct components.
Instances For
Exact operation history issued by spanningForestEdges endpts es: one Op.union call per input
edge in order.
Exact conservation law: the final component count plus the number of accepted forest edges equals the initial component count.
The final partition after spanningForestEdges endpts es is the exact equivalence closure of
p together with all endpoint pairs in es.
Discarding cycle edges preserves connectivity: running spanningForestEdges on the retained
forest edges reaches the exact same partition as running it on es.