Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.DecreaseKey

Fibonacci-heap decreaseKey over a record of named cells #

decreaseKey n v k lowers the key of v. If v would precede its parent, cut moves v to the root list, and cascade walks up the ancestors: a marked ancestor is cut in turn, the first unmarked one is marked Fredman and Tarjan Cormen et al., Section 19.3. Then setKey stores the key and makes v the minimum root if it precedes it. Cutting before storing the key keeps every tree valid throughout. (The hand-stepped decreaseKeyCode stores the key first; its proof reads the forest with the old keys, through CutsNode.withKey.)

The proof splits as in Basic.lean:

Each cascading cut costs at most 22 record operations and turns a marked non-root into an unmarked root, which releases credit = 24, so the cascade is paid by the potential and decreaseKey costs at most 104 operations amortized.

The module adapts Renfei Zhou's native formalization (WordRAM/FibonacciHeap, its Cut.lean) to the record level: his cut (cutNodeCode_raw and its forest transition, with its circular-list uniqueness argument) and cascading-cut development become cut, CutsNode.cascading, IsCircList.tail_unique and cascade, and his forest lemmas are reused unchanged. The hand-stepped proofs in turn now cite these lemmas.

Main definitions #

Main results #

References #

Programs #

Make the unlinked node z an unmarked root after the minimum root.

Cut z from its parent p: unlink z from p's child list, splice it into the root list after the minimum root, and clear its parent and mark.

Cascading cuts from z, which has just lost a child, with at most fuel cuts: stop at a root; mark an unmarked node and stop; cut a marked node and continue with its parent.

Store the lowered key k of v, and make v the minimum root if it precedes it.

Lower the key of v to k on a heap of n handles: if v now precedes its parent, cut it and cascade from the parent; then store the key and update the minimum root.

The effect of cutting cur from its parent p when the root list is m :: rtail: the facts of the hand-stepped cutNodeCode_raw, on the raw view.

The state after a cut, given the sibling pointers left₁, right₁ and p's child pointer child₁ after unlinking cur, and the old right neighbour r' of the minimum root m.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.ChainLinks.tail_unique {n : ℕ} {left right : Fin n → ℕ} {first prev : Fin n} {t₁ t₂ : List (Fin n)} :
(first :: t₁).Nodup → (first :: t₂).Nodup → WordRAM.FibonacciHeap.ChainLinks left right first prev t₁ → WordRAM.FibonacciHeap.ChainLinks left right first prev t₂ → t₁ = t₂

A circular list is determined by its head: from first, the right pointers trace it.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.IsCircList.tail_unique {n : ℕ} {left right : Fin n → ℕ} {h : Fin n} {t₁ t₂ : List (Fin n)} (h₁ : WordRAM.FibonacciHeap.IsCircList left right (h :: t₁)) (h₂ : WordRAM.FibonacciHeap.IsCircList left right (h :: t₂)) :
t₁ = t₂

A circular list is determined by its head.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.cutsNode_cutState {n : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {cur p m : Fin n} {rtail : List (Fin n)} (hdp_pos : 1 ≤ st.deg p) (hdp2 : 2 ≤ st.deg p → ∃ (h : Fin n) (tail : List (Fin n)), st.child p = ↑h + 1 ∧ WordRAM.FibonacciHeap.IsCircList st.left st.right (h :: tail) ∧ cur ∈ h :: tail ∧ 2 ≤ (h :: tail).length ∧ ∀ u ∈ h :: tail, st.par u = ↑p + 1) (hcirc_r : WordRAM.FibonacciHeap.IsCircList st.left st.right (m :: rtail)) (hroots_par0 : ∀ u ∈ m :: rtail, st.par u = 0) (hpar_cur : st.par cur = ↑p + 1) {left₁ right₁ : Fin n → ℕ} {child₁ : ℕ} (hunlink : st.deg p = 1 ∧ child₁ = 0 ∧ left₁ = st.left ∧ right₁ = st.right ∨ 2 ≤ st.deg p ∧ ∃ (r : Fin n) (l : Fin n), st.right cur = ↑r ∧ st.left cur = ↑l ∧ child₁ = ↑r + 1 ∧ left₁ = Function.update st.left r ↑l ∧ right₁ = Function.update st.right l ↑r) {r' : Fin n} (hr' : right₁ m = ↑r') :
CutsNode st cur p m rtail (cutState st cur p m r' left₁ right₁ child₁)

The effect of a cut, from p's child pointer and the sibling pointers after unlinking cur (hunlink) and the old right neighbour r' of the minimum root m.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.CutsNode.withKey {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {cur p m : Fin n} {rtail : List (Fin n)} (h : CutsNode st cur p m rtail st') (key : Fin n → ℕ) :
CutsNode { key := key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := st.cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } cur p m rtail { key := key, par := st'.par, child := st'.child, left := st'.left, right := st'.right, deg := st'.deg, mark := st'.mark, inHeap := st'.inHeap, cons := st'.cons, minRoot := st'.minRoot, numRoots := st'.numRoots, numMarked := st'.numMarked, sz := st'.sz }

A cut does not read or write keys: it has the same effect on states with other keys.

Forest mathematics of cascading cuts #

The forest during cascading cuts from v: FibRawInv with one pending ancestor that may have lost a child, and with the minimum-root comparison waived for v. With v a root, this is the existing CutInv.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Cascading.cutInv {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {v m : Fin n} {pending : Option (Fin n)} {ts : List (WordRAM.FibonacciHeap.FHTree n)} (h : Cascading n K s st v m pending ts) (hv : ∃ T ∈ ts, T.root = v) :
WordRAM.FibonacciHeap.CutInv n K s st v m pending

With v a root, Cascading is the existing CutInv.

A valid heap is Cascading with nothing pending.

In a valid subtree with distinct nodes, every node has fewer children than the tree has nodes.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.CutsNode.cascading {n K : ℕ} {s : IndexedMinPQ.State n} {st st' : WordRAM.FibonacciHeap.RawState n} {cur p v m : Fin n} {pending : Option (Fin n)} {ts : List (WordRAM.FibonacciHeap.FHTree n)} {rtail : List (Fin n)} (h : Cascading n K s st v m pending ts) (hperm_r : (m :: rtail).Perm (List.map WordRAM.FibonacciHeap.FHTree.root ts)) (hpend : pending = none ∨ pending = some cur) (hv_root : cur = v ∨ ∃ T ∈ ts, T.root = v) (hcur_ne_v : ∀ T ∈ ts, cur ∈ T.nodes → T.root ≠ v) (hcur : cur ∈ WordRAM.FibonacciHeap.forestNodes ts) (hpar_cur : st.par cur = ↑p + 1) (hcut : CutsNode st cur p m rtail st') :
∃ (ts' : List (WordRAM.FibonacciHeap.FHTree n)), Cascading n K s st' v m (some p) ts' ∧ (∃ T ∈ ts', T.root = v) ∧ (∀ T ∈ ts', p ∈ T.nodes → T.root ≠ v) ∧ p ∈ WordRAM.FibonacciHeap.forestNodes ts'

A cut keeps the forest Cascading, with the parent p now pending. The cut node cur becomes the root of a new tree; its old tree loses that subtree.

A node of the forest with a parent p is a strict descendant of its tree.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Cascading.cut_pre {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {v m : Fin n} {pending : Option (Fin n)} {ts : List (WordRAM.FibonacciHeap.FHTree n)} {rtail : List (Fin n)} (h : Cascading n K s st v m pending ts) (hperm_r : (m :: rtail).Perm (List.map WordRAM.FibonacciHeap.FHTree.root ts)) {cur p : Fin n} (hcur : cur ∈ WordRAM.FibonacciHeap.forestNodes ts) (hpar_cur : st.par cur = ↑p + 1) :
1 ≤ st.deg p ∧ (2 ≤ st.deg p → ∃ (h : Fin n) (tail : List (Fin n)), st.child p = ↑h + 1 ∧ WordRAM.FibonacciHeap.IsCircList st.left st.right (h :: tail) ∧ cur ∈ h :: tail ∧ 2 ≤ (h :: tail).length ∧ ∀ u ∈ h :: tail, st.par u = ↑p + 1) ∧ st.deg p ≤ n ∧ (∀ u ∈ m :: rtail, st.par u = 0) ∧ st.numRoots + 1 ≤ n ∧ st.numMarked ≤ n

The preconditions of a cut of a non-root cur from its parent p, as wp_cut takes them.

The cascade stops at a root: a pending root has lost nothing that matters.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Cascading.mark {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {v m z : Fin n} {ts : List (WordRAM.FibonacciHeap.FHTree n)} (h : Cascading n K s st v m (some z) ts) (hz : z ∈ WordRAM.FibonacciHeap.forestNodes ts) (hpz : st.par z ≠ 0) (hmz : st.mark z = 0) :
Cascading n K s { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := Function.update st.mark z 1, inHeap := st.inHeap, cons := st.cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked + 1, sz := st.sz } v m none ts

The cascade stops at an unmarked node, which it marks.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Cascading.lowerRootKey {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {v m : Fin n} {ts : List (WordRAM.FibonacciHeap.FHTree n)} (h : Cascading n K s st v m none ts) (hv : ∃ T ∈ ts, T.root = v) {k₀ k : ℕ} (hsv : s v = some k₀) (hk₀ : k ≤ k₀) (hK : k ≤ K) :
WordRAM.FibonacciHeap.CutInv n K (Function.update s v (some k)) { key := Function.update st.key v k, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := st.cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } v m none

Lowering the key of the root v keeps every tree valid, and with v a root the forest satisfies the existing CutInv, ready for the minimum-root update.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.FibRawInv.lowerKey_noCut {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {v p : Fin n} {k₀ k : ℕ} (h : WordRAM.FibonacciHeap.FibRawInv n K s st) (hsv : s v = some k₀) (hk₀ : k ≤ k₀) (hK : k ≤ K) (hpv : st.par v = ↑p + 1) (hord : ¬IndexedMinPQ.PrioLT (v, k) (p, st.key p)) :
WordRAM.FibonacciHeap.FibRawInv n K (Function.update s v (some k)) { key := Function.update st.key v k, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := st.cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } ∧ ∀ (m : Fin n), st.minRoot = ↑m + 1 → ¬IndexedMinPQ.PrioLT (v, k) (m, Function.update st.key v k m)

Lowering a key without a cut. If v keeps its heap order with its parent p, the heap stays valid, and v cannot precede the minimum root.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_spliceAfter {n w : ℕ} {g : Cell → ℕ} {h u r : Fin n} (hr : (view n g).right h = ↑r) (hfit : WordRAM.Fits w n) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : Q () (Function.update (Function.update (Function.update (Function.update g (Cell.left ↑u) ↑h) (Cell.left ↑r) ↑u) (Cell.right ↑u) ↑r) (Cell.right ↑h) ↑u) 5) :
Record.wp (Cell.ok n) w (spliceAfter ↑h ↑u) Q g

Splicing u after h, whose right neighbour is r, makes four writes.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_toRoot {n w : ℕ} {g : Cell → ℕ} {cur m r' : Fin n} (hm : (view n g).minRoot = ↑m + 1) (hr' : (view n g).right m = ↑r') (hfit : WordRAM.Fits w n) (hnr : (view n g).numRoots + 1 ≤ n) (hnm : (view n g).numMarked ≤ n) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : ∀ (g' : Cell → ℕ), (view n g' = have __src := view n g; { key := __src.key, par := Function.update (view n g).par cur 0, child := __src.child, left := Function.update (Function.update (view n g).left cur ↑m) r' ↑cur, right := Function.update (Function.update (view n g).right cur ↑r') m ↑cur, deg := __src.deg, mark := Function.update (view n g).mark cur 0, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := (view n g).numRoots + 1, numMarked := (view n g).numMarked - (view n g).mark cur, sz := __src.sz }) → Q () g' 13) :
Record.wp (Cell.ok n) w (toRoot ↑cur) Q g

Moving the unlinked cur to the root list after the minimum root m, whose right neighbour is r', makes thirteen operations.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_cut {n w : ℕ} {g : Cell → ℕ} {cur p m : Fin n} {rtail : List (Fin n)} (hdp_pos : 1 ≤ (view n g).deg p) (hdp2 : 2 ≤ (view n g).deg p → ∃ (h : Fin n) (tail : List (Fin n)), (view n g).child p = ↑h + 1 ∧ WordRAM.FibonacciHeap.IsCircList (view n g).left (view n g).right (h :: tail) ∧ cur ∈ h :: tail ∧ 2 ≤ (h :: tail).length ∧ ∀ u ∈ h :: tail, (view n g).par u = ↑p + 1) (hdp_le : (view n g).deg p ≤ n) (hmin : (view n g).minRoot = ↑m + 1) (hcirc_r : WordRAM.FibonacciHeap.IsCircList (view n g).left (view n g).right (m :: rtail)) (hroots_par0 : ∀ u ∈ m :: rtail, (view n g).par u = 0) (hpar_cur : (view n g).par cur = ↑p + 1) (hfit : WordRAM.Fits w n) (hnr : (view n g).numRoots + 1 ≤ n) (hnm : (view n g).numMarked ≤ n) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : ∀ (g' : Cell → ℕ), ∀ t ≤ 20, CutsNode (view n g) cur p m rtail (view n g') → Q () g' t) :
Record.wp (Cell.ok n) w (cut ↑cur ↑p) Q g

One cut. Cutting cur from its parent p evaluates to CutsNode in at most 20 operations. The hypotheses are those of the hand-stepped cutNodeCode_raw.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_cascade {n w K : ℕ} {s : IndexedMinPQ.State n} {v m : Fin n} (hfit : WordRAM.Fits w n) (fuel : ℕ) {g : Cell → ℕ} {z : Fin n} {ts : List (WordRAM.FibonacciHeap.FHTree n)} :
Cascading n K s (view n g) v m (some z) ts → (∃ T ∈ ts, T.root = v) → (∀ T ∈ ts, z ∈ T.nodes → T.root ≠ v) → z ∈ WordRAM.FibonacciHeap.forestNodes ts → (view n g).numMarked < fuel → Record.wp (Cell.ok n) w (cascade fuel ↑z) (fun (x : Unit) (g' : Cell → ℕ) (t : ℕ) => ∃ (ts' : List (WordRAM.FibonacciHeap.FHTree n)), Cascading n K s (view n g') v m none ts' ∧ (∃ T ∈ ts', T.root = v) ∧ t + potential n g' ≤ 53 + potential n g) g

Cascading cuts. From a forest with z pending, the cascade leaves a forest with nothing pending. Each cut costs at most 22 operations and releases one credit (one more root, one mark less), so the whole cascade costs at most 53 operations plus the potential it releases.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_setKey {n w K : ℕ} {s' : IndexedMinPQ.State n} {g : Cell → ℕ} {v m : Fin n} {k : ℕ} (hm : (view n g).minRoot = ↑m + 1) (hk : WordRAM.Fits w k) (hfit : WordRAM.Fits w n) (hpost : WordRAM.FibonacciHeap.CutInv n K s' (have __src := view n g; { key := Function.update (view n g).key v k, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := __src.deg, mark := __src.mark, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }) v m none ∨ WordRAM.FibonacciHeap.FibRawInv n K s' (have __src := view n g; { key := Function.update (view n g).key v k, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := __src.deg, mark := __src.mark, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }) ∧ ¬IndexedMinPQ.PrioLT (v, k) (m, Function.update (view n g).key v k m)) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : ∀ (g' : Cell → ℕ), ∀ t ≤ 4, WordRAM.FibonacciHeap.FibRawInv n K s' (view n g') → (view n g').numRoots = (view n g).numRoots → (view n g').numMarked = (view n g).numMarked → Q () g' t) :
Record.wp (Cell.ok n) w (setKey (↑v) k) Q g

Storing the key. With the forest ready (v a root, or v not preceding the minimum root), storing the key and updating the minimum root restores FibRawInv.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_decreaseKey {n w K : ℕ} {g : Cell → ℕ} {v : Fin n} {k₀ k : ℕ} (hI : Inv n K g) (hsv : abs n g v = some k₀) (hk₀ : k ≤ k₀) (hK : k ≤ K) (hfit : WordRAM.Fits w n) (hKfit : WordRAM.Fits w K) :
Record.wp (Cell.ok n) w (decreaseKey n (↑v) k) (fun (x : Unit) (g' : Cell → ℕ) (t : ℕ) => abs n g' = Function.update (abs n g) v (some k) ∧ Inv n K g' ∧ t + potential n g' ≤ 104 + potential n g) g

Decreasing a key. decreaseKey n v k lowers the key of the present handle v to k, preserves the invariant, and costs at most 104 operations amortized.