22-Binomial Heap-08-03-2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

BINOMIAL HEAP

TEST TIME ON THE CELEBRITY PROBLEM

URL:
BINOMIAL HEAP

EXPLANATION

Binomial Heap is an extension of Binary Heap

It provides faster union or merge operation together with other operations


provided by Binary Heap.

Binary heap is used to implement priority queue, as seen in the previous


sections

“A Binomial Heap is a collection of Binomial Trees”


BINOMIAL HEAP

EXPLANATION

A binomial Heap is a collection of Binomial Trees.

A binomial tree Bk is an ordered tree defined recursively.

A binomial Tree B0 is consists of a single node.

A binomial tree Bk is consisting of k binomial trees Bk-1

that are linked together

The root of one is the left most child of the root of the

other
BINOMIAL HEAP

PROPERTIES

A Binomial Heap is a set of Binomial Trees where each Binomial Tree follows Min Heap

property

There can be at most one Binomial Tree of any degree.

Properties Each binomial tree in H is heap-ordered.

So the key of a node is greater than or equal to the key of its parent.

There is at most one binomial tree in H, whose root has a given degree.
BINOMIAL HEAP

PROPERTIES AND EXAMPLE

A Binomial Tree of order k has following properties.

It has exactly 2^k nodes. It has depth as k.

There are exactly nodes at depth i for i = 0, 1, . . . , k.

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

EXAMPLE

This binomial Heap H consists of binomial trees

B0, B2 and B3.

They have 1, 4 and 8 nodes respectively. In

total n = 13 nodes.

The root of binomial trees are linked by a

linked list in order of increasing degree


BINOMIAL HEAP

OPERATIONS

The main operation in Binomial Heap is union()

All other operations mainly use this operation.

The union() operation is to combine two Binomial Heaps into one


BINOMIAL HEAP

OPERATIONS

insert(H, k): Inserts a key ‘k’ to Binomial Heap ‘H’. This operation first

creates a Binomial Heap with single key ‘k’, then calls union on H and the new

Binomial heap.

getMin(H): A simple way to getMin() is to traverse the list of root of

Binomial Trees and return the minimum key.


BINOMIAL HEAP

OPERATIONS

extractMin(H): We first call getMin() to find the minimum key Binomial Tree,

then we remove the node and create a new Binomial Heap by connecting all

subtrees of the removed minimum node.


BINOMIAL HEAP

OPERATIONS

Finally, we call union() on H and the newly created Binomial Heap. delete(H):

Like Binary Heap, delete operation first reduces the key to minus infinite,

then calls extractMin(). decreaseKey(H): decreaseKey() is also similar to

Binary Heap. We compare the decreases key with it parent and if parent’s key

is more, we swap keys and recur for the parent. We stop when we either reach a

node whose parent has a smaller key or we hit the root node.
BINOMIAL HEAP

OPERATIONS
Union OperationGiven two Binomial Heaps H1 and H2, union(H1, H2) creates a

single Binomial Heap.

The first step is to simply merge the two Heaps in non-decreasing order of

degrees.After the simple merge, we need to make sure that there is at most one

Binomial Tree of any order.

To do this, we need to combine Binomial Trees of the same order. We traverse

the list of merged roots, we keep track of three-pointers, prev, x and next-x.
BINOMIAL HEAP

OPERATIONS
There can be following 4 cases when we traverse the list of roots.

Orders of x and next-x are not same, we simply move ahead.

In following 3 cases orders of x and next-x are same.

If the order of next-next-x is also same, move ahead.

If the key of x is smaller than or equal to the key of next-x, then make

next-x as a child of x by linking it with x.

If the key of x is greater, then make x as the child of next. 4


BINOMIAL HEAP

RESULT
BINOMIAL HEAP

RESULT
BINOMIAL HEAP

import java.io.*; BinomialHeapNode ret;


class BinomialHeapNode { if (sibling != null)
int key, degree; ret = sibling.reverse(this);
BinomialHeapNode parent; else
BinomialHeapNode sibling; ret = this;
BinomialHeapNode child; sibling = sibl;
public BinomialHeapNode(int k) return ret;
{ }
public BinomialHeapNode findMinNode()
key = k; {
degree = 0; BinomialHeapNode x = this, y =
parent = null; this;
sibling = null; int min = x.key;
child = null; while (x != null) {
}
public BinomialHeapNode
reverse(BinomialHeapNode sibl)
{
BINOMIAL HEAP

if (x.key < min) { else {


y = x; node=temp.child.findANodeWithKey(valu
min = x.key; e);
} if (node == null)
temp = temp.sibling;
x = x.sibling; else
} break;
return y; }
} }
public BinomialHeapNode return node;
findANodeWithKey(int value) }
{ public int getSize()
BinomialHeapNode temp = this, node = {
null; return (1 + ((child == null) ? 0 :
while (temp != null) { child.getSize())+ ((sibling == null) ? 0
if (temp.key == value) { : sibling.getSize()));
node = temp; }
break; }
} class BinomialHeap {
if (temp.child == null) private BinomialHeapNode Nodes;
temp = temp.sibling; private int size;
BINOMIAL HEAP

public BinomialHeap() Nodes = temp;


{ size = 1;
Nodes = null; }
size = 0; else {
} unionNodes(temp);
public boolean isEmpty() { return size++;
Nodes == null; } }
public int getSize() { return size; } }
public void makeEmpty() }
{ private void merge(BinomialHeapNode
Nodes = null; binHeap)
size = 0; {
} BinomialHeapNode temp1 = Nodes,
public void insert(int value) temp2 = binHeap;
{ while ((temp1 != null) && (temp2
!= null)) {
if (value > 0) { if (temp1.degree == temp2.degree)
BinomialHeapNode temp= new {
BinomialHeapNode(value);
if (Nodes == null) {
BINOMIAL HEAP

BinomialHeapNode tmp = temp2; else {


temp2 = temp2.sibling; BinomialHeapNode tmp = temp1;
tmp.sibling = temp1.sibling; temp1 = temp2;
temp1.sibling = tmp; temp2 = temp2.sibling;
temp1 = tmp.sibling; temp1.sibling = tmp;
} if (tmp == Nodes) {
else { Nodes = temp1;
if (temp1.degree < temp2.degree) { }
if ((temp1.sibling == null)|| else {
(temp1.sibling.degree> temp2.degree)) { }
BinomialHeapNode tmp = temp2; temp2 = }
temp2.sibling; }
tmp.sibling = temp1.sibling; }
temp1.sibling = tmp; if (temp1 == null) {
temp1 = tmp.sibling; temp1 = Nodes;
} while (temp1.sibling != null) {
else { temp1 = temp1 = temp1.sibling;
temp1.sibling; }
}
}
BINOMIAL HEAP

temp1.sibling = temp2; else {


} if (temp.key <= nextTemp.key) {
else { temp.sibling = nextTemp.sibling;
} nextTemp.parent = temp;
} nextTemp.sibling = temp.child;
private void temp.child = nextTemp;
unionNodes(BinomialHeapNode binHeap) temp.degree++;
{ }
merge(binHeap); else {
BinomialHeapNode prevTemp = null, if (prevTemp == null) {
temp = Nodes, Nodes = nextTemp;
nextTemp = Nodes.sibling; }
while (nextTemp != null) { else {
if ((temp.degree != prevTemp.sibling = nextTemp;
nextTemp.degree)|| ((nextTemp.sibling != }
null)&& (nextTemp.sibling.degree== temp.parent = nextTemp;
temp.degree))) {
prevTemp = temp;
temp = nextTemp;
}
BINOMIAL HEAP

temp.sibling = nextTemp.child; extractMin();


nextTemp.child = temp; }
nextTemp.degree++; }
temp = nextTemp; public void decreaseKeyValue(int
} old_value,
} int new_value)
nextTemp = temp.sibling; {
} BinomialHeapNode temp=
} Nodes.findANodeWithKey(old_value);
public int findMinimum() if (temp == null)
{ return;
return Nodes.findMinNode().key; temp.key = new_value;
} BinomialHeapNode tempParent =
public void delete(int value) temp.parent;
{ while ((tempParent != null) &&
if ((Nodes != null)&& (temp.key < tempParent.key)) {
(Nodes.findANodeWithKey(value) != null)) int z = temp.key;
{ temp.key = tempParent.key;
decreaseKeyValue(value, findMinimum()-1); tempParent.key = z;
BINOMIAL HEAP

temp = tempParent; else {


tempParent = tempParent.parent; prevTemp.sibling = temp.sibling;
} }
} temp = temp.child;
public int extractMin() BinomialHeapNode fakeNode = temp;
{ while (temp != null) {
if (Nodes == null) temp.parent = null;
return -1; temp = temp.sibling;
BinomialHeapNode temp = Nodes, }
prevTemp = null; if ((Nodes == null) && (fakeNode
BinomialHeapNode minNode = == null)) {
Nodes.findMinNode(); size = 0;
while (temp.key != minNode.key) { }
prevTemp = temp; else {
temp = temp.sibling; if ((Nodes == null) && (fakeNode !=
} null)) {
if (prevTemp == null) { Nodes = fakeNode.reverse(null);
Nodes = temp.sibling; size = Nodes.getSize();
} }
BINOMIAL HEAP

else { private void displayHeap(BinomialHeapNode


if ((Nodes != null) && (fakeNode == r)
null)) { {
size = Nodes.getSize(); if (r != null) {
} displayHeap(r.child);
else { System.out.print(r.key + " ");
unionNodes(fakeNode.reverse(null)); displayHeap(r.sibling);
size = Nodes.getSize(); }
} }
} }
} public class main {
return minNode.key; public static void main(String[] args)
} {
public void displayHeap() BinomialHeap binHeap = new
{ BinomialHeap();
System.out.print("\nHeap : ");
displayHeap(Nodes);
System.out.println("\n");
}
binHeap.insert(12); binHeap.delete(15);
binHeap.insert(8); binHeap.delete(8);
binHeap.insert(5); System.out.println("Size of the
binHeap.insert(15); binomial heap is "+ binHeap.getSize());
binHeap.insert(7); binHeap.displayHeap();
binHeap.insert(2); binHeap.makeEmpty();
binHeap.insert(9); System.out.println(binHeap.isEmpty
System.out.println("Size of the ());
binomial heap is "+ binHeap.getSize()); }
binHeap.displayHeap(); }
INTERVIEW QUESTIONS

1.What is Binomial Heap?

Answer: A binomial heap is a type of data structure used to maintain

a collection of elements, typically with a focus on efficient priority

queue operations. It is a collection of binomial trees where each tree

follows the min-heap property.


INTERVIEW QUESTIONS

2. What are the key operations in a Binomial Heap?


Answer: The key operations in a binomial heap are:

insert(H, k): Inserts a key 'k' into the binomial heap 'H'.

getMin(H): Finds the minimum key in the binomial heap 'H'.

extractMin(H): Removes and returns the minimum key from the binomial heap 'H'.

delete(H): Deletes a specific element from the binomial heap.

decreaseKey(H): Decreases the key of a specific element in the binomial heap.

union(H1, H2): Combines two binomial heaps into one.


INTERVIEW QUESTIONS

3. How does the insert operation work in a Binomial Heap?

Answer: The insert operation in a binomial heap creates a new binomial

heap with a single key 'k' and then uses the union operation to merge it

with the existing binomial heap.


INTERVIEW QUESTIONS

4. What is the purpose of the union operation in a Binomial Heap?

Answer: The union operation is used to combine two binomial heaps into

one. It involves merging the two heaps in non-decreasing order of their

degrees and then combining binomial trees of the same degree to ensure

that there is at most one binomial tree of any order in the heap.
INTERVIEW QUESTIONS

5. How does the decreaseKey operation work in a Binomial Heap?

Answer: The decreaseKey operation compares the decreased key with its

parent and swaps keys recursively until it reaches a node whose parent has

a smaller key or the root node.


INTERVIEW QUESTIONS

6. What is the Min Heap property, and how does it apply to a Binomial
Heap?

Answer: The Min Heap property states that the key of a node is greater

than or equal to the key of its parent. In a binomial heap, each binomial

tree follows the Min Heap property, and the root of the entire heap is the

minimum key among all the trees.


INTERVIEW QUESTIONS

7. Explain the process of extracting the minimum key in a Binomial Heap.

Answer: To extract the minimum key in a binomial heap, we first call

the getMin operation to find the minimum key binomial tree. Then, we

remove the node with the minimum key and create a new binomial heap by

connecting all the subtrees of the removed node. Finally, we call the

union operation on the original heap and the newly created heap.
INTERVIEW QUESTIONS

8. What are the properties of a Binomial Heap?

Answer: The properties of a binomial heap are:

Each binomial tree in the heap is heap-ordered (follows the Min Heap

property).

There is at most one binomial tree with a given degree in the heap.
INTERVIEW QUESTIONS

9. Can you compare a Binomial Heap to a Binary Heap?

Answer: Binomial heaps have certain advantages over binary heaps, such

as faster union and decrease key operations. They can be used for priority

queue operations efficiently. However, binary heaps are easier to

implement and have better average time complexity for some operations.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like