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

Mergeable Heap

Uploaded by

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

Mergeable Heap

Uploaded by

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

(a) A Fibonacci heap consisting of five min-

(b) A more complete representation showing pointers p (up


heap-ordered trees and 14 nodes.
arrows),
child (down arrows), and left and right (sideways arrows).
The dashed line indicates the root list.

The minimum node of the heap is the node


containing the key 3.
The three marked nodes are blackened. The
potential of this particular Fibonacci heap is
5+2·3 = 11.
lines 1-6 initialize the structural fields of node x, making it its own circular, doubly linked list,

line 7 adds x to the root list of H in O(1) actual time.


Thus, node x becomes a single-node min-heap-ordered tree, and thus an unordered binomial tree, in the Fibonacci heap.
It has no children and is unmarked.

Lines 8-9 update the pointer to the minimum node of Fibonacci heap H if necessary.

line 10 increments n[H] to reflect the addition of the new node.


Inserting a node into a Fibonacci heap.

(a) A Fibonacci heap H


(b) Fibonacci heap H after the node with key 21 has been inserted.
The node becomes its own min-heap ordered tree and is then added to the root list, becoming the left sibling of the root

FIB-HEAP-INSERT makes no attempt to consolidate the trees within the Fibonacci heap. If k consecutive FIB-HEAP-INSERT
operations occur, then k single-node trees are added to the root list.
Lines 1-3 concatenate the root lists of H1 and H2 into a new root list H.

Lines 2, 4, and 5 set the minimum node of H , and

line 6 sets n[H] to the total number of nodes.

Line 7,the Fibonacci heap objects H1 and H2 are freed

line 8 returns the resulting Fibonacci heap H.

As in the FIB-HEAP-INSERT procedure, no consolidation of trees occurs


The process of extracting the minimum node is the most complicated of the operations.

It is also where the delayed work of consolidating trees in the root list finally occurs.

The following pseudocode extracts the minimum node.

The code assumes for convenience that when a node is removed from a linked list, pointers remaining in the list are updated, but pointers in the
extracted node are left unchanged. It also uses the auxiliary procedure CONSOLIDATE
The binomial tree Bk is an ordered tree defined recursively. (An ordered tree is a rooted tree in which the children of each node
are ordered. That is, if a node has k children, then there is a first child, a second child,..., and a kth child.)

For e.g., the binomial tree B0 consists of a single node.


The binomial tree Bk consists of two binomial trees Bk-1 that are linked together: the root of one is the leftmost child of the root
of the other.
Figure 19.2(b) shows the binomial trees B0 through B4

(a) The recursive definition of the binomial tree Bk. Triangles represent rooted subtrees. (b) The binomial trees B0 through B4
Proof:
The proof is by induction on k.
For each property, the basis is the binomial tree B0.
Verifying that each property holds for B0 is trivial.

For the inductive step, we assume that the lemma holds for Bk-1.

1. Binomial tree Bk consists of two copies of Bk-1, and so Bk has 2k-1 + 2k-1 = 2k nodes.

2. Because of the way in which the two copies of Bk-1 are linked to form Bk, the maximum depth of a node in Bk is one greater
than the maximum depth in Bk-1. By the inductive hypothesis, this maximum depth is (k - 1) + 1 = k.

3. Let D(k, i) be the number of nodes at depth i of binomial tree Bk. Since Bk is composed of two copies of Bk-1 linked together, a
node at depth i in Bk-1 appears in Bk once at depth i and once at depth i + 1. In other words, the number of nodes at depth i in Bk
is the number of nodes at depth i in Bk-1 plus the number of nodes at depth i - 1 in Bk-1. Thus,

4. The only node with greater degree in Bk than in Bk-1 is the root, which has one more child than in Bk-1. Since the root of
Bk-1 has degree k - 1, the root of Bk has degree k. Now, by the inductive hypothesis, from left to right, the children of the root
of Bk-1 are roots of Bk-2, Bk-3, ..., B0. When Bk-1 is linked to Bk-1, therefore, the children of the resulting root are roots of
Bk-1, Bk-2, ..., B0.
The maximum degree of any node in an n-node binomial tree is lg n.

The term "binomial tree" comes from property 3, since the terms are the binomial
coefficients
A binomial heap H with 13 nodes.
The binary representation of 13 is 1101,
and H consists of min-heap-ordered
binomial trees B3, B2, and B0, having 8,
4, and 1 nodes respectively, for a total of
13 nodes.

A more detailed representation of binomial heap


H.
Each binomial tree is stored in the left-child,
right-sibling representation, and each node stores
its degree.
Each binomial tree within a binomial heap is stored in the left-child, right-sibling representation.

Each node has a key field and any other satellite information required by the application. In addition, each node x contains:
a. pointers p[x] to its parent,
b. child[x] to its leftmost child, and
c. sibling[x] to the sibling of x immediately to its right.
d. field degree[x], which is the number of children of x.

If node x is a root, then p[x] = NIL.

If node x has no children, then child[x] = NIL, and

If x is the rightmost child of its parent, then sibling[x] = NIL.

Root-list: The roots of the binomial trees within a binomial heap are organized in a linked list.The degrees of the roots strictly increase as we traverse the
root list.
By the second binomial-heap property, in an n-node binomial heap the degrees of the roots are a subset of {0, 1, ..., ⌊lg n⌋}.

The sibling field has a different meaning for roots than for nonroots. If x is a root, then sibling[x] points to the next root in the root list. (As usual, sibling[x]
= NIL if x is the last root in the root list.)

head[H}: A given binomial heap H is accessed by the field head[H], which is simply a pointer to the first root in the root list of H . If binomial heap H has
no elements, then head[H] = NIL
To make an empty binomial heap,
MAKE-BINOMIAL-HEAP procedure simply allocates and
returns an object H , where head[H ] = NIL.
The running time is Θ(1).
The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node binomial heap H. This implementation
assumes that there are no keys with value ∞

Since a binomial heap is min-heap-ordered, the minimum key must reside in a root node.

The BINOMIAL-HEAP-MINIMUM procedure checks all roots, which number at most⌊lg n⌋ + 1,


saving the current minimum in min and a pointer to the current minimum in y.

When called on the binomial heap, BINOMIAL-HEAP-MINIMUM returns a pointer to the node with
key 1 (refer same example shared earlier).

Because there are at most ⌊lg n⌋ + 1 roots to check, the running time of BINOMIAL-HEAP-
MINIMUM is O(lg n).
The operation of uniting two binomial heaps is used as a subroutine by most of the remaining operations.

The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have the same
degree.

The following procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted at node z; that is, it
makes z the parent of y. Node z thus becomes the root of a Bk tree.

The BINOMIAL-LINK procedure makes node y the new head of


the linked list of node z's children in O(1) time. It works because
the left-child, right-sibling representation of each binomial tree
matches the ordering property of the tree: in a Bk tree, the
leftmost child of the root is the root of a Bk-1 tree
The following procedure unites binomial heaps H1 and H2, returning the resulting heap.

It destroys the representations of H1 and H2 in the process.

Besides BINOMIAL-LINK, the procedure uses an auxiliary procedure BINOMIAL-HEAP-MERGE that merges the root lists of H1 and H2 into a single
linked list that is sorted by degree into monotonically increasing order.
The running time of BINOMIAL-HEAP-UNION is O(lg n), where n is the total number of nodes in binomial heaps H1 and H2.

Let H1 contain n1 nodes and H2 contain n2 nodes, so that n = n1 + n2.


Then H1 contains at most ⌊lg n1⌋+1 roots and H2 contains at most ⌊lg n2⌋+1 roots, and
H contains at most ⌊lg n1⌋+⌊lg n2⌋+2 ≤ 2⌊lg n⌋+2 = O(lg n) roots immediately after the call of BINOMIAL-HEAP-MERGE.
The time to perform BINOMIAL-HEAP-MERGE is thus O(lg n). Each iteration of the while loop takes O(1) time, and there are at most ⌊lg n1⌋ +
⌊lgn2⌋ + 2 iterations because each iteration either advances the pointers one position down the root list of H or removes a root from the root
list. The total time is thus O(lg n).
The following procedure inserts node x into binomial heap H , assuming that x has already been allocated and key[x] has already
been filled in.

The procedure simply makes a one-node binomial heap H′ in O(1) time and unites it with the n-node binomial heap H in O(lg n)
time.
The call to BINOMIAL-HEAP-UNION takes care of freeing the temporary binomial heap H′.
The following procedure extracts the node with the minimum key from binomial heap H and returns a pointer to the extracted node.

This procedure works as follows:


The input binomial heap H is shown in (a) and (b) shows the situation after line 1: the root x with the minimum key has been removed from the
root list of H .
If x is the root of a Bk-tree, then by property 4 of Lemma 19.1, x's children, from left to right, are roots of Bk-1-, Bk-2-, ..., B0-trees.
(c) shows that by reversing the list of x's children in line 3, we have a binomial heap H′ that contains every node in x's tree except for x itself.
Because x's tree was removed from H in line 1, the binomial heap that results from uniting H and H′ in line 4, shown in (d), contains all the nodes
originally in H except for x.
Finally, line 5 returns x.

Since each of lines 1-4 takes O(lg n) time if H has n nodes, BINOMIAL-HEAP-EXTRACT-MIN runs in O(lg n) time
The action of BINOMIAL-HEAP-EXTRACT-MIN
(a) A binomial heap H
(b) The root x with minimum key is removed from the root list of H
(c) The linked list of x's children is reversed, giving another binomial heap H′
(d) The result of uniting H and H'.
The following procedure decreases the key of a node x in a binomial heap H to a new value k. It signals an error if k is greater than x's current key

As shown in Figure 19.8, this procedure decreases a key in the same manner as in a binary min-heap:
by "bubbling up" the key in the heap.
After ensuring that the new key < current key and then assigning the new key to x, the procedure goes up the tree, with y initially pointing to node x.
In each iteration of the while loop of lines 6-10, key[y] is checked against the key of y's parent z.
If y is the root or key[y] ≥ key[z], the binomial tree is now min-heap-ordered.
Otherwise, node y violates min-heap ordering, and so 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.
It is easy to delete a node x's key and satellite information from binomial heap H in O(lg n) time. The following implementation assumes that
no node currently in the binomial heap has a key of -∞.

The BINOMIAL-HEAP-DELETE procedure makes node x have the unique minimum key in the entire binomial heap by giving it a key of -∞.

It then bubbles this key and the associated satellite information up to a root by calling BINOMIAL-HEAP-DECREASE-KEY.

This root is then removed from H by a call of BINOMIAL-HEAP-EXTRACT-MIN. The BINOMIAL-HEAP-DELETE procedure takes O(lg n)
time.
Refer the link mentioned below for animated view of the operations over Binomial
Heap
https://www.chrislaux.com/binomialheap.html

You might also like