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.
absRawandabsdecode the abstractIndexedMinPQ.State n;InvisFibRawInvof the decoded state. The abstraction is a function, asFibRawInv.uniqueshows it must be.potentialisunit * (numRoots + 2 * numMarked), the textbook potential Cormen et al., Section 19.1 counted in record operations.isEmpty,getKeyandinsertare programs over the record, and their specifications (wp_isEmpty,wp_getKey,wp_insert) are stated withRecord.wp: the operations are admissible, the result and the decoded state are those ofIndexedMinPQ.step, the invariant is preserved, and the amortized cost is bounded.
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 #
Cell,Cell.ok,view: the record's cells and its reading asRawState n.absRaw,abs,Inv,potential: abstraction, invariant and potential.RootList: the root-list part ofFibRawInv.spliceAfter,addRoot,isEmpty,getKey,insert: record programs.
Main results #
view_update_key, …: writes as updates ofRawState n.absRaw_eq_of_fibRawInv: the invariant determines the abstract state.RootList.addRoot: adding a valid root tree keeps a valid root list.wp_isEmpty,wp_getKey,wp_insert: the first three operations.
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:
0for a root,p + 1for parentp. - child (v : ℕ) : Cell
Some child of a handle:
0for none,c + 1for childc. - 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:0for none,u + 1for rootu. - minRoot : Cell
The minimum root:
0for an empty heap,m + 1for rootm. - 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.
Programs over the heap's record.
Read a cell.
Writes as updates of the raw view #
Abstraction, invariant and potential #
The priority queue a raw state stores: present handles with their keys.
The priority queue a record stores.
The invariant determines the stored queue.
A record satisfies the invariant when its raw view satisfies FibRawInv of any queue.
Credits per root and per two marks, in record operations.
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.
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.
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 #
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.
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.