CSE548 Lecture 9
CSE548 Lecture 9
Lecture 9
( Binomial Heaps )
Rezaul A. Chowdhury
Department of Computer Science
SUNY Stony Brook
Fall 2017
Mergeable Heap Operations
MAKE-HEAP( ): return a new heap containing only element
INSERT( , ): insert element into heap
MINIMUM( ): return a pointer to an element in containing
the smallest key
EXTRACT-MIN( ): delete an element with the smallest key from
and return a pointer to that element
UNION( , ): return a new heap containing all elements of
heaps and , and destroy the input heaps
More mergeable heap operations:
DECREASE-KEY( , , ): change the key of element of heap to
assuming the current key of
DELETE( , ): delete element from heap
Mergeable Heap Operations
MAKE-HEAP Θ 1 Θ 1
INSERT Ο log Θ 1
MINIMUM Θ 1 Θ 1
UNION Θ Θ 1
DECREASE-KEY Ο log
DELETE Ο log
Binomial Trees
A binomial tree is an ordered tree defined recursively as follows.
consists of a single node
0,
−
0 # $ 0 %& ,
, !1 # 0,
, , %'()&* ).
, ,
, 1 , , ,
, , ,
, ,
Binomial Trees
0 # $ 0 %& ,
, !1 # 0,
, , %'()&* ).
⇒ , / 0 0 01 , , 0
Generating function: , - , , - , - … , -
, 2 - 3 , - 3 , - 3 , - / 01 3 / 01-
4 4 4 4
3 , - -3 , - 0
4 4
, - -, - 0 1 - , - / 01
1 # 0,
⇒, - 5
1 - , - %'()&* ).
1 -
Equating the coefficient of - from both sides: ,
Binomial Heaps
A binomial heap is a set of binomial trees that satisfies the
following properties:
Binomial Heaps
A binomial heap is a set of binomial trees that satisfies the
following properties:
1. each node has a key
2. each binomial tree in obeys the min-heap property
3. for any integer 0 0, there is at most one binomial tree
in whose root node has degree
6 1 10
min
8 14 29 12 25
11 17 38 18
27
Rank of Binomial Trees
The rank of a binomial tree node , denoted &9 , is the
number of children of .
3
The figure on the right shows the rank
of each node in .
2 1 0
Observe that &9 &%%'
1 0 0
.
producing a ; . 8 14 29
8 12
min
11 17
27
<
6 1 18
min
14 29 25
38
min <
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
<
6 1 18
min
14 29 25
38
12
min
18
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
6 1 18
min
14 29 25
<
38
12
min
18
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
6 1 18
min
14 29 25
<
38
1 12
12 25 18
18
min
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
<
6 1 18
min
14 29 25
38
12 25
18
min
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
<
6 1 18
min
14 29 25
38
6 1
8 14 29 12 25
11 17 38 18
27 min
Binomial Heap Operations: UNION( , )
8 12
min
11 17
27
6 1 18
min
14 29 25
38
=>?@> ,
6 1
8 14 29 12 25
11 17 38 18
27 min
Binomial Heap Operations: UNION( , )
UNION , works in exactly the same way as binary addition.
If F % , , then can
be viewed as a log bit
binary number ,
where .
Binomial Heap Operations: UNION( , )
UNION , works in exactly the same way as binary addition.
Takes Θ 1 time.
5
min ′
Step 2: ← UNION , ′
8 12
( in-place at )
Takes Ο log time, where
min
11 17
27
min
11 17 12
27
Step 1:
min
Step 2: remove the binomial tree with
remove the smallest root from the input heap
6 10
minimum
element 8 14 29 12 25
11 17 38 18
27
10
min <
12 25
,
the removed root
K
Step 4: UNION and update the min pointer
′
8 14 29
8 14 29
min ′ <
11 17 38
10 11 17 38
min
27
12 25 27
18
Binomial Heap Operations: EXTRACT-MIN( )
6
Step 1:
min
Step 2: remove the binomial tree with
remove the smallest root from the input heap
Θ 1
6 10
minimum
element
Θ 1
8 14 29 12 25
11 17 38 18
27
10
min <
12 25
,
the removed root
Ο log
K
Step 4: UNION and update the min pointer
Ο log
′
8 14 29
8 14 29
min ′ <
11 17 38
10 11 17 38
min
27
12 25 27
18
Thus, the worst-case cost of
EXTRACT-MIN is Ο log
Binomial Heap Operations
Heap
Worst-case
Operation
MAKE-HEAP Θ 1
INSERT Ο log
MINIMUM Θ 1
EXTRACT-MIN Ο log
UNION Ο log
Amortized Analysis ( Accounting Method )
We maintain a credit account for every tree in the heap, and
always maintain the following invariant:
L M&)N ' E 1
OP ∈Q
MAKE-HEAP( ):
actual cost, M 1 ( for creating the singleton heap )
extra charge, R 1 ( for storing in the credit account
of the new tree )
amortized cost, M̂ M R 2 Θ 1
Amortized Analysis ( Accounting Method )
We maintain a credit account for every tree in the heap, and
always maintain the following invariant:
L M&)N ' E 1
OP ∈Q
LINK( T ,T
A B A B
):
actual cost, M 1 ( for linking the two trees )
We use M&)N '
A B
pay for this actual work.
Let ; be the newly created tree. We restore the credit invariant
by transferring M&)N ' to M&)N '
A B
; .
Hence, amortized cost, M̂ M R 1 1 0
Amortized Analysis ( Accounting Method )
We maintain a credit account for every tree in the heap, and
always maintain the following invariant:
L M&)N ' E 1
OP ∈Q
INSERT( , ):
Amortized cost of MAKE-HEAP is 2
Then UNION , K is simply a sequence of free LINK operations
with only a constant amount of additional work that do not create
any new trees. Thus the credit invariant is maintained, and the
amortized cost of this step is 1.
Hence, amortized cost of INSERT, M̂ 2 1 3 Θ 1
Amortized Analysis ( Accounting Method )
We maintain a credit account for every tree in the heap, and
always maintain the following invariant:
L M&)N ' E 1
OP ∈Q
UNION( , ):
UNION , includes a sequence of free LINK operations that
maintain the credit invariant.
But it also includes Ο log other operations that are not free
( e.g., consider melding a heap with 2 elements with one
containing 1 elements ). These operations do not create new
trees (and so do not violate the credit invariant), and each cost Θ 1 .
Hence, amortized cost of UNION, M̂ Ο log
Amortized Analysis ( Accounting Method )
We maintain a credit account for every tree in the heap, and
always maintain the following invariant:
L M&)N ' E 1
OP ∈Q
EXTRACT-MIN( ):
Steps 1 & 2: The Θ 1 actual cost is paid for by the credit released
by the deleted tree.
Step 3: Exposes Ο log new trees, and we charge 1 unit of extra
credit for storing in the credit account of each such tree.
Step 4: Performs a UNION that has Ο log amortized cost.
Hence, amortized cost of EXTRACT-MIN, M̂ Ο log
Amortized Analysis ( Potential Method )
Potential Function,
Φ V M W ( #trees in the data structure after the -th operation ),
where M is a constant.
Clearly, Φ V 0 ( no trees in the data structure initially )
and for all 0, Φ V 0 0 ( #trees cannot be negative )
MAKE-HEAP( ):
actual cost, M 1 ( for creating the singleton heap )
potential change, Δ Φ V Φ V M
( as #trees increases by 1 )
amortized cost, M̂ M Δ 1 M Θ 1
Amortized Analysis ( Potential Method )
Potential Function,
Φ V M W ( #trees in the data structure after the -th operation ),
where M is a constant.
INSERT( , ):
The number of trees increases by 1 initially.
Then the operation scans 0 ( say ) locations of the array
of tree pointers. Observe that we use tree linking A 1B times each
of which reduces the number of trees by 1.
actual cost, M 1
potential change, Δ Φ V Φ V MA1 1 B
M M 1
amortized cost, M̂ M Δ 2 M AM 1BA 1B
For M 0 1, we have, M̂ 2 M Θ 1
Amortized Analysis ( Potential Method )
Potential Function,
Φ V M W ( #trees in the data structure after the -th operation ),
where M is a constant.
UNION( , ):
Suppose the operation scans 0 locations of the array of
tree pointers, and uses the link operation < times. Observe that
< 0 0. Each link reduces the number of trees by 1.
actual cost, M
potential change, Δ Φ V Φ V MW<
amortized cost, M̂ M Δ MW<
potential change, Δ Φ V Φ V
M W & 1 A removing b element in step 1
removes 1 tree but creates & new ones B
MW< A linkings in step 4
reduces #trees by < B
Amortized Analysis ( Potential Method )
Potential Function,
Φ V M W ( #trees in the data structure after the -th operation ),
where M is a constant.
EXTRACT-MIN( ):
Let in Step 1: & rank of the tree with the smallest key
and in Step 4: #locations of pointer array scanned during UNION
< #link operations during UNION
' #trees in the heap after the UNION
actual cost, M 2 ' &
potential change, Δ Φ V Φ V MW & < 1
Then amortized cost, M̂ M Δ 2 ' & MW & < 1
Since Ο log , < Ο log , ' Ο log && Ο log ,
we have, M̂ Ο log for any M.
Binomial Heap Operations
Heap
Worst-case Amortized
Operation
MAKE-HEAP Θ 1 Θ 1
INSERT Ο log Θ 1
MINIMUM Θ 1 Θ 1
INSERT( , ):
Constant amount of work is done by MAKE-HEAP and UNION,
and MAKE-HEAP creates a new tree.
actual cost, M 1 1 2
potential change, Δ Φ V Φ V M
amortized cost, M̂ M Δ 2 M Θ 1
Binomial Heap Operations with Lazy Union
We use exactly the same potential function as in the previous version,
Φ V M W ( #trees in the data structure after the -th operation ),
where M is a constant.
EXTRACT-MIN( ):
Cost of creating the array of pointers is log 1.
Suppose we start with ' trees in the doubly linked list, and perform <
link operations during the conversion from linked list to array version.
So we perform ' < work, and end up with ' < trees.
Cost of converting to the linked list version is ' <.
But ' < log 1 ( as we have at most one tree of each rank )
Θ 1 Θ 1 Θ 1
MAKE-
HEAP
INSERT Ο log Θ 1 Θ 1
MINIMUM Θ 1 Θ 1 Θ 1