Binomial heap (a concept of Data Structure)

29
Binomial Heap Data Structures & Algorithms By Saliha Bilal

description

binomial heap is a collection of binary trees.

Transcript of Binomial heap (a concept of Data Structure)

Page 1: Binomial heap (a concept of Data Structure)

Binomial Heap

Data Structures &

Algorithms

By Saliha Bilal

Page 2: Binomial heap (a concept of Data Structure)

Contents:Binomial Heap

Binomial tree

Implementations

Heap making

Insertion

Find minimum

Extract minimumUnion of heaps

Delete key

Decrease key

Performance

Aplications

Page 3: Binomial heap (a concept of Data Structure)

A binomial heap is a heap similar to a binary  heap but  also  supports  quick  merging  of  two heaps.

A  binomial  heap  is  implemented  as  a collection of binomial trees

Binomial Heap

Page 4: Binomial heap (a concept of Data Structure)

Binomial Tree

A  binomial  heap  is  implemented  as  a  collection of binomial trees (compare  with  a binary  heap, which has a shape of a single binary tree).

A binomial tree is defined recursively:A binomial tree of order 0 is a single nodeA binomial  tree of order k has a root node whose children  are  roots  of  binomial  trees  of orders k−1, k−2, ..., 2, 1, 0 (in this order).

Page 5: Binomial heap (a concept of Data Structure)

Binomial Tree

Binomial Trees of order 0 to 3

Page 6: Binomial heap (a concept of Data Structure)

Binary heap supports various operationsquickly: extract-min, insert, decrease-key

If we already have two min-heaps, A and B,there is no efficient way to combine theminto a single min-heap•Introduce Binomial Heap It can support efficient union operation

Binomial Heap

Page 7: Binomial heap (a concept of Data Structure)

Mergeable Heaps

A data structure that supports the following 5 operations:•Make-Heap( ) : return an empty heap•Insert(H , x , k) : insert an item x withkey k into a heap H•Find-Min(H) : return item with min key•Extract-Min(H) : return and remove•Union(H1, H2) : merge heaps H1 and H2

Page 8: Binomial heap (a concept of Data Structure)

Mergeable Heaps

Examples:Binomial HeapFibonacci HeapBoth heaps also supportDecrease-Key(H,x,k) : assign item x with a smaller key kDelete(H,x) : remove item x

Page 9: Binomial heap (a concept of Data Structure)

Binomial heap

Operation ↓

Binomial heap

Make heap O(1)

Find minimum

O(log n)

Extract minimum

O(log n)

Insert  O(log n)

Delete  O(log n)

Decrease key

O(log n)

Union  O(log n)

Page 10: Binomial heap (a concept of Data Structure)

Binomial Heap

Unlike binary heap which consists of asingle tree, a binomial heap consists of asmall set of component trees no need to rebuild everything whenunion is perform

Each component tree is in a specialformat, called a binomial tree

Page 11: Binomial heap (a concept of Data Structure)

Binomial treeA binomial tree of order k, denoted byBk, is defined recursively as follows:

B0 is a tree with a single node

For k ≥1, Bk is formed by joining twoBk-1, such that the root of one treebecomes the leftmost child of theroot of the other

Page 12: Binomial heap (a concept of Data Structure)

Binomial treeB0

B1 B2

B3

B4

Page 13: Binomial heap (a concept of Data Structure)

Properties of Binomial Tree

For a binomial tree Bk

There are 2k nodesheight = kdeg(root) = k ; deg(other node) ˂kChildren of root, from left to right,are Bk-1, Bk-2, …, B1, B0

Page 14: Binomial heap (a concept of Data Structure)

Binomial Heap

Binomial  heap  of  n  elements  consists  of  a  specific set of binomial treesEach binomial tree satisfies min-heapordering: for each node x,key(x) ≥ key(parent(x))For each k, at most one binomial treewhose root has degree k(i.e., for each k, at most one Bk)

Page 15: Binomial heap (a concept of Data Structure)

Binomial heap with 13 elements

12 8

35

25 1319

33

32

4152

15

16

21

Page 16: Binomial heap (a concept of Data Structure)

Binomial Heap Operations

With the binomial heap,•Make-Heap( ): O(1) time

•Find-Min( ): O(log n) time

•Decrease-Key( ): O(log n) time[ Decrease-Key assumes we have the pointer tothe item x in which its key is changed ]

•Remaining operations : Based on Union( )

Page 17: Binomial heap (a concept of Data Structure)

Union Operation

•Let H1 and H2 be two binomial heaps•To Union them, we process all binomialtrees in the two heaps with same ordertogether, starting with smaller orderfirst•Let k be the order of the set of binomialtrees we currently processThere are three cases:1. If there is only one Bk →done2. If there are two Bk

 → Merge together, forming Bk+13. If there are three Bk

 → Leave one, merge remaining to Bk+1After that, process next k

Page 18: Binomial heap (a concept of Data Structure)

Union of two BH with 5 and 13 nodes 

128

35

25 13 19

33

32

41 52

15

16

21

4

9

14 11

31H1H2

Page 19: Binomial heap (a concept of Data Structure)

Union of two BH with 5 and 13 nodes 

12

8

35

25 13 19

33

32

41 52

15

16

21

4

9

14 11

31

After processingK=0

Page 20: Binomial heap (a concept of Data Structure)

Union of two BH with 5 and 13 nodes 

12 8

35

25 13

19

33

32

41

52

15

16

21

4

9

14 11

31

After processingK=1,2

Page 21: Binomial heap (a concept of Data Structure)

Union of two BH with 5 and 13 nodes 

12

8

35

25 13

19

33

32 41

52

15

16

21

4

9

14 11

31

After processingK=3

Page 22: Binomial heap (a concept of Data Structure)

Binomial Heap Operations

So, Union( ) takes O(log n) time

For remaining operations,Insert( ), Extract-Min( ), Delete( )how can they be done with Union?

Insert(H, x, k):→Create new heap H’, storing the item xwith key k; then, Union(H, H’)

Page 23: Binomial heap (a concept of Data Structure)

Extract-Min(H):→Find the tree Bj containing the min;Detach Bj from H → forming a heap H1 ;Remove root of Bj → forming a heap H2 ;Finally, Union(H, H’)

Delete(H, x):→Decrease-Key(H,x,-∞); Extract-Min(H);

Binomial Heap Operations

Page 24: Binomial heap (a concept of Data Structure)

Extract- min (H)Step1: select the tree Bj containing the min value

12 8

35

25 1319

33

32

4152

15

16

21

Bj with Min

Page 25: Binomial heap (a concept of Data Structure)

Extract- min (H)Step2: forming two heaps & extracting “8” from H2

128

35

25 13

19

33

32

41

52

15

16

21

H2

H1

Page 26: Binomial heap (a concept of Data Structure)

Extract- min (H)Step3: union of H1 and H2

12

35

25 13 19

33

32

4152

15

16

21

Page 27: Binomial heap (a concept of Data Structure)

Performance:Insert a new element to the heapFind the element with minimum keyDelete the element with minimum key from the heapDecrease key of a given elementDelete given element from the heapMerge two given heaps to one heapFinding the element with minimum key can also be done in O(1) by using an additional pointer to the minimum.

Page 28: Binomial heap (a concept of Data Structure)

Applications:

Discrete event simulation

Priority queues

Page 29: Binomial heap (a concept of Data Structure)

THANK

YOU!