binomial heaps
DESCRIPTION
Binomial Heaps. Data structure with: Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn). This class. Why binomial heaps Binomial trees Binomial max heaps Insert Merging two binomial heaps Delete max. Why not Heaps?. MaxHeap operations are: find-max() is O(1) - PowerPoint PPT PresentationTRANSCRIPT
04/19/23 Cutler/Head Binomial Heaps1
Binomial Heaps
Data structure with:
Insert in amortized cost O(1),
Merge in O(lgn)
DeleteMax in O(lgn)
04/19/23 Cutler/Head Binomial Heaps2
This class
• Why binomial heaps• Binomial trees• Binomial max heaps• Insert• Merging two binomial heaps• Delete max
04/19/23 Cutler/Head Binomial Heaps3
Why not Heaps?
• MaxHeap operations are:– find-max() is O(1)– insert(k ), delete-max() is O(lgn)– merge(heap1, heap2) is O(n)– 2 methods for merge:
• “append” the heaps and then use fast-build-heap. O(n) where n is the total number of elements
• “append” the heaps and then percolate the nodes in the smaller heap
– If number elements in small heap proportional to n, the worst case time complexity is O(n lg n)
04/19/23 Cutler/Head Binomial Heaps4
Why Binomial Heaps?
• Binomial Heaps have improved runtime for some of the primary operations of heaps:– find-max() is O(lg n) (O(1) if
additional pointer)– delete-max() is O(lgn)– insert(k ) amortized O(1) and worst
case is O(lg n)– merge(heap1, heap2) with a total of
n elements is O(lgn)– Example of a Binomial heap shown
below
B1 B3
04/19/23 Cutler/Head Binomial Heaps5
Binomial Trees
• The i th binomial tree, Bi , i 0 has a root with i children, Bi-1, … , B0
B0 B1 B2 B3 ... Bi
n 1 2 4 8 ... 2i
depth 0 1 2 3 ... i = lgn
B1 B3B2B0
04/19/23 Cutler/Head Binomial Heaps6
The Number of Nodes
• The number of nodes at depth 0,1,2, …, i are
B4
i
iii,...
1,
0
0
1
2
3
4
d n
0
4
1
4
3
4
4
4
2
4
04/19/23 Cutler/Head Binomial Heaps7
Binomial (Maximum) Heap
• Is a collection of binomial trees of distinct sizes each of which has the heap property.
• The roots of the binomial trees are connected (as a doubly linked list) in the order of increasing size.
• (There may be a pointer to the largest key).
35 23
21 914
1
20
517
8
B1 B3
29
B0
04/19/23 Cutler/Head Binomial Heaps8
Binary numbers and binomial heaps
• Note correspondence of binary representation of n, and the structure of a binomial heap with n nodes
• n can be represented uniquely as
a binary number blast…b1b0 with
last = lg n and lg n+1 bits• A binomial heap for n nodes
where n = blast…b1b0 will have a binomial tree for all i where bi =1.
• For example 11001 will have
B4-- B3– B0.
04/19/23 Cutler/Head Binomial Heaps9
A node of the binomial heap
Pointer to parentkeydegreepointer to pointer tochild sibling
04/19/23 Cutler/Head Binomial Heaps10
A binomial heap
282
71
110
50
28
7 11
5
H
15150
04/19/23 Cutler/Head Binomial Heaps11
Merging same size binomial heaps Bi into a B i+1
• Link trees - Make the root of the tree with the smaller max value, the i+1th child of the binomial tree with the larger max value.– O(1)
15
57
2
12
29
3
+
15
57
2
12
29
3
H1 H2
H1
04/19/23 Cutler/Head Binomial Heaps12
Code for merging same size heaps into 1 binomial tree
JoinSameBinomialTrees(T1, T2)
//T1 and T2 are not NULLIf degree[T1]=degree[T2] If key[T1]>key[T2] degree[T1]++ parent[T2] = T1 sibling[T2]=child[T1] child[T1] = T2 return T1 else degree[T2]++ parent[T1] = T2 sibling[T1]=child[T2] child[T2] = T1 return T2else send error message
04/19/23 Cutler/Head Binomial Heaps13
Insert New
Note: The correspondence between “count” and inserting a new key into a binomial heap H
1) Convert item to be inserted into a B*0
2) Set i to 0
3) If H includes a Bi
A) Remove Bi from H.
B) Link Bi with B*i to form B*i+1
C) Set i to i +1
D) Repeat step 3
4) Else link B*i with H (and update max pointer).
Worst case time is O(lgn)
04/19/23 Cutler/Head Binomial Heaps14
B1 B2B0B4 4 trees
23 nodes
Example for insertion
i = 0. Remove B0 and link it with B0* getting B1*
B*0
merge with
B*1
i =0
i = 1. Remove B1, link B1* with B1 getting B2*
B*2
i =1
B4B2
B1
H
H
H
New
04/19/23 Cutler/Head Binomial Heaps15
Example Continued
I = 2. Remove B2, link B2* with B2 getting B3*
B*3
i =2
B4
i = 3. H does not include B3. link B*3 with B4 . Binomial heap has 2 binomial trees and 24 nodes..
B3B4
i =3
2 trees24 nodes
H
H
04/19/23 Cutler/Head Binomial Heaps16
Doing a sequence of insertions
10
Insert 10
20
Insert 20
10
H contains B0 so remove B0. Join into B1 and add to H
H
20 B*0
B0
H
04/19/23 Cutler/Head Binomial Heaps17
Doing a sequence of insertion
20
Insert 3
10
3
H does not contain B0 so add B0
* to H
3 B*0
B0 B1
H
04/19/23 Cutler/Head Binomial Heaps18
Insert 8
Doing a sequence of insertions
First 3 is removedThen 3 and 8 are joined into a B1
*
Then the two B1s are joined
8 B*0
20
8 10
3
H
20
10
3
B
0
B
1
H
04/19/23 Cutler/Head Binomial Heaps19
Doing a sequence of insertions
20
Insert 30
8 10
3
30
20
8 10
3
H
04/19/23 Cutler/Head Binomial Heaps20
Amortized analysis for insert new
Assume n=2k inserts
Inserts H Links per insert
n/2 no B0 1
n/4 B0 no B1 2
n/8 B0, B1 no B2 3
...
n/2k = 1 B0 , B1,…Bk-2 no k
Bk-1
1 B0 , B1,…Bk-2 Bk-1 (k+1)
no BkNote correspondence:
Flips for binary increment
and links for binomial heap insert
04/19/23 Cutler/Head Binomial Heaps21
Amortized analysis for insert new
The total time (link operations) 1*(n/2)+2*(n/4)+3*(n/8)+…+
k*(n/2k)+ (k+1)*1=
So amortized cost for insertion is O(1)
3for 3
1lg2
)1(2
1
1
nn
ni
n
ki
n
ii
k
ii
04/19/23 Cutler/Head Binomial Heaps22
Merge 2 Binomial Heaps,H1 and H2 into Result
Note: The correspondence between adding two binary numbers and merging two binomial heaps
Merge H1 and H2 into a new binomial heap, result.
Let n = max(n1, n2). The largest binomial tree in the heaps is Blg n
stage 0:1. If neither contain B0 do nothing2. If only one binomial heap includes a B0 put it into the result.3. If both include B0 , link them into B1 and save for next stage. (like carry bit in binary addition)
04/19/23 Cutler/Head Binomial Heaps23
Merge 2 Binomial Heaps,H1 and H2 into Result
stage i (i = 1,…, lg n): There may be 0 to 3 Bi ‘s, one from H1, one from H2 and one from the previous stage.1. If no Bi do nothing2. If there is only one Bi put it into the result.3. Otherwise link two Bi ‘s into Bi+1 and save for next stage.4. If there is still a Bi put it in the result.
If there is a saved B, add to result
There are exactly lg n +1 stages. Each stage is O(1). So worst case growth rate is O(lgn)
04/19/23 Cutler/Head Binomial Heaps24
deleting-max
1) Remove the Bi containing the max from H, joining remaining parts into a new binomial heap H1.
2) Remove root of Bi .link the binomial subtrees of the root into a new binomial heap H2.
3) Merge H1 and H2.
Time:1) O(lgn) when no max pointer2) Since Bi has i children there are i links. Bi contains 2i
n nodes, so ilg n and the worst case time is O(lg n) 3) O(lg n)
TOTAL O(lg n)