Documentation

CslibFrontier.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.Forest

Forest invariants and pure state transitions for a Fibonacci heap #

This file lifts the single-tree model of Trees.lean to the full multi-root forest invariant FibRawInv and proves the structural transitions required by all priority-queue operations:

Main definitions #

Main results #

References #

List surgery lemmas for child removal and replacement #

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.forestNodes_nodup_idx_disjoint {n : ℕ} {cs : List (FHTree n)} (hnd : (forestNodes cs).Nodup) {i j : ℕ} (hi : i < cs.length) (hj : j < cs.length) (hij : i ≠ j) {x : Fin n} (hxi : x ∈ cs[i].nodes) (hxj : x ∈ cs[j].nodes) :
theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.forestNodes_set_perm {n : ℕ} (cs : List (FHTree n)) (i : ℕ) (hi : i < cs.length) (T_cut c' : FHTree n) :
(T_cut.nodes ++ c'.nodes).Perm cs[i].nodes → (T_cut.nodes ++ forestNodes (cs.set i c')).Perm (forestNodes cs)

Parent witnesses and subtree cutting #

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.par_of_mem_children {n : ℕ} {st : RawState n} {pending : Option (Fin n)} {T : FHTree n} (hT : ValidSubtree st pending T) (x : Fin n) :
x ∈ forestNodes T.children → ∃ q ∈ T.nodes, st.par x = ↑q + 1

Every strict descendant x of T has st.par x = q.val + 1 for some q ∈ T.nodes.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.frame_root_mark {n : ℕ} {st st' : RawState n} {pending pending' : Option (Fin n)} {T : FHTree n} (hT : ValidSubtree st pending T) (hnd : T.nodes.Nodup) (hrm : st'.mark T.root ≤ 1) (hroot : st'.inHeap T.root = st.inHeap T.root ∧ st'.deg T.root = st.deg T.root ∧ st'.child T.root = st.child T.root ∧ st'.key T.root = st.key T.root) (hdesc_node : ∀ x ∈ forestNodes T.children, st'.inHeap x = st.inHeap x ∧ st'.mark x = st.mark x ∧ st'.deg x = st.deg x ∧ st'.child x = st.child x ∧ st'.key x = st.key x) (hdesc_ptr : ∀ x ∈ forestNodes T.children, st'.par x = st.par x ∧ st'.left x = st.left x ∧ st'.right x = st.right x ∧ (some x = pending' ↔ some x = pending)) :
ValidSubtree st' pending' T

Updating only the root's mark to a value ≤ 1 while keeping all other subtree fields fixed preserves ValidSubtree.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.exists_parent_info {n : ℕ} {st : RawState n} {pending : Option (Fin n)} {T : FHTree n} (hT : ValidSubtree st pending T) {cur p : Fin n} (hcur : cur ∈ forestNodes T.children) (hpar_cur : st.par cur = ↑p + 1) :
p ∈ T.nodes ∧ st.inHeap p ≠ 0 ∧ st.mark p ≤ 1 ∧ st.inHeap cur ≠ 0 ∧ st.mark cur ≤ 1 ∧ 1 ≤ st.deg p ∧ (2 ≤ st.deg p → ∃ (h : Fin n) (tail : List (Fin n)), st.child p = ↑h + 1 ∧ IsCircList st.left st.right (h :: tail) ∧ cur ∈ h :: tail ∧ 2 ≤ (h :: tail).length ∧ ∀ x ∈ h :: tail, st.par x = ↑p + 1)

Pre-cut structural properties of cur and its parent p inside any valid subtree containing cur as a strict descendant.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.exists_cut {n : ℕ} {st st' : RawState n} {pending : Option (Fin n)} {T : FHTree n} (hT : ValidSubtree st pending T) (hnd : T.nodes.Nodup) {cur p : Fin n} (hcur : cur ∈ forestNodes T.children) (hpar_cur : st.par cur = ↑p + 1) (hpend : pending = none ∨ pending = some cur) (hin_eq : ∀ x ∈ T.nodes, st'.inHeap x = st.inHeap x ∧ st'.key x = st.key x) (hmark' : st'.mark cur = 0 ∧ ∀ x ∈ T.nodes, x ≠ cur → st'.mark x = st.mark x) (hdeg' : st'.deg p + 1 = st.deg p ∧ ∀ x ∈ T.nodes, x ≠ p → st'.deg x = st.deg x) (hpar' : st'.par cur = 0 ∧ ∀ x ∈ T.nodes, x ≠ cur → st'.par x = st.par x) (hchild_p : (st.deg p = 1 → st'.child p = 0) ∧ ∀ (h : Fin n) (tail : List (Fin n)), 2 ≤ st.deg p → st.child p = ↑h + 1 → IsCircList st.left st.right (h :: tail) → cur ∈ h :: tail → ∃ (r : Fin n) (rest : List (Fin n)), st'.child p = ↑r + 1 ∧ IsCircList st'.left st'.right (r :: rest) ∧ (r :: rest).Perm ((h :: tail).erase cur)) (hchild_other : ∀ x ∈ T.nodes, x ≠ p → st'.child x = st.child x) (hlr_other : ∀ x ∈ T.nodes, st.par x ≠ ↑p + 1 → st.par x ≠ 0 → st'.left x = st.left x ∧ st'.right x = st.right x) :
∃ (T_cut : FHTree n) (T' : FHTree n), T_cut.root = cur ∧ T'.root = T.root ∧ p ∈ T'.nodes ∧ (T_cut.nodes ++ T'.nodes).Perm T.nodes ∧ ValidRoot st' none T_cut ∧ ValidSubtree st' (some p) T'

Cutting a strict descendant cur (with st.par cur = p.val + 1) from T splits T into a new root tree T_cut (T_cut.root = cur) and a pruned tree T' (T'.root = T.root, p ∈ T'.nodes) satisfying ValidRoot st' none T_cut and ValidSubtree st' (some p) T'.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.mark_pending {n : ℕ} {st st' : RawState n} {z : Fin n} {T : FHTree n} (hT : ValidSubtree st (some z) T) (hm0 : st.mark z = 0) (hm1 : st'.mark z = 1) (hm_other : ∀ x ∈ T.nodes, x ≠ z → st'.mark x = st.mark x) (hunch : ∀ x ∈ T.nodes, st'.inHeap x = st.inHeap x ∧ st'.deg x = st.deg x ∧ st'.child x = st.child x ∧ st'.key x = st.key x) (hptr : ∀ x ∈ forestNodes T.children, st'.par x = st.par x ∧ st'.left x = st.left x ∧ st'.right x = st.right x) :

Marking an unmarked pending = some z node (st.mark z = 0, st'.mark z = 1) absorbs the transient rank slack and restores ValidSubtree st' none T.

Full heap representation invariant and core consequences #

Full structural, key, counter, and root-list invariant relating s : IndexedMinPQ.State n to st : RawState n.

Transient forest invariant during decreaseKey v k while cascading cuts propagate from pending.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.FibRawInv.empty {n K_max : ℕ} {st : RawState n} (hin : ∀ (v : Fin n), st.inHeap v = 0) (hcons : ∀ d ≤ n, st.cons d = 0) (hmin : st.minRoot = 0) (hnr : st.numRoots = 0) (hnm : st.numMarked = 0) (hsz : st.sz = 0) :

FibRawInv holds on the empty priority queue whenever inHeap and cons[0..n] are zeroed and the four scalar counters are 0.

All scalar counters of a valid Fibonacci heap are bounded by n.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.FibRawInv.unique {n K_max : ℕ} {s₁ s₂ : IndexedMinPQ.State n} {st : RawState n} (h₁ : FibRawInv n K_max s₁ st) (h₂ : FibRawInv n K_max s₂ st) :
s₁ = s₂

Concrete state uniquely determines the represented IndexedMinPQ.State n.

When st.minRoot = 0, dom s = ∅ and IndexedMinPQ.findMin s = none.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.FibRawInv.findMin_eq_some {n K_max : ℕ} {s : IndexedMinPQ.State n} {st : RawState n} (h : FibRawInv n K_max s st) {z : Fin n} (hz : st.minRoot = ↑z + 1) :
IndexedMinPQ.findMin s = some (z, st.key z) ∧ s z = some (st.key z) ∧ st.key z ≤ K_max

When st.minRoot = z.val + 1, IndexedMinPQ.findMin s = some (z, st.key z).

Forest facts used by several operations #

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.descendant_ne_root_of_nodup {n : ℕ} {ts : List (FHTree n)} (hnd : (forestNodes ts).Nodup) {T R : FHTree n} (hT : T ∈ ts) (hR : R ∈ ts) {x : Fin n} (hx : x ∈ forestNodes T.children) :
x ≠ R.root

In any Nodup forest ts, no strict descendant x ∈ forestNodes T.children (T ∈ ts) can equal the root of any tree R ∈ ts.

A tree is rebuilt from its root and children.

Framing and pure-ℕ degree bounds during consolidation #

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidRoot.frame_tree {n : ℕ} {st st' : RawState n} {T : FHTree n} (hT : ValidRoot st none T) (hnode : ∀ u ∈ T.nodes, st'.inHeap u = st.inHeap u ∧ st'.mark u = st.mark u ∧ st'.deg u = st.deg u ∧ st'.child u = st.child u ∧ st'.key u = st.key u ∧ st'.par u = st.par u) (hlr : ∀ u ∈ forestNodes T.children, st'.left u = st.left u ∧ st'.right u = st.right u) :

Framing ValidRoot st none T across a state transition preserving node fields on T.nodes and pointer fields on strict descendants forestNodes T.children.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidRoot.frame_nodes {n : ℕ} {st st' : RawState n} {T : FHTree n} (hT : ValidRoot st none T) (h : ∀ u ∈ T.nodes, st'.inHeap u = st.inHeap u ∧ st'.mark u = st.mark u ∧ st'.deg u = st.deg u ∧ st'.child u = st.child u ∧ st'.key u = st.key u ∧ st'.par u = st.par u ∧ st'.left u = st.left u ∧ st'.right u = st.right u) :

Framing ValidRoot st none T when all eight vertex arrays agree on T.nodes.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.deg_bounds_of_extra {n sz : ℕ} {st : RawState n} {T : FHTree n} {z : Fin n} (hsz : T.size ≤ sz) (hT : ValidSubtree st none T) (hnd : (z :: T.nodes).Nodup) :
T.deg ≤ 2 * Nat.log 2 sz + 1 ∧ T.deg + 2 ≤ n

Every valid subtree T with T.size ≤ sz disjoint from at least one other handle z : Fin n satisfies both T.deg ≤ 2 * Nat.log 2 sz + 1 and T.deg + 2 ≤ n.

Consolidation table invariant SlotInv #

Invariant for the degree consolidation table st.cons holding slotTrees within 0 .. maxDeg, where sz bounds the total number of nodes being consolidated.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.empty {n sz : ℕ} {st : RawState n} (hcons : ∀ d ≤ n, st.cons d = 0) :
SlotInv n sz st [] 0

Initial empty consolidation table when ∀ d ≤ n, st.cons d = 0.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.frame {n sz maxDeg : ℕ} {st st' : RawState n} {slotTrees : List (FHTree n)} (hs : SlotInv n sz st slotTrees maxDeg) (hcons : st'.cons = st.cons) (hvalid : ∀ T ∈ slotTrees, ValidRoot st' none T) :
SlotInv n sz st' slotTrees maxDeg

Framing SlotInv across a transition preserving cons and ValidRoot on slotTrees.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.lookup {n sz maxDeg : ℕ} {st : RawState n} {slotTrees : List (FHTree n)} (hs : SlotInv n sz st slotTrees maxDeg) {d : ℕ} (hd : d ≤ n) :
(st.cons d = 0 ∧ ∀ T ∈ slotTrees, T.deg ≠ d) ∨ ∃ (i : ℕ) (hi : i < slotTrees.length), slotTrees[i].deg = d ∧ st.cons d = ↑slotTrees[i].root + 1 ∧ (slotTrees.eraseIdx i).length + 1 = slotTrees.length ∧ ∀ R ∈ slotTrees.eraseIdx i, R.deg ≠ d

Lookup in st.cons d (d ≤ n): either st.cons d = 0 and no tree in slotTrees has degree d, or a unique index i < slotTrees.length has (slotTrees[i]).deg = d and st.cons d = (slotTrees[i]).root.val + 1.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.step_collision {n sz maxDeg : ℕ} {st st' : RawState n} {slotTrees : List (FHTree n)} (hs : SlotInv n sz st slotTrees maxDeg) {i d : ℕ} (hi : i < slotTrees.length) (hdeg_i : slotTrees[i].deg = d) (_hd : d ≤ n) (hno_other : ∀ R ∈ slotTrees.eraseIdx i, R.deg ≠ d) (hcons' : st'.cons = Function.update st.cons d 0) (hvalid' : ∀ R ∈ slotTrees.eraseIdx i, ValidRoot st' none R) :
SlotInv n sz st' (slotTrees.eraseIdx i) maxDeg

Clearing slot d = (slotTrees[i]).deg (d ≤ n) removes slotTrees[i] from SlotInv.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.insert_empty {n sz maxDeg : ℕ} {st : RawState n} {slotTrees : List (FHTree n)} (hs : SlotInv n sz st slotTrees maxDeg) {T_cur : FHTree n} {d : ℕ} (_hd_n : d + 2 ≤ n) (hd_log : d ≤ 2 * Nat.log 2 sz + 1) (hT_deg : T_cur.deg = d) (hnone : ∀ R ∈ slotTrees, R.deg ≠ d) (hT_valid : ValidRoot st none T_cur) :
SlotInv n sz { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := Function.update st.cons d (↑T_cur.root + 1), minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } (T_cur :: slotTrees) (max maxDeg d)

Storing a consolidated tree T_cur of unoccupied degree d into st.cons d and updating maxDeg to max maxDeg d inserts T_cur into SlotInv.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.SlotInv.done_of_cleared {n sz maxDeg : ℕ} {st : RawState n} {slotTrees : List (FHTree n)} (hs : SlotInv n sz st slotTrees maxDeg) (hcleared : ∀ d < maxDeg + 1, ∀ T ∈ slotTrees, T.deg ≠ d) :
slotTrees = [] ∧ ∀ d ≤ n, st.cons d = 0

Once every degree < maxDeg + 1 has been cleared from SlotInv, slotTrees = [] and all entries st.cons d (d ≤ n) are 0.

Lemmas for decreaseKey #

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidRoot.par_eq_zero_iff_root {n : ℕ} {st : RawState n} {pending : Option (Fin n)} {T : FHTree n} (hT : ValidRoot st pending T) {u : Fin n} (hu : u ∈ T.nodes) :
st.par u = 0 ↔ u = T.root

Inside a valid root tree T, st.par u = 0 holds iff u = T.root.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.par_ne_self {n : ℕ} {st : RawState n} {pending : Option (Fin n)} {U : FHTree n} (hU : ValidSubtree st pending U) (hnd : U.nodes.Nodup) {v : Fin n} (hv : v ∈ forestNodes U.children) :
st.par v ≠ ↑v + 1

In a valid subtree with Nodup nodes, no strict descendant v is its own parent.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.ValidSubtree.lower_key_no_cut {n : ℕ} {st st' : RawState n} {T : FHTree n} (hT : ValidSubtree st none T) (hnd : T.nodes.Nodup) {v : Fin n} {k : ℕ} (hv : v ∈ T.nodes) (hle : k ≤ st.key v) (hk_v : st'.key v = k) (hk_other : ∀ x ∈ T.nodes, x ≠ v → st'.key x = st.key x) (hunch : ∀ x ∈ T.nodes, st'.inHeap x = st.inHeap x ∧ st'.mark x = st.mark x ∧ st'.deg x = st.deg x ∧ st'.child x = st.child x) (hptr : ∀ x ∈ forestNodes T.children, st'.par x = st.par x ∧ st'.left x = st.left x ∧ st'.right x = st.right x) (hord : v = T.root ∨ ∃ (p : Fin n), st.par v = ↑p + 1 ∧ ¬IndexedMinPQ.PrioLT (v, k) (p, st.key p)) :

Lowering key[v] to k ≤ st.key v preserves ValidSubtree st' none T whenever v = T.root or v has parent p (st.par v = p.val + 1) with ¬IndexedMinPQ.PrioLT (v, k) (p, st.key p).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap.CutInv.to_fibRawInv {n K_max : ℕ} {s : IndexedMinPQ.State n} {st st' : RawState n} {v m : Fin n} (h : CutInv n K_max s st v m none) (heq : st'.key = st.key ∧ st'.par = st.par ∧ st'.child = st.child ∧ st'.left = st.left ∧ st'.right = st.right ∧ st'.deg = st.deg ∧ st'.mark = st.mark ∧ st'.inHeap = st.inHeap ∧ st'.cons = st.cons ∧ st'.numRoots = st.numRoots ∧ st'.numMarked = st.numMarked ∧ st'.sz = st.sz) (hmin : IndexedMinPQ.PrioLT (v, st.key v) (m, st.key m) ∧ st'.minRoot = ↑v + 1 ∨ ¬IndexedMinPQ.PrioLT (v, st.key v) (m, st.key m) ∧ st'.minRoot = ↑m + 1) :
FibRawInv n K_max s st'

Once cascading cuts finish (pending = none) and minRoot is updated against (v, st.key v), CutInv upgrades back to FibRawInv.