0% found this document useful (0 votes)
16 views28 pages

Slides - Binomial Heaps - KCS503

Uploaded by

097Shashank Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

Slides - Binomial Heaps - KCS503

Uploaded by

097Shashank Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Binomial Heap

Binomial Tree
Binomial Tree
Binomial Tree: Properties
A Binomial Tree of order k has following properties.

1. It has exactly 2k nodes.


2. It has depth as k.
3. There are exactly kCi nodes at depth i for i = 0, 1, . . . , k.
4. The root has degree k and children of root are themselves Binomial
Trees with order k-1, k-2,.. 0 from left to right.
Binomial Heap: Properties
A binomial heap H is a set of binomial trees that satisfies the following
binomial heap properties.
1. Each binomial tree in H obeys the min-heap property: the key of a
node is greater than or equal to the key of its parent. We say that each
such tree is min-heap-ordered.
2. For any nonnegative integer k, there is at most one binomial tree in H
whose root has degree k.
Binomial Heap: Examples

Nodes: 12
Degree, k= 2 & 3

Nodes: 13
Degree, k= 0, 2 & 3
Binomial Heap: Examples

Parent
Key
Degree
Child Sibling
Finding Minimum Key
BINOMIAL-HEAP-MINIMUM(H)
y ← NIL
x ← head[H]
min ← ∞
while x ≠ NIL
do
if key[x] < min
then min ← key[x]
y←x
x ← sibling[x]
return y
Linking Two Binomial Trees: Equal Degree
BINOMIAL-LINK(y, z)
p[y] ← z
Parent
sibling[y] ← child[z]
Key
child[z] ← y Degree
degree[z] ← degree[z] + 1 Child Sibling
Uniting Two Binomial Heaps

• Min-Heap property should be maintained

• No two Binomial Heaps can have same degree


Step 1: Merge & Initialize Pointers

H ← MAKE-BINOMIAL-HEAP()
head[H] ← BINOMIAL-HEAP-MERGE(H1, H2)
free the objects H1 and H2 but not the lists they point to
prev-x ← NILif head[H] = NIL
then return H

x ← head[H]
next-x ← sibling[x]
Case 3: Link Same Degree Binomial Heaps

if key[x] ≤ key[next-x]
then sibling[x] ← sibling[next-x]
BINOMIAL-LINK(next-x, x)
Case 2: Updating Pointers

if (degree[x] ≠ degree[next-x]) or
(sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])
then prev-x ← x
x ← next-x
Case 4: Linking & Reconnect with Previous

if prev-x = NIL
then head[H] ← next-x
else
sibling[prev-x] ← next-x
BINOMIAL-LINK(x, next-x)
x ← next-x
Case 3: Link Same Degree Binomial Heaps

while next-x ≠ NIL


Uniting Two Binomial Heaps
BINOMIAL-HEAP-UNION(H1, H2)
H ← MAKE-BINOMIAL-HEAP()
head[H] ← BINOMIAL-HEAP-MERGE(H1, H2)
free the objects H1 and H2 but not the lists they point to
if head[H] = NIL
then return H
prev-x ← NIL
x ← head[H]
next-x ← sibling[x]
while next-x ≠ NIL
do if (degree[x] ≠ degree[next-x]) or (sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])
then prev-x ← x
x ← next-x
else if key[x] ≤ key[next-x]
then sibling[x] ← sibling[next-x]
BINOMIAL-LINK(next-x, x)
else if prev-x = NIL
then head[H] ← next-x
else sibling[prev-x] ← next-x
BINOMIAL-LINK(x, next-x)
x ← next-x
next-x ← sibling[x]
return H
Inserting node in a Binomial Heap
Node x is inserted into binomial heap H, assuming that x has already been allocated and
key[x] has already been filled in.

BINOMIAL-HEAP-INSERT(H, x)
H’ ← MAKE-BINOMIAL-HEAP()
p[x] ← NIL
child[x] ← NIL
sibling[x] ← NIL
degree[x] ← 0
head[H’] ← x
H ← BINOMIAL-HEAP-UNION(H, H’)
Finding Minimum Key in a Binomial Heap
Separate the Node with Minimum key

find the root x with the minimum key in the


root list of H, and remove x from the root
list of H
Create a New Binomial Heap

H’ ← MAKE-BINOMIAL-HEAP()
reverse the order of the linked list of x’s children, and
set head[H’] to point to the head of the resulting list
Unite two Binomial Heaps

H ← BINOMIAL-HEAP-UNION(H, H’)
Finding Minimum Key in a Binomial Heap
BINOMIAL-HEAP-EXTRACT-MIN(H)
find the root x with the minimum key in the root list of H, and
remove x from the root list of H
H’ ← MAKE-BINOMIAL-HEAP()
reverse the order of the linked list of x’s children, and set
head[H’] to point to the head of the resulting list
H ← BINOMIAL-HEAP-UNION(H, H’)
return x
Decreasing a Key Value in a Binomial Heap
DECREASE-KEY(H, x, k) assigns to node x within heap H the new key
value k, which is assumed to be no greater than its current key value.

BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
if k > key[x]
then error “new key is greater than current key”
Decreasing a Key Value in a Binomial Heap

key[x] ← k
y←x
z ← p[y]
Decreasing a Key Value in a Binomial Heap

while z ≠ NIL and key[y] < key[z]


do exchange key[y] ↔ key[z]
y←z
z ← p[y]
Decreasing a Key Value in a Binomial Heap

while z ≠ NIL and key[y] < key[z]


do exchange key[y] ↔ key[z]
y←z
z ← p[y]
Decreasing a Key Value in a Binomial Heap
DECREASE-KEY(H, x, k) assigns to node x within heap H the new key
value k, which is assumed to be no greater than its current key value.

BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
if k > key[x]
then error “new key is greater than current key”
key[x] ← k
y←x
z ← p[y]
while z ≠ NIL and key[y] < key[z]
do exchange key[y] ↔ key[z]
y←z
z ← p[y]
Deleting a Key in a Binomial Heap
BINOMIAL-HEAP-DELETE(H, x)
BINOMIAL-HEAP-DECREASE-KEY(H, x, −∞)
BINOMIAL-HEAP-EXTRACT-MIN(H)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy