fibonacci heaps - read only FH
fibonacci heaps - read only FH
Binomial heaps
Fibonacci heaps
December 3, 2013
Bk −1
Bk −1
B0 Bk
Another view
Lemma 1
The binomial tree Bk has the following properties:
1 It has 2k nodes.
2 It has height k .
k
3 There are exactly i nodes at depth i for i = 0, 1, . . . , k .
4 The root has degree k , which is greater than that of any
other node. Moreover, if the children of the root are
numbered from left to right by k − 1, k − 2, . . . , 0, then child
i is the root of a subtree Bi .
12 25 8 14 29
18 11 17 38
27
A binomial heap H with n = 13 nodes, made of trees B0 , B2 , and B3 . The key of any node is ≥ the key of its parent.
The binomial heap is represented as a linked list of the roots of the trees it contains, in order of increasing degree.
B INOMIAL L INK(y , z)
1 y ->p := z
2 y ->sibling := z->child
3 z->child := y
4 z->degree := z->degree + 1
It works in 2 phases:
1 First, B INOMIAL H EAP M ERGE(H1 , H2 ) merges the root lists
of H1 and H2 into a single list H that is sorted by degree
into monotonically increasing order.
2 There might be at most 2 roots of each degree ⇒ the
second phase links roots of equal degree until at most one
root remains of each degree.
The meaning of the local pointers used in the algorithm is:
x points to the root currently being examined.
prev _x points to the root preceding x on the root list, thus
prev _x->sibling = x.
next_x points to the root following x on the root list, thus
x->sibling = next_x.
Bk Bl Bk Bl
Bk Bk Bk Bk Bk Bk
Bk Bk Bl Bk Bl
x->key ≤ next_x->key Bk
Bk +1
Bk Bk Bl Bk Bl
x->key > next_x->key Bk
Bk +1
(a) H.head 25 12 6
37 18 10 8 14 29
42 16 28 13 11 17 38
x 26 23 77 27
42
(a) H.head 25 12 6
37 18 10 8 14 29
42 z 16 28 13 11 17 38
y 7 23 77 27
42
(b) H.head 25 12 6 z
37 18 z 10 8 14 29
42 y 7 28 13 11 17 38
16 23 77 27
42
(c) H.head 25 12 6 z
37 18 y 7 8 14 29
42 10 28 13 11 17 38
16 23 77 27
42
H.min
23 7 3 17 24
18 52 38 30 26 46
39 41 35
A Fibonacci heap made of 5 heap-ordered trees. The dashed line indicates the root
list. The 3 marked nodes are blackened.
A more complete representation of the previous heap, showing the pointers p (up
arrows), child (down arrows), and left and right (sideways arrows), is illustrated below.
H.min
Example
The Fibonacci heap illustrated before has t(H) = 5 and
m(H) = 2. Thus, the potential of H is
Φ(H) = 5 + 2 · 3 = 5 + 6 = 11.
Properties
If an n-node Fibonacci heap is made of unordered binomial
trees, then D(n) = log2 n.
P ROOF. Obvious.
Main idea. To make mergeable-heap operations performant,
delay their work as long as possible.
F IB H EAP I NSERT(H, x)
1 x->degree := 0
2 x->p := N IL
3 x->child := N IL
4 x->left := x
5 x->right := x
6 x->mark := false
7 concatenate the root list containing x with the root list H
8 if H.min = N IL or x->key < (H.min)->key
9 H.min = x
10 H.n := H.n + 1
H.min H.min
23 7 3 17 24 23 7 21 3 17 24
18 52 38 30 26 46 18 52 38 30 26 46
39 41 35 39 41 35
(a) (b)
(a) A Fibonacci heap H. (b) Fibonacci heap H 0 produced after
inserting the node with key 21 in H. The node becomes its own
heap-ordered tree and is then added to the root list, becoming the left
sibling of the root.
t(H 0 ) = t(H) + 1
the increase in potential is
⇒
m(H 0 ) = m(H) ((t(H) + 1 + 2 m(H)) − (t(H) + 2 m(H)) = 1.
F IB H EAP U NION(H1 , H2 )
1 H := M AKE F IB H EAP()
2 H.min := H1 .min
3 concatenate the root list of H2 with the root list of H
4 if (H1 .min = N IL) or (H2 .min 6= N IL and H2 .min < H1 .min)
5 H.min := H2 .min
6 H.n := H1 .n + H2 .n
7 free the objects H1 and H2
8 return H
C ONSOLIDATE(H)
1. for i := 0 to D(H.n)
2. A[i] = N IL
3. for each node w in the root list of H
4. x := w
5. d := x->degree
6. while A[d] 6= N IL
7. y := A[d]
8. if x->key > y ->key
9 exchange x ↔ y
10. F IB H EAP L INK(H, y , x)
11. A[d] := N IL
12. d := d + 1
13. A[d] := x
14. H.min := N IL
15. for i := 0 to D(H.n)
16. if A[i] 6= N IL
17. add A[i] to the root list of H
18. if H.min = N IL or A[i]->key < H.min->key
19. H.min := A[i]
F IB H EAP L INK(H, y , x)
1. remove y form the root list of H
2. make y a child of x, incrementing x->degree
3. y ->mark := false
F IB H EAP L INK(H, y , x)
1. remove y form the root list of H
2. make y a child of x, incrementing x->degree
3. y ->mark := false
Ilustrated example
(b) The situation after the minimum node z is removed from the root list and its children are added to the root list.
(c)-(d) The array A and the trees after each of the first 2 iterations of the for loop of lines 3-13 of C ONSOLIDATE.
(e) The array A and the trees after each of the 3rd iteration of the for loop of lines 3-13 of C ONSOLIDATE. (f)-(h) The
next iteration of the for loop, with the values of w and x shown at the end of each iteration of the while loop of lines
6-12. Part (f) shows the situation after the first time through the while loop. The node with key 23 has been linked to
the node with key 7, which is now pointed to by x. In part (g), the node with key 17 has been linked to the node with
(i)-(l) The situation after each of the next four iterations of the while loop.
(m) Fibonacci heap after reconstruction of the root list from the array A and determination of the new H.min pointer.
Observation:
The operations presented so far for Fibonacci heaps did
preserve the property that all trees in the Fibonacci heap
are unordered binomial trees Un .
The operations that will be presented do not preserve this
property.
C UT(H, x, y )
1 remove x from the child list of y , decrementing y ->degree
2 add x to the root list of H
3 x->p := N IL
4 x->mark := false
Lectures 9-10: Mergeable Heaps
Decreasing a key (2)
C ASCADING C UT(H, y )
1 z := y ->p
2 if z 6= N IL
3 if y ->mark = false
4 y ->mark := true
5 else C UT(H, y , z)
6 C ASCADING C UT(H, z)
Lines 1-3 of F IB H EAP D ECREASE K EY ensure that new key should be < current key.
If x is a root (that is, x->p = N IL) or else x->key ≥ x->p->key , then no structural
changes need occur because the heap order is preserved by the key replacement.
If the heap order is violated ⇒ x is cut out from its siblings in line 6, and moved into the
root list of the heap.
The purpose of the mark fields is to ensure short time bounds for the heap operations.
To understand how it is used, let’s consider that x is a pointer to a node that went
through the following situations:
At some point, x was a root.
Later on, x was linked to another node.
Later on, two children of x were removed by cuts.
When the 2nd child is cut, x is cut from its parent and becomes a new root. We have
x->mark = true if steps 1 and 2 happened and one child of x has been cut. Thus,
C UT sets x->mark = false in line 4 because it performs step 1.
The C ASCADING C UT calls in line 7 of F IB H EAP D ECREASE K EY take care of the
situation when x might be the second child cut from its parent y since the time that y
was linked to another node. It recurses up the tree until either a root or an unmarked
node is found. After all cascading cuts were done, lines 8-9 of F IB H EAP D ECREASE K EY
update the value of H.min accordingly.
(a) 7 18 38 (b) 15 7 18 38
24 17 23 21 39 41 24 17 23 21 39 41
26 46 30 52 26 30 52
35 H.min 35 H.min
(c) 15 5 7 18 38 (d) 15 5 26 7 18 38
24 17 23 21 39 41 24 17 23 21 39 41
26 30 52 30 52
H.min
(e) 15 5 26 24 7 18 38
17 23 21 39 41
30 52
(a) The initial Fibonacci heap. (b) key 46 was decreased to 15. (3) The node becomes
a root, and its parent (with key 24) gets marked. (c)–(e) the node with key 35 has its
key decreased to 5. Its parent (key 26) is marked, and a cascading cut occurs. The
node with key 26 is cut from its parent and becomes an unmarked root in (d). Another
cascading occurs since node with key 24 is marked too. This node gets cut from its
parent and made an unmarked root in (e). At this stage, cascading stops.
F IB H EAP D ELETE(H, x)
1 F IB H EAP D ECREASE K EY(H, x, −∞)
2 F IB H EAP E XTRACT M IN(H)