Priority Queues

advertisement
Chapter 19: Fibonacci Heap
Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore
Priority Queue ADT
 Priority Queue is an ADT for maintaining a set
S of elements, each with a key value and
supports the following operations:
 INSERT(S, x)
inserts element x into S
(also write as S  S  {x}
 MINIMUM(S)
returns element in S
with min key
 EXTRACT-MIN(S)
removes and returns element
in S with min key
 DECREASE-KEY(S, x, k) decreases the value of
element x’s key to a new
(smaller) value k.
PQ implementations…
 Many data structures proposed for PQ
1964
Binary Heap
J. W. J. Williams
1972
Leftist Heap
C. A. Crane
1978
Binomial Heap
J. Vuillemin
1984
Fibonacci Heap
M. L. Fredman,
R. E. Tarjan
1985
Skew Heap
1988
Relaxed Heap
D. D. Sleator
R. E. Tarjan
Driscoll, Gabow
Shrairman, Tarjan
Different Priority Queues
 Time Bounds for different PQ implementations
 n is the number of items in the PQ
Data Str
INSERT
MIN
Extract
-MIN
D-KEY
DELETE
Union
Binary H
O(lg n)
O(1)
O(lg n)
O(lg n)
O(lg n)
O(n)
Binomial H
O(lg n)
O(lg n)
O(lg n)
O(lg n)
O(lg n)
O(lg n)
O(1)
O(1)
O(lg n)
O(1)
O(lg n)
O(1)
Fibonacci
Binary Min-Heap (as in Heapsort)
 Binary min-heap is an array A[1..n] that can be
viewed as a nearly complete binary tree
 Number the nodes using level order traversal
 LEFT(i) = 2i and RIGHT(i) = 2i+1 and
 PARENT(i) = i/2
 Height of tree  lg n
 Heap Property: (Each node ≥ its parent node)
 A[PARENT(i)]  A[i]
MinHeap – Two views
Left (i) = 2i
Right (i) = 2i + 1
Array index ::=
1
level order traversal
2
15
8
3
1
2
4
6
A[1..10]
8
5
9
16
3
Parent (i) = i/2
6
12
4
4
9
4
10 12
6
27
8
7
11
Heap Property: for all i
A[Parent(i)]  A[i]
10
5
10
6
7
8
9
27 11 15 16
10
9
MinHeap – Insert operation (1)
HEAP-INSERT(A, key):
1
insert at end of array
SIFT-UP to restore HP
2
4
8
3
8
5
9
4
6
12
15
Example:
HEAP-INSERT(A, 5)
16
9
27
6
7
5
10
10
Restore heap property
with SIFT-UP
1
2
3
4
5
6
7
8
9
10
11
4
6
10
12
8
27
11
15
16
9
5
A[1..10]
11
MinHeap – Insert operation (2)
HEAP-INSERT(A, key):
1
insert at end of array
SIFT-UP to restore HP
2
4
8
3
5
5
9
4
6
12
15
Example:
HEAP-INSERT(A, 5)
16
9
27
6
7
8
10
10
Restore heap property
with SIFT-UP
1
2
3
4
5
6
7
8
9
10
11
4
6
10
12
5
27
11
15
16
9
8
A[1..10]
11
MinHeap – Insert operation (3)
HEAP-INSERT(A, key):
1
insert at end of array
SIFT-UP to restore HP
2
4
8
3
6
5
9
4
5
12
15
Example:
HEAP-INSERT(A, 5)
16
9
27
6
7
8
10
10
Restore heap property
with SIFT-UP
1
2
3
4
5
6
7
8
9
10
11
4
5
10
12
6
27
11
15
16
9
8
A[1..10]
11
MinHeap – Insert operation (4)
HEAP-INSERT(A, key):
1
insert at end of array
SIFT-UP to restore HP
2
4
8
3
6
5
9
4
5
12
15
Example:
HEAP-INSERT(A, 5)
16
9
27
6
10
7
11
8
10
Done!
1
2
3
4
5
6
7
8
9
10
11
4
5
10
12
6
27
11
15
16
9
8
A[1..10]
MinHeap – Insert operation (5)
HEAP-INSERT(A, key) ⊳ A is a heap (array)
heap-size(A)  heap-size(A) + 1
A[heap-size(A)]  key
SIFT-UP(A, heap-size(A))
THEAP-INSERT(n) = TSIFT-UP(n) + (1)
SIFT-UP(A, n)
= O(lg n)
key  A[n]
in
while i > 1 and A[PARENT(i)] > key
do A[i]  A[PARENT(i)]
i  PARENT(i)
A[i]  key TSIFT-UP(n) = O(h) = O(lg n)
(h comparisons, h data-moves)
MinHeap – Extract-Min operation (1)
HEAP-EXTRACT-MIN(A):
1
Exchange A[1] and A[n]
SIFT-DOWN to restore HP
2
4
8
6
3
12
15
8
5
9
4
16
9
27
6
10
10
7
11
A[1]
A[1] is
is the
the minimum.
minimum
Exchange A[1] with A[n]
1
2
3
4
5
6
7
8
9
10
4
6
10
12
8
27
11
15
16
9
A[1..10]
MinHeap – Extract-Min operation (2)
HEAP-EXTRACT-MIN(A):
1
Exchange A[1] and A[n]
SIFT-DOWN to restore HP
2
4
8
15
3
8
5
9
4
9
6
12
16
4
27
6
10
10
7
11
SIFT-DOWN to restore
heap property
1
2
3
4
5
6
7
8
9
10
9
6
10
12
8
27
11
15
16
4
A[1..10]
return
min
MinHeap – Extract-Min operation (3)
HEAP-EXTRACT-MIN(A):
1
Exchange A[1] and A[n]
SIFT-DOWN to restore HP
2
4
6
9
3
12
5
8
27
6
8
15
1
2
3
4
5
6
7
8
9
6
9
10
12
8
27
11
15
16
A[1..10]
9
16
10
7
11
SIFT-DOWN to restore
heap property
10
MinHeap – Extract-Min operation (4)
HEAP-EXTRACT-MIN(A):
1
Exchange A[1] and A[n]
SIFT-DOWN to restore HP
2
4
8
8
3
12
15
5
9
6
9
27
6
10
7
11
16
Done!
1
2
3
4
5
6
7
8
9
6
8
10
12
9
27
11
15
16
A[1..10]
10
MinHeap – Extract-Min operation (5)
HEAP-EXTRACT-MIN(A, key)
⊳ A is a heap (array)
min  A[1]
A[1]  A[heap-size(A)]
heap-size(A)  heap-size(A)  1
SIFT-DOWN(A, 1)
TSIFT-DOWN (n) = O(h) = O(lg n)
(2h comparisons, h data-moves)
THEAP-EXTRACT-MIN
(n) = O(lg n)
SIFT-DOWN(A,
i)
while i is not a leaf
do let j be the index of min child of i
if A[i] > A[j]
then exchange A[i]  A[j]
ij
else i  heap-size(A)+1 ⊳ make i a leaf
MinHeap – Decrease-Key (1)
HEAP-DECREASE-KEY(A, i, key):
1
update the key of A[i]
SIFT-UP to restore HP
2
4
8
3
8
5
9
4
6
12
15
HEAP-DECREASE-KEY(A,9,3)
16
9
27
6
7
10
1
2
3
4
5
6
7
8
9
10
4
6
10
12
8
27
11
15
16
9
A[1..10]
10
11
MinHeap – Decrease-Key (2)
HEAP-DECREASE-KEY(A, i, key):
1
update the key of A[i]
SIFT-UP to restore HP
2
4
8
3
8
5
9
4
6
12
15
HEAP-DECREASE-KEY(A,9,3)
3
9
27
6
7
11
10
Restore heap property
with SIFT-UP
1
2
3
4
5
6
7
8
9
10
4
6
10
12
8
27
11
15
3
9
A[1..10]
10
MinHeap – Decrease-Key (3)
HEAP-DECREASE-KEY(A, i, key):
1
update the key of A[i]
SIFT-UP to restore HP
2
4
8
3
8
5
9
4
6
3
15
HEAP-DECREASE-KEY(A,9,3)
12
9
27
6
7
10
1
2
3
4
5
6
7
8
9
10
4
6
10
3
8
27
11
15
12
9
A[1..10]
10
11
MinHeap – Decrease-Key (4)
HEAP-DECREASE-KEY(A, i, key):
1
update the key of A[i]
SIFT-UP to restore HP
2
4
8
3
8
5
9
4
3
6
15
HEAP-DECREASE-KEY(A,9,3)
12
9
27
6
7
10
1
2
3
4
5
6
7
8
9
10
4
3
10
6
8
27
11
15
12
9
A[1..10]
10
11
MinHeap – Decrease-Key (5)
HEAP-DECREASE-KEY(A, i, key):
1
update the key of A[i]
SIFT-UP to restore HP
2
4
8
3
8
5
9
3
4
6
15
HEAP-DECREASE-KEY(A,9,3)
12
9
27
6
10
7
11
10
DONE !
1
2
3
4
5
6
7
8
9
10
3
4
10
6
8
27
11
15
12
9
A[1..10]
MinHeap – Insert operation (6)
HEAP-DECREASE-KEY (A, i, key) ⊳ Update A[i] to key
A[i]  key
SIFT-UP(A, i)
TDECREASE-KEY(n) = TSIFT-UP(n) + (1)
= O(lg n)
(h comparisons, h data-moves)
Binary Heap (Summary)
 HEAP-MINIMUM(A)
 return root
O(1)
 HEAP-INSERT(A, x)
 append to end, sift-up
O(lg n)
 HEAP-EXTRACT-MIN(A)
 Replace root, sift-down
O(lg n)
 HEAP-DECREASE-KEY(A, i, k)
 update key, sift-up
O(lg n)
 What about
 HEAP-DELETE(A, i)
 HEAP-MELD(A, B)
How about Union?
 A mergeable heap is any data structure
that supports the basic heap operation
plus union
 Union (H1, H2) creates and returns a new
heap
Other Heaps
Binomial Trees
Recursive definition:
Bk
Bk
B0
Bk1
B2
Bk1
Bk1
Some examples:
B0
B1
B2
B3
B4
B1
B0
Properties of Binomial Trees
 For a Binomial Tree Bk (of order k)
1.
2.
3.
4.
there are 2k nodes,
the height of the tree is k,
root has degree k and
deleting the root gives binomial trees B0, B1, …, Bk1
Proof: (DIY, by induction)
depth
0
1
2
3
B0
B1
B2
B3
B4
4
Defining Property of Binomial Trees
k 
 
There are exactly  i  nodes at depth i,
(0  i  k)
depth
B4
Nodes
0
1
1
4
2
6
3
4
4
1
Binomial Heap (Vuillemin, 1978)
 A sequence of binomial trees that satisfy
 binomial heap property (each tree Bk is a min-heap)
 0 or 1 binomial tree Bk of order k,
 There are at most lg n + 1 binomial trees.
 Eg: A binomial heap H with n = 11 nodes.
head[H]
18
3
6
37
48
11 = (1011)2
29
10
31
17
50
B0
B1
B3
44
Representing Binomial Heaps (1)
 Each node x stores
 key[x]
 degree[x]
 p[x]
 child[x]
 sibling[x]
parent
key
10
1
degree
sibling
 (3 pointers per node)
child
Representing Binomial Heap (2)
head[H]
18
3
6
37
48
29
10
31
17
44
50
head[H]
18
0
Each node x has
p[x]
child[x]
sibling[x]
degree[x], key[x]
3
1
37
0
48
1
50
2
29
2
10
1
31
0
17
0
6
3
44
0
The Binomial Heap:
It’s actual
implementation
Operations on a Binomial Heap
 MAKE-BINOMIAL-HEAP(H)
 Allocate object H, make head[H] = NIL. (1).
 BINOMIAL-HEAP-MINIMUM(H)
 Search the root list for minimum. O(lg n).
head[H]
18
3
6
37
48
29
10
31
17
50
B0
B1
B3
44
Linking Step: Fundamental Op
 BINOMIAL-LINK (y, z)
y
z
Bk1
Bk1
Assume z  y
Linking step
z
y
Bk1
Bk1
BINOMIAL-LINK (y, z) ⊳ Assume z  y
p[y]  z
sibling[y]  child[z]
Constant time O(1)
child[z]  y
degree[z]  degree[z] + 1
Binomial Heap Union
8
30
45
+
32
23
24
22
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
55
41
19 + 7 = 26
+
1
1
1
1
0
0
1
1
0
0
1
1
1
1
1
0
1
0
18
12
Binomial Heap Union
8
30
45
+
55
32
23
24
22
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
41
18
12
12
Binomial Heap Union
8
30
45
+
55
32
23
24
22
48
29
10
31
17
18
6
3
44
37
15
7
33
25
50
28
41
18
12
7
3
12
37
18
25
8
30
45
+
55
32
23
24
22
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
41
12
18
18
12
3
28
15
7
33
25
37
7
3
12
37
18
25
41
8
30
45
+
55
32
23
24
22
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
41
12
18
18
12
3
28
15
7
33
25
37
7
3
12
37
18
25
41
8
30
45
+
32
23
24
22
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
55
41
28
41
15
7
33
25
3
12
37
18
18
12
3
28
15
7
33
25
37
7
3
12
37
18
25
41
8
30
45
+
32
23
22
24
48
29
10
31
17
6
3
44
37
15
7
33
25
50
28
55
41
6
8
45
55
30
23
32
24
22
48
50
29
10
31
17
44
28
41
15
7
33
25
3
12
37
18
18
12
Binomial Heap Union
 MAKE-BINOMIAL-HEAP-UNION (H1, H2) :
 Create a heap H that is the union of two heaps H1
and H2
 Analogous to binary addition of n1 and n2
 Running time.: O(log n)
[n = n1 + n2]
 Prop to # of trees in root lists  2( log2 n + 1).
19 + 7 = 26
+
1
1
1
1
0
0
1
1
0
0
1
1
1
1
1
0
1
0
More Operations (1)
 BINOMIAL-HEAP-INSERT(H, x)
 Create a one-item (x) binomial heap H1 and then union
H and H1. O(lg n).
 BINOMIAL-HEAP-EXTRACT-MIN(H)
 Find minimum, remove root, then union. O(lg n).
head[H]
18
3
6
37
48
29
10
31
17
50
B0
B1
B3
44
More Operations (2)
 BINOMIAL-HEAP-DECREASE (H, x, k)
 BINOMIAL-HEAP-DELETE (H, x)
head[H]
18
3
6
37
48
29
10
31
17
50
B0
B1
B3
44
Binomial Heaps (Summary)
 MINIMUM(H)
O(lg n)
 UNION(H1, H2)
O(lg n)
 INSERT(H, x)
O(lg n)
 EXTRACT-MIN(H)
O(lg n)
 DECREASE-KEY (H, x, k) O(lg n)
 DELETE (H, x)
O(lg n)
Binomial  Fibonacci Heaps
 Key advantages of Binomial Heap
 Elegant and fast – O(lg n)
 But, it takes O(lg n) to “maintain” the
elegant structure all the time.
 Fibonacci Heaps
 Want fast INSERT, DECREASE-KEY
 Be lazy…
 Do as little as possible, until there is no choice.
 Relax the structure slightly
Fibonacci Heap (Fredman & Tarjan)
 Modified from Binomial Heaps
 With following relaxations…
 Set of heap-ordered trees
 Any number of trees of each rank
 Trees are doubly-linked, but not ordered
min[H]
7
30
24
26
35
23
46
17
marked
nodes
H
3
18
39
52
41
44
Representing Fibonacci Heaps (1)
 Each node x stores
 key[x]
 degree[x] (also rank(x))
 mark[x]
 p[x]
 child[x]
key, degree
 left[x]
left
 right [x]
(4 pointers per node)
parent
10
1
FALSE
mark
right
child
Representing Fibonacci Heaps (2)
min[H]
7
30
24
26
23
17
46
3
18
35
52
41
44
39
min[H]
7
30
24
26
35
23
46
17
3
18
H
39
52
41
44
Properties and Potential Function
 Properties of FIB-HEAP H:
 Mark(x) = TRUE (marked, black in my notes),
(yellow in notes)
 n[H] = number of nodes in H
 t(H) = number of trees in H
 m(H) = number of marked notes in H
FALSE
Potential Function for Amortized Analysis
(H) = t(H) + 2 m(H) .
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
min[H]
17
3
18
H
39
52
41
44
Operations on a Fibonacci Heap
 MAKE-FIB-HEAP(H)
 Allocate object H, make min[H] = NIL. (1).
 FIB-HEAP-MINIMUM(H)
 min[H] points to minimum element.
O(1).
min[H]
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
17
3
18
H
39
52
41
44
Lazy Operation: INSERT (1)
 FIB-HEAP-INSERT(H, x)
 Be lazy: Create singleton tree with node x
21
Eg: Insert 21
min[H]
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
17
3
18
H
39
52
41
44
Lazy Operation: INSERT (2)
 FIB-HEAP-INSERT(H, x)
 Be lazy: Create singleton tree with node x
 Concatenate x with root list of H
 Update min[H], n[H].
Eg: Insert 21
min[H]
7
n[H] = 15
14
t(H) = 65
m(H) = 3
30
24
26
35
23
46
17
21
3
18
H
39
52
41
44
Pseudocode for INSERT
FIB-HEAP-INSERT (H, x)
O(1)
Worst case time = O(1)
degree[x]  0
Amortized time = O(1)
p[x]  NIL
child[x]  NIL
left[x]  x
right[x]  x
mark[x]  FALSE
concatenate the root list containing x with root list H
if min[H] = NIL or key[x] < key[min[H]]
then min[H]  x
n[H]  n[H] + 1
Amortized Time of INSERT:
c’(H) = c(H) + (H’)  (H) = O(1) + 1 = O(1)
Lazy Operations: MELD
 FIB-HEAP-MELD(H1, H2)
 Concatenate the two root lists
 Update min[H], n[H]
min[H2]
min[H1]
7
n[H1] = 7
t(H1) = 3
m(H1) = 1
30
24
26
35
46
H1
23
17
n[H2] = 7
t(H2) = 2
m(H2) = 2
3
18
52
41
39
H2
44
Lazy Operations: MELD
 FIB-HEAP-MELD(H1, H2)
 Concatenate the two root lists
 Update min[H], n[H]
min[H]
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
17
3
18
H
39
52
41
44
What have we got now?
With lazy INSERT and MELD,
We have a doubly-linked list, with pointer to
minimum…
( So, when do we need to do real work? )
Linking Step: Fundamental Op
 FIB-HEAP-LINK (H, y, x)
y
x
Linking step
x
y
Assume z  y
FIB-HEAP-LINK (H, y, x)
⊳ Assume x  y
remove y from the root list of H
make y a child of x, incrementing degree[x]
mark[z]  FALSE
Constant time O(1)
DELETE-MIN Operation (1)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
min[H]
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
17
3
18
H
39
52
41
44
DELETE-MIN Operation (2)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Then what? Where is the new minimum?
min[H]
?
7
n[H] = 13
t(H) = 7
m(H) = 3
30
24
26
35
23
46
17
18
39
H
52
41
44
DELETE-MIN Operation (3)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
current
min
7
n[H] = 13
t(H) = 7
m(H) = 3
30
24
26
35
23
46
17
18
39
H
52
41
44
DELETE-MIN Operation (4)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
current
min
7
n[H] = 13
t(H) = 7
m(H) = 3
30
24
26
35
23
46
17
18
39
H
52
41
44
DELETE-MIN Operation (5)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
min
7
n[H] = 13
t(H) = 7
m(H) = 3
30
24
26
35
46
23
current
H
17
18
39
52
41
44
DELETE-MIN Operation (5)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
3
current
min
7
n[H] = 13
t(H) = 7
m(H) = 3
2
30
24
26
35
23
46
17
18
39
H
52
41
44
Link 17 and 23
(degree 0)
DELETE-MIN Operation (6)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
3
current
min
7
n[H] = 13
t(H) = 6
m(H) = 3
2
30
24
26
35
17
46
23
H
18
39
52
41
44
Link 17 and 7
(degree 1)
DELETE-MIN Operation (7)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
n[H] = 13
t(H) = 5
m(H) = 3
26
35
7
46
17
H
23
3
current
min
24
2
30
18
39
52
41
44
Link 7 and 24
(degree 2)
DELETE-MIN Operation (8)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
min
26
35
24
17
H
46
23
3
current
7
n[H] = 13
t(H) = 4
m(H) = 3
2
30
18
39
52
41
44
DELETE-MIN Operation (9)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
7
26
35
24
17
H
46
23
3
current
min
n[H] = 13
t(H) = 4
m(H) = 3
2
30
18
39
52
41
44
DELETE-MIN Operation (10)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
current
min
7
n[H] = 13
t(H) = 4
m(H) = 3
26
35
24
17
H
46
23
30
18
39
52
41
44
DELETE-MIN Operation (11)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
current
min
7
n[H] = 13
t(H) = 4
m(H) = 3
24
26
35
H
46
17
23
30
18
39
52
41
44
Link 41 and 18
(degree 1)
DELETE-MIN Operation (12)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
0
1
2
3
current
min
7
n[H] = 13
t(H) = 3
m(H) = 3
24
26
35
H
46
17
18
52
41
30
DONE !
23
44
39
DELETE-MIN Operation (13)
 FIB-HEAP-DELETE-MIN(H)
 Delete the min and concatenate its children into root list
 Consolidate trees (so that no two roots of same degree)
min[H]
Final outcome
7
n[H] = 13
t(H) = 3
m(H) = 3
26
35
24
17
46
23
18
52
41
30
H
44
39
Analysis of DELETE-MIN
 Notations:
 Let H be the heap before DELETE-MIN op;
 t(H) is number of trees in H; D(n) is max degree in any n-node FH
 k linking steps are done during consolidation
 Steps in DELETE-MIN





Remove min node x
Concat children of x to root list
Initialize ptr data structure A
Consolidation step…
TOTAL:
# trees
Cost
O(1)
O(D(n))
O(D(n))
O(D(n)+k)
O(D(n)+k)
Amortized Time:
c’(H) = c(H) +  = O(D(n))
t(H)
t(H)+D(n)1
t(H)+D(n)1k
 = D(n)1k
Show later that
D(n) = O(lg n)
What have we got now? (2)
With Mergeable Heap Operations,
i.e., INSERT and MELD, DELETE-MIN
can show that all the tree in H are
actually Binomial Trees !
( Now, how about DECREASE-KEY? )
DECREASE-KEY Operation (1-1)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[1st example: case 1]
 Decrease key of x to k
 Cut off x from its parent,
min[H]
7
n[H] = 16
t(H) = 3
m(H) = 3
35
24
17
26
46
45
30
88
72
23
21
18
38
39
41
52
Decrease 45 to 16
DECREASE-KEY Operation (1-2)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[1st example: case 1]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
min[H]
7
n[H] = 16
t(H) = 3
m(H) = 3
35
24
17
26
46
16
30
88
72
23
21
18
38
39
41
52
Decrease 45 to 16
DECREASE-KEY Operation (1-3)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[1st example: case 1]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
 If p[x] is not marked, mark p[x]
min[H]
7
46
16
72
Mark p[x]
n[H] = 16
t(H) = 4
m(H) = 43
24
26
30
Done !
35
88
17
23
21
18
38
39
41
52
Decrease 45 to 16
DECREASE-KEY Operation (2-1)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[2nd example: case 2]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
min[H]
7
46
16
24
72
n[H] = 16
t(H) = 4
m(H) = 4
26
35
88
17
30
23
21
18
38
39
41
52
Decrease 35 to 4
DECREASE-KEY Operation (2-2)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[2nd example: case 2]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
 If p[x] is marked, then cut off p[x], unmark, insert…
min[H]
46
16
72
n[H] = 16
t(H) = 5
m(H) = 4
4
7
p[x] is marked
26
88
24
17
30
23
21
18
38
39
41
52
Decrease 35 to 4
DECREASE-KEY Operation (2-3)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[2nd example: case 2]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
 If p[x] is marked, then cut off p[x], unmark, insert… REPEAT
min[H]
46
16
72
n[H] = 16
t(H) = 6
m(H) = 3
4
26
88
7
24
p[p[x]] is marked
17
30
23
21
18
38
39
41
52
Decrease 35 to 4
DECREASE-KEY Operation (2-4)
 FIB-HEAP-DECREASE-KEY(H, x, k)
[2nd example: case 2]
 Decrease key of x to k
 Cut off x from its parent, insert x into root list, update min[H]
 If p[x] is marked, then cut off p[x], unmark, insert… REPEAT
min[H]
46
16
72
n[H] = 16
t(H) = 7
m(H) = 2
4
26
24
88
7
17
30
23
21
18
38
39
41
52
Done !
Decrease 35 to 4
Analysis of DECREASE-KEY
 Notations:
 Let H be the heap before DECREASE-KEY op;
 t(H) is number of trees in H; m(H) is # marked nodes in H
 c cascading cuts are done in total
 Steps in DECREASE-KEY




Decrease key of x
Cascading cuts (unmark)
Mark final parent node
TOTAL:
Amortized Time:
c’(H) = c(H) +  = O(1)
Cost
# trees
O(1)
O(c)
O(1)
O(c)
t(H)
t(H)+c
t(H)+c
# mark
m(H)
m(H)c+1
m(H)c+2
 = c + 2(2c)
= (4  c )
Viola !!
DELETE Operation
 FIB-HEAP-DELETE(H, x)
 DECREASE-KEY(H, x, )
 DELETE-MIN(H)
 Amortized Time: O(D(n))
min[H]
7
n[H] = 14
t(H) = 5
m(H) = 3
30
24
26
35
23
46
17
3
18
H
39
52
41
44
Bounding the Maximum Degree
D(n) ::= maximum degree in any FIB-HEAP
with n nodes
Shall prove that
D(n)  lg  n = O(lg n)
where  = (1+5)/2 = 1.61803 [golden ratio]
and lg  n = 1.44 lg n
Bounding D(n)
(1)
Lemma 1:
Let x be any node in FIB-HEAP with degree[x] = k.
Let y1, y2,…, yn be children of x in the order in which they
are linked to x (from earliest to latest). Then
x
degree[y1]  0 and
degree[yi]  i2 for i=2, … ,k.
…
yk
…
yi
y2 y1
Proof:
When yi was linked to x, degree[x] = i1
since all of y1, y2,…, yi1 were already children of x.
Thus, degree[yi] = degree[x] = i1 at the time of linking.
In addition, yi could lose at most 1 child, so degree[yi]  i2
Bounding D(n)
(2)
To bound D(n), two possible approaches:
1. For a given size n, what is the maximum degree[x] ?
2. For a given degree k, what is the smallest size ?
Define
sk ::= minimum size of a tree of degree k in a FH.
& let fk be such a minimal degree-k tree.
f0
f1
f2
f3
f0 f0
Fibonacci numbers
x
f4
f2
f1
…
yk
…
yi
y2 y1
Bounding D(n)
(3)
Lemma:
Let x be node in FH, and degree[x]=k.
Then, size(x)  sk  Fk+2  k .
Proof: First some small values:
s0 = 1, s1 = 2, s2 = 3
General Case:
x
…
yk
…
yi
y2 y1
f0
f1
f2
k
sk  1   sdeg[ yk ] Then show:
i 1
Fk+2 = 1 + (F0+F1+…+Fk)
k
Fk+2  k
sk  2   si 2
i 2
sk  Fk+2
Corollary: D(n)  lgn = 1.44 lg n = O(lg n)
Fibonacci Heaps (Summary)
 MINIMUM(H)
O(1)
 UNION(H1, H2)
O(1)
 INSERT(H, x)
O(1)
 EXTRACT-MIN(H)
O(lg n)*
 DECREASE-KEY (H, x, k) O(1)*
 DELETE (H, x)
O(lg n)*
Download