Documentation

CslibFrontier.DataStructure.DisjointSet.Tarjan

Inverse-Ackermann amortized step bounds for Union–Find #

This module is an abstract step-count step toward #3558 and #5441 (not a registered cited endpoint of [Tar75] or [CLRS09]): it proves inverse-Ackermann amortized upper bounds on the abstract findSteps/link traces of Cslib.DataStructure.DisjointSet.UnionFind.impl (Batteries.UnionFind with union by rank and full path compression) using Cslib.DataStructure.Amortized certificates over Execution.ofStep.

The rank-shifted potential potentialCore adapts the level/iteration potential from [CLRS09] Section 21.4 (pp. 573–581, Lemmas 21.11–21.13 and Theorem 21.14) using the + d rank shift (d := max 1 d ≥ 1). Each node i < uf.size receives a potential built from the level k = ackLevel (uf.rank i + d) (uf.rank (uf.parent i) + d) and iteration count i₀ = ackIter (uf.rank i + d) (uf.rank (uf.parent i) + d) in the (j + 1)-fold hierarchy Nat.ackermann so that ackermann k (uf.rank i + d) ≤ (ackermann k)^[i₀] (uf.rank i + d) ≤ uf.rank (uf.parent i) + d. Shifting node ranks by d ≥ 1 ensures 1 ≤ uf.rank i + d everywhere, avoiding separate rank-zero handling during find.

A single parameterized potential potentialCore zeroSingletons q d uf unifies two regimes:

  1. zeroSingletons = true, d = 1 (tarjanAmortized): initial potential at init n is 0, giving single-parameter amortized charges 2 * α(n) + 2 for Op.same, 5 * α(n) + 2 for Op.union, and 1 for Op.numComponents, hence total id traces ≤ m * (5 * α(n) + 2).
  2. zeroSingletons = false, d = m / n + 1 (tarjanAmortizedAt): initial potential at init n is n * (q * (m / n + 1)), while each link has amortized charge at most q. When n ≤ m, n * (m / n + 1) ≤ 2 * m, yielding the two-parameter step bound total id traces ≤ m * (5 * α(m, n) + 2) (tarjan_run_two_param) for every m, n, together with the linear 7 * m corollary once n * Nat.log 2 n ≤ m (tarjan_run_linear_of_dense).

Cost model and scope #

Like Cslib.DataStructure.DisjointSet.UnionFind, total id traces counts only the exact abstract forest steps recorded by UnionFind.step (findSteps parent-pointer inspections per find, 1 link step per Op.union, and 1 counter read per Op.numComponents), excluding the O(n) creation of init n. These theorems make no Word-RAM or pointer-machine instruction-cost claim (#3558, #5441). Moreover, Nat.inverseAckermannTwo m n uses shift d = m / n + 1 in the [CLRS09] recurrence rather than [Tar75]'s 4 * ⌈m / n⌉ threshold convention, matching Tarjan's bound only up to small constant shifts/factors and the omitted init n cost.

Main definitions #

Main results #

References #

Find-path traversal and path compression characterization #

Level, iteration index, and non-root potential #

Public potentials, amortized certificates, and client step bounds #

Potential function on Fin n used for the single-parameter O(α(n)) step analysis. Singletons carry zero potential, so tarjanPotential n _ (init n) = 0.

Per-operation single-parameter amortized charge on Fin n: 2 * α(n) + 2 for Op.same, 5 * α(n) + 2 for Op.union, and 1 for Op.numComponents.

Every disjoint-set operation has single-parameter charge at most 5 * α(n) + 2.

@[simp]

Initial single-parameter potential at init n is 0.

Single-parameter O(α(n)) amortized abstract step certificate over impl n.

Uniform single-parameter amortized abstract step certificate 5 * α(n) + 2 over impl n.

theorem Cslib.DataStructure.DisjointSet.UnionFind.tarjan_run_correct {n : ℕ} {β : Type} (client : (spec n).Client β) :
match (spec n).runClient (Partition.discrete n) client with | (value, finalState, history) => (run (init n) client).value = value ∧ Rep finalState (run (init n) client).state ∧ total id (run (init n) client).traces ≤ (spec n).totalCost (tarjanCharge n) (Partition.discrete n) history ∧ total id (run (init n) client).traces ≤ history.length * (5 * n.inverseAckermann + 2)

Running the concrete union-find interpreter from init n agrees with abstract Partition n semantics, preserves Rep, and satisfies both the exact per-operation and uniform 5 * α(n) + 2 abstract step bounds (excluding init n).

Rank-shifted potential function parameterized by d on Fin n used for the two-parameter O(m α(m, n)) abstract step analysis. Initial potential at init n is n * (inverseAckermannAt d n * max 1 d).

Per-operation amortized charge parameterized by d on Fin n: 2 * α_d(n) + 2 for Op.same, 3 * α_d(n) + 2 for Op.union, and 1 for Op.numComponents.

Every disjoint-set operation has rank-shifted charge at most 3 * α_d(n) + 2.

@[simp]

Initial rank-shifted potential at init n.

Rank-shifted amortized abstract step certificate over impl n.

Uniform rank-shifted amortized step certificate 3 * α_d(n) + 2 over impl n.

theorem Cslib.DataStructure.DisjointSet.UnionFind.tarjan_run_two_param {n : ℕ} {β : Type} (client : (spec n).Client β) :
match (spec n).runClient (Partition.discrete n) client with | (value, finalState, history) => (run (init n) client).value = value ∧ Rep finalState (run (init n) client).state ∧ total id (run (init n) client).traces ≤ history.length * (5 * history.length.inverseAckermannTwo n + 2)

Two-parameter inverse-Ackermann abstract step bound (step toward #3558 and #5441): for every universe size n and adaptive client on Fin n issuing m = history.length operations from init n, the concrete Batteries.UnionFind execution agrees with Partition n and records at most m * (5 * Nat.inverseAckermannTwo m n + 2) pointer/link steps (excluding init n).

Linear abstract step-count corollary of tarjan_run_two_param: whenever n * Nat.log 2 n ≤ m, every m-operation adaptive client on Fin n records at most 7 * m pointer/link steps (excluding init n).