Beruflich Dokumente
Kultur Dokumente
Lecture
BINOMIAL HEAPS
CS 473
Lecture X
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
Mergeable Heaps
DELETE (H, x)
Deletes node x from heap H.
CS 473
Lecture X
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
B1
B1
B0 B1
B0
B1
B2
B3
B3
B2 B1
B0
CS 473
B4
Lecture X
Binomial Trees
B1 B2
Bo
Bk-1
Bk-2
Bk
CS 473 Lecture X 8
d=i
Bk-1
Bk-1
D(k,i)=D(k-1,i -1) + D(k-1,i) = true by induction
k-1 i -1
CS 473 Lecture X
k-1 + i =
k i
11
B1 B0 Bk-3 Bk-2
B1 B0
B2
Bk-3
Bk-2
B2
CS 473
Bk-1
Lecture X
13
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, n b
lgn
,b
lgn
lgn
i b 2 i=0 i
By property 1 of the lemma (Bi contains 2i nodes) Bi appears in H iff bit bi=1
CS 473 Lecture X 16
Binomial Heaps
Example: A binomial heap with n = 13 nodes
3 2 1 0
B0 B2
CS 473
12
25
18
11
17
38
27 Lecture X
B3
17
10 10
10 10
10 10
10 10
10 10
10 10
child
10 10
sibling
10 10
10 10
10 10
10
CS 473
Lecture X
19
CS 473
Lecture X
21
end
CS 473
Lecture X
22
RUNNINGTIME = O (lgn)
CS 473 Lecture X 23
CS 473
Lecture X
24
CS 473
Lecture X
25
+1
NIL
sibling [y]
CS 473
Lecture X
26
Bk
l >k
prev-x
Bl
next-x
Bk
CS 473
Bl
Lecture X 29
BK
prev-x
BK
x
BK
next-x
b BK
d BK
30
BK
Lecture X
CS 473
CS 473
Lecture X
31
b
Bk
c
Bk x
d
Bl next-x l>k
CASE 3
prev-x
a c
prev-x
next-x
a b
CS 473
CASE 4
Lecture X
32
CS 473
Lecture X
33
Therefore, BINOMIAL-HEAP-MERGE runs in O(lgn) time and BINOMIAL-HEAP-UNION runs in O (lgn) time
CS 473
Lecture X
34
Binomial-Heap-Union Procedure
BINOMIAL-HEAP-MERGE PROCEDURE
- Merges the root lists of H1 & H2 into a single linkedlist - Sorted by degree into monotonically increasing order
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
else
if prev-x = NIL then head [H] next-x else sibling [prev-x] next-x endif BINOMIAL-LINK(x, next-x) x next-x endif next-x sibling [x] endwhile return H end
CS 473 Lecture X 37
CS 473
Lecture X
38
x
MERGE H CASE1 MARCH Cin=0 1+0=1
B0
next-x
B1 B1 B2 B2 B4 B5 B5
B0
x
B1
next-x
B1 B2 B2 B4 B5 B5
x
B1 B1
next-x
B2 B2 B4 B5 B5
B2
B0 B2
x
B2
next-x
B2 B4 B5 B5
Lecture X
39
x
B0 B2 B2
next-x
B4 B5 B5
B2
B3
x
B2 B3 B4
next-x
B5 B5
x
B5
next-x
B5
x
B0 B2 B3 B4 B5
Lecture X
B5
B6
40
Inserting a Node
BINOMIAL-HEAP-INSERT (H,x)
H' MAKE-BINOMIAL-HEAP (H, x) P [x] NIL child [x] NIL RUNNING-TIME= O(lg n) sibling [x] NIL degree [x] O head [H] x H BINOMIAL-HEAP-UNION (H, H) end
CS 473 Lecture X 41
H = < 110011> =
B0 B1 B4
{ B0, B1 ,B4, B5 }
B5
x
B0
next-x
B0 B1 B4 B5
x
LINK
B0
next-x
B1 B4 B5
B0
B2
B1 B1
B4
B4
B5
B5
5 4 3 2 1 0 1 1 1 0 0 1 1 1 + 1 1 0 1 0 0
LINK
CS 473
Lecture X
42
CS 473
Lecture X
43
head [H]
B0 B1 B4 B2 B1 B0
CS 473
Lecture X
45
x head [H] B0 B1 B4 B2 B1 B0
head [H]
CS 473
Lecture X
46
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
Similar to DECREASE-KEY in BINARY HEAP
BUBBLE-UP the key in the binomial tree it resides in RUNNING TIME: O(lgn)
CS 473
Lecture X
49
Deleting a Key
BINOMIAL- HEAP- DELETE (H,x) yx z p [y] RUNNING-TIME= O(lg n) while z NIL do 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 zs children and set head [H] head of the resulting list H BINOMIAL-HEAP-UNION (H, H)
CS 473
Lecture X
50
CS 473
Lecture X
51