Beruflich Dokumente
Kultur Dokumente
BINOMIAL HEAPS
PRESENTED BY:PRAMOD TRIPATHI M.TECH 1ST SEM E.NO.112109
Introduction
A way to implement mergeable heaps.
Useful in scheduling algorithms and graph algorithms.
Operations:
Make-Heap(). Insert(H, x), where x is a node H. Minimum(H). Extract-Min(H). Union(H1, H2): merge H1 and H2, creating a new heap. Decrease-Key(H, x, k): decrease x.key (x is a node in H) to k. (Its assumed that k e x.key.)
Jim Anderson
Binomial Heaps - 2
Definitions
Binomial Heap: Collection of binomial trees (satisfying some properties). Binomial Trees Definition is inductive. These are ordered trees, i.e., order of children is important.
1 2 4 5 3 Different Trees 3 4 1 2 5
Bk-1
is a binomial tree.
Examples
B0
B1 B2
B3 depth # nodes 0 1 1 2 3 4
Jim Anderson
B4
4 6 4 1
Binomial Heaps - 4
Bk-2 Bk-1 Bk
B2
B1
B0
Jim Anderson
Binomial Heaps - 5
2. Induction on k.
height of B k ! 1 height of B k -1 ! 1 (k 1) !k
Binomial Heaps - 6
! 2k
Proof (Continued)
3. Let D(k,i) = # nodes at depth i in Bk. Want to show D(k,i) =
depth i in this Bk-1 Bk Bk-1 depth i1 in this Bk-1 So, Bk-1 depth i in Bk
k i
D(k, i) ! D(k 1, i) D(k 1, i 1) k 1 k 1 ! , by ind. hyp. i i 1 (k 1)! (k 1)! ! i!(k 1 i)! (i 1)!(k i)!
Jim Anderson
(k 1)!(k i) i(k 1)! ! i!(k i)! k!i(k 1)!i(k 1)! ! i!(k i)! k ! i
Binomial Heaps - 7
Proof (Continued)
4. Root degree of Bk = 1 + root degree of Bk-1 = 1 + k1 , induction hypothesis =k
Jim Anderson
Binomial Heaps - 8
Binomial Heaps
Set of binomial trees satisfying binomial-heap properties:
1 Heap ordered: In each binomial tree, key of a node is at least parents key .
Implies root of a binomial tree has the smallest key in that tree.
2 Set includes at most one binomial tree whose root is a given degree.
Implies B.H. with n nodes has at most lg n + 1 B.T.s.
Jim Anderson
Binomial Heaps - 9
12 18
8 17
14 38
29
11 27
10
1 25
12 18
8 17
14 38
29
11 27
Jim Anderson
Binomial Heaps - 10
y Bk-1
z Bk-1 Link
z y Bk-1 Bk-1
Jim Anderson
Binomial Heaps - 12
Union
H 1, H 2 H1 = Union H1 H2 H2 =
First, simply merge the two root lists by root degree (like merge sort).
Remaining Problem: Can have two trees with the same root degree.
Jim Anderson Binomial Heaps - 13
Union (Continued)
Union traverses the new root list like this:
prev-x x next-x
Depending on what x, next-x, and sibling[next-x] point to, Union links trees with the same root degree. Note: We may temporarily create three trees with the same root degree.
Jim Anderson Binomial Heaps - 14
Analogy
head[H1]
12 7 25 15 33
head[H2]
18
3 37
28 41
8 22 48 50
29 31
10
44
30 32
23 24
17
45
Union
55
prev-x head[H]
12 18 3 37 8 25 30 41 45 55 32 23 24 22 48 50 29 31
x
6
Like binary addition: 1 1 1 (carries) 00111 10011 11010 temporarily have three trees of this degree
Binomial Heaps - 15
15 33
10
44
28
17
Jim Anderson
prev-x
next-x sibling[next-x]
Cases
Case 1 a
prev-x
next-x
b
Bk
c
Bl
b
Bk
c
Bl
x
prev-x
next-x sibling[next-x]
b
Bk
c
Bk
d
Bk
Case 2
prev-x
next-x
b
Bk
c
Bk
x
d
Bk
next-x
prev-x
next-x sibling[next-x]
Case 3
prev-x
a c
b
Bk Bk Bk+1
d
Bl
Bl Bk Bk key[x] e key[next[x]]
prev-x
prev-x
next-x sibling[next-x]
Case 4
next-x
a b
c
Bk Bk
d
Bl
Union Example
head[H1]
12 7 25 15 33
head[H2]
18
3 37
28 41
8 22 48 50
29 31
10
44
30 32
23 24
17
45 55
Merge
x head[H]
12
next-x
18 7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6
28
10
44
17
45 55
Jim Anderson
Binomial Heaps - 18
next-x
18 7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6
28
10
44
17
45 55
Case 3
x head[H]
12 18
next-x
7 25 3 37 28 41 30 32 23 24 22 48 50 15 33 8 29 31 6
10
44
17
45 55
Jim Anderson
Binomial Heaps - 19
next-x
7 25 3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28
10
44
17
45 55
Case 2
prev-x head[H]
12 18
x
7 25
next-x
3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28 44
10
17
45 55
Jim Anderson
Binomial Heaps - 20
x
7 25
next-x
3 37 15 33 8 41 30 32 23 24 22 48 50 29 31 6 28 44
10
17
45
Case 4
55
prev-x head[H]
12 18
x
3 37 28 41
next-x
15 33 8 29 31 6 44
7 25
10
30 32
23 24
22
48 50
17
45 55
Jim Anderson
Binomial Heaps - 21
x
3 37 28 41
next-x
15 33 8 29 31 6 44
7 25
10
30 32
23 24
22
48 50
17
45
Case 3
55
prev-x head[H]
12 18
x
3 37 8 25 30 23 24 22 48 50 29 31
next-x
6 44
15 33
10
28 41
17
45 55
32
Jim Anderson
Binomial Heaps - 22
x
3 37 8 25 30 23 24 22 48 50 29 31
next-x
6 44
15 33
10
28 41
17
45
32
Case 1
55
prev-x head[H]
12 18 3 37 8 25 30 41 45 55 32 23 24 22 48 50 29 31
x
6
15 33
10
44
28
17
Jim Anderson
Jim Anderson
Binomial Heaps - 24
Extract-Min Example
head[H]
37 41 28 77 8 17 14 38 29 26 42 10 13 6 16 23 1 25 12
18
11 27
head[H]
x
37 41 28 77 8 17 14 38 29 26 42 10 13 6 16 23 1 25 12
18
11 27
Jim Anderson
Binomial Heaps - 25
head[Hd]
25
12
16 23
6 29
18
26 42
8 17
14 38
11 27
head[H]
25 37 41
12 18 10 13 11 27 8 17
6 29
14
16 23
28 77
38
26 42
Jim Anderson
Binomial Heaps - 26
Decrease-Key
Decrease-Key(H, x, k)
if k > key[x] then error ;} key[x] := k; y := x; z := p[y]; while z { NIL and key[y] < key[z] do exchange key[y] and key[z]; y := z; z := p[y] }
O(lg n)
Jim Anderson Binomial Heaps - 27
Decrease-Key Example
head[H]
25 37 41 16 23 28 77 13 11 27 12 18 10 8 17 6 29 14 38
26 42
Decrease key 26 to 7
head[H]
25 37 41
12 18 10 8 17
6 29
14
z y
7 42
16 23
28 77
13
11 27
38
Jim Anderson
Binomial Heaps - 28
z
7 23 28 77
10 13 11 27
8 17
14
29
y
16 42
38
head[H]
z
25 37 41 10 23 28 77 13 11 27 12 18 6
y
7
8 17
14
29
38
16 42
Jim Anderson
Binomial Heaps - 29
Delete
Delete(H, x) Decrease-Key(H, x, g); Extract-Min(H)
Time is O(lg n)
Jim Anderson
Binomial Heaps - 30