Binomial heap (a concept of Data Structure)
-
Upload
saliha-bilal -
Category
Software
-
view
264 -
download
0
description
Transcript of Binomial heap (a concept of Data Structure)
Binomial Heap
Data Structures &
Algorithms
By Saliha Bilal
Contents:Binomial Heap
Binomial tree
Implementations
Heap making
Insertion
Find minimum
Extract minimumUnion of heaps
Delete key
Decrease key
Performance
Aplications
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
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).
Binomial Tree
Binomial Trees of order 0 to 3
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
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
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
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)
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
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
Binomial treeB0
B1 B2
B3
B4
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
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)
Binomial heap with 13 elements
12 8
35
25 1319
33
32
4152
15
16
21
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( )
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
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
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
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
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
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’)
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
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
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
Extract- min (H)Step3: union of H1 and H2
12
35
25 13 19
33
32
4152
15
16
21
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.
Applications:
Discrete event simulation
Priority queues
THANK
YOU!