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.
- Linking (
LinksAs,Linked): making one valid root the newest child of another of equal degree yields a valid root (LinksAs.linked, through the existingValidRoot.link). - Placing one tree into the table (
Placing): an empty slot takes it (Placing.place); a full slot is emptied and its tree linked with it (Placing.collide). - Scattering a list of trees (
Scattering): the head is made an unmarked root (Scattering.unmark) and placed (Scattering.placing), and the rest is scattered (Scattering.step). - Both results are
Consolidated: the table holds trees on exactly the nodes scattered, and nothing else changed (Scattered). - Gathering the table into a root list (
Gathering,Gathered):Gathering.takeadds one slot's tree,Gathering.doneends with an empty table. extractMinas a whole (Detached):FibRawInv.detachsplits a heap into the minimum root's children and the other trees, andDetached.finishreassemblesFibRawInvfrom the three phases' results.
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 #
LinksAs,Linked: linking two roots.Scattered,Consolidated: what a scattering phase changes, and its result.Placing,Scattering,Gathering,Gathered: the phases of consolidation.unmarkState,detachState: the states after unmarking a root and detaching the minimum.Detached: a nonempty heap split at its minimum root.
Main results #
LinksAs.linked: linking two valid roots of equal degree yields a valid root.Placing.place,Placing.collide,Scattering.step,Scattering.unmark,Gathering.take: one step of each loop.FibRawInv.detach,Detached.finish:extractMinpreservesFibRawInv.
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.
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.
Clearing the parent and mark of a valid subtree's root makes it a valid root tree.
Unmarking one present handle lowers the number of marks by its mark.
The children of a valid subtree are valid subtrees.
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.
Every degree up to the table's largest is a table index.
Splitting a forest at one tree.
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.
- numMarked : st.numMarked = WordRAM.FibonacciHeap.markedCount st → st'.numMarked = WordRAM.FibonacciHeap.markedCount st'
Nothing changed.
Changing only the table.
Scattered composes.
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.
- table : WordRAM.FibonacciHeap.SlotInv n sz st' slots' md'
- perm : (WordRAM.FibonacciHeap.forestNodes slots').Perm A
- scattered : Scattered st st' A
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.
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.
- valid : WordRAM.FibonacciHeap.ValidRoot st' none T
Linking two valid roots of equal degree, the one that precedes becoming the parent.
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.
- table : WordRAM.FibonacciHeap.SlotInv n sz st slots md
- valid : WordRAM.FibonacciHeap.ValidRoot st none T
The tree being placed has logarithmic degree, with room for one more link.
The table holds fewer trees than there are handles.
An empty slot. If no tree in the table has T's degree, storing T there places it.
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.
- table : WordRAM.FibonacciHeap.SlotInv n sz st slots md
- valid (T : WordRAM.FibonacciHeap.FHTree n) : T ∈ rest → WordRAM.FibonacciHeap.ValidSubtree st none T
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.
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.
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.
- table : WordRAM.FibonacciHeap.SlotInv n sz st slots md
- roots : RootList st 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.
- roots : RootList st' gs'
- perm : (WordRAM.FibonacciHeap.forestNodes gs').Perm A
Adding a root keeps every other valid root tree valid: only roots change sibling pointers.
The end of the table. Once no tree has degree at most md, the table is empty.
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.
- chainChildren : WordRAM.FibonacciHeap.RightChain st.right (List.map WordRAM.FibonacciHeap.FHTree.root cs)
- chainRoots : WordRAM.FibonacciHeap.RightChain st.right (List.map WordRAM.FibonacciHeap.FHTree.root E)
- children (T : WordRAM.FibonacciHeap.FHTree n) : T ∈ cs → WordRAM.FibonacciHeap.ValidSubtree st none T
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.
After both scatterings, with the root list emptied, the table is ready to be gathered.
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.