Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.Memory

The Fibonacci heap as an amortized refinement over memory #

The five operations of Basic.lean, DecreaseKey.lean and ExtractMin.lean implement the indexed min-priority queue indexedMinPQSpec n K over the heap's record (refinement), with amortized charges O(1) for every operation but extractMin, which is charged O(log m) on a queue of m handles (amortized). The potential is credit * (numRoots + 2 * numMarked) record operations.

Placing the record in memory at a base address (layout) and composing with Record.refinement gives the heap over Memory.spec w (memRefinement). The potentials stack across the composition (Refinement.Amortized.comp): every memory operation is one record operation, so the charges are the same in memory units (mem_amortized).

The heap reads and writes only its 9 n + 5 cells (footprint). It is the record's framed structure (Record.framed) with the heap's programs on top (Memory.Framed.comp), so it joins products with other structures in one memory (Memory.Framed.prod), and its potential, read through the record, is local to its footprint (potential_local). allocator allocates the record in zeroed cells, which hold the empty queue (framed_alloc), as a Memory.Allocator; its cost certificate is amortized_allocator.

Main definitions #

Main results #

The heap's operations as record programs.

The amortized charge of each operation, in record operations.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.wp_methods {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) {g : Cell → ℕ} (hI : Inv n K g) (o : IndexedMinPQ.Op n) (hv : IndexedMinPQ.valid K (abs n g) o) :
Record.wp (Cell.ok n) w (methods n w K o) (fun (a : (indexedMinPQSpec n K).Result o) (g' : Cell → ℕ) (t : ℕ) => IndexedMinPQ.step (abs n g) o = (abs n g', a) ∧ Inv n K g' ∧ t + potential n g' ≤ charge n (abs n g) o + potential n g) g

Every operation meets IndexedMinPQ.step, preserves the invariant, and costs at most its charge, amortized.

The Fibonacci heap over its record.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.amortized {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) :
(refinement hn hK).Amortized (fun (x : (Record.spec (Cell.ok n) w).State) (x_1 : (Record.spec (Cell.ok n) w).Op) => 1) (charge n) (potential n)

Amortized costs over the record: one unit per record operation, with potential credit * (numRoots + 2 * numMarked).

In memory #

The record at 9 n + 5 consecutive cells from base: the eight arrays, the table, and the four scalars.

The Fibonacci heap over memory, at base address base.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.mem_amortized {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) (base : ℕ) :
(memRefinement hn hK base).Amortized Memory.charge (charge n) fun (m : (Memory.spec w).State) => potential n ((layout n base).abs m) + 0

Amortized costs over memory. The potentials stack across the composition: every memory unit is one record operation.

A framed structure in memory #

The heap's cells: 9 n + 5 consecutive addresses from base.

Every cell of the heap lies in its footprint.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.layout_abs_congr {n base : ℕ} {s s' : Memory.State} (h : ∀ a ∈ footprint n base, s'.mem a = s.mem a) :
(layout n base).abs s' = (layout n base).abs s

The record stored in memory depends only on the footprint.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.lt_frontier_of_inv {n base : ℕ} {s : Memory.State} (hs : (layout n base).Inv s) {a : ℕ} (ha : a ∈ footprint n base) :

The heap's record is allocated once its last cell, the count sz, is.

The heap's record at base as a framed structure over memory.

The Fibonacci heap as a framed structure over memory: the heap's programs over its record, framed by the record's footprint, so it composes with other structures in one memory (Memory.Framed.prod).

The framed heap's footprint is its 9 n + 5 cells.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.framed_room {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) (base : ℕ) (s : Memory.State) :
(framed hn hK base).room s = 0

The framed heap reserves no room beyond its footprint.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.potential_local {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) (base : ℕ) :
(framed hn hK base).Local fun (m : Memory.State) => potential n ((layout n base).abs m) + 0

The framed heap's potential reads only its footprint, as Memory.Framed.amortized_prod needs.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.framed_amortized {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) (base : ℕ) :
(framed hn hK base).Amortized Memory.charge (charge n) fun (m : (Memory.spec w).State) => potential n ((layout n base).abs m) + 0

The framed heap's amortized costs are those of mem_amortized.

Allocation #

The all-zero record is the empty heap.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.framed_alloc {n w K : ℕ} (hn : WordRAM.Fits w n) (hK : WordRAM.Fits w K) (s : Memory.State) :
(framed hn hK s.frontier).inv { mem := WordRAM.clear s.mem s.frontier (9 * n + 5), frontier := s.frontier + (9 * n + 5) } ∧ (framed hn hK s.frontier).abs { mem := WordRAM.clear s.mem s.frontier (9 * n + 5), frontier := s.frontier + (9 * n + 5) } = IndexedMinPQ.empty n

Allocation of an empty heap: 9 n + 5 zeroed cells at the frontier, all arrays at once. The heap at the old frontier holds the empty queue.

Allocating the heap's record: 9 n + 5 zeroed cells at the frontier, the count sz last.

Allocating an empty heap: its record in 9 n + 5 zeroed cells at the frontier, with the heap's programs on top.

The allocated heap is empty.

The allocated heap's costs: the charges of charge in memory units, with no potential once allocated.