Chapter 19: Fibonacci Heap - PowerPoint PPT Presentation

1 / 89
About This Presentation
Title:

Chapter 19: Fibonacci Heap

Description:

BINOMIAL-HEAP-EXTRACT-MIN(H) Find minimum, remove root, then union. O(lg n). B3 B0 B1 37 3 18 50 48 31 17 44 29 10 6 head[H] More Operations (2) ... – PowerPoint PPT presentation

Number of Views:456
Avg rating:3.0/5.0
Slides: 90
Provided by: LeongH9
Category:

less

Transcript and Presenter's Notes

Title: Chapter 19: Fibonacci Heap


1
Chapter 19 Fibonacci Heap
Many of the slides are from Prof. Leong Hon Wais
resources at National University of Singapore
2
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 xs key to a new (smaller)
    value k.

3
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 D. D. SleatorR. E. Tarjan
1988 Relaxed Heap Driscoll, GabowShrairman, Tarjan
4
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)
Fibonacci O(1) O(1) O(lg n) O(1) O(lg n) O(1)


5
(No Transcript)
6
Binary Min-Heap (as in Heapsort)
  • Binary min-heap is an array A1..n that can be
    viewed as a nearly complete binary tree
  • Number the nodes using level order traversal
  • LEFT(i) 2i and RIGHT(i) 2i1 and
  • PARENT(i) ?i/2?
  • Height of tree ? lg n
  • Heap Property (Each node its parent node)
  • APARENT(i) ? Ai

7
MinHeap Two views
Left (i) 2i Right (i) 2i 1 Parent (i)
?i/2?
Heap Property for all i AParent(i) ? Ai
A1..10
8
MinHeap Insert operation (1)
HEAP-INSERT(A, key) insert at end of array
SIFT-UP to restore HP
Example HEAP-INSERT(A, 5)
9
MinHeap Insert operation (2)
HEAP-INSERT(A, key) insert at end of array
SIFT-UP to restore HP
Example HEAP-INSERT(A, 5)
4
6
10
12
5
27
11
8
9
16
15
Restore heap propertywith SIFT-UP
10
MinHeap Insert operation (3)
HEAP-INSERT(A, key) insert at end of array
SIFT-UP to restore HP
Example HEAP-INSERT(A, 5)
4
5
10
12
6
27
11
8
9
16
15
Restore heap propertywith SIFT-UP
11
MinHeap Insert operation (4)
HEAP-INSERT(A, key) insert at end of array
SIFT-UP to restore HP
Example HEAP-INSERT(A, 5)
4
5
10
12
6
27
11
8
9
16
15
Done!
12
MinHeap Insert operation (5)
HEAP-INSERT(A, key) ? A is a heap
(array) heap-size(A) ? heap-size(A)
1 Aheap-size(A) ? key SIFT-UP(A, heap-size(A))
THEAP-INSERT(n) TSIFT-UP(n) ?(1)
O(lg n)
SIFT-UP(A, n) key ? An i ? n while i gt 1 and
APARENT(i) gt key do Ai ? APARENT(i) i ?
PARENT(i) Ai ? key
TSIFT-UP(n) O(h) O(lg n) (h comparisons,
h data-moves)
13
MinHeap Extract-Min operation (1)
HEAP-EXTRACT-MIN(A) Exchange A1 and An
SIFT-DOWN to restore HP
HEAP-EXTRACT-MIN(A)
4
6
10
12
8
27
11
9
16
15
A1 is the minimum.
A1 is the minimum Exchange A1 with An
14
MinHeap Extract-Min operation (2)
HEAP-EXTRACT-MIN(A) Exchange A1 and An
SIFT-DOWN to restore HP
SIFT-DOWN to restoreheap property
15
MinHeap Extract-Min operation (3)
HEAP-EXTRACT-MIN(A) Exchange A1 and An
SIFT-DOWN to restore HP
16
MinHeap Extract-Min operation (4)
HEAP-EXTRACT-MIN(A) Exchange A1 and An
SIFT-DOWN to restore HP
Done!
17
MinHeap Extract-Min operation (5)
HEAP-EXTRACT-MIN(A, key) ? A is a heap
(array) min ? A1 A1 ? Aheap-size(A) heap-siz
e(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 Ai gt Aj then
exchange Ai ? Aj i ? j else i ?
heap-size(A)1 ? make i a leaf
18
MinHeap Decrease-Key (1)
HEAP-DECREASE-KEY(A, i, key) update the key
of Ai SIFT-UP to restore HP
HEAP-DECREASE-KEY(A,9,3)
19
MinHeap Decrease-Key (2)
HEAP-DECREASE-KEY(A, i, key) update the key
of Ai SIFT-UP to restore HP
HEAP-DECREASE-KEY(A,9,3)
Restore heap propertywith SIFT-UP
20
MinHeap Decrease-Key (3)
HEAP-DECREASE-KEY(A, i, key) update the key
of Ai SIFT-UP to restore HP
HEAP-DECREASE-KEY(A,9,3)
21
MinHeap Decrease-Key (4)
HEAP-DECREASE-KEY(A, i, key) update the key
of Ai SIFT-UP to restore HP
HEAP-DECREASE-KEY(A,9,3)
22
MinHeap Decrease-Key (5)
HEAP-DECREASE-KEY(A, i, key) update the key
of Ai SIFT-UP to restore HP
HEAP-DECREASE-KEY(A,9,3)
DONE !
23
MinHeap Insert operation (6)
HEAP-DECREASE-KEY (A, i, key) ? Update Ai to
key Ai ? key SIFT-UP(A, i)
TDECREASE-KEY(n) TSIFT-UP(n) ?(1)
O(lg n) (h comparisons, h
data-moves)
24
Binary Heap (Summary)
  • HEAP-MINIMUM(A) O(1)
  • return root
  • HEAP-INSERT(A, x) O(lg n)
  • append to end, sift-up
  • HEAP-EXTRACT-MIN(A) O(lg n)
  • Replace root, sift-down
  • HEAP-DECREASE-KEY(A, i, k) O(lg n)
  • update key, sift-up
  • What about
  • HEAP-DELETE(A, i)
  • HEAP-MELD(A, B)

25
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

26
Other Heaps
27
Binomial Trees
Recursive definition
Some examples
28
Properties of Binomial Trees
  • For a Binomial Tree Bk (of order k)
  • 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)

29
Defining Property of Binomial Trees
B4
30
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.

11 (1011)2
31
Representing Binomial Heaps (1)
  • Each node x stores
  • keyx
  • degreex
  • px
  • childx
  • siblingx
  • (3 pointers per node)

32
Representing Binomial Heap (2)
Each node x has px childx siblingx
degreex, keyx
33
The Binomial Heap
Its actual implementation
34
Operations on a Binomial Heap
  • MAKE-BINOMIAL-HEAP(H)
  • Allocate object H, make headH NIL. ?(1).
  • BINOMIAL-HEAP-MINIMUM(H)
  • Search the root list for minimum. O(lg n).

35
Linking Step Fundamental Op
  • BINOMIAL-LINK (y, z)

BINOMIAL-LINK (y, z) ? Assume z ? y py ?
z siblingy ? childz childz ? y degreez ?
degreez 1
Constant time O(1)
36
Binomial Heap Union

19 7 26
37
Binomial Heap Union

38
Binomial Heap Union

39

40

41

42

43
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).

44
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).

45
More Operations (2)
  • BINOMIAL-HEAP-DECREASE (H, x, k)
  • BINOMIAL-HEAP-DELETE (H, x)

46
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)

47
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

48
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

49
Representing Fibonacci Heaps (1)
  • Each node x stores
  • keyx
  • degreex (also rank(x))
  • markx
  • px
  • childx
  • leftx
  • right x
  • (4 pointers per node)

50
Representing Fibonacci Heaps (2)
51
Properties and Potential Function
  • Properties of FIB-HEAP H
  • Mark(x) TRUE (marked, black in my notes),
    FALSE (yellow in notes)
  • nH number of nodes in H
  • t(H) number of trees in H
  • m(H) number of marked notes in H

Potential Function for Amortized Analysis ?(H)
t(H) 2 m(H) .
nH 14 t(H) 5 m(H) 3
52
Operations on a Fibonacci Heap
  • MAKE-FIB-HEAP(H)
  • Allocate object H, make minH NIL. ?(1).
  • FIB-HEAP-MINIMUM(H)
  • minH points to minimum element. O(1).

nH 14 t(H) 5 m(H) 3
53
Lazy Operation INSERT (1)
  • FIB-HEAP-INSERT(H, x)
  • Be lazy Create singleton tree with node x

Eg Insert 21
21
nH 14 t(H) 5 m(H) 3
54
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 minH, nH.

Eg Insert 21
21
nH 15 t(H) 6 m(H) 3
nH 14 t(H) 5 m(H) 3
55
Pseudocode for INSERT
FIB-HEAP-INSERT (H, x) degreex ? 0 px ?
NIL childx ? NIL leftx ? x rightx ?
x markx ? FALSE concatenate the root list
containing x with root list H if minH NIL
or keyx lt keyminH then minH ? x nH ?
nH 1
Worst case time O(1) Amortized time O(1)
Amortized Time of INSERT c(H) c(H)
?(H) ? ?(H) O(1) 1 O(1)
56
Lazy Operations MELD
  • FIB-HEAP-MELD(H1, H2)
  • Concatenate the two root lists
  • Update minH, nH

minH1
nH1 7 t(H1) 3 m(H1) 1
nH2 7 t(H2) 2 m(H2) 2
57
Lazy Operations MELD
  • FIB-HEAP-MELD(H1, H2)
  • Concatenate the two root lists
  • Update minH, nH

nH 14 t(H) 5 m(H) 3
58
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? )
59
Linking Step Fundamental Op
  • FIB-HEAP-LINK (H, y, x)

FIB-HEAP-LINK (H, y, x) ? Assume x ? y
remove y from the root list of H make y a child
of x, incrementing degreex markz ? FALSE
Constant time O(1)
60
DELETE-MIN Operation (1)
  • FIB-HEAP-DELETE-MIN(H)
  • Delete the min and concatenate its children into
    root list

nH 14 t(H) 5 m(H) 3
61
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?

minH
?
17
23
7
24
41
18
52
nH 13 t(H) 7 m(H) 3
30
44
26
46
39
35
H
62
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)

17
23
7
24
41
18
52
nH 13 t(H) 7 m(H) 3
30
44
26
46
39
35
H
63
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)

17
23
7
24
41
18
52
nH 13 t(H) 7 m(H) 3
30
44
26
46
39
35
H
64
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)

17
23
7
24
41
18
52
nH 13 t(H) 7 m(H) 3
30
44
26
46
39
35
H
65
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)

current
17
23
7
24
41
18
52
nH 13 t(H) 7 m(H) 3
30
44
26
46
39
Link 17 and 23(degree 0)
35
H
66
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)

current
7
24
41
18
52
nH 13 t(H) 6 m(H) 3
30
44
26
46
39
Link 17 and 7 (degree 1)
35
H
67
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)

current
min
24
41
18
52
nH 13 t(H) 5 m(H) 3
44
26
46
39
Link 7 and 24 (degree 2)
35
H
68
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)

min
current
41
18
52
nH 13 t(H) 4 m(H) 3
44
39
H
69
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)

current
min
41
18
52
nH 13 t(H) 4 m(H) 3
44
39
H
70
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)

current
min
41
18
52
nH 13 t(H) 4 m(H) 3
44
39
H
71
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)

current
min
41
18
52
nH 13 t(H) 4 m(H) 3
44
39
Link 41 and 18 (degree 1)
H
72
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)

current
min
52
nH 13 t(H) 3 m(H) 3
DONE !
H
73
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)

Final outcome
nH 13 t(H) 3 m(H) 3
H
74
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

trees t(H) t(H)D(n)?1 t(H)D(n)?1?k
  • Steps in DELETE-MIN Cost
  • Remove min node x O(1)
  • Concat children of x to root list O(D(n))
  • Initialize ptr data structure A O(D(n))
  • Consolidation step O(D(n)k)
  • TOTAL O(D(n)k)

Amortized Time c(H) c(H) ??
O(D(n))
Show later that D(n) O(lg n)
75
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? )
76
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,

nH 16 t(H) 3 m(H) 3
Decrease 45 to 16
77
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 minH

nH 16 t(H) 3 m(H) 3
Decrease 45 to 16
78
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 minH
  • If px is not marked, mark px

24
nH 16 t(H) 4 m(H) 4
nH 16 t(H) 4 m(H) 3
Done !
Decrease 45 to 16
79
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 minH

nH 16 t(H) 4 m(H) 4
Decrease 35 to 4
80
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 minH
  • If px is marked, then cut off px, unmark,
    insert

minH
4
nH 16 t(H) 5 m(H) 4
Decrease 35 to 4
81
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 minH
  • If px is marked, then cut off px, unmark,
    insert REPEAT

minH
4
26
88
nH 16 t(H) 6 m(H) 3
Decrease 35 to 4
82
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 minH
  • If px is marked, then cut off px, unmark,
    insert REPEAT

minH
4
26
24
88
nH 16 t(H) 7 m(H) 2
Done !
Decrease 35 to 4
83
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

trees t(H) t(H)c t(H)c
  • Steps in DECREASE-KEY Cost
  • Decrease key of x O(1)
  • Cascading cuts (unmark) O(c)
  • Mark final parent node O(1)
  • TOTAL O(c)

mark m(H) m(H)?c1 m(H)?c2
?? c 2(2?c) (4 ? c )
Amortized Time c(H) c(H) ?? O(1)
Viola !!
84
DELETE Operation
  • FIB-HEAP-DELETE(H, x)
  • DECREASE-KEY(H, x, ??)
  • DELETE-MIN(H)
  • Amortized Time O(D(n))

nH 14 t(H) 5 m(H) 3
85
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
ratioand lg ? n 1.44 lg n
86
Bounding D(n) (1)
Lemma 1 Let x be any node in FIB-HEAP with
degreex k. Let y1, y2,, yn be children of
x in the order in which they are linked to x
(from earliest to latest). Then degreey1
? 0 and degreeyi ? i?2 for i2, ,k.
Proof When yi was linked to x, degreex
i?1 since all of y1, y2,, yi?1 were
already children of x. Thus, degreeyi
degreex i?1 at the time of linking. In
addition, yi could lose at most 1 child, so
degreeyi ? i?2
87
Bounding D(n) (2)
To bound D(n), two possible approaches 1.
For a given size n, what is the maximum degreex
? 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.
f3
f4
Fibonacci numbers
88
Bounding D(n) (3)
Lemma Let x be node in FH, and degreexk.
Then, size(x) ? sk ? Fk2 ? ?k .
Proof First some small values s0 1,
s1 2, s2 3 General Case
Then show Fk2 1 (F0F1Fk) Fk2 ? ?k
sk ? Fk2
Corollary D(n) ? lg?n 1.44 lg n O(lg n)
89
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)
Write a Comment
User Comments (0)
About PowerShow.com