# %load ../../preconfig.py
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['axes.grid'] = False
from __future__ import division
#import numpy as np
#import pandas as pd
#import itertools
import logging
logger = logging.getLogger()
dual purpose:
support a set of operations of 'mergeable heap'.
its several Fibonacci-heap operations run in constant amortized time.
plt.figure(figsize=(15,10))
plt.imshow(plt.imread('./res/fig19_1.png'))
<matplotlib.image.AxesImage at 0x10918be48>
In theory:
Fibonacci heaps are especially desiable when the number of EXTRAC-MIN and DELETE operations is small relative to the number of other operations performed.
In practice:
Ordinary binary heaps are more preferred because of the constant factors and programing complexity of Fibonacci heaps.
A Fibonacci heap is a collection of rooted trees that are min-heap ordered(the key of a node is greater than or equal to the key of its parent).
plt.figure(figsize=(15,10))
plt.imshow(plt.imread('./res/fig19_2.png'))
<matplotlib.image.AxesImage at 0x10a47b5c0>
Links:
each node $x$ contains a pointer $x.p$ to its parent and a pointer $x.child$ to any one of its children.
child list of $x$: The children of $x$ are linked together in a circular, doubly linked list. Each child $y$ in a child list has pointers $y.left$ and $y.right$.
Why doubly linked lists?
Attributes:
For each node $x$:
For heap $H$:
where $t(H)$ is the number of trees in the root list and $m(H)$ is the number of marked nodes.
An upper bound $D(n)$ on the maximum degree of any node in an $n$-node Fibonacci heap: $D(n) \leq \lfloor \lg n \rfloor$.
plt.figure()
plt.imshow(plt.imread('./res/fig19_3a.png'))
plt.figure()
plt.imshow(plt.imread('./res/fig19_3.png'))
<matplotlib.image.AxesImage at 0x10d283ba8>
plt.imshow(plt.imread('./res/fig19_unit.png'))
<matplotlib.image.AxesImage at 0x10dd21630>
$O(\lg n)$
extracte the minimum node, and also consolidate trees in the root list finally occurs.
plt.imshow(plt.imread('./res/fig19_extract_min.png'))
<matplotlib.image.AxesImage at 0x10df49518>
consolidating the root list consists of repeatedly excuting the following steps until every root in the root list has a distince degree values:
Find two roots $x$ and $y$ in the root list with the same degree. Let $x.key \leq y.key$.
Link $y$ to $x$: remove $y$ from the root list, and make $y$ a child of $x$ by calling the FIB-HEAP-LINK procedure.
plt.figure(figsize=(20,15))
plt.subplot(2,1,1)
plt.imshow(plt.imread('./res/fig19_4a.png'))
plt.subplot(2,1,2)
plt.imshow(plt.imread('./res/fig19_4b.png'))
<matplotlib.image.AxesImage at 0x11b568588>
plt.imshow(plt.imread('./res/fig19_decrease.png'))
<matplotlib.image.AxesImage at 0x108cfce80>
CUT: As soon as the second child has been lost, we cut $x$ from its parent, making it a new root.
CASCADING-CUT: because $x$ might be the second child cut from its parent $y$ since the time that $y$ was linked to another node, we perform a cascading-cut operation on $y$.
O(lg n)
FIB-HEAP-DELETE(H, x)
FIB-HEAP-DECREASE-KEY(H, x, $-\inf$)
FIB-HEAP-EXTRACT-MIN(H)
#Exercises
To prove FIB-HEAP-EXTRACT-MIN is $O(\lg n)$, we shall show that $D(n) \leq \lfloor \log_{\phi} n \rfloor$.
证明见书。