An indexed binary heap over the memory oracle #
The indexed min-priority queue indexedMinPQSpec n K is implemented as a binary heap by programs
over Memory.spec w. The storage is a Layout: a one-cell size field and three arrays from
base, exactly as in BinaryHeap.Model: the key of each handle, the heap of handles at
positions 1, …, size, and the position of each handle (0 when absent). The abstraction
function is the model's stateOfMem, and the invariant is the model's HeapInv at the stored
size. Entries are ordered by (key, handle), so extractMin returns exactly the spec's
findMin.
Sift-up and sift-down are recursive memory programs (siftUp, siftDown). Their proofs are
inductions on the recursion, one level per call: the order lemmas of BinaryHeap.Order move the
single order defect one level, and the memory steps are discharged by the rules of
Memory.Triple. A level costs at most 8 memory units on the way up and 10 on the way down, so
every operation costs at most 10 ⌊log₂ n⌋ + 14 memory units (amortized). No register, address
arithmetic beyond the layout, or instruction count appears; word fit enters only through the
admissibility of Memory.spec w and two static facts of the layout, that n and the largest key
fit a word.
The methods write only the layout's cells and never allocate (methods_frame), and the
abstraction and invariant read only those cells (abs_congr, Inv.congr), so the heap is framed
by its layout (framed) and composes with other structures in the same memory
(Memory.Framed.prod). Allocated in memory that is zero above the frontier, the heap starts empty
at a cost of two memory units (allocLayout_bind, inv_allocated).
BinaryHeap.Native realizes these same memory programs as one fixed word-RAM program, with
their exact source state and history preserved and native arithmetic and control charged.
Main definitions #
Layout,Layout.footprint,Layout.Valid: the size cell and the three arrays, the cells they own, and their static well-formedness.swap,siftUp,siftDown: heap repair as memory programs.isEmpty,getKey,insert,decreaseKey,extractMin,methods: the operations.abs,Inv: the abstraction function and the implementation invariant.opCost: the memory cost of one operation,10 ⌊log₂ n⌋ + 14.refinement,framed: the binary heap as aRefinementofindexedMinPQSpec n K, framed by its layout.allocLayout,allocated: allocate an empty heap, and the layout it hands out.
Main results #
siftUp_triple,siftDown_triple: sift-up and sift-down repair one order defect within8 ⌊log₂ k⌋ + 4and10 (⌊log₂ S⌋ - ⌊log₂ k⌋) + 6memory units.methods_triple: each operation realizes the specified step withinopCost n.amortized: every operation costs at mostopCost nmemory units;opCost_lebounds it by14 (⌊log₂ n⌋ + 1).methods_frame,abs_congr,Inv.congr: the frame of a heap.allocLayout_bind,valid_allocated,inv_allocated: allocation yields an empty heap.
References #
The storage of an indexed binary heap on the handles Fin n: a one-cell field holding the
number of entries, and three arrays from base, laid out as in BinaryHeap.Model.
- size : WordRAM.Block
One cell holding the number of entries.
- base : ℕ
The start of the key, heap and position arrays.
The key of each handle, from base.
The handle at each heap position 1, …, size, from base + n; position 0 is unused.
The heap position of each handle, 0 when the handle is absent, from base + 2 n + 1.
The cells of the three arrays.
Static well-formedness at word size w for keys at most K: the size field is one cell
outside the touched array cells, and n and K fit a word. Heap position zero is unused and may
hold that field without enlarging the array block.
The size field is one cell.
The size cell is outside the arrays, or at their unused heap position zero.
- n_fits : WordRAM.Fits w n
The number of handles fits a word, hence so does every handle and position.
- key_fits : WordRAM.Fits w K
The largest key fits a word.
A specialization of the existing layout using exactly space n cells. The size occupies
the unused heap position zero; the key, positive heap positions and position array do not move.
The compact layout owns precisely the original three-array block.
Compact storage adds no extra word or width requirement.
The programs #
Swap the handles va at heap position a and vb at heap position b.
Sift-up from heap position k: while the entry at k has higher priority than its parent,
swap them and continue from the parent.
One step of sift-down: if the child entry (vc, kc) at position c has higher priority than
the entry (vk, kk) at position k, swap them and continue with next.
Sift-down from heap position k in a heap of size S: while some child has higher priority
than the entry at k, swap the entry with its child of highest priority and continue from that
child.
Test for emptiness: read the size.
The key of handle v: absent when its position is 0. Correctness is stated for v < n
through the typed indexed-PQ operations; the program accepts the runtime index word directly.
Insert the absent handle v with key k: store the key, append v at the end of the heap,
and sift it up.
Lower the key of the present handle v to k, and sift it up from its position.
The answer of extractMin for the handle u with key k.
Remove the root and return it: move the last entry to the root, sift it down, and store the new size.
The operations of the indexed min-priority queue as memory programs.
The operations as methods of indexedMinPQSpec n K over Memory.spec w.
The represented queue: the model's decoding of the key and position arrays.
The implementation invariant: the fields are allocated, and the arrays hold a binary heap of the stored size.
The size cell is allocated.
The arrays are allocated.
The arrays hold a binary heap of the stored size.
The memory cost of one operation on n handles: 10 ⌊log₂ n⌋ + 14 units.
Layout facts #
Rearranging the heap #
s' is s with the heap and position arrays rearranged: the frontier, the keys, the presence
of each handle and every cell outside those two arrays are unchanged.
Every state is a rearrangement of itself.
Rearrangements compose.
Sift-up and sift-down #
Swapping two heap positions writes four cells.
Sift-up repairs an upward defect at heap position k within 8 ⌊log₂ k⌋ + 4 memory
units.
One step of sift-down: a swap with the child when it has higher priority, then next.
Sift-down repairs a downward defect at heap position k of a heap of size S within
10 (⌊log₂ S⌋ - ⌊log₂ k⌋) + 6 memory units.
The abstraction #
A rearrangement represents the same queue.
A memory that keeps the key and the presence of every handle but v represents the queue
updated at v.
The operations #
What a run of the operation o from s establishes: the specified step from the represented
queue, the invariant, and the frame: the frontier and every cell outside the layout are
unchanged.
An operation that only reads realizes a step that leaves the queue unchanged.
isEmpty reads the size.
getKey reads the position and, for a present handle, the key.
insert appends the handle and sifts it up, within 8 ⌊log₂ n⌋ + 9 memory units.
decreaseKey lowers the key and sifts the handle up, within 8 ⌊log₂ n⌋ + 6 memory units.
The postcondition of an extraction that removes the minimum (u, k).
extractMin removes the root, moves the last entry up and sifts it down, within opCost n
memory units.
Each operation realizes the specified step within opCost n memory units, keeps the
invariant, and changes neither the frontier nor any cell outside the layout.
The run of an operation, by methods_triple.
An operation changes no cell outside the layout.
An operation leaves the frontier where it was.
The refinement and its frame #
The binary heap implements the indexed min-priority queue with keys at most K.
The invariant reads only the layout's cells and survives a growing frontier.
The binary heap, with its layout's cells as a constant footprint, so that it shares memory
with other structures (Memory.Framed.prod).
Every operation costs at most opCost n = 10 ⌊log₂ n⌋ + 14 memory units, with no
potential.
Zero count and position cells suffice for the empty invariant; memory outside the layout can be arbitrary. This is a representation fact, not a zeroing program or an allocation charge.
A locally zero owned block supplies the compact empty heap, without a global ZeroAbove
assumption. Native initialization must still pay its actual reservation and setup instructions.
Allocation #
Allocate a heap on n handles without clearing its storage: in memory that is zero above
the frontier, it starts empty.
The layout that allocLayout n hands out at the frontier f.
Allocation takes two memory units and hands out the layout at the frontier.
The allocated layout is well formed when n and the largest key fit a word.
Allocated from memory that is zero above the frontier, the heap satisfies the invariant and is empty.