Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.Consolidation

Forest mathematics of Fibonacci-heap consolidation #

This file holds the forest reasoning behind extractMin (ExtractMin.lean) as statements about RawState n alone: no program appears. Each phase of extractMin has a precondition and a result, and the lemmas here carry a phase's precondition to the next step's, moving trees between the root list, the list still to be scattered and the consolidation table Fredman and Tarjan Cormen et al., Section 19.2.

The table invariant is the existing SlotInv, with its lemmas lookup, step_collision, insert_empty and done_of_cleared; root lists reuse RootList.addRoot. The module adapts the forest and table reasoning of Renfei Zhou's native formalization (WordRAM/FibonacciHeap, its Consolidate.lean) to the record level, as ExtractMin.lean does for his programs.

Main definitions #

Main results #

References #

Facts about subtrees, chains and the table #

The degree of a valid subtree's root is its number of children.

The child pointer of a valid subtree's root heads the circular list of its children.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.ValidSubtree.exists_child {n : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {pending : Option (Fin n)} {T : WordRAM.FibonacciHeap.FHTree n} (hT : WordRAM.FibonacciHeap.ValidSubtree st pending T) (h0 : st.deg T.root ≠ 0) :
∃ (c : Fin n) (r : Fin n), st.child T.root = ↑c + 1 ∧ st.right c = ↑r

A valid subtree's root with children has a head child c, whose right neighbour is a handle.

The root of a valid subtree has mark at most one.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.ValidSubtree.unmarkRoot {n : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {T : WordRAM.FibonacciHeap.FHTree n} (hT : WordRAM.FibonacciHeap.ValidSubtree st none T) (hnd : T.nodes.Nodup) (numMarked : ℕ) :
WordRAM.FibonacciHeap.ValidRoot { key := st.key, par := Function.update st.par T.root 0, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := Function.update st.mark T.root 0, inHeap := st.inHeap, cons := st.cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := numMarked, sz := st.sz } none T

Clearing the parent and mark of a valid subtree's root makes it a valid root tree.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.markedCount_unmark {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {x : Fin n} (hin : st.inHeap x ≠ 0) (hmk : st.mark x ≤ 1) (hx : st'.mark x = 0) (hinh : st'.inHeap = st.inHeap) (hother : ∀ (y : Fin n), y ≠ x → st'.mark y = st.mark y) :

Unmarking one present handle lowers the number of marks by its mark.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.RightChain.right_head {n : ℕ} {right : Fin n → ℕ} {x : Fin n} {xs : List (Fin n)} (h : WordRAM.FibonacciHeap.RightChain right (x :: xs)) (y : Fin n) :
y ∈ xs.head? → right x = ↑y

In a chain of right pointers, right x is the next element.

The tail of a chain of right pointers is a chain.

The table holds at most one tree per degree up to maxDeg.

The largest degree in the table is logarithmic in the number of nodes.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.SlotInv.le_n {n sz maxDeg : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {slots : List (WordRAM.FibonacciHeap.FHTree n)} (h : WordRAM.FibonacciHeap.SlotInv n sz st slots maxDeg) {d : ℕ} (hd : d ≤ maxDeg) :
d ≤ n

Every degree up to the table's largest is a table index.

What a phase changes #

st' agrees with st except on the tree fields and marks of nodes, the table and the number of marks; and the number of marks stays correct and does not grow.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Scattered.cons {n : ℕ} (st : WordRAM.FibonacciHeap.RawState n) (cons : ℕ → ℕ) (A : List (Fin n)) :
Scattered st { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := cons, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } A

Changing only the table.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Scattered.trans {n : ℕ} {st st' st'' : WordRAM.FibonacciHeap.RawState n} {A : List (Fin n)} (h₁ : Scattered st st' A) (h₂ : Scattered st' st'' A) :
Scattered st st'' A

Scattered composes.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Scattered.mono {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {A B : List (Fin n)} (h : Scattered st st' A) (hAB : ∀ y ∈ A, y ∈ B) :
Scattered st st' B

Scattered grows with the set of changed nodes.

A valid root tree outside the changed nodes stays valid.

A valid subtree outside the changed nodes stays valid.

The result of consolidating the nodes A from st: the table of st' holds slots', of largest degree md', on exactly the nodes A, and only the nodes of A changed.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Consolidated.pre {n sz md' : ℕ} {st st₁ st' : WordRAM.FibonacciHeap.RawState n} {A B : List (Fin n)} {slots' : List (WordRAM.FibonacciHeap.FHTree n)} (hs : Scattered st st₁ A) (hc : Consolidated sz st₁ st' B slots' md') (hB : B.Perm A) :
Consolidated sz st st' A slots' md'

A step that changed only nodes of A can precede a consolidation of A.

Once a forest cs is consolidated into slots, adding the trees E changes nothing.

Linking two roots #

The effect of making root x the newest child of root u: x gets parent u and joins u's child list (alone, or after the head child c), and u's degree grows by one.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.LinksAs.validRoot {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {T₁ T₂ : WordRAM.FibonacciHeap.FHTree n} (h₁ : WordRAM.FibonacciHeap.ValidRoot st none T₁) (h₂ : WordRAM.FibonacciHeap.ValidRoot st none T₂) (hnd : (T₁.nodes ++ T₂.nodes).Nodup) (hdeg : T₁.deg = T₂.deg) (hlt : IndexedMinPQ.PrioLT (T₁.root, st.key T₁.root) (T₂.root, st.key T₂.root)) (hlink : LinksAs st T₁.root T₂.root st') :
WordRAM.FibonacciHeap.ValidRoot st' none (WordRAM.FibonacciHeap.FHTree.node T₁.root (T₁.children ++ [T₂])) ∧ ∀ y ∉ T₁.nodes ++ T₂.nodes, st'.par y = st.par y ∧ st'.left y = st.left y ∧ st'.right y = st.right y ∧ st'.child y = st.child y ∧ st'.deg y = st.deg y

Linking keeps a valid root: if u precedes x and their trees have equal degrees, making x the newest child of u yields a valid root tree; nodes outside the two trees keep every field.

The result of linking the roots of T₁ and T₂ into the tree T.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.LinksAs.linked {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {T₁ T₂ : WordRAM.FibonacciHeap.FHTree n} (h₁ : WordRAM.FibonacciHeap.ValidRoot st none T₁) (h₂ : WordRAM.FibonacciHeap.ValidRoot st none T₂) (hnd : (T₁.nodes ++ T₂.nodes).Nodup) (hdeg : T₁.deg = T₂.deg) (hlt : IndexedMinPQ.PrioLT (T₁.root, st.key T₁.root) (T₂.root, st.key T₂.root)) (hlink : LinksAs st T₁.root T₂.root st') :
Linked st st' T₁ T₂ (WordRAM.FibonacciHeap.FHTree.node T₁.root (T₁.children ++ [T₂]))

Linking two valid roots of equal degree, the one that precedes becoming the parent.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Linked.symm {n : ℕ} {st st' : WordRAM.FibonacciHeap.RawState n} {T₁ T₂ T : WordRAM.FibonacciHeap.FHTree n} (h : Linked st st' T₂ T₁ T) (hdeg : T₁.deg = T₂.deg) :
Linked st st' T₁ T₂ T

Linking is symmetric in the two trees given.

Placing one tree into the table #

The precondition of placing the valid root tree T into a consolidation table holding slots: z is a handle outside all of them, and sz bounds their number of nodes.

The tree being placed has logarithmic degree, with room for one more link.

The table holds fewer trees than there are handles.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Placing.place {n sz md : ℕ} {z : Fin n} {st : WordRAM.FibonacciHeap.RawState n} {T : WordRAM.FibonacciHeap.FHTree n} {slots : List (WordRAM.FibonacciHeap.FHTree n)} (h : Placing sz z st T slots md) (hnone : ∀ R ∈ slots, R.deg ≠ T.deg) :
Consolidated sz st { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := Function.update st.cons T.deg (↑T.root + 1), minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz } (WordRAM.FibonacciHeap.forestNodes (T :: slots)) (T :: slots) (max md T.deg)

An empty slot. If no tree in the table has T's degree, storing T there places it.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Placing.collide {n sz md : ℕ} {z : Fin n} {st : WordRAM.FibonacciHeap.RawState n} {T : WordRAM.FibonacciHeap.FHTree n} {slots : List (WordRAM.FibonacciHeap.FHTree n)} (h : Placing sz z st T slots md) {i : ℕ} (hi : i < slots.length) (hdi : slots[i].deg = T.deg) (hno : ∀ R ∈ slots.eraseIdx i, R.deg ≠ T.deg) {st₁ : WordRAM.FibonacciHeap.RawState n} (h₁ : st₁ = { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := Function.update st.cons T.deg 0, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz }) :
WordRAM.FibonacciHeap.ValidRoot st₁ none T ∧ WordRAM.FibonacciHeap.ValidRoot st₁ none slots[i] ∧ (T.nodes ++ slots[i].nodes).Nodup ∧ ∀ {st₂ : WordRAM.FibonacciHeap.RawState n} {T' : WordRAM.FibonacciHeap.FHTree n}, Linked st₁ st₂ T slots[i] T' → Placing sz z st₂ T' (slots.eraseIdx i) md ∧ ∀ {st' : WordRAM.FibonacciHeap.RawState n} {slots' : List (WordRAM.FibonacciHeap.FHTree n)} {md' : ℕ}, Consolidated sz st₂ st' (WordRAM.FibonacciHeap.forestNodes (T' :: slots.eraseIdx i)) slots' md' → Consolidated sz st st' (WordRAM.FibonacciHeap.forestNodes (T :: slots)) slots' md'

A collision. If the table's tree slots[i] has T's degree, then after emptying its slot the two trees can be linked; the linked tree is then placed into the rest of the table, and placing it places T.

Scattering a list of trees #

The precondition of scattering the trees rest into a table holding slots: the roots of rest are linked by right in this order, and without prep they are already unmarked roots. z is a handle outside all the trees, and sz bounds their number of nodes.

The state after making x an unmarked root: its parent and mark are cleared, and the number of marks drops by its mark.

With nothing left to scatter, the table is the result.

Without prep, the next tree is a valid root.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Scattering.placing {n sz md : ℕ} {z : Fin n} {prep : Bool} {st : WordRAM.FibonacciHeap.RawState n} {T : WordRAM.FibonacciHeap.FHTree n} {slots rest : List (WordRAM.FibonacciHeap.FHTree n)} (h : Scattering sz z prep st slots (T :: rest) md) (hT : WordRAM.FibonacciHeap.ValidRoot st none T) :
Placing sz z st T slots md

Once the next tree is a valid root, it can be placed.

Making the next tree an unmarked root, as prep does: the precondition still holds, the tree is a valid root, and only its root changed.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Scattering.step {n sz md : ℕ} {z : Fin n} {prep : Bool} {st : WordRAM.FibonacciHeap.RawState n} {T : WordRAM.FibonacciHeap.FHTree n} {slots rest : List (WordRAM.FibonacciHeap.FHTree n)} {st₂ : WordRAM.FibonacciHeap.RawState n} {slots₂ : List (WordRAM.FibonacciHeap.FHTree n)} {md₂ : ℕ} (h : Scattering sz z prep st slots (T :: rest) md) (hc : Consolidated sz st st₂ (WordRAM.FibonacciHeap.forestNodes (T :: slots)) slots₂ md₂) :
Scattering sz z prep st₂ slots₂ rest md₂ ∧ ∀ {st' : WordRAM.FibonacciHeap.RawState n} {slots' : List (WordRAM.FibonacciHeap.FHTree n)} {md' : ℕ}, Consolidated sz st₂ st' (WordRAM.FibonacciHeap.forestNodes (slots₂ ++ rest)) slots' md' → Consolidated sz st st' (WordRAM.FibonacciHeap.forestNodes (slots ++ T :: rest)) slots' md'

Scattering the next tree. Once the next tree is placed, the precondition holds for the rest, and scattering the rest scatters the whole list.

Gathering the table into a root list #

The precondition of gathering a table holding slots into the root list holding gs.

The result of gathering the nodes A from st: the root list of st' holds gs', on exactly the nodes A, the table is empty, and keys, presence flags and marks did not change.

Adding a root keeps every other valid root tree valid: only roots change sibling pointers.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Gathering.done {n sz md : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {gs slots : List (WordRAM.FibonacciHeap.FHTree n)} (h : Gathering sz st gs slots md) (hge : ∀ T ∈ slots, md + 1 ≤ T.deg) :

The end of the table. Once no tree has degree at most md, the table is empty.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Gathering.take {n sz md : ℕ} {st : WordRAM.FibonacciHeap.RawState n} {gs slots : List (WordRAM.FibonacciHeap.FHTree n)} (h : Gathering sz st gs slots md) {i d : ℕ} (hi : i < slots.length) (hdi : slots[i].deg = d) (hd : d ≤ n) (hno : ∀ R ∈ slots.eraseIdx i, R.deg ≠ d) {st₁ : WordRAM.FibonacciHeap.RawState n} (h₁ : st₁ = { key := st.key, par := st.par, child := st.child, left := st.left, right := st.right, deg := st.deg, mark := st.mark, inHeap := st.inHeap, cons := Function.update st.cons d 0, minRoot := st.minRoot, numRoots := st.numRoots, numMarked := st.numMarked, sz := st.sz }) :
(st₁.minRoot = 0 ∨ ∃ (m : Fin n) (rtail : List (Fin n)), st₁.minRoot = ↑m + 1 ∧ WordRAM.FibonacciHeap.IsCircList st₁.left st₁.right (m :: rtail)) ∧ st₁.numRoots + 1 ≤ n ∧ ∀ {st₂ : WordRAM.FibonacciHeap.RawState n}, AddsRoot st₁ slots[i].root st₂ → Gathering sz st₂ (slots[i] :: gs) (slots.eraseIdx i) md ∧ ∀ {st' : WordRAM.FibonacciHeap.RawState n} {gs' : List (WordRAM.FibonacciHeap.FHTree n)}, Gathered st₂ st' (WordRAM.FibonacciHeap.forestNodes (slots[i] :: gs ++ slots.eraseIdx i)) gs' → Gathered st st' (WordRAM.FibonacciHeap.forestNodes (gs ++ slots)) gs'

Taking one slot's tree. If the table's tree slots[i] has degree d, then after emptying its slot the tree can be added to the root list, as addRoot needs; then the precondition holds for the rest, and gathering the rest gathers the whole table.

Extracting the minimum #

The state after removing the minimum root m from the presence flags and the count.

A nonempty heap as extractMin sees it: the minimum root m has children cs, listed in the order of their sibling list, and the other trees are E, listed in root-list order from m's right neighbour. The other fields are the rest of FibRawInv.

Splitting a heap at its minimum root.

Every other node differs from the minimum root.

After detaching m, the children are ready to be scattered with prep.

After the children are scattered, the other trees are ready to be scattered without prep.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Detached.gathering {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {m : Fin n} {cs E : List (WordRAM.FibonacciHeap.FHTree n)} (hD : Detached n K s st m cs E) {st₂ st₃ : WordRAM.FibonacciHeap.RawState n} {slots₁ slots₂ : List (WordRAM.FibonacciHeap.FHTree n)} {md₁ md₂ : ℕ} (hc₁ : Consolidated (WordRAM.FibonacciHeap.forestNodes (cs ++ E)).length (detachState st m) st₂ (WordRAM.FibonacciHeap.forestNodes ([] ++ cs)) slots₁ md₁) (hc₂ : Consolidated (WordRAM.FibonacciHeap.forestNodes (cs ++ E)).length st₂ st₃ (WordRAM.FibonacciHeap.forestNodes (slots₁ ++ E)) slots₂ md₂) :
Gathering (WordRAM.FibonacciHeap.forestNodes (cs ++ E)).length { key := st₃.key, par := st₃.par, child := st₃.child, left := st₃.left, right := st₃.right, deg := st₃.deg, mark := st₃.mark, inHeap := st₃.inHeap, cons := st₃.cons, minRoot := 0, numRoots := 0, numMarked := st₃.numMarked, sz := st₃.sz } [] slots₂ md₂

After both scatterings, with the root list emptied, the table is ready to be gathered.

theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.Detached.finish {n K : ℕ} {s : IndexedMinPQ.State n} {st : WordRAM.FibonacciHeap.RawState n} {m : Fin n} {cs E : List (WordRAM.FibonacciHeap.FHTree n)} (hD : Detached n K s st m cs E) {st₂ st₃ st' : WordRAM.FibonacciHeap.RawState n} {slots₁ slots₂ gs' : List (WordRAM.FibonacciHeap.FHTree n)} {md₁ md₂ : ℕ} (hc₁ : Consolidated (WordRAM.FibonacciHeap.forestNodes (cs ++ E)).length (detachState st m) st₂ (WordRAM.FibonacciHeap.forestNodes ([] ++ cs)) slots₁ md₁) (hc₂ : Consolidated (WordRAM.FibonacciHeap.forestNodes (cs ++ E)).length st₂ st₃ (WordRAM.FibonacciHeap.forestNodes (slots₁ ++ E)) slots₂ md₂) (hG : Gathered { key := st₃.key, par := st₃.par, child := st₃.child, left := st₃.left, right := st₃.right, deg := st₃.deg, mark := st₃.mark, inHeap := st₃.inHeap, cons := st₃.cons, minRoot := 0, numRoots := 0, numMarked := st₃.numMarked, sz := st₃.sz } st' (WordRAM.FibonacciHeap.forestNodes ([] ++ slots₂)) gs') :

Reassembling the heap. After both scatterings and the gathering, FibRawInv holds of the queue without m; the number of marks has not grown, and the table's largest degree is logarithmic.