Strict Fibonacci heap explained

Strict Fibonacci heap
Type:Heap
Invented By:Gerth S. Brodal, George Lagogiannis, and Robert E. Tarjan
Invented Year:2012
Space Worst:O(n)
Insert Worst:O(1)
Find Min Worst:O(1)
Delete Min Worst:O(log n)
Decrease Key Worst:O(1)
Merge Worst:O(1)

In computer science, a strict Fibonacci heap is a priority queue data structure with low worst case time bounds. It matches the amortized time bounds of the Fibonacci heap in the worst case. To achieve these time bounds, strict Fibonacci heaps maintain several invariants by performing restoring transformations after every operation. These transformations can be done in constant time by using auxiliary data structures to track invariant violations, and the pigeonhole principle guarantees that these can be fixed. Strict Fibonacci heaps were invented in 2012 by Gerth S. Brodal, George Lagogiannis, and Robert E. Tarjan.

Along with Brodal queues, strict Fibonacci heaps belong to a class of asymptotically optimal data structures for priority queues.[1] All operations on strict Fibonacci heaps run in worst case constant time except delete-min, which is necessarily logarithmic. This is optimal, because any priority queue can be used to sort a list of

n

elements by performing

n

insertions and

n

delete-min operations.[2] However, strict Fibonacci heaps are simpler than Brodal queues, which make use of dynamic arrays and redundant counters,[3] whereas the strict Fibonacci heap is pointer based only.

Structure

A strict Fibonacci heap is a single tree satisfying the minimum-heap property. That is, the key of a node is always smaller than or equal to its children. As a direct consequence, the node with the minimum key always lies at the root.

Like ordinary Fibonacci heaps,[4] strict Fibonacci heaps possess substructures similar to binomial heaps. To identify these structures, we label every node with one of two types. We thus introduce the following definitions and rules:

Invariants

Invariant 1: Structure
  • The

    i

    th rightmost active child

    ci

    of an active node satisfies

    ci.rank+ci.loss\gei-1

    .

    Thus, the loss of an active node can be viewed as a generalisation of Fibonacci heap 'marks'. For example, a subtree consisting of only active nodes with loss zero is a binomial tree.

    In addition, several invariants which impose logarithmic bounds on three main quantities: the number of active roots, the total loss, and the degrees of nodes. This is in contrast to the ordinary Fibonacci heap, which is more flexible and allows structural violations to grow on the order of

    O(n)

    to be cleaned up later, as it is a lazy data structure.

    To assist in keeping the degrees of nodes logarithmic, every non-root node also participates in a queue

    Q

    . In the following section, and for rest of this article, we define the real number

    R=2lgn+6

    , where

    n

    is the number of nodes in the heap, and

    lg

    denotes the binary logarithm.
    Invariant 2: Active roots
  • The total number of active roots is at most

    R+1

    .
    Invariant 3: Total loss
  • The total loss in the heap is at most

    R+1

    .
    Invariant 4: Root degree
  • The degree of the root is at most

    R+3

    .
    Invariant 5: Non-root degrees
  • For an active node with zero loss, the degree is at most

    2lg(2n-p)+10

    , where

    p

    is its position in

    Q

    (with 1 as the first element). For all other non-root nodes, the degree is at most

    2lg(2n-p)+9

    .
    Corollary 1: Maximum degree
  • The degree of any non-root node is at most

    R+6

    .

    Proof:

    This follows immediately from invariant 5. Letting

    p=0

    , we have

    2lg(2n-0)+10=2lgn+12=R+6

    Lemma 1: Maximum rank
  • If invariant 1 holds, the maximum rank

    r

    or any active node is at most

    lgn+\sqrt{2L}+2

    , where

    L

    is the total loss.[5]

    Proof:

    We proceed by contradiction. Let

    x

    be an active node with maximal rank

    r

    in a heap with

    n

    nodes and total loss

    L

    , and assume that

    r\gelgn+k+1

    , where

    k

    is the smallest integer such that

    k(k+1)/2\geL

    . Our goal is to show that the subtree

    Tx

    rooted at

    x

    contains

    n+1

    nodes, which is a contradiction because there are only

    n

    nodes in the heap.

    Discard all subtrees rooted at passive nodes from

    Tx

    , leaving it with only active nodes. Cut off all the grandchildren of

    x

    whose subtrees contain any node of positive loss, and increase the loss of the children of

    x

    accordingly, once for each grandchild lost. The quantity

    rank+loss

    is unchanged for the remaining nodes, preserving invariant 1. Furthermore, the total loss is still at most

    L

    .

    The children of

    x

    now consists of loss-free subtrees and leaf nodes with positive loss. Currently,

    Tx

    satisfies

    ci.rank+ci.loss\gei-1

    for the

    i

    th rightmost child

    ci

    of

    x

    . We make this an exact equality by first reducing the loss of each

    ci

    , and pruning any grandchildren if necessary. Afterwards,

    ci.rank+ci.loss=i-1

    exactly. All other descendants of

    x

    are also converted into binomial subtrees by pruning children as necessary.

    We now attempt to reconstruct a minimal version of

    Tx

    by starting with a binomial tree of degree

    r

    , containing

    2r

    active nodes. We wish to increase the loss to

    L

    , but keep the rank of

    x

    as

    r

    and the number of nodes as low as possible. For a binomial tree of degree

    r

    , there is one child of each degree from

    0

    to

    r-1

    . Hence, there are

    j

    grandchildren of order

    r-j-1

    . If we cut all the grandchildren whose degree

    \ler-k-1

    , then we have cut \sum_^k j = k(k+1)/2 grandchildren, which is sufficient to bring the loss up to

    L

    . All grandchildren with degree

    \ler-k-2

    survive. Let

    w

    be the child of

    x

    with degree

    r-k-1

    and loss 0. By assumption,

    r-k-1\gelgn

    , and

    w

    is a complete binomial tree, so it has at least

    2lg=n

    nodes. Since this would mean

    Tx

    has at least

    n+1

    nodes, we have reached a contradiction, and therefore

    r<lgn+k+1

    . Noting that

    k<\sqrt{k(k+1)}=\sqrt{2L}

    , we obtain

    r<lgn+\sqrt{2L}+2

    .
    Corollary 2: Maximum rank
  • If invariants 1 and 3 both hold, then the maximum rank is

    R

    .

    Proof:

    From invariant 3, we have

    L\leR+1

    . By substituting this into lemma 1, we calculate as follows:

    \begin{align} r&\lelgn+\sqrt{2L}+2\\ &\lelgn+\sqrt{2(R+1)}+2\\ &=lgn+\sqrt{4lgn+14}+2\\ &\lelgn+\sqrt{(lgn)2+24lgn+42}+2\\ &=lgn+(lgn+4)+2\\ &=R \end{align}

    Transformations

    The following transformations restore the above invariants after a priority queue operation has been performed. There are three main quantities we wish to minimize: the number of active roots, the total loss in the heap, and the degree of the root. All transformations can be performed in

    O(1)

    time, which is possible by maintaining auxiliary data structures to track candidate nodes (described in the section on implementation).

    Active root reduction

    Let

    x

    and

    y

    be active roots with equal rank

    r

    , and assume

    x.key\ley.key

    . Link

    y

    as the leftmost child of

    x

    and increase the rank of

    x

    by 1. If the rightmost child

    z

    of

    x

    is passive, link

    z

    to the root.

    As a result,

    y

    is no longer an active root, so the number of active roots decreases by 1. However, the degree of the root node may increase by 1,

    Since

    y

    becomes the

    (r+1)

    th rightmost child of

    x

    , and

    y

    has rank

    r

    , invariant 1 is preserved.
    Lemma 2: Availability of active root reduction
  • If invariant 2 is violated, but invariants 1 and 3 hold, then active root reduction is possible.
  • Proof:
  • Because invariant 2 is broken, there are more than

    R+1

    active roots present. From corollary 2, the maximum rank of a node is

    R

    . By the pigeonhole principle, there exists a pair of active roots with the same rank.

    Loss reduction

    One node loss reduction

    Let

    x

    be an active non-root with loss at least 2. Link

    x

    to the root, thus turning it into an active root, and resetting its loss to 0. Let the original parent of

    x

    be

    y

    .

    y

    must be active, since otherwise

    x

    would have previously been an active root, and thus could not have had positive loss. The rank of

    y

    is decreased by 1. If

    y

    is not an active root, increase its loss by 1.

    Overall, the total loss decreases by 1 or 2. As a side effect, the root degree and number of active roots increase by 1, making it less preferable to two node loss reduction, but still a necessary operation.

    Two node loss reduction

    Let 

    x

    and 

    y

     be active nodes with equal rank 

    r

    and loss equal to 1, and let

    z

    be the parent of

    y

    . Without loss of generality, assume that

    x.key\ley.key

    . Detach

    y

    from

    z

    , and link

    y

    to

    x

    . Increase the rank of

    x

    by 1 and reset the loss of

    x

    and

    y

    from 1 to 0.

    z

    must be active, since

    y

    had positive loss and could not have been an active root. Hence, the rank of

    z

    is decreased by 1. If

    z

    is not an active root, increase its loss by 1.

    Overall, the total loss decreases by either 1 or 2, with no side effects.

    Lemma 3: Availability of loss reduction
  • If invariant 3 is violated by 1, but invariant 2 holds, then loss reduction is possible.
  • Proof: We apply the pigeonhole principle again. If invariant 3 is violated by 1, the total loss is

    L=R+2

    . Lemma 1 can be reformulated to also work with

    L=R+2

    . Thus, corollary 2 holds. Since the maximum rank is

    R

    , either there either exists a pair of active nodes with equal rank and loss 1, or an active node with

    loss\ge2

    . Both cases present an opportunity for loss reduction.

    Root degree reduction

    Let

    x

    ,

    y

    , and

    z

    be the three rightmost passive linkable children of the root. Detach them all from the root and sort them such that

    x.key\ley.key\lez.key

    . Change

    x

    and

    y

    to be active. Link

    z

    to

    y

    , link

    y

    to

    x

    , and link

    x

    as the leftmost child of the root. As a result,

    x

    becomes an active root with rank 1 and loss 0. The rank and loss of

    y

    is set to 0.

    The net change of this transformation is that the degree of the root node decreases by 2. As a side effect, the number of active roots increases by 1.

    Lemma 4: Availability of root degree reduction
  • If invariant 4 is violated, but invariant 2 holds, then root degree reduction is possible.
  • Proof:
  • If invariant 4 is broken, then the degree of the root is at least

    R+4

    . The children of the root fall into three categories: active roots, passive non-linkable nodes, and passive linkable nodes. Each passive non-linkable node subsumes an active root, since its subtree contains at least one active node. Because the number of active roots is at most

    R+1

    , the rightmost three children of the root must therefore be passive linkable.

    Summary

    The following table summarises the effect of each transformation on the three important quantities. Individually, each transformation may violate invariants, but we are only interested in certain combinations of transformations which do not increase any of these quantities.

    Effect of transformations!!Active roots!Total loss!Root degree
    Active root reduction

    -1

    0

    0or{+}1

    Root degree reduction

    +1

    0

    -2

    One node loss reduction

    +1

    atleast{-}1

    +1

    Two node loss reduction

    0

    -1or{-}2

    0

    When deciding which transformations to perform, we consider only the worst case effect of these operations, for simplicity. The two types of loss reduction are also considered to be the same operation. As such, we define 'performing a loss reduction' to mean attempting each type of loss reduction in turn.

    Worst case effect of transformations!!Active roots!Total loss!Root degree
    Active root reduction

    -1

    0

    +1

    Root degree reduction

    +1

    0

    -2

    Loss reduction

    +1

    -1

    +1

    Implementation

    Linking nodes

    To ensure active nodes lie to the left of passive nodes, and preserve invariant 1, the linking operation should place active nodes on the left, and passive nodes on the right. It is necessary for active and passive nodes to coexist in the same list, because the merge operation changes all nodes in the smaller heap to be passive. If they existed in two separate lists, the lists would have to be concatenated, which cannot be done in constant time for all nodes.

    For the root, we also pose the requirement that passive linkable children lie to the right of the passive non-linkable children. Since we wish to be able link nodes to the root in constant time, a pointer to the first passive linkable child of the root must be maintained.

    Finding candidate nodes

    The invariant restoring transformations rely on being able to find candidate nodes in

    O(1)

    time. This means that we must keep track of active roots with the same rank, nodes with loss 1 of the same rank, and nodes with loss at least 2.

    The original paper by Brodal et al. described a fix-list and a rank-list as a way of tracking candidate nodes.

    Fix-list

    The fix-list is divided into four parts:

    1. Active roots ready for active root reduction – active roots with a partner of the same rank. Nodes with the same rank are kept adjacent.
    2. Active roots not yet ready for active reduction – the only active roots for that rank.
    3. Active nodes with loss 1 that are not yet ready for loss reduction – the only active nodes with loss 1 for that rank.
    4. Active nodes that are ready for loss reduction – This includes active nodes with loss 1 that have a partner of the same rank, and active nodes with loss at least 2, which do not need partners to be reduced. Nodes with the same rank are kept adjacent.

    To check if active root reduction is possible, we simply check if part 1 is non-empty. If it is non-empty, the first two nodes can be popped off and transformed. Similarly, to check if loss reduction is possible, we check the end of part 4. If it contains a node with loss at least 2, one node loss reduction is performed. Otherwise, if the last two nodes both have loss 1, and are of the same rank, two node loss reduction is performed.

    Rank-list

    The rank-list is a doubly linked list containing information about each rank, to allow nodes of the same rank to be partnered together in the fix-list.

    For each node representing rank

    r

    in the rank-list, we maintain:

    r

    . If such a node does not exist, this is NULL.

    r

    and loss 1. If such a node does not exist, this is NULL.

    r+1

    and

    r-1

    , to facilitate the incrementation and decrementation of ranks.

    The fix-list and rank-list require extensive bookkeeping, which must be done whenever a new active node arises, or when the rank or loss of a node is changed.

    Shared flag

    The merge operation changes all of the active nodes of the smaller heap into passive nodes. This can be done in

    O(1)

    time by introducing a level of indirection. Instead of a boolean flag, each active node has a pointer towards an active flag object containing a boolean value. For passive nodes, it does not matter which active flag object they point to, as long as the flag object is set to passive, because it is not required to change many passive nodes into active nodes simultaneously.

    Storing keys

    The decrease-key operation requires a reference to the node we wish to decrease the key of. However, the decrease-key operation itself sometimes swaps the key of a node and the key root.

    Assume that the insert operation returns some opaque reference that we can call decrease-key on, as part of the public API. If these references are internal heap nodes, then by swapping keys we have mutated these references, causing other references to become undefined. To ensure a key is always stays with the same reference, it is necessary to 'box' the key. Each heap node now contains a pointer to a box containing a key, and the box also has a pointer to the heap node. When inserting an item, we create a box to store the key in, link the heap node to the box both ways, and return the box object. To swap the keys between two nodes, we re-link the pointers between the boxes and nodes instead.

    Operations

    Merge

    Let

    h1

    and

    h2

    be strict Fibonacci heaps. If either is empty, return the other. Otherwise, let

    n1

    and

    n2

    be their corresponding sizes. Without loss of generality, assume that

    n1\len2

    . Since the sizes of the fix-list and rank-list of each heap are logarithmic with respect to the heap size, it is not possible to merge these auxiliary structures in constant time. Instead, we throw away the structure of the smaller heap

    h1

    by discarding its fix-list and rank-list, and converting all of its nodes into passive nodes. This can be done in constant time, using a shared flag, as shown above. Link

    h1

    and

    h2

    , letting the root with the smaller key become the parent of the other. Let

    Q1

    and

    Q2

    be the queues of

    h1

    and

    h2

    respectively. The queue of resulting heap is set to

    Qmerged=Q1+\{rlarger\}+Q2

    , where

    rlarger

    is the root with the larger key.

    The only possible structural violation is the root degree. This is solved by performing 1 active root reduction, and 1 root degree reduction, if each transformation is possible.

    Active rootsTotal lossRoot degree
    State after merge

    0

    0

    +1

    1 x

    Active root reduction

    -1

    0

    +1

    1 x

    Root degree reduction

    +1

    0

    -2

    Total

    0

    0

    0

    Proof of correctness

    Invariants 1, 2, and 3 hold automatically, since the structure of the heap is discarded. As calculated above, any violations of invariant 4 are solved by the root degree reduction transformation.

    To verify invariant 5, we consider the final positions of nodes in

    Q

    . Each node has its degree bounded by

    2lg(2n-p)+10

    or

    2lg(2n-p)+9

    .

    For the smaller heap

    h1

    the positions in

    Q1

    are unchanged. However, all nodes in

    h1

    are now passive, which means that their constraint may change from the

    +10

    case to the

    +9

    case. But noting that

    n1\len2

    , the resulting size

    n=n1+n2

    is at least double

    n1

    . This results in an increase of at least 1 on each constraint, which eliminates the previous concern.

    The root with the larger key between

    h1

    and

    h2

    becomes a non-root, and is placed between

    Q1

    and

    Q2

    at position

    n1

    . By invariant 4, its degree was bounded by either

    R1+3=2lgn1+9

    or

    R2+3=2lgn2+9

    , depending on which heap it came from. It is easy to see that this is less than

    2lg{(2n1+2n2-n1)}+9

    in any case.

    For the larger heap, the positions increase by

    n1

    . But since the resulting size is

    n=n1+n2

    , the value

    2n-p

    actually increases, weakening the constraint.

    Insert

    Insertion can be considered a special case of the merge operation. To insert a single key, create a new heap containing a single passive node and an empty queue, and merge it with the main heap.

    Find-min

    Due to the minimum-heap property, the node with the minimum key is always at the root, if it exists.

    Delete-min

    If the root is the only node in the heap, we are done by simply removing it. Otherwise, search the children of the root to find the node

    x

    with minimum key, and set the new root to

    x

    . If

    x

    is active, make it passive, causing all active children of

    x

    to implicitly become active roots. Link the children of the old root to

    x

    . Since

    x

    is now the root, move all of its passive linkable children to the right, and remove

    x

    from

    Q

    .

    The degree of the root approximately doubles, because we have linked all the children of the old root to

    x

    . We perform the following restorative transformations:
    1. Repeat twice: rotate

    Q

    by moving the head

    y

    of

    Q

    to the back, and link the two rightmost passive children of

    y

    to the root.
    1. If a loss reduction is possible, perform it.
    2. Perform active root reductions and root degree reductions until neither is possible.

    To see how step 3 is bounded, consider the state after step 3:

    Active rootsTotal lossRoot degree
    State after delete-min

    +R

    0

    +R+6

    2 x

    Queue rotation

    0

    0

    +4

    1 x

    Loss reduction

    +1

    -1

    +1

    Total

    +R+1

    -1

    +R+11

    Observe that, 3 active root reductions and 2 root reductions decreases the root degree and active roots by 1:
    Active rootsTotal lossRoot degree

    3 x

    Active root reduction

    -3

    0

    +3

    2 x

    Root degree reduction

    +2

    0

    -4

    Total

    -1

    0

    -1

    Since

    R=2lgn+6

    , step 3 never executes more than

    O(logn)

    times.

    Proof of correctness

    Invariant 1 holds trivially, since no active roots are created.

    The size of the heap

    n

    decreases by one, causing

    R=2lgn+6

    decreases by at most one. Thus, invariant 3 is violated by at most 1. By lemma 3, loss reduction is possible, which has been done by step 2.

    Invariants 1 and 3 now hold. If invariants 2 and 4 were still violated after step 3, it would be possible to apply active root reduction and root degree reduction, by lemmas 2 and 4. However, active root reduction and root degree reduction have already been exhaustively applied. Therefore, invariants 2 and 4 also hold.

    To show that invariant 5 is satisfied, we first note that the heap size

    n

    has decreased by 1. Because the first 2 nodes in

    Q

    are popped in step 1, the positions of the other elements in

    Q

    decrease by 2. Therefore, the degree constraints

    2lg(2n-p)+10

    and

    2lg(2n-p)+9

    remain constant for these nodes. The two nodes which were popped previously had positions 1 and 2 in

    Q

    , and now have positions

    n-2

    and

    n-1

    respectively. The effect is that their degree constraints have strengthened by 2, however, we cut off two passive children for each of these nodes, which is sufficient to satisfy the constraint again.

    Decrease-key

    Let

    x

    be the node whose key has been decreased. If

    x

    is the root, we are done. Otherwise, detach the subtree rooted at

    x

    , and link it to the root. If the key of

    x

    is smaller than the key of the root, swap their keys.

    Up to three structural violations may have occurred. Unless

    x

    was already a child of the root, the degree of the root increases by 1. When

    x

    was detached from its original parent

    y

    , we have the following cases:

    x

    is passive, then there are no extra violations.

    x

    was previously an active root with

    y

    passive, then moving

    x

    from being a child of

    y

    to a child of the root does not create any additional active roots, nor does it increase the loss of any node.

    x

    and

    y

    are active, then the loss of

    y

    increases by 1, and an extra active root is created (by linking

    x

    to the root).In the worst case, all three quantities (root degree, total loss, active roots) increase by 1.

    After performing 1 loss reduction, the worst case result is still that the root degree and number of active roots have both increased by 2. To fix these violations, we use the fact that 3 active root reductions and 2 root reductions decrease both of these quantities by 1. Hence, applying these transformations 6 and 4 times respectively is sufficient to eliminate all violations.

    Active rootsTotal lossRoot degree
    State after decrease-key

    +1

    +1

    +1

    1 x

    Loss reduction

    +1

    -1

    +1

    6 x

    Active root reduction

    -6

    0

    +6

    4 x

    Root degree reduction

    +4

    0

    -8

    Total

    0

    0

    0

    Proof of correctness

    The nodes which were previously the left siblings of

    x

    move to fill the gap left by

    x

    , decreasing their index. Since their constraint has weakened, invariant 1 is unaffected. Invariant 5 trivially holds as

    Q

    is unchanged.

    Lemmas 2, 3 and 4 guarantee the availability of active root reduction, loss reduction, and root degree reduction. Therefore, invariants 2, 3 and 4 hold.

    Performance

    Although theoretically optimal, strict Fibonacci heaps are not useful in practical applications. They are extremely complicated to implement, requiring management of more than 10 pointers per node. While most operations run in

    O(1)

    time, the constant factors may be very high, making them up to 20 times slower than their more common counterparts such as binary heaps or pairing heaps.[6] Despite being relatively simpler, experiments show that in practice the strict Fibonacci heap performs slower than the Brodal queue.[7]

    External links

    Notes and References

    1. Brodal . Gerth Stølting. Okasaki . Chris. November 1996. Optimal purely functional priority queues . Journal of Functional Programming. 6 . 6 . 839–857. 10.1017/S095679680000201X. 0956-7968.
    2. Book: Knuth, Donald E. . The Art of Computer Programming: Sorting and Searching, Volume 3. 1998-04-24. Addison-Wesley Professional . 978-0-321-63578-5. en.
    3. Brodal . Gerth Stølting . 1996-01-28 . Worst-case efficient priority queues . Proceedings of the Seventh Annual ACM-SIAM Symposium on Discrete Algorithms . SODA '96 . USA . Society for Industrial and Applied Mathematics . 52–58 . 978-0-89871-366-4.
    4. Fredman . Michael L. . Tarjan . Robert Endre . 1987-07-01 . Fibonacci heaps and their uses in improved network optimization algorithms . Journal of the ACM . 34 . 3 . 596–615 . 10.1145/28869.28874 . 0004-5411.
    5. Book: Brodal . Gerth Stølting . Proceedings of the forty-fourth annual ACM symposium on Theory of computing . Lagogiannis . George . Tarjan . Robert E. . 2012-05-19 . Association for Computing Machinery . 978-1-4503-1245-5 . STOC '12 . New York, NY, USA . 1177–1184 . Strict fibonacci heaps . 10.1145/2213977.2214082 . https://doi.org/10.1145/2213977.2214082.
    6. Larkin . Daniel. Sen . Siddhartha . Tarjan . Robert. 2014. A Back-to-Basics Empirical Study of Priority Queues. Proceedings of the Sixteenth Workshop on Algorithm Engineering and Experiments. 61–72 . 1403.0252 . 2014arXiv1403.0252L . 10.1137/1.9781611973198.7 . 978-1-61197-319-8 . 15216766.
    7. Book: Mrena . Michal. Sedlacek . Peter. Kvassay . Miroslav. June 2019. 2019 International Conference on Information and Digital Technologies (IDT). IEEE. 9781728114019. Zilina, Slovakia. 335–344. Practical Applicability of Advanced Implementations of Priority Queues in Finding Shortest Paths. 10.1109/DT.2019.8813457 . 201812705.