Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.Basic

A Fibonacci heap as a record of named cells #

This file sets up the Fibonacci heap Fredman and Tarjan of Cslib.DataStructure.PriorityQueue.WordRAM.FibonacciHeap as a program over a record of named cells (Record.spec), rather than as hand-stepped word-RAM code. The record has the same struct of arrays: per handle key, par, child, left, right, deg, mark and inHeap, a consolidation table cons, and four scalars. view reads a record as the existing RawState n, so the existing invariant FibRawInv and its forest lemmas apply unchanged.

The forest reasoning behind insert is RootList.addRoot, a statement about RawState n alone: adding a valid root tree to a valid root list. The program-level proofs evaluate the programs with simp and apply it.

The module adapts Renfei Zhou's native formalization (WordRAM/FibonacciHeap, in particular Program.lean and BasicOps.lean) to the record level: his programs insertCode and addRootAndUpdateMin become insert and addRoot, and their forest reasoning becomes FibRawInv.insert and RootList.addRoot.

Main definitions #

Main results #

References #

The record #

The cells of a Fibonacci heap: eight per-handle arrays, the consolidation table and four scalars. Indices are words; Cell.ok says which cells exist.

  • key (v : ℕ) : Cell

    The key of a handle.

  • par (v : ℕ) : Cell

    The parent of a handle: 0 for a root, p + 1 for parent p.

  • child (v : ℕ) : Cell

    Some child of a handle: 0 for none, c + 1 for child c.

  • left (v : ℕ) : Cell

    The predecessor of a handle in its circular sibling list.

  • right (v : ℕ) : Cell

    The successor of a handle in its circular sibling list.

  • deg (v : ℕ) : Cell

    The number of children of a handle.

  • mark (v : ℕ) : Cell

    The cascading-cut mark of a handle.

  • inHeap (v : ℕ) : Cell

    Whether a handle is present.

  • cons (d : ℕ) : Cell

    The consolidation table entry for degree d: 0 for none, u + 1 for root u.

  • minRoot : Cell

    The minimum root: 0 for an empty heap, m + 1 for root m.

  • numRoots : Cell

    The number of roots.

  • numMarked : Cell

    The number of marked handles.

  • sz : Cell

    The number of handles present.

The cells of a heap on n handles: array entries below n, table entries up to n, and the scalars.

Instances For

    The record as the existing raw view of a Fibonacci heap.

    @[reducible, inline]

    Programs over the heap's record.

    @[reducible, inline]

    Read a cell.

    @[reducible, inline]

    Write a cell.

    Writes as updates of the raw view #

    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_key {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.key ↑v) x) = have __src := view n g; { key := Function.update (view n g).key v x, 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 }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_par {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.par ↑v) x) = have __src := view n g; { key := __src.key, par := Function.update (view n g).par v x, 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 }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_child {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.child ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := Function.update (view n g).child v x, 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 }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_left {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.left ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := Function.update (view n g).left v x, 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 }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_right {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.right ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := __src.left, right := Function.update (view n g).right v x, deg := __src.deg, mark := __src.mark, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_deg {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.deg ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := Function.update (view n g).deg v x, mark := __src.mark, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_mark {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.mark ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := __src.deg, mark := Function.update (view n g).mark v x, inHeap := __src.inHeap, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_inHeap {n : ℕ} (g : Cell → ℕ) (x : ℕ) (v : Fin n) :
    view n (Function.update g (Cell.inHeap ↑v) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := __src.deg, mark := __src.mark, inHeap := Function.update (view n g).inHeap v x, cons := __src.cons, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_cons {n : ℕ} (g : Cell → ℕ) (x d : ℕ) :
    view n (Function.update g (Cell.cons d) x) = have __src := view n g; { key := __src.key, par := __src.par, child := __src.child, left := __src.left, right := __src.right, deg := __src.deg, mark := __src.mark, inHeap := __src.inHeap, cons := Function.update (view n g).cons d x, minRoot := __src.minRoot, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_minRoot {n : ℕ} (g : Cell → ℕ) (x : ℕ) :
    view n (Function.update g Cell.minRoot x) = have __src := view n g; { key := __src.key, 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 := x, numRoots := __src.numRoots, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_numRoots {n : ℕ} (g : Cell → ℕ) (x : ℕ) :
    view n (Function.update g Cell.numRoots x) = have __src := view n g; { key := __src.key, 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 := x, numMarked := __src.numMarked, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_numMarked {n : ℕ} (g : Cell → ℕ) (x : ℕ) :
    view n (Function.update g Cell.numMarked x) = have __src := view n g; { key := __src.key, 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 := x, sz := __src.sz }
    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.view_update_sz {n : ℕ} (g : Cell → ℕ) (x : ℕ) :
    view n (Function.update g Cell.sz x) = have __src := view n g; { key := __src.key, 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 := x }

    Abstraction, invariant and potential #

    The priority queue a raw state stores: present handles with their keys.

    @[reducible, inline]

    The priority queue a record stores.

    The implementation invariant: the existing Fibonacci-heap invariant, of the decoded queue.

    A record satisfies the invariant when its raw view satisfies FibRawInv of any queue.

    @[reducible, inline]

    Credits per root and per two marks, in record operations.

    The Fibonacci-heap potential credit * (numRoots + 2 * numMarked).

    The root list #

    The root-list part of FibRawInv: an empty heap has no minimum root; otherwise the roots form a circular list headed by the minimum root m.

    The effect of adding root u: on an empty heap, u forms the root list alone; otherwise u is spliced in after the minimum root m and becomes the minimum if it precedes m. Either way the number of roots grows by one.

    Adding a valid root tree to a forest of valid root trees with a valid root list keeps every tree a valid root and the root list valid. Only roots change their sibling pointers, and ValidSubtree reads sibling pointers only below the root.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.AddsRoot.exists_eq {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {u : Fin n} (h : AddsRoot st u st') :
    ∃ (left : Fin n → ℕ) (right : Fin n → ℕ) (minRoot : ℕ), st' = { key := st.key, par := st.par, child := st.child, left := left, right := right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := st.cons, minRoot := minRoot, numRoots := st.numRoots + 1, numMarked := st.numMarked, sz := st.sz }

    Adding a root changes only the root list and the number of roots.

    A new handle v with key k, as a childless unmarked root, before it joins the root list.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.FibRawInv.insert {n K : ℕ} {s : IndexedMinPQ.State n} {st st' : WordRAM.FibonacciHeap.RawState n} {v : Fin n} {k : ℕ} (h : WordRAM.FibonacciHeap.FibRawInv n K s st) (hv : s v = none) (hk : k ≤ K) (hadd : AddsRoot (newNode st v k) v st') :
    WordRAM.FibonacciHeap.FibRawInv n K (Function.update s v (some k)) { 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 := st'.cons, minRoot := st'.minRoot, numRoots := st'.numRoots, numMarked := st'.numMarked, sz := st'.sz + 1 }

    Insertion preserves the invariant. Creating v as a singleton tree and adding it to the root list stores v with key k, with one more root and one more handle.

    Programs #

    Splice u into a circular list right after h.

    Add root u to the root list, updating the minimum root and the number of roots.

    Whether the heap is empty: whether there is no minimum root.

    Insert handle v with key k as a new singleton root.

    Evaluating the programs #

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_addRoot {n w : ℕ} {g : Cell → ℕ} {u : Fin n} (hroot : (view n g).minRoot = 0 ∨ ∃ (m : Fin n) (rtail : List (Fin n)), (view n g).minRoot = ↑m + 1 ∧ WordRAM.FibonacciHeap.IsCircList (view n g).left (view n g).right (m :: rtail)) (hfit : WordRAM.Fits w n) (hnr : WordRAM.Fits w ((view n g).numRoots + 1)) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : ∀ (g' : Cell → ℕ), ∀ t ≤ 11, AddsRoot (view n g) u (view n g') → Q () g' t) :
    Record.wp (Cell.ok n) w (addRoot ↑u) Q g

    Adding a root evaluates to AddsRoot in at most eleven operations.

    The root list of a valid heap is empty or headed by the minimum root, as addRoot needs.

    With a handle absent, there is room for one more root and one more handle.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_isEmpty {n w K : ℕ} {g : Cell → ℕ} (hI : Inv n K g) :
    Record.wp (Cell.ok n) w isEmpty (fun (b : Bool) (g' : Cell → ℕ) (t : ℕ) => b = decide (IndexedMinPQ.dom (abs n g) = ∅) ∧ g' = g ∧ t = 1) g

    Emptiness. isEmpty reads the minimum root once.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_getKey {n w : ℕ} {g : Cell → ℕ} (v : Fin n) :
    Record.wp (Cell.ok n) w (getKey ↑v) (fun (a : Option ℕ) (g' : Cell → ℕ) (t : ℕ) => a = abs n g v ∧ g' = g ∧ t ≤ 2) g

    Key lookup. getKey v reads the presence flag and, if present, the key. No invariant is needed: the abstraction reads the same two cells.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_insert {n w K : ℕ} {g : Cell → ℕ} {v : Fin n} {k : ℕ} (hI : Inv n K g) (hv : abs n g v = none) (hk : k ≤ K) (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) :
    Record.wp (Cell.ok n) w (insert (↑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' ≤ 19 + credit + potential n g) g

    Insertion. insert v k stores v with key k, preserves the invariant, and costs at most 19 operations plus one credit for the new root.