0% found this document useful (0 votes)
53 views17 pages

10 Binomial Heap

The document discusses binomial heaps, which are a type of mergeable priority queue data structure. It describes binomial trees, how binomial heaps are represented and implemented, and operations like make-heap, insert, decrease-key, and union in O(log n) time. Binomial heaps have better performance than other structures for certain operations like union.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views17 pages

10 Binomial Heap

The document discusses binomial heaps, which are a type of mergeable priority queue data structure. It describes binomial trees, how binomial heaps are represented and implemented, and operations like make-heap, insert, decrease-key, and union in O(log n) time. Binomial heaps have better performance than other structures for certain operations like union.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Binomial Heaps

Dr. Ashish Sharma


 Binomial Trees
 Binomial Heaps
 Representing Binomial Heaps
 Why Binomial Heaps?
 Binomial Heap Operations
 -Make-Binomial-Heap
 -Binomial-Heap-Minimum
 -Binomial-Heap-Union
 -Binomial-Heap-Insert
 -Binomial-Heap-Decrease-Key
 Applications
Binomial Trees
 A Binomial tree can be defined recursively where:
A binomial tree Bk consists of two Bk-1 binomial trees that are linked
together. The root of one is the leftmost child of the root of the other.
 Properties of a binomial tree are given by the following lemma:
1. there are 2k nodes
2. the height of the tree is k
3. there are exactly KC3 nodes at depth I for I = 0, 1, …, k and
4. the root has degree k, which is greater than that of any other node

Bk

B0 B0 B1 B2 B3
Bk-1 Bk-1
Binomial Heaps
A binomial heap,
heap H, is a set of binomial trees that satisfies the
following binomial-heap properties.
1. Each binomial tree in H is heap-ordered:
heap-ordered the key of a node is greater
than or equal to the key of its parent
- the root of a heap-ordered tree contains the smallest key in a
given sub-tree
2. There is at most one binomial sub-tree in H whose root has
a given degree
- there are at most log(n) + 1 binomial sub-trees.
Proof: Binary representation of n,< b(logn), b(logn–1), …, b0 > can be given
by: i = logn

n = Σ (bi2i)
I=0

By property of 1 of Lemma, bi appears in H if and only if bit bi is 1.


Representing Binomial Heaps

 x: key
 head[x]: pointer to the root (if NIL, binomial heap is empty)
 p[x]: pointer to the parent(if x is the root, then NIL)
 child[x]: leftmost child (if node has no children,then NIL)
 sibling[x]: sibling of x immediately to its right (rightmost child of
parent, then NIL)
Why Binomial Heaps?

Binomial Heap presents data structures known as mergeable


heaps. The worst case for the UNIION operation is
considerably much better than other data structures.
Procedure Binary Heap Binomial Heap Fibonacci heap
Worst Case (worst-case) (amortized)
Make Heap (1) (1) (1)
Insert (lgn) O(lg n) (1)

Minimum (1) O(lg N) (1)

Extract-Min (lg n) (lg n) O(lg n)

Union (n) O(lg n) (1)

Decrease-Key (lg n) (lg n) (1)

Delete (lg n) (lg n) (lg n)


Make-Binomial-Heap

Make-Binomial-Heap()

 Allocates and returns an object H, where head[H] = NIL

 Running time: O(1)


Binomial-Heap-Minimum
-returns a pointer to the node with the minimum key in an n-node
binomial heap
Binomial-Heap-Minimum(H)
1. y  NIL
2. x  head[H}
3. min  ∞
4. while x ≠ NIL
do if key[x] < min
then min  key[x]
yx
x  sibling[z]
//go to the right till no more siblings Running time: O(logn)
5. return y
Binomial-Heap-Union:
Merging Heaps
Binomial-Heap-Union(H1, H2)
1. H  Make-Binomial-Heap()
Stage 1
2. head[H]  Binomial-Heap-Merge(H1, H2)
3. free the objects H1 and H2 but not the lists they point to
4. if head[H] = NIL Stage 2
5. then return H
6. prev-x  NIL Stage 3
7. x  head[H]
8. next-x  sibling[x]
9. while next-x ≠ NIL
10. do if (degree[x] ≠ degree[next-x]) or Case 1 & 2
(sibling[next-x] ≠ NIL
and degree[sibling[next-x]] = degree[x])
11. then prev-x  x Case 3
12. x  next-x
13. else if key[x] <= key[next-x]
Stage 4
14. then sibling[x]  sibling[next-x]
15. Binomial-Link(next-x, x) Case 4
16. else if prev-x = NIL
17. then head[H]  next-x
18. else sibling[prev-x]  next-x
19. Binomial-Link(x, next-x)
20. x  next-x
21. next-x  sibling[x]
Binomial-Heap-Union

Stage 1: Start merging root lists of binomial heaps H 1 and H2 into a


single root list H. The root list is strictly in increasing degree
Stage 2: Check for empty heap
Stage 3: Initialize prev-x, x and next-x
Stage 4: decide which sub-trees to link to each other depending
types of cases:
Case 1: degree[x] ≠ degree[next-x]
Case 2: degree[x] = degree[next-x] =degree[sibling[next-x]]
Case 3 & 4: degree[x] = degree[next-x] ≠ degree[sibling[next-x]]

AIM: to merge two trees until there is no more than 1 sub- tree
of a certain degree.
Binomial-Heap-Union
H1 H2

Case 3: degree[x] = degree[next-x]

x next-x
H
Binomial-Heap-Union

prev-x x next-x
H

Case 4: degree[x] = degree[next-x] ≠ degree[sibling[next-x]]


Binomial-Heap-Union

Finally, the union of H1 and H2 will look like this:

Running time for examining the sum of roots of


heaps H1 and H2 : O(log n )
Binomial-Heap-Insert
-inserts node x into binomial heap H

Binomial-Heap-Insert(H, x)
1. H’  Make-Binomial-Heap()
2. p[x]  NIL
3. child[x[  NIL
4. sibling[x]  NIL
5. degree[x]  0
6. head[H’]  x
7. H  Binomial-Heap-Union(H, H’)

Running time:
Make-Binomial-Heap() = O(1)
Binomial-Heap-Union = O(logn)
Binomial-Heap-Decrease-Key
-decreases the key of a node x in a binomial heap H to a new
value k
Binomial-Heap-Decrease-Key(H, x, k)
1. if k > Key[x]
2. then error “new key is greater than current
key”
3. key[x]  k
4. yx
5. z  p[y]
6. while z ≠ NIL and Key[y] key[z]
7. do exchange key[y] ↔ key[z]
8. > if y and z have satellite fields, exchange
them, too
9. yz
z  p[y]
Applications

Mergeable heaps are used:


 In priority queues:
-scheduling user tasks on a network.

 to find minimum spanning trees of undirected graphs


-highways
-computer networks
-telephone lines
-television cables
References

 Thomas H. Cormen, Charles E. Leiserson, Ronald L, Rivest,


Introduction to Algorithms, MIT Press; New York :
McGraw-Hill, c1990

 http://wwwcsif.cs.ucdavis.edu/~fletcher/classes/110/lecture_
notes.html

 http://www2.ics.hawaii.edu/~sugihara/course/ics311s00/pla
n.html

You might also like