Proofs: Fibonacci heap
These are the human proofs of Fibonacci heap, at full length.
Amortized bounds of the Fibonacci heap and the degree bound full
This proof shows: time.amortized, space.
We use the invariants (S), (O), (M) and (L) of the correctness proof. Let , and , and let , so that .
The degree bound
Lemma 1. Let be a node, and let be the children of in the order in which they were linked to , from the earliest. Then and for .
Proof. The first claim is clear. Let . When LINK made a child of , the nodes were already children of , so . LINK is called only on two roots of the same degree (line L20 of CONSOLIDATE), so then . Since that link, has stayed a child of , and by (L) it has lost at most one child. A node gains children only by LINK, and only while it is a root, so has gained none. So .
Lemma 2. and for each .
Proof. Both by induction on . For : and . For , . For the step, , and .
Lemma 3. A node of degree has at least nodes in its subtree.
Proof. We show for each node , by induction on the height of . A leaf has degree 0 and size . Let have degree and the children as in Lemma 1. Each child has a smaller height than , so the claim holds for it. By this and Lemma 1, the subtree of has at least nodes for , since the Fibonacci numbers do not decrease, and adds at least 1 node. Counting too,
using , and Lemma 2. The second inequality of the claim is Lemma 2.
Corollary. In a heap of nodes, each degree is at most .
Proof. A node of degree has by Lemma 3, so .
The amortized costs
Let be the number of roots and the number of marked nodes, and let
with for the empty heap. Scale the unit of work so that each line of the main block that is not a loop, and each iteration of a loop, costs at most 1 unit, and scale by the same constant . Then for each sequence of operations from the empty heap, the total actual cost is at most the sum of the amortized costs , because starts at 0 and stays non-negative.
Insert. actual work, , . So .
Union. actual work. The potential of the union is the sum of the two potentials, so , and .
Find-min. , with no change.
Extract-min. Let before the call. Line L11 adds at most roots, the children of , and line L12 removes . So CONSOLIDATE starts with at most roots. Each iteration of the loop of line L18 costs plus for each LINK, and each LINK removes one root, so there are fewer links than roots. Lines L26 to L30 go over at most entries of , by the corollary. The actual cost is . At the end, the roots have pairwise different degrees in , so , and no mark is set. So
when .
Decrease-key. Let the call do cuts in total, counting the cut of line L37 and the cuts of line L45. The actual cost is . The cuts add roots. Each cut of line L45 removes the mark of a marked node, by line L41, and there are at least such cuts when . At most one mark is set, at line L44. So
and when . If , the change is 0 and the cost is .
Delete. One decrease-key and one extract-min, so .
Theorem. For each sequence of operations on Fibonacci heaps that starts from empty heaps, the total time is at most for each insert, find-min, union and decrease-key, plus for each extract-min and delete, where is the size of the heap at that time. The space is .
Proof. By the cases above, the amortized costs are and , and by the corollary. Each node has a fixed number of fields, and the table has at most entries.
The operations of a Fibonacci heap keep its invariants and return a minimum full
This proof shows: correctness, termination.
We write for the set of nodes of the trees of , and for the multiset of their keys. The structure invariant (S) says: the trees are disjoint rooted trees on ; the roots form the root list and have ; the children of each node form its child list, each child has , and is the number of children; and . The other invariants are
- (O) for each non-root node ;
- (M) if , and else is a root with for each ;
- (L) each non-root node has lost no child since it last became a child (by LINK) if , and exactly one if .
A node loses a child when a CUT removes that child from its child list. We show that each operation keeps (S), (O), (M) and (L), and changes as the priority queue type says.
Lemma 1 (LINK). Let and be two different roots with . Then LINK keeps (S) and (O) and does not change ; afterwards is a non-root with no lost child and no mark, so (L) holds at .
Proof. Line L31 takes out of the root list and line L32 puts it in the child list of with and adds 1 to . So the trees stay disjoint and (S) holds. The only new edge is , and , so (O) holds. Line L33 clears the mark, and has just become a child, so it has lost no child since. No node is added or removed.
Lemma 2 (CUT). Let be a child of . Then CUT keeps (S) and (O) and does not change . It makes a root, and loses the child .
Proof. Line L40 removes from the child list of and lowers by 1, and line L41 puts in the root list with . The subtree of comes along unchanged. (S) holds. An edge was removed and none was added, so (O) holds.
Lemma 3 (CASCADING-CUT). Assume (S) and (O), and assume that (L) holds at each node except , and that , if it is not a root, has lost exactly one more child than (L) allows for its mark. Then CASCADING-CUT stops, keeps (S) and (O), restores (L), and does not change .
Proof. By induction on the depth of . If is a root (line L43), (L) says nothing about , and the procedure stops. Else let . If is not marked, it had lost no child, and now it has lost one. Line L44 marks it, so (L) holds at , and the procedure stops. If is marked, it had lost one child, and now it has lost two. Line L45 cuts from . By Lemma 2, (S) and (O) hold, is a root, so (L) says nothing about it, and has lost one more child. The depth of is one less than the depth of , so by induction the call CASCADING-CUT stops and restores (L).
Insert. Line L1 makes a tree of one node, and line L2 adds it to the root list, so (S) holds after line L4, and (O) holds because there is no new edge. Line L3 keeps (M), since the new minimum is the smaller of the old minimum and . No node lost a child, so (L) holds. gains .
Union. Line L5 joins the two root lists, so the trees of become trees of , and lines L7 and L8 keep the counts. (S), (O) and (L) hold, since they hold in each heap and no edge changes. A minimum of is the smaller of the two minima, which line L6 picks, so (M) holds. becomes empty, which satisfies all invariants.
Extract-min. Let . By (M), holds a minimum of . Line L11 makes each child of a root. These children were non-roots and become roots, so (L) says nothing about them now; the edges from to them are gone, and (O) loses edges only. Line L12 removes , which is a root with no children left. So loses one copy of its minimum, and (S) holds with lowered by 1 at line L15. If before, the heap is now empty, and line L13 sets (M). Else CONSOLIDATE runs.
Lemma 4 (CONSOLIDATE). Assume (S), (O) and (L) and that the root list is not empty. Then CONSOLIDATE stops, keeps (S), (O) and (L), does not change , makes the degrees of the roots pairwise different, and sets (M).
Proof. Let be the list of roots at line L18, taken once before the loop. We claim that, before each iteration of line L18, the set of roots that are entries of , together with the roots of not yet visited, is the set of all roots, each entry is a root of degree , and the entries have pairwise different degrees.
At the start, is empty and is the root list. In an iteration with , the inner loop keeps this claim: is a root of degree that is not in ; if is set, then is another root of degree , and after line L22, . By Lemma 1, LINK keeps (S), (O) and (L), and makes a child of ; then has degree , and line L24 clears . Each LINK lowers the number of roots by 1, so the inner loop stops after at most links in total. When it stops, is empty and line L25 stores there. So the claim holds after the iteration, with visited. When the loop ends, the roots are exactly the entries of , with pairwise different degrees.
Lines L26 to L29 rebuild the root list from these entries, and line L30 keeps the smallest key seen, so at the end is a root of least key among the roots. By (O) and the global form of the heap property, each key is at least the key of its root, so (M) holds. No node is added or removed, so is unchanged.
So after line L16, the invariants hold, has lost one minimum, and the returned node holds that minimum.
Decrease-key. Let , as line L34 checks. Line L35 lowers . The edges from to its children stay ordered, because only got smaller. The only edge that may break (O) is , where . If or , (O) holds. Else line L37 cuts ; by Lemma 2, (S) and (O) hold, and has lost one child. So the hypothesis of Lemma 3 holds at , and line L38 restores (L). In both cases, line L39 keeps (M): the keys other than did not change, is a root if its key is now below its parent, and else . has the old key of replaced by .
Delete. Line L46 decreases to a value below all keys, so by the case above becomes a root and line L39 sets . Line L47 then removes . So loses , and the invariants hold.
Termination. Each operation except CONSOLIDATE and CASCADING-CUT has no loop or a loop over a finite list. Lemma 3 and Lemma 4 show that these two stop.
Theorem. Starting from the empty heap, after each sequence of operations the invariants (S), (O), (M) and (L) hold, FIND-MIN and EXTRACT-MIN return a node of minimum key, and the multiset is the one that the priority queue type gives for the same sequence.
Proof. The empty heap satisfies all invariants. Each operation keeps them and changes as the type says, by the cases above. By (M), FIND-MIN returns a node of minimum key, and the case of EXTRACT-MIN shows the same for it.