Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.ExtractMin

Fibonacci-heap extractMin over a record of named cells #

extractMin n removes the minimum root z and consolidates Fredman and Tarjan Cormen et al., Section 19.2:

  1. scatterList … true walks z's children along their sibling list, makes each an unmarked root, and scatters it into the degree table cons. Scattering links the tree with the table's tree of equal degree (link, linkAs) until it reaches an empty slot.
  2. scatterList … false does the same for the other roots, walking the root list from z's right neighbour.
  3. gather scans the table up to the largest degree placed, moving each tree to a new root list with addRoot, and empties the table.

The largest degree placed is a program value, returned by scatter and threaded through the loops, so the record layout is the same as for the other operations.

The lemmas here only evaluate the programs: each wp_* lemma reads and writes cells, and cites the forest mathematics of Consolidation.lean for what the phase does to the forest and the table (LinksAs.linked, Placing.place, Placing.collide, Scattering.unmark, Scattering.step, Gathering.take, FibRawInv.detach, Detached.finish). The loop lemmas are inductions on the loops' fuel and counters.

Each link costs 14 operations and removes a tree; each scattered root costs at most 9 more; each table slot costs at most 13. With credit = 24 per root, the roots' credits pay for their scattering, and the amortized cost is at most 92 * (log₂ m + 1) on a queue of m handles (extractCharge), by the Fibonacci degree bound.

The module adapts Renfei Zhou's native formalization (WordRAM/FibonacciHeap, its Program.lean and Consolidate.lean) to the record level: his extraction and consolidation programs (extractMinCode, the scattering and gathering loops) and their forest and table reasoning become the programs and loop lemmas here, and his table invariant SlotInv and its lemmas are reused unchanged.

Main definitions #

Main results #

References #

Programs #

Make root x the newest child of root u.

Place root u in the consolidation table: while the slot of its degree holds a root, empty the slot, link the two roots and continue with the new root. Returns the largest degree placed, at least md. fuel bounds the number of links.

Scatter the k roots of a list linked by right from x. With prep, each is first made an unmarked root: these are the children of the extracted minimum.

Move the table's roots at degrees d, d + 1, … (k slots) to the root list.

Remove and return the minimum of a heap on n handles: detach the minimum root z, scatter its children and the other roots into the table, and gather the table into a new root list.

The amortized charge of extractMin on a queue with m handles.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_linkAs {n w : ℕ} {g : Cell → ℕ} {u x : Fin n} (hch : (view n g).deg u ≠ 0 → ∃ (c : Fin n) (r : Fin n), (view n g).child u = ↑c + 1 ∧ (view n g).right c = ↑r) (hdeg : (view n g).deg u + 1 ≤ n) (hfit : WordRAM.Fits w n) {Q : Unit → (Cell → ℕ) → ℕ → Prop} (hQ : ∀ (g' : Cell → ℕ), ∀ t ≤ 9, LinksAs (view n g) u x (view n g') → Q () g' t) :
Record.wp (Cell.ok n) w (linkAs ↑u ↑x) Q g

Making x the newest child of u evaluates to LinksAs in at most nine operations.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_scatter {n w sz : ℕ} {z : Fin n} (hfit : WordRAM.Fits w n) (fuel : ℕ) {g : Cell → ℕ} {T : WordRAM.FibonacciHeap.FHTree n} {slots : List (WordRAM.FibonacciHeap.FHTree n)} {md : ℕ} :
Placing sz z (view n g) T slots md → slots.length < fuel → Record.wp (Cell.ok n) w (scatter fuel (↑T.root) md) (fun (md' : ℕ) (g' : Cell → ℕ) (t : ℕ) => ∃ (slots' : List (WordRAM.FibonacciHeap.FHTree n)), Consolidated sz (view n g) (view n g') (WordRAM.FibonacciHeap.forestNodes (T :: slots)) slots' md' ∧ t + 14 * slots'.length ≤ 3 + 14 * (slots.length + 1)) g

Scattering one root. Placing a tree into the table consolidates its nodes and the table's. Each link costs 14 operations and removes a tree, so the cost is at most 3 plus 14 per tree removed.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_scatterList {n w sz : ℕ} {z : Fin n} (hfit : WordRAM.Fits w n) (prep : Bool) {fuel : ℕ} (hfuel : n < fuel) (k : ℕ) {g : Cell → ℕ} {x : ℕ} {slots rest : List (WordRAM.FibonacciHeap.FHTree n)} {md : ℕ} :
Scattering sz z prep (view n g) slots rest md → rest.length = k → (∀ y ∈ (List.map WordRAM.FibonacciHeap.FHTree.root rest).head?, x = ↑y) → Record.wp (Cell.ok n) w (scatterList fuel prep k x md) (fun (md' : ℕ) (g' : Cell → ℕ) (t : ℕ) => ∃ (slots' : List (WordRAM.FibonacciHeap.FHTree n)), Consolidated sz (view n g) (view n g') (WordRAM.FibonacciHeap.forestNodes (slots ++ rest)) slots' md' ∧ t + 14 * slots'.length ≤ (if prep = true then 9 else 4) * k + 14 * (slots.length + k)) g

Scattering a list of roots. Walking the trees rest along their roots' right pointers (each first made an unmarked root if prep) and scattering each consolidates them with the table. Apart from links, each root costs 4 operations, or 9 with prep.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_gather {n w sz : ℕ} (hfit : WordRAM.Fits w n) (k : ℕ) {g : Cell → ℕ} {d md : ℕ} {gs slots : List (WordRAM.FibonacciHeap.FHTree n)} :
Gathering sz (view n g) gs slots md → (∀ T ∈ slots, d ≤ T.deg) → d + k = md + 1 → Record.wp (Cell.ok n) w (gather k d) (fun (x : Unit) (g' : Cell → ℕ) (t : ℕ) => ∃ (gs' : List (WordRAM.FibonacciHeap.FHTree n)), Gathered (view n g) (view n g') (WordRAM.FibonacciHeap.forestNodes (gs ++ slots)) gs' ∧ gs'.length = gs.length + slots.length ∧ t ≤ 13 * k) g

Gathering the table. Scanning the slots d, …, md moves every tree of the table to the root list, leaving the table empty, in at most 13 operations per slot.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_extractMin {n w K : ℕ} {g : Cell → ℕ} (hI : Inv n K g) (hfit : WordRAM.Fits w n) :

Extracting the minimum. extractMin n implements IndexedMinPQ.step on .extractMin, preserves the invariant, and costs at most extractCharge of the queue's size, amortized.