09 Binomial Heap
09 Binomial Heap
1
Binomial Heaps
DATA STRUCTURES: MERGEABLE HEAPS
• MAKE-HEAP ( )
– Creates & returns a new heap with no elements.
• INSERT (H,x)
– Inserts a node x whose key field has already been
filled into heap H.
• MINIMUM (H)
– Returns a pointer to the node in heap H whose key
is minimum.
CS 473 Lecture X 2
Mergeable Heaps
• EXTRACT-MIN (H)
– Deletes the node from heap H whose key is
minimum. Returns a pointer to the node.
• DECREASE-KEY (H, x, k)
– Assigns to node x within heap H the new value k
where k is smaller than its current key value.
CS 473 Lecture X 3
Mergeable Heaps
• DELETE (H, x)
– Deletes node x from heap H.
CS 473 Lecture X 4
Binomial Trees
• A binomial heap is a collection of binomial trees.
• The binomial tree Bk is an ordered tree defined
recursively
Bo Consists of a single node
.
.
.
Bk Consists of two binominal trees Bk-1
linked together. Root of one is the
leftmost child of the root of the other.
CS 473 Lecture X 5
Binomial Trees
B k-1
B k-1
Bk
CS 473 Lecture X 6
Binomial Trees B
2
B1
B1 B0
B1
B0 B1 B2 B3
B3
B2 B0
B1
CS 473
B4 Lecture X 7
Binomial Trees
B1 Bo
B2
Bk-2
Bk-1
Bk
CS 473 Lecture X 8
Properties of Binomial Trees
LEMMA: For the binomial tree Bk ;
1. There are 2k nodes,
2. The height of tree is k,
k
3. There are exactly nodes at depth i for
i
i = 0,1,..,k and
4. The root has degree k > degree of any other
node if the children of the root are numbered
from left to right as k-1, k-2,...,0; child i is the
root of a subtree Bi.
CS 473 Lecture X 9
Properties of Binomial Trees
PROOF: By induction on k
Each property holds for the basis B 0
INDUCTIVE STEP: assume that Lemma
holds for Bk-1
1. Bk consists of two copies of Bk-1
| Bk | = | Bk-1 | + | Bk-1| = 2k-1 +2k-1 = 2k
2. hk-1 = Height (Bk-1) = k-1 by induction
hk=hk-1+1 = k-1 +1 = k
CS 473 Lecture X 10
Properties of Binomial Trees
3. Let D(k,i) denote the number of nodes at depth i of a Bk ;
d=1
d=i-1 d=i d=i
Bk-1
Bk-1
true by induction
k-1 k
D(k,i)=D(k-1,i -1) + D(k-1,i) = k-1
+ =
i -1 i i
CS 473 Lecture X 11
Properties of Binomial Trees(Cont.)
CS 473 Lecture X 12
Properties of Binomial Trees (Cont.)
B1 B0
Bk-3 B2
Bk-2
B 1 B0
Bk-3 B2
Bk-2
Bk-1
CS 473 Lecture X 13
Properties of Binomial Trees (Cont.)
• COROLLARY: The maximum degree of any
node in an n-node binomial tree is lg(n)
CS 473 Lecture X 14
Binomial Heaps
A BINOMIAL HEAP H is a set of BINOMIAL
TREES that satisfies the following “Binomial
Heap Properties”
1. Each binomial tree in H is HEAP-ORDERED
• the key of a node is ≥ the key of the parent
• Root of each binomial tree in H contains the
smallest key in that tree.
CS 473 Lecture X 15
Binomial Heaps
2. There is at most one binomial tree in H whose
root has a given degree,
– n-node binomial heap H consists of at most
[lgn] + 1 binomial trees.
– Binary represantation of n has lg(n) + 1 bits,
13 =< 1, 1, 0, 1>2
Consists of B0, B2, B3
head[H]
10 1 6
B0 12 25 8 14 29
18
B2 11 17 38
B3
27
CS 473 Lecture X 17
Representation of Binomial Heaps
• Each binomial tree within a binomial heap is stored in
the left-child, right-sibling representation
• Each node X contains POINTERS
– p[x] to its parent
– child[x] to its leftmost child
– sibling[x] to its immediately right sibling
• Each node X also contains the field degree[x] which
denotes the number of children of X.
CS 473 Lecture X 18
Representation of Binomial Heaps
HEAD [H]
10 10
10 ROOT LIST (LINKED LIST)
10 10 10
parent
key 10 10 10 10 10
degree 10 10 10 10 10
child 10 10 10 10
10 sibling
10 10 10
10
CS 473 Lecture X 19
Representation of Binomial Heaps
CS 473 Lecture X 20
Operations on Binomial Heaps
CREATING A NEW BINOMIAL HEAP
MAKE-BINOMIAL-HEAP ( )
allocate H RUNNING-TIME= Θ(1)
head [ H ] NIL
return H
end
CS 473 Lecture X 21
Operations on Binomial Heaps
BINOMIAL-HEAP-MINIMUM (H)
x Head [H]
min key [x]
x sibling [x]
while x ≠ NIL do
if key [x] < min then
min key [x]
yx
endif
x sibling [x]
endwhile
return y
end
CS 473 Lecture X 22
Operations on Binomial Heaps
RUNNING–TIME = O (lgn)
CS 473 Lecture X 23
Uniting Two Binomial Heaps
BINOMIAL-HEAP-UNION
Procedure repeatedly link binomial trees whose roots
have the same degree
BINOMIAL-LINK
Procedure links the Bk-1 tree rooted at node y to
the Bk-1 tree rooted at node z it makes z the parent of y
CS 473 Lecture X 24
Uniting Two Binomial Heaps
BINOMIAL-LINK (y,z)
p [y] z
sibling [y] child [z]
child [z] y
degree [z] degree [z] + 1
end
CS 473 Lecture X 25
Uniting Two Binomial Heaps
NIL
z
+1
child[z]
NIL
p[y]
sibling [y]
CS 473 Lecture X 26
Uniting Two Binomial Heaps: Cases
We maintain 3 pointers into the root list
Bk Bl
l >k
prev-x x next-x
a b c d
Bk Bl
CS 473 Lecture X 29
Uniting Two Binomial Heaps: Cases
CASE 2: Occurs when x is the first of 3 roots of equal degree
degree [x] = degree [next-x] = degree [sibling[next-x]]
prev-x x next-x sibling [next-x]
a b c d
BK BK BK
prev-x x next-x
a b c d
BK BK BK
CS 473 Lecture X 30
Uniting Two Binomial Heaps: Cases
CASE 3 & 4: Occur when x is the first of 2 roots of equal degree
degree [x] = degree [next-x] ≠ degree [sibling [next-x]]
• The root with the smaller key becomes the root of the linked tree
CS 473 Lecture X 31
Uniting Two Binomial Heaps: Cases
CASE 3 & 4 CONTINUED
prev-x x next-x sibling [next-x]
a b c d
Bk Bk Bl l>k
prev-x x next-x
CASE 3
a b d
key [b] ≤ key [c]
c
prev-x x next-x
CASE 4
a c d
b key [c] ≤ key [b]
CS 473 Lecture X 32
Uniting Two Binomial Heaps: Cases
The running time of binomial-heap-union operation is
O (lgn)
CS 473 Lecture X 33
Uniting Two Binomial Heaps: Cases
• So H contains at most
lgn1 + lgn2 +2 ≤ 2 lgn +2= O (lgn) roots
immediately after BINOMIAL-HEAP-MERGE
CS 473 Lecture X 34
Binomial-Heap-Union Procedure
BINOMIAL-HEAP-MERGE PROCEDURE
CS 473 Lecture X 35
Binomial-Heap-Union Procedure
BINOMIAL-HEAP-UNION (H1,H2)
H MAKE-BINOMIAL-HEAP ( )
head [ H ] BINOMIAL-HEAP-MERGE (H1,H2)
free the objects H1 & H2 but not the lists they point to
prev-x NIL
x HEAD [H]
next-x sibling [x]
while next-x ≠ NIL do
if ( degree [x] ≠ degree [next-x] OR
(sibling [next-x] ≠ NIL and degree[sibling [next-x]] = degree [x]) then
prev-x x CASE 1 and 2
x next-x CASE 1 and 2
elseif key [x] ≤ key [next-x] then
sibling [x] sibling [next -x] CASE 3
CS 473 Lecture X 36
Binomial-Heap-Union Procedure (Cont.)
BINOMIAL- LINK (next-x, x) CASE 3
else
if prev-x = NIL then
head [H] next-x CASE 4
else CASE 4
sibling [prev-x] next-x CASE 4
endif
BINOMIAL-LINK(x, next-x)
CASE 4
x next-x CASE 4
endif
next-x sibling [x]
endwhile
return H
end
CS 473 Lecture X 37
Uniting Two Binomial Heaps vs
Adding Two Binary Numbers
H1 with n1 NODES : H1 =
H2 with n2 NODES : H2 =
5 4 3 2 1 0
1 0 0 1 1 1
ex: n1= 39 : H1 = < > = { B0, B1, B2, B5 }
1 1 0 1 1 0
n2 = 54 : H2= < > = { B1, B2, B4, B5 }
CS 473 Lecture X 38
x next-x
MERGE H
B0 B1 B1 B2 B2 B4 B5 B5
CASE1 MARCH
Cin=0
1+0=1
x next-x
B0 B1 B1 B2 B2 B4 B5 B5
CASE3 or 4
LINK Cin=0
1+1=10
x next-x
B0 B1 B2 B2 B4 B5 B5
CASE2 MARCH B1
then CASE3 and B2
CASE4 LINK x next-x
Cin=1 B0 B2 B2 B2 B4 B5 B5
1+1=11
CS 473 Lecture X 39
x next-x
B0 B2 B2 B4 B5 B5
CASE1
B2 B3
MARCH
Cin=1
0+0=1
x next-x
B0 B2 B3 B4 B5 B5
CASE1
MARCH
Cin=0
x next-x
0+1=1
B0 B2 B3 B4 B5 B5
CASE3 OR 4 x
LINK Cin=0 B0 B2 B3 B4 B5
1+0=10
B5 B6
CS 473 Lecture X 40
Inserting a Node
BINOMIAL-HEAP-INSERT (H,x)
CS 473 Lecture X 41
Relationship Between Insertion &
Incrementing a Binary Number
H : n1=51 H = < 110011> = { B0, B1 ,B4, B5 }
H
B0 B1 B4 B5
MERGE x next-x
( H,H’) B0 B0 B1 B4 B5
5 4 3 2 1 0
x next-x
LINK B0 B1 B4 B5 1
1 1 0 0 1 1
B0 B2 B4 B5
1
B1 B4 B5
LINK
+
B1
CS 473 Lecture X 1 1 0 1 0 0 42
A Direct Implementation that does not Call
Binomial-Heap-Union
- More effıcient
- Case 2 never occurs
- While loop should terminate whenever
case 1 is encountered
CS 473 Lecture X 43
Extracting the Node with the Minimum Key
BINOMIAL-HEAP-EXTRACT-MIN (H)
(1) find the root x with the minimum key in the
root list of H and remove x from the root list of H
(2) H’ MAKE-BINOMIAL-HEAP ( )
(3) reverse the order of the linked list of x’ children
and set head [H’] head of the resulting list
(4) H BINOMIAL-HEAP-UNION (H, H’)
return x
end
CS 473 Lecture X 44
Extracting the Node with the Minimum Key
x
head [H]
B0 B1
B4
B1 B0
B2
CS 473 Lecture X 45
Extracting the Node with the Minimum Key
x
head [H]
B0 B1
B4
B2 B1 B0
head [H’]
CS 473 Lecture X 46
Extracting the Node with the Minimum Key
CS 473 Lecture X 47
Decreasing a Key
BINOMIAL-HEAP-DECREASE-KEY (H, x, k)
key [x] k
y x
z p[y]
while z ≠ NIL and key [y] < key [z] do
exchange key [y] key [z]
exchange satellite fields of y and z
yz
z p [y]
endwhile
end
CS 473 Lecture X 48
Decreasing a Key
CS 473 Lecture X 49
Deleting a Key
BINOMIAL- HEAP- DELETE (H,x)
y←x
z ← p [y]
while z ≠ NIL do RUNNING-TIME= O(lg n)
key [y] ← key [z]
satellite field of y ← satellite field of z
y ← z ; z ← p [y]
endwhile
H’← MAKE-BINOMIAL-HEAP
remove root z from the root list of H
reverse the order of the linked list of z’s children
and set head [H’] ← head of the resulting list
H ← BINOMIAL-HEAP-UNION (H, H’)
CS 473 Lecture X 50
Deleting a Key (Cont.)
H’ ← MAKE-BINOMIAL-HEAP
remove root z from the root list of H
reverse the order of the linked list of z’s children
set head [H’] ← head of the resulting list
H ← BINOMIAL-HEAP-UNION (H, H’)
end
CS 473 Lecture X 51