Documentation

CslibFrontier.DataStructure.PriorityQueue.BinaryHeap.Order

Indexed binary min-heap: the order lemmas of sift-up and sift-down #

These are the machine-free steps of sift-up and sift-down on the indexed binary heap of Cslib.DataStructure.PriorityQueue.BinaryHeap.Model: swapping two heap positions keeps the heap shape (HeapShape.swap), and a swap with the parent or with the child of highest priority moves a single order defect one level (UpOrdered.swap, DownOrdered.swap) until it disappears (UpOrdered.ordered, DownOrdered.ordered).

These are the machine-free lemmas extracted from Renfei Zhou's original native heap proof. The memory programs and their native realizations share them without importing a separate execution proof. They extend the namespace of BinaryHeap.Model, whose definitions they use.

Main definitions #

Main results #

References #

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.UpOrdered.ordered {f : ℕ → ℕ × ℕ} {S a : ℕ} (h : UpOrdered f S a) (hle : 2 ≤ a → a ≤ S → IndexedMinPQ.RawPrioLE (f (a / 2)) (f a)) :

A heap with one upward defect at a is ordered once a is ordered with its parent.

An ordered heap has at most an upward defect at the root.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.UpOrdered.swap {f g : ℕ → ℕ × ℕ} {S a : ℕ} (h : UpOrdered f S a) (ha : 2 ≤ a) (haS : a ≤ S) (hlt : IndexedMinPQ.RawPrioLT (f a) (f (a / 2))) (hga : g a = f (a / 2)) (hgp : g (a / 2) = f a) (hgo : ∀ (i : ℕ), 1 ≤ i → i ≤ S → i ≠ a → i ≠ a / 2 → g i = f i) :
UpOrdered g S (a / 2)

One step of sift-up. Swapping an entry a with its parent when it has higher priority moves the upward defect to the parent.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.DownOrdered.ordered {f : ℕ → ℕ × ℕ} {S a : ℕ} (h : DownOrdered f S a) (hle : ∀ (i : ℕ), 2 ≤ i → i ≤ S → i / 2 = a → IndexedMinPQ.RawPrioLE (f a) (f i)) :

A heap with one downward defect at a is ordered once a is ordered with its children.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.DownOrdered.swap {f g : ℕ → ℕ × ℕ} {S a c : ℕ} (h : DownOrdered f S a) (hc : c / 2 = a) (hcS : c ≤ S) (hmin : ∀ (i : ℕ), 2 ≤ i → i ≤ S → i / 2 = a → IndexedMinPQ.RawPrioLE (f c) (f i)) (hlt : IndexedMinPQ.RawPrioLT (f c) (f a)) (hga : g a = f c) (hgc : g c = f a) (hgo : ∀ (i : ℕ), 1 ≤ i → i ≤ S → i ≠ a → i ≠ c → g i = f i) :

One step of sift-down. Swapping an entry a with its child c of highest priority, when c has higher priority than a, moves the downward defect to c.

def Cslib.DataStructure.PriorityQueue.BinaryHeap.swapMem (N base : ℕ) (mem : ℕ → ℕ) (a b va vb : ℕ) :
ℕ → ℕ

Memory after swapping heap positions a and b, which hold the handles va and vb: the heap array exchanges the two handles and the position array records their new positions.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.HeapShape.swap {N base S : ℕ} {mem : ℕ → ℕ} {a b : ℕ} (h : HeapShape N base S mem) (ha : 1 ≤ a) (haS : a ≤ S) (hb : 1 ≤ b) (hbS : b ≤ S) (hab : a ≠ b) :
have m' := swapMem N base mem a b (mem (base + N + a)) (mem (base + N + b)); HeapShape N base S m' ∧ heapEntry N base m' a = heapEntry N base mem b ∧ heapEntry N base m' b = heapEntry N base mem a ∧ (∀ (i : ℕ), 1 ≤ i → i ≤ S → i ≠ a → i ≠ b → heapEntry N base m' i = heapEntry N base mem i) ∧ (∀ v < N, m' (base + 2 * N + 1 + v) = 0 ↔ mem (base + 2 * N + 1 + v) = 0) ∧ ∀ (c : ℕ), c ≤ base + N ∨ base + 3 * N < c → m' c = mem c

Swapping two heap positions keeps the heap shape, exchanges the two entries, leaves the other entries, the presence of every handle, and every cell outside the heap and position arrays unchanged.

The child of a with the highest priority in a heap of size S: the right child when it exists and has strictly higher priority than the left child, and the left child otherwise.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.minChild_spec {S : ℕ} {f : ℕ → ℕ × ℕ} {a : ℕ} (ha : 2 * a ≤ S) :
(minChild f S a = 2 * a ∨ minChild f S a = 2 * a + 1) ∧ minChild f S a ≤ S ∧ ∀ i ≤ S, i / 2 = a → IndexedMinPQ.RawPrioLE (f (minChild f S a)) (f i)

minChild is a child of a within the heap, of priority at least that of every child.