# heaps

## Operations on Binomial Heap – Decrease key

Decrease key refers to reducing the key value of any node.If we want to decrease a key in Binomial heap,we will replace the key with reduced value and will repeatedly exchange the reduced key with the parent in order to restore min-heap property.The running time to perform this operation is O(log n).

Example

Say we want to decrease 50 to 4 in binomial heap given in Fig 1.The steps further followed are given in Fig 2,3,4,5 respectively.

Fig 1 : Binomial heap before decrease-key operation

Fig 2 : Replace 50 by 4

4 and 21 are swapped in Fig 3 and 4 and  8 are swapped in Fig 4 in order to retain min-heap property.

Fig 3 : Swap 3 and 21

Fig 4 : Swap 4 and 8

Fig 5 : Binomial heap after decrease-key operation

Short URL: http://tinyit.cc/9b8900
June 11, 2014 | Computer Science
Tags:

## Operations on Binomial Heap – Extract-min

In Extract-Min operation node with minimum key is deleted from the binomial heap h.The running time to extract minimum value is  O(log n).The steps followed  are :

• Find the root (say x) with minimum key.
• Delete the root.
• Break the binomial heap into h and h’.
• Perform the union operation to h and h’.

Given the binomial heap h in Fig 1.The first step is to find the root with minimum key.Node with value 1 is smallest so it is deleted and h is broken down into h and h’(Fig 2).

Fig 1 : Binomial heap h

Fig 2 : Binomial heaps h and h’

Perform the union operation on h and h’.

Fig 3 : Union on h and h’

After the union operation on h and h’ the structure seems like as given in Fig 4

Fig 4 : Structure after union of h and h’

Different cases are performed according to the situation.x points to 11,next-x points to 2,sibling[next-x] points to 3 as shown in Fig 5

Fig 5 : Structure after union of h and h’

Case 1 is applied in Fig 6.x now points to 2,next-x points to 3 and sibling[next-x] points to 7.

Fig 6 : Case 1 applied

Case 3 is applied to Fig 7.Trees with root 2 and 3 are of same order i.e. 1 and 2 is less than 3 so 3 becomes the child of 2.next-x will now point to 7 and sibling[next-x] will point to 4.

Fig 7 : Case 3 applied

Case 3 is applied again in Fig 8.2 is less than 7 so 7 becomes the child of 2.next-x will now point to 4.

Fig 8 : Case 3

Case 3 is applied in Fig 9 as 2<4 .So 4 becomes the child of 2.x now points to 2 and next-x points to null.Thus the process stops here.

Fig 9 : Case 3 applied

Short URL: http://tinyit.cc/29008e
|
Tags:

## Operations On Binomial Heap – Union

The union of two heaps is the merging root lists by root degree.But if we simply merge two heaps then a problem can arise.There may be a chance that we get two or more trees of same root degree.This violates the property of binomial heap.To deal with this problem we have four cases and solution to these cases respectively. Referring to all figures we choose arbitrary names like :

• x is the second node of merged heaps.
• prev-x points to previous node of x.
• next-x points to next node of x.
• sibling[next-x] points to next node of next-x.
• Bk and  B1 are Binomial Heaps of order k and 1 respectively.
• We apply different cases according to the situation until next-x becomes nil.

## Cases in union of binomial heaps

Case 1

If we merge  heaps and have a case like  x points to binomial heap of order k and next-x points to binomial heap of order 1 then ,then we need to shift our pointers towards right.x will now point to binomial heap with order 1.next-k will point to heap with root node d and prev-x will point to heap with root node b and order k.

Fig 1 : Case 1

Case 2

If in union operation the heaps with root b,c,d are of same order k and x points to heap of order k and root node b,next-x points to next heap of x with same order and root node c and sibling[next-x] points to next heap of next-x again with same order and root node d then pointers shift by one. x now points to heap with root node c,next-x points heap with root d and prev-x points to heap with root b.

Fig 2 : Case 2

Case 3

After the union operation,if  b,c are of same order and key[x] is less than key[next[x]] then c becomes the left child of b and x remain on pointing to b but next-z now points to points to d which was earlier pointed by sibling[next-x].

Fig 3 : Case 3

Case 4

The pointer points to same binomial heaps as in case 3 except the difference that key of x is greater than key[next[x]].In this case,b becomes the child of c.c is pointed by x and d is pointed by next-x.

Fig 4 : Case 4

Example

Consider two heaps h1 and h2 as given in Fig 5.The union operation has to be performed on these two heaps.h1 has binomial trees of order 0,1 and 2.h2 has binomial trees of order 0,1 and 3.At the point of union operation both the heaps are combined together but the resultant structure does not follows heap property i.e the resultant structure has binomial trees of same order.In Fig 5,two trees with root 2 and 5 are of order 0 and two trees with roots 1 and 3 are of order 1 violating the property of heaps.In order to make a structure satisfying heap properties we follow case according to the situation.The union of h1 and h2 is shown in Fig 6.

Fig 5 : Heaps h1 and h2

Fig 6 : Union of h1 and h2

In Fig 7,2 is less than 5 and both the trees are of same order i.e 0 so case 3 is followed in which root pointed by next-x(5) becomes the child of x(2) and pointer next-x shifts to right i.e it shifts to 1 and sibling[next-x] also shifts pointer to right i.e to 3.

Fig 7 : Case 3 applied

In Fig 8,all three binomial trees pointed by x,next-x and sibling[next-x] are of same order i.e 1 so case 2 is followed in which all three pointers shift to right by one i.e x will point to 1,next-x will point to  3 and sibling[next-x] will point to 7.

Fig 8 : Case 2 applied

In Fig 9,Case 3 is followed as root 1 is less than root 3 and their trees are of same order.Tree with root 3 becomes child of tree with root 1.

Fig 9 : Case 3 applied

In Fig 10,Case 3 is followed again as root 1 is less than root 7 and their trees are of same order i.e. 2.Tree with root 1 becomes the child of tree with root 7.

Fig 10 : Case 3 applied

In Fig 11,Case 3 is followed as 1 is less than 4 and their trees are of same order i.e.3.Tree with root 1 becomes the child of tree with root 4. The next-x becomes nil  and so the process of applying cases stop here.We get two binomial trees of different orders.They are of order 1 and 4 respectively.Union of heaps is done satisfying all the properties of binomial heap.

Fig 11 : Case 3 applied

Short URL: http://tinyit.cc/f799fd
|
Tags:

## Binomial Heaps

Binomial Heaps are similar to binary heaps with additional feature of implementing binomial series as sequence of trees.The heap(Fig 1) is represented using left child right sibling pointers.It has three links per node (parent,left,right) and the roots of tree are connected using single linked list.The degrees of tree decrease from left to right.

### Properties of Binomial Heap

• Each Binomial tree in the heap obey min-heap property.
• There can be only 0 or 1 binomial trees for each order .

The root  value of every binomial tree is smaller than its children and there will be at most log n +1 binomial trees of binomial heap with n nodes.

Fig 1 : Binomial Heap

Each binomial tree corresponds to one digit in binary representation of a number n.For e.g.the binary representation of 13(Fig 2) is 1101(23+22+20=8+4+1).The binomial heap will have 13 nodes with binomial trees of order 3,2 and 0 respectively.The orders came from the powers of 2. Either there will be no tree or there will one tree of each order i.e we cannot have any two trees of same order in the binomial heap.Similarly for a number like 15,the binary representation is 1111(23+22 +21 +20 =8+4+2+1).The binomial heap will have 15 nodes(Fig 4)with binomial trees of order 3,2,1,0 respectively.

### Binomial Heap of 13

Fig 2 : Binomial Heap of 13

### Details of Binomial Heap in Fig 2

Fig 3 : Details of Binomial Heap in Fig 2

### Binomial Heap of 15

Fig 3 : Binomial Heap of 15

### Details of Binomial heap in Fig 3

Fig 4 : Details of Binomial Heap in Fig 3

### Representing Binomial Heaps

The nodes in the binomial heap can be represented by a structure with parent,key,degree,child and sibling attributes ( Fig 5).

Fig 5 : Structure to represent node of Binomial Heap

Example

Consider the binomial heap as given in Fig 6.Say we want to represent 9(Say) then the structure would be like as given in Fig 7 or if we want represent 31 the structure would be like as given in Fig 8

Fig 6 : Binomial Heap

Fig 7 : Structure to represent node 9

Fig 8 : Structure to represent node 31

### Operations on Binomial Heaps

The various operations that can be performed on Binomial heaps are:

1. Union
2. Delete Min
3. Decrease Key
4. Delete
5. Insert

Short URL: http://tinyit.cc/de9cf4
June 4, 2014 |
Tags:

## Heapsort

The Maxheap sorts the elements in increasing order whereas Minheap sorts the element in decreasing order.To sort the heap three steps have to be followed

• Heapify -The process picks the largest child key and compare it to the parent key. If parent key is larger than key then heapify quits, otherwise it swaps the parent key with the largest child key. So that the parent  now becomes larger than its children.
• Build Heap – Use the procedure ‘Heapify’ in a bottom-up fashion to convert an array  into a heap. The process starts from last nodes and not the leaves.
• Heap Sort – Sorting is done by removing the max element and storing in array .The storage starts from last position and goes till first index of array.

### Time Analysis

• Build Heap Algorithm will run in O(n) time
• There are n-1 calls to Heapify each call requires O(log n)time
• Heap sort program combine Build Heap program and Heapify, therefore it has the running time of  O(n log n) time
• Total time complexity: O(n log n)

Example

Given an array in Fig 1 .We need to convert it to Maxheap and sort it.

Fig 1 : Array

Array is converted to heap in Fig 2

Fig 2 : Heap

The further steps are illustrated through following figures

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

Step 7

Step 8

Step 9

Step 10

Step 11

Step 12

Step 13

Step 14

Step 15

Step 16

Step 17

Step 18

Step 19

Step 20

Step 21

Short URL: http://tinyit.cc/020f01
May 22, 2014 |
Tags:

## Implementation and Merging of Heaps

### Implementation

Heaps of n keys can be represented by array of length n+1(Fig 2).For a node at rank i ,the left child is at rank 2i +1 and the right child is at rank  2i +2.The links between nodes are not stored explicitly.

• In Fig 1,heap has 7 keys i.e n =7.
• Array will be of length  8(n+1=7+1).

Fig 1 : Heap

Fig 2 : Implementation of Heap

### Merging two heaps

If two heaps are to be merged and a key(k) has to be inserted then we follow these steps :

• A new heap is created with root node storing k and two given heaps as subtrees.
• Perform downheap to restore  relational property of heap.

Example

Given are the two heaps h1 and h2 in Fig 3.We need to merge them and add 5 to it.

Fig 3 : h1 and h2

A new heap h3  is created with root node storing 5 and h1,h2 as subtrees(Fig 4).

Fig 4 : Merging of h1 and h2 with insertion of key 5.

Perform downheap operation to restore heap property(Fig 5).

Fig 5 : Downheap operation

Short URL: http://tinyit.cc/eff0
|
Tags:

## Heaps

A Heap is a Binary tree(Refer http://letslearncs.com/binary-trees/) that stores keys at it’s internal nodes and satisfies two additional properties which are :

• Relational Property
• Structural Property

### Relational Property

It follows either  Min-Heap or Max-Heap property.The  Max-Heap property says that  keys of each and every child  will be less than the key   of parent(Fig 2)whereas Min-Heap  property says that  keys of each and every child are either greater than or equal to the key of parent(Fig 1).

Fig 1 : Maxheap

Fig 2 : Minhea

### Structural Property

All the levels are full except the last level which is left justified.Left justified means that the parents of terminal nodes will have left child and may/may not have right child(Fig 4).Tree in Fig 3 fails to satisfy structural property.

Fig 3 : Incomplete Binary Tree

Fig 4 : Left Filled Binary Tree

### Height of a Heap

The height of heap is O(log n).

• Let h be the height of heap with n keys.
• The maximum no. of keys in heap  can be 2n.
• Since there 2at depth i =0,1,2,3,….h-1 and at least one key at depth h,we have   1 +2 +4 + 2h-1 +1.
• Thus n=2and height= O( log n).
• h=O(log n).

### Insertion in Heap

The insertion in Heap follows three steps :

• Find the node(Say n) where key(k) has to be inserted.
• Store key(k) at node(n).
• After the insertion of new key,heap property can get violated.
• Use Upheap technique to restore heap property.
• The upheap technique swaps the key along with the upward path from the insertion node.
• This method is followed until the key(k) reaches a node whose parent has a smaller/equal value to k.

Example

Fig 5 : Insert 5 at terminal node

Fig 6: Use upheap method

Fig 7 : Use upheap again

### Removal from a Heap

• This method removes the root i.e the minimum value from the heap.
• The root is deleted and the last key takes the place at root node
• Downheap technique is followed till the heap property is restored.
• Downheap swaps key along the downward path.
• Terminate downheap method leaf/terminal level is reached and key of parent is less than key of child.

Example

Fig 8 : Remove Min element

Fig 9 : Remove min element

Fig 9 : Move 26 to root

Fig 10 : Use downheap method

Fig 11 : Use downheap

Fig 11 : Use downheap

Short URL: http://tinyit.cc/c9d404
May 21, 2014 | Computer Science
Tags: