0% found this document useful (0 votes)
12 views

fibonacci heaps - read only FH

Lectures 9-10 cover mergeable heaps, focusing on binomial and Fibonacci heaps, detailing their operations and properties. Binomial heaps consist of binomial trees that are heap-ordered and allow efficient merging, insertion, and extraction of minimum elements. The lectures also outline the running time complexities of various operations, comparing them across different heap types.

Uploaded by

vishalporwar99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

fibonacci heaps - read only FH

Lectures 9-10 cover mergeable heaps, focusing on binomial and Fibonacci heaps, detailing their operations and properties. Binomial heaps consist of binomial trees that are heap-ordered and allow efficient merging, insertion, and extraction of minimum elements. The lectures also outline the running time complexities of various operations, comparing them across different heap types.

Uploaded by

vishalporwar99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Lectures 9-10: Mergeable Heaps

Binomial heaps
Fibonacci heaps

December 3, 2013

Lectures 9-10: Mergeable Heaps


Mergeable heaps
Data structures designed to support well the following
operations:
M AKE H EAP(): creates and returns a new empty heap.
I NSERT(H, x): inserts node x, whose key field has already been
filled in, into heap H.
M INIMUM(H): returns a pointer to the node with minimum key in
heap H.
E XTRACT M IN(H): deletes the node from heap H whose key is
minimum, returning a pointer to the node.
U NION(H1 , H2 ): creates and returns a new heap that contains all
the nodes of heaps H1 and H2 . Heaps H1 and H2 are
“destroyed” by this operation.
and also
D ECREASE K EY(H, x, k ) assigns to node x within heap H the
new key value k . It is assumed that key ≤ x.key .
D ELETE(H, x) deletes node x from heap H.
Lectures 9-10: Mergeable Heaps
Content of the lecture
In this lecture we will study binomial heaps. Later on, after we
will explain the notion of amortized time bounds of algorithms,
we will study Fibonacci heaps. If we also add binary heaps to
the picture (see Lecture 9), we will be able to draw a
comparison table of their running times:
Binary heap Binomial heap Fibonacci heap
Procedure (worst-case) (worst case) (amortized)
M AKE H EAP Θ(1) Θ(1) Θ(1)
I NSERT Θ(log n) O(log n) Θ(1)
M INIMUM Θ(1) O(log n) Θ(1)
E XTRACT M IN Θ(log n) Θ(log n) O(log n)
U NION Θ(log n) O(log n) Θ(1)
D ECREASE K EY Θ(log n) Θ(log n) Θ(1)
D ELETE Θ(log n) Θ(log n) Θ(log n)

Note. The notion of amortized time bounds will be explained in


another lecture.
Lectures 9-10: Mergeable Heaps
Binomial trees
A binomial tree is an element of the set of ordered trees
{Bk | k ∈ N} defined recursively as follows:
B0 consists of a single node.
Bk consists of two binomial trees Bk −1 that are linked such
that one of them has the other one as left child of its root.

Example (B0 , B1 , B2 , B3 , and B4 )


depth
0
1
2
3
4
B0 B1 B2 B3 B4

Lectures 9-10: Mergeable Heaps


The structure of binomial trees

The recursive view (Cf. the definition)

Bk −1
Bk −1

B0 Bk

Another view

Lectures 9-10: Mergeable Heaps


Properties of binomial trees

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 .

Lectures 9-10: Mergeable Heaps


Binomial heaps

A binomial heap H is a set of binomial trees that satisfies the


following binomial heap properties:
1 Each binomial tree in H is heap-ordered: the key of a node
is greater than or equal to the key of its parent.
2 There is at most one binomial tree in H whose root has a
given degree.

Lectures 9-10: Mergeable Heaps


Binomial heaps

A binomial heap H is a set of binomial trees that satisfies the


following binomial heap properties:
1 Each binomial tree in H is heap-ordered: the key of a node
is greater than or equal to the key of its parent.
2 There is at most one binomial tree in H whose root has a
given degree.
Consequences of the definition
The root of a heap-ordered tree contains the smallest key in the
tree.
The second property implied that an n-node binomial heap H
has at most blog2 nc + 1 binomial trees. This is so because:
The binary representation of nhas ≤ blog2 nc + 1 bits, say
Pblog nc
bblog2 nc , bblog2 nc−1 , . . . , b0 , so that n = i=0 2 bi · 2i . From
Lemma 1 we learn that Bi appears in H if and only if bi = 1.
Thus, H contains at most blog2 nc + 1 binomial trees.

Lectures 9-10: Mergeable Heaps


Binomial heaps
H.head 10 1 6

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.

A more detailed view of the illustrated heap.

Lectures 9-10: Mergeable Heaps


Representing binomial heaps
Binomial trees are stored in the left-child, right-sibling
representation.
Each node x contains the following pointers:
x.p: pointer to its parent. It is nil if x is a root.
x.child: pointer to its left child. It is nil if x has no children.
x.sibling: pointer to its right sibling. It is nil if x is the
rightmost child of its parent.
and the fields
x.key : the key of the node.
possibly other fields for the satellite information required by
the application.
x.degree: the number of children of x.
The roots of the binomial trees of a binomial heap H for a linked
list of nodes, connected through their sibling pointers. This list is
called called the root list of the heap. The degrees of the roots
strictly increase as we traverse the root list.
A binomial heap H is accessed by the field H.head, which is a
pointer to the first node in the root list of H.
Lectures 9-10: Mergeable Heaps
Binomial heaps
Property

In a binomial heap with n nodes, the degrees of the roots of its


binomial trees are a subset of {0, 1, . . . , blog2 nc}.

Lectures 9-10: Mergeable Heaps


Operations on binomial heaps (1)
M AKE B INOMIAL H EAP() creates an empty binomial heap h by
allocating space for H and setting H.head = N IL. The running
time is, obviously, Θ(1).
B INOMIAL H EAP M IN(H) returns a pointer to the node with the
minimum key in an n-node heap. It is assumed that there are no
keys with value ∞ in H.
Binomial heaps are heap-ordered ⇒ the minimum key is in
a node in the root list of the heap.
The root list had length ≤ blog2 nc + 1 ⇒ the running time of
this operation is O(log2 n).
B INOMIAL H EAP M IN(H)
1 y := N IL
2 x := H.head
3 min := ∞
4 while x 6= N IL
5 if x->key < min
6 min = x->key
7 y := x
8 x := x->sibling
9 return y

Lectures 9-10: Mergeable Heaps


Operations on binomial heaps (2)
Uniting two binomial heaps

B INOMIAL H EAP U NION(H1 , H2 ) performs the union of binomial


heaps H1 , H2 by linking repeatedly binomial trees whose roots
have the same degree.
If y and z are roots of two Bk −1 -trees, then B INOMIAL L INK(y , z)
makes a Bk -tree with root z by setting the parent of y to be z, the
leftmost child of z to be y , and increasing the degree of z by 1:

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

B INOMIAL H EAP M ERGE(H1 , H2 ) merges the root lists of


binomial heaps H1 and H2 into a single linked list that is sorted
by degree into monotonically increasing order.

Lectures 9-10: Mergeable Heaps


Operations on binomial heaps (2)
B INOMIAL H EAP U NION(H1 , H2 )
1 H := M AKE B INOMIAL H EAP()
2 H.head = BinomialHeapMerge(H1 , H2 )
3 free the objects H1 and H2 , but not the lists they point to
4 if H.head = N IL
5 return H
6 prev _x := N IL
7 x := H.head
8 next_x := x->sibling
9 while next_x 6= N IL
10 if x->degree 6= next_x->degree or
(next_x->sibling 6= N IL and next_x->sibling->degree =x->degree)
11 prev _x := x // Cases 1 and 2
12 x := next_x // Cases 1 and 2
13 else if x->key ≤ next_x->key
14 x->sibling := next_x->sibling // Case 3
15 B INOMIAL L INK(next_x, x) // Case 3
16 else if prev _x = N IL // Case 4
17 H.head := next_x // Case 4
18 else prev _x->sibling := next_x // Case 4
19 B INOMIAL L INK(x, next_x) // Case 4
20 x := next_x // Case 4
21 next_x := x->sibling
22 return H
Lectures 9-10: Mergeable Heaps
B INOMIAL H EAP U NION
Remarks about the implementation

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.

Lectures 9-10: Mergeable Heaps


B INOMIAL H EAP U NION
A study by cases

There are four cases, illustrated below. Labels a, b, c, d serve


only to identify the roots involved; they do not indicate the
degrees or keys of these roots.
Case 1: x->degree 6= next_x->degree.
prev _x x next_x next_x->sibling prev _x x next_x
... a b c d ... ... a b c d ...
Case 1

Bk Bl Bk Bl

Case 2: x->degree = next_x->degree = next_x->sibling->degree.

prev _x x next_x next_x->sibling prev _x x next_x


... a b c d ... ... a b c d ...
Case 2

Bk Bk Bk Bk Bk Bk

Lectures 9-10: Mergeable Heaps


B INOMIAL H EAP U NION
A study by cases (continued)

Case 3: x->degree = next_x->degree 6= next_x->sibling->degree and


x->key ≤ next_x->key .
prev _x x next_x next_x->sibling prev _x x next_x
... a b c d ... ... a b d ...
Case 3 c

Bk Bk Bl Bk Bl
x->key ≤ next_x->key Bk
Bk +1

Case 3: x->degree = next_x->degree 6= next_x->sibling->degree and


x->key ≤ next_x->key .
prev _x x next_x next_x->sibling prev _x x next_x
... a b c d ... ... a c d ...
Case 4
b

Bk Bk Bl Bk Bl
x->key > next_x->key Bk
Bk +1

Lectures 9-10: Mergeable Heaps


Execution of B INOMIAL H EAP U NION
Running example

Lectures 9-10: Mergeable Heaps


Execution of B INOMIAL H EAP U NION
Running example

Lectures 9-10: Mergeable Heaps


Run time complexity of B INOMIAL H EAP U NION

Assumptions. H1 contains n1 nodes, H2 contains n2 nodes,


and let n = n1 + n2 .
Then
H1 contains ≤ blog2 n1 c + 1 roots, and H2 contains
≤ blog2 n2 c + 1 roots
⇒ H contains at most
blog2 n1 c + blog2 n2 c + 2 ≤ 2blog2 nc + 2 = O(log2 n) roots
immediately after the call of B INOMIAL H EAP M ERGE.
B INOMIAL H EAP M ERGE(H1 , H2 ) takes O(log2 n) time.
Each iteration of the while loop takes O(1) time, and there
are at most blog2 n1 c + blog2 n2 c + 2 iterations because
each iteration either advances the pointers one position
down the root list or removes a root from the root list.
⇒ total run time is O(log2 n).

Lectures 9-10: Mergeable Heaps


Inserting a node

Assumptions: Node x has already been allocated, and key


x->key has already been filled in.

B INOMIAL H EAP I NSERT(H, x)


1 H 0 := M AKE B INOMIAL H EAP()
2 x->p := N IL
3 x->child := N IL
4 x->sibling := N IL
5 x->degee := 0
6 H 0 .head = x
7 H := B INOMIAL H EAP U NION(H, H 0 )

Lectures 9-10: Mergeable Heaps


Extracting the node with minimum key

B INOMIAL H EAP E XTRACT M IN(H)


1 Find the root with the minimum key in the root list of H
and remove x from the root list of H
0
2 H := M AKE B INOMIAL H EAP()
3 Reverse the order of the linked list of x’s children,
and set H 0 .head to point to the head of the resulting list
4 H := B INOMIAL H EAP U NION(H, H 0 )
5 return x

Lectures 9-10: Mergeable Heaps


Extracting the node with minimum key
Example

Lectures 9-10: Mergeable Heaps


Extracting the node with minimum key
Example continued

Lectures 9-10: Mergeable Heaps


Decreasing a key

B INOMIAL H EAP D ECREASE K EY(H, x, k ) decreases the key of a


node x in a binomial heap to a new value k . It signals an error if
k > x->key .
B INOMIAL H EAP D ECREASE K EY(H, x, k )
1 if k > x->key
2 error “new key is greater than current key”
3 x->key := k
4 y := x
5 z := y ->p
6 while z 6= N IL and y ->key < z->key
7 exchange y ->key ↔ z->key
8 if y and z have satellite fields, exchange them, too
9 y := z
10 z := y ->p

Lectures 9-10: Mergeable Heaps


B INOMIAL H EAP D ECREASE K EY(H, x, k )
Example

Let’s decrease the key of node x in H to k = 7.

(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

Lectures 9-10: Mergeable Heaps


B INOMIAL H EAP D ECREASE K EY(H, x, k )
Example (continued)

(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

Lectures 9-10: Mergeable Heaps


B INOMIAL H EAP D ECREASE K EY(H, x, k )
Remarks about the implementation

The decreased key “bubbles up” in the heap:


After ensuring that k ≤ x->key and then assigning key k to x,
the procedure goes up the tree, with y initially pointing to x.
In each iteration of the while loop of lines 6-10, y ->key is
checked against the key of y ’s parent z:
If y is the root or y ->key ≥ z->key , the tree is
heap-ordered.
Otherwise, node y violates the heap ordering, therefore its
key is exchanged with the key of its parent z, along with any
other satellite information.
The procedure then sets y to z, going up one level in the tree,
and continues with the next iteration.
The time complexity of B INOMIAL H EAP D ECREASE K EY(H, x, k )
is O(log2 n) because the maximum depth of x is blog2 nc, so the
while loop iterates at most blog2 nc times.

Lectures 9-10: Mergeable Heaps


Deleting a node

Deleting a node x from a binomial heap H is trivial:


First, decrease the key of x to a value smaller than any key
in H, e.g., −∞.
Next, extract from H the node with minimal key, which is x
with key −∞.

B INOMIAL H EAP D ELETE(H, x)


1 B INOMIAL H EAP D ECREASE K EY(H, x, −∞)
2 B INOMIAL H EAP E XTRACT M IN(H)

If H has n nodes, then B INOMIAL H EAP D ELETE(H, x) takes


O(log2 n) time.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Structure of Fibonacci heaps (I)

A Fibonacci heap is a collection of heap-ordered trees. The


trees are rooted, but unordered:
Each node x contains
the fields
x.key and possibly more fields for satellite data associated
with the key.
x.degree: number of children in the child list of node x.
x.mark : a boolean value, which indicates whether node x
has lost a child since the last time x was made the child of
another node. Newly created nodes are unmarked.
the pointers
x.p: pointer to the parent node; N IL if x is a root node.
x.child: pointer to any one of its children.
The children of a node x are linked together in a circular,
doubly-linked list, called the child list of x.
Each node n in a child list has pointers n.left and n.right
which point to its left and right siblings, respectively. If y is a
reference to the only child, then y ->left = y ->right = y .
The order in which children appear in the child list is arbitrary.
Lectures 9-10: Mergeable Heaps
Fibonacci heaps
Structure of Fibonacci heaps (II)

The roots of all trees in a Fibonacci heap H are linked together


into a circular, doubly linked list, using the left and right fields of
the root nodes. This circular list is called the root list of the
Fibonacci heap.
The order of the trees in the root list is arbitrary.
A Fibonacci heap H has 2 fields:
H.min: a pointer to the root of the tree in H with minimum
key; this node is called the minimum node of the Fibonacci
heap.
H.n: the number of nodes currently in H.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Structure of Fibonacci heaps (III)

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

Lectures 9-10: Mergeable Heaps


The potential function of a Fibonacci heap

Assumption: H is a Fibonacci heap. We define the following


notions:
t(H): the number of trees in the root list of H
m(H): the number of marked nodes in H.
Φ(H) := t(H) + 2 · m(H) is called the potential of H.
The potential of a set of Fibonacci heaps is the sum of the
potentials of its constituent Fibonacci heaps.

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.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Assumptions about the potential function

Φ will be used for amortized analysis with the potential


method (Cf. Lecture 8.)
1 unit of potential can pay for a constant amount of work,
where the constant is sufficiently large to cover the cost of
any specific constant-time piece of work that we might
encounter.
The Fibonacci heap is initially empty ⇒ initial potential is 0.
By definition, Φ(H) ≥ 0 always holds ⇒ for a sequence of
heap operations, the total amortized cost is an upper
bound of the total actual cost.
D(n) is an upper bound on the maximum degree of any
node in an n-node Fibonacci heap.

Lectures 9-10: Mergeable Heaps


Mergeable heap operations
Assumption. Only M AKE H EAP, I NSERT, M INUMUM,
E XTRACT M IN and U NION are supported. ⇒ Fibonnacci heap =
collection of unordered binomial trees, that is, elements of the
set {Un | n ∈ N} defined recursively as follows:
U0 has a single node.
Uk is obtained from two trees Uk −1 , for which the root of
one is made into any child of the root of the other.
Properties of unordered binomial trees
For the unordered binomial tree Uk ,
1 there are 2k nodes,
2 the height is 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. The children of the root are roots of subtrees
U0 , U1 , . . . , Uk −1 in some order.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps

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.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Creating a heap: M AKE F IB H EAP

M AKE F IB H EAP allocates and returns the Fibonacci heap object


H with H.n = 0 and H.min = N IL.
. Φ(H) = 0
. For M AKE B IN H EAP: amortized cost = actual cost = O(1).

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Inserting a node (2)

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

Note. Unlike B INOMIAL H EAP I NSERT, F IB H EAP I NSERT does not


attempt to merge trees within the Fibonacci heap.

Lectures 9-10: Mergeable Heaps


Fibonnacci heaps
Inserting a node (2)

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.

Actual cost of F IB H EAP I NSERT is O(1) ⇒


Amortized cost of F IB H EAP I NSERT is O(1) + 1 = O(1).

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Finding the minimum node. Uniting Fibonacci heaps (1)

H.min points to the minimum node of H ⇒ finding it takes O(1)


actual time.
Φ(H) does not change ⇒ amortized cost of
F IB H EAP M INIMUM is O(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

Note. No consolidation of trees occurs in the Fibonacci heap.


Lectures 9-10: Mergeable Heaps
Fibonacci heaps
Uniting Fibonacci heaps (2)

The change in potential is

Φ(H) − (Φ(H1 ) + Φ(H2 ))


= (t(H) + 2 m(H)) − ((t(H1 ) + 2 m(H1 ))
+ ((t(H2 ) + 2 m(H2 ))
= 0,

because t(H) = t(H1 ) + t(H2 ) and m(H) = m(H1 ) + m(H2 )


⇒ amortized cost of F IB H EAP U NION = actual cost = O(1).

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Extracting the minimum node

Preliminary remark. This is the operation where all work


delayed by other operations is done.
delayed work = consolidation (or merging) of the trees in
the root list.
F IB H EAP E XTRACT M IN(H)
1 z := H.min
2 if z 6= N IL
3 for each child x of z
4 add x to the root list of H
5 x->p := N IL
6 remove z from the root list of H
7 if z == z->right
8 H.min = N IL
9 else H.min = z->right
10 C ONSOLIDATE(H)
11 H.n := H.n − 1
12 return z
Lectures 9-10: Mergeable Heaps
Fibonacci heaps
Extracting the minimum node - description of the pseudocode

F IB H EAP E XTRACT M IN makes a root out of each of the


minimum node-s children and removes the minimum node
from the root list.
Next, it consolidates the root list by linking roots of equal
degree until at most one root remains of each degree.
Consolidate(H) consolidates the root list of H by executing
repeatedly the following steps until every root in the root list
has a distinct degree value:
1 Find two roots x and y from the root list with the same
degree, and with x->key ≤ y ->key .
2 Link y to x: remove y from the root list, and make y a child
of x. This operation is performed by F IB H EAP L INK.
Consolidate(H) uses an auxiliary array A[0..D(H.n)]; if
A[i] = y then y is currently a root with y ->degree = i.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node
The C ONSOLIDATE procedure

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]

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node

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

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node

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.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node

Illustrated example continued

(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

key 7, which is still pointed to by x.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node

Illustrated example continued

(i)-(l) The situation after each of the next four iterations of the while loop.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node

Illustrated example continued

(m) Fibonacci heap after reconstruction of the root list from the array A and determination of the new H.min pointer.

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node
The amortized cost (1)

Let H be the Fibonacci heap before the call of


F IB H EAP E XTRACT M IN (H ).
F IB H EAP E XTRACT M IN(H) contributes O(D(n)) from the
extraction of at most D(n) children of the minimum node that are
processed in F IB H EAP E XTRACT M IN and from the work done in
lines 1-2 and 14-19 of C ONSOLIDATE.
It remains to analyze the contribution of the for loop of
lines 3-13.
When C ONSOLIDATE is called, the root list has size
≤ D(n) + t(H) − 1.
Every while loop of lines 6-12 links one root to another ⇒
the amount of work performed in the while loop is
≤ D(n) + t(H).
⇒ total actual work is O(D(n) + t(H)).

Lectures 9-10: Mergeable Heaps


Fibonacci heaps – extracting the minimum node
The amortized cost (2)

The potential before extracting the minimum node is


t(H) + 2 m(H).
At most D(n) + 1 roots remain and no nodes become
marked during the operation ⇒ the potential after
extracting the minimum node is ≤ (D(n) + 1) + 2 m(H).
⇒ the amortized cost is at most

O(D(n) + t(H)) + ((D(n) + 1) + 2 m(H)) − (t(H) + 2 m(H))


= O(D(n)) + O(t(H)) − t(H)
= O(D(n)),

because the units of potential can be scaled to dominate


the constant hidden in O(t(H)).

Lectures 9-10: Mergeable Heaps


Fibonacci heaps
Decreasing a key and deleting a node

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.

Lectures 9-10: Mergeable Heaps


Decreasing a key (1)
F IB H EAP D ECREASE K EY(H, x, k ) decreases the key of a node
x in a binomial heap to a new value k . It signals an error if
k > x->key .
F IB H EAP D ECREASE K EY(H, x, k )
1 if k > x->key
2 error “new key is greater than current key”
3 x->key := k
4 y := x->p
5 if y 6= N IL and x->key < y ->key
6 C UT(H, x, y )
7 C ASCADING C UT(H, y )
8 if x->key < H.min->key
9 H.min := x

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)

Lectures 9-10: Mergeable Heaps


Decreasing a key (3)
Comments on the implementation

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.

Lectures 9-10: Mergeable Heaps


Decreasing a key: Example
H.min H.min

(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.

Lectures 9-10: Mergeable Heaps


Deleting a node

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)

The amortized execution time of F IB H EAP D ELETE(H, x) is


the sum of the amortized time O(1) to perform
F IB H EAP D ECREASE K EY(H, x, −∞), with the amortized
time O(D(n)) to perform F IB H EAP E XTRACT M IN(H).

Lectures 9-10: Mergeable Heaps


Bounding the maximum degree

The last thing to do is to compute an upper bound D(n) for


the maximal degree of the unordered trees in the
Fibonacci heap.
We noticed that if all trees in the Fibonacci heap are
unordered binomial trees, then D(n) = blog2 nc. But the
(cascading) cuts may cause the occurrence of trees that
are not unordered binomial.
Therefore, a slightly weaker result√
still holds:
D(n) ≤ blogφ (n)c where φ = (1 + 5)/2.
For a proof of this result, see Chapter 21 of the book
Introduction to Algorithms by Cormen et al.

Lectures 9-10: Mergeable Heaps

You might also like