0% found this document useful (0 votes)
8 views66 pages

FibonacciHeaps 207

The document discusses Fibonacci heaps, a data structure that allows for efficient priority queue operations such as insert, extract-min, and decrease-key. It highlights their performance advantages, particularly the amortized time complexity for operations, and their applications in improving algorithms like Dijkstra's shortest path. The structure and operations of Fibonacci heaps are explained, including the linking of trees and the potential function used for amortized analysis.

Uploaded by

Anindya Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views66 pages

FibonacciHeaps 207

The document discusses Fibonacci heaps, a data structure that allows for efficient priority queue operations such as insert, extract-min, and decrease-key. It highlights their performance advantages, particularly the amortized time complexity for operations, and their applications in improving algorithms like Dijkstra's shortest path. The structure and operations of Fibonacci heaps are explained, including the linking of trees and the potential function used for amortized analysis.

Uploaded by

Anindya Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

F IBONACCI H EAPS

‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

Lecture slides by Kevin Wayne


h t t p : / / w w w . c s . p r i n c e t o n . e d u / ~w a y ne / k l e i nb e r g - t a r d o s

Abdur Rashid Tushar


Lecturer, CSE, BUET

Last updated on 27.07.23 07:19


Priority queues performance cost summary

operation linked list binary heap binomial heap Fibonacci heap †

MAKE-HEAP O(1) O(1) O(1) O(1)

IS-EMPTY O(1) O(1) O(1) O(1)

INSERT O(1) O(log n) O(log n) O(1)

EXTRACT-MIN O(n) O(log n) O(log n) O(log n)

DECREASE-KEY O(1) O(log n) O(log n) O(1)

DELETE O(1) O(log n) O(log n) O(log n)

MELD O(1) O(n) O(log n) O(1)

FIND-MIN O(n) O(1) O(log n) O(1)

† amortized

Ahead. O(1) INSERT and DECREASE-KEY, O(log n) EXTRACT-MIN.

2
Fibonacci heaps

Theorem. [Fredman-Tarjan 1986] Starting from an empty Fibonacci heap,


any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations
involving n INSERT operations takes O(m + n log n) time.

this statement is a bit weaker


than the actual theorem

3
Fibonacci heaps

Theorem. [Fredman–Tarjan 1986] Starting from an empty Fibonacci heap,


any sequence of m INSERT, EXTRACT-MIN, and DECREASE-KEY operations
involving n INSERT operations takes O(m + n log n) time.

History.
・Ingenious data structure and application of amortized analysis.
・Original motivation: improve Dijkstra’s shortest path algorithm
from O(m log n) to O(m + n log n).
・Also improved best-known bounds for all-pairs shortest paths,
assignment problem, minimum spanning trees.

4
F IBONACCI H EAPS

‣ structure
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

SECTION 19.1
Fibonacci heaps

Basic idea.
・Similar to binomial heaps, but less rigid structure.
・Binomial heap: eagerly consolidate trees after each INSERT;
implement DECREASE-KEY by repeatedly exchanging node with its parent.

・Fibonacci heap: lazily defer consolidation until next EXTRACT-MIN;


implement DECREASE-KEY by cutting off node and splicing into root list.

Remark. Height of Fibonacci heap is Θ(n) in worst case, but it doesn’t use
sink or swim operations.

6
Fibonacci heap: structure

・Set of heap-ordered trees.

each child no smaller


than its parent

heap-ordered tree

17 24 23 7 3 root

30 26 46
18 52 41

heap H
35
39 44
7
Fibonacci heap: structure

・Set of heap-ordered trees.


・Set of marked nodes.

used to keep trees bushy


(stay tuned)

min

17 24 23 7 3

30 26 46
18 52 41

heap H marked
35
39 44
8
Fibonacci heap: structure

Heap representation.
・Store a pointer to the minimum node.
・Maintain tree roots in a circular, doubly-linked list.

min

17 24 23 7 3

30 26 46
18 52 41

heap H
35
39 44
9
Fibonacci heap: representation

Node representation. Each node stores:


・A pointer to its parent.
・A pointer to any of its children.
・A pointer to its left and right siblings.
・Its rank = number of children.
・Whether it is marked.

min

17 24 23 7 3 rank = 3

30 26 46
18 52 41

heap H children are in a


35
circular doubly-linked list 39 44
10
Fibonacci heap: representation

Operations we can do in constant time:


・Determine rank of a node.
・Find the minimum element.
・Merge two root lists together.
・Add or remove a node from the root list.
・Remove a subtree and merge into root list.
・Link the root of a one tree to root of another tree.

min

17 24 23 7 3 rank = 3

30 26 46
18 52 41

heap H
35
39 44
11
Fibonacci heap: notation

notation meaning

n number of nodes

rank(x) number of children of node x

rank(H) max rank of any node in heap H

trees(H) number of trees in heap H

marks(H) number of marked nodes in heap H

n = 14 rank(H) = 3 trees(H) = 5 marks(H) = 3


min

rank = 1 17 24 23 7 3 rank = 3

30 26 46
18 52 41

heap H
35
39 44
12
Fibonacci heap: potential function

Potential function.

Φ(H) = trees(H) + 2 ⋅ marks(H)

Φ(H) = 5 + 2⋅3 = 11 trees(H) = 5 marks(H) = 3


min

17 24 23 7 3

30 26 46
18 52 41

heap H
35
39 44
13
F IBONACCI H EAPS

‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

SECTION 19.2
Fibonacci heap: insert

・Create a new singleton tree.


・Add to root list; update min pointer (if necessary).

insert 21

21

min

17 24 23 7 3

30 26 46
18 52 41

heap H
35
39 44
15
Fibonacci heap: insert

・Create a new singleton tree.


・Add to root list; update min pointer (if necessary).

insert 21

min

17 24 23 7 21 3

30 26 46
18 52 41

heap H
35
39 44
16
Fibonacci heap: insert analysis

Actual cost. ci = O(1).

Change in potential. ∆Φ = Φ(Hi) – Φ(Hi–1) = +1. one more tree;


no change in marks

Amortized cost. ĉi = ci + ∆Φ = O(1).

Φ(H) = trees(H) + 2 ⋅ marks(H)

min

17 24 23 7 21 3

30 26 46
18 52 41

heap H
35
39 44
17
F IBONACCI H EAPS

‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

SECTION 19.2
Linking operation

Useful primitive. Combine two trees T1 and T2 of rank k.


・Make larger root be a child of smaller root.
・Resulting tree T ′ has rank k + 1.

tree T1 tree T2 tree T′

15 3 3

56 24 33 18 52 41 15 18 52 41

77 39 44 56 24 33 39 44

77
still heap-ordered

19
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

min

7 24 23 17 3

30 26 46
18 52 41

35 39 44

20
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

min 7 24 23 17 18 52 41

30 26 46 39 44

35

21
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

current
min 7 24 23 17 18 52 41

30 26 46 39 44

35

22
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min 7 24 23 17 18 52 41

30 26 46 39 44

35

23
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min 7 24 23 17 18 52 41

30 26 46 39 44

35

24
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min 7 24 23 17 18 52 41

30 26 46 39 44

35

25
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank
link 23 to 17
0 1 2 3

current
min 7 24 23 17 18 52 41

30 26 46 39 44

35

26
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank
link 17 to 7
0 1 2 3

current
min 7 24 17 18 52 41

30 26 46 23 39 44

35

27
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank
link 24 to 7
0 1 2 3

current
min
24 7 18 52 41

26 46 17 30 39 44

35 23

28
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min
7 18 52 41

24 17 30 39 44

26 46 23

35
29
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min
7 18 52 41

24 17 30 39 44

26 46 23

35
30
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min
7 18 52 41

24 17 30 39 44

26 46 23

35
31
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank
link 41 to 18
0 1 2 3

current
min
7 18 52 41

24 17 30 39 44

26 46 23

35
32
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min
7 52 18

24 17 30 41 39

26 46 23 44

35
33
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

rank

0 1 2 3

current
min
7 52 18

24 17 30 41 39

26 46 23 44

35
34
Fibonacci heap: extract the minimum

・Delete min; meld its children into root list; update min.
・Consolidate trees so that no two roots have same rank.

stop (no two trees have same rank)

min
7 52 18

24 17 30 41 39

26 46 23 44

35
35
Fibonacci heap: extract the minimum analysis

Actual cost. ci = O(rank(H)) + O(trees(H)).


・O(rank(H)) to meld min’s children into root list. ≤ rank(H) children

・O(rank(H)) + O(trees(H)) to update min. ≤ rank(H) + trees(H) – 1 root nodes

・O(rank(H)) + O(trees(H)) to consolidate trees. number of roots decreases by 1 after


each linking operation

Change in potential. ∆Φ ≤ rank(H′) + 1 – trees(H).


・No new nodes become marked.
・trees(H′) ≤ rank(H′) + 1. no two trees have same rank after consolidation

Amortized cost. O(log n).


・ĉi = ci + ∆Φ = O(rank(H)) + O(rank(H′)).
・The rank of a Fibonacci heap with n elements is O(log n).
Fibonacci lemma
(stay tuned)
Φ(H) = trees(H) + 2 ⋅ marks(H)

36
Fibonacci heap vs. binomial heaps

Observation. If only INSERT and EXTRACT-MIN operations, then all trees are
binomial trees.

we link only trees of equal rank

B0 B1 B2 B3

Binomial heap property. This implies rank(H) ≤ log2 n.

Fibonacci heap property. Our DECREASE-KEY implementation will not preserve


this property, but we will implement it in such a way that rank(H) ≤ logφ n.

37
F IBONACCI H EAPS

‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

SECTION 19.3
Fibonacci heap: decrease key

Intuition for deceasing the key of node x.


・If heap-order is not violated, decrease the key of x.
・Otherwise, cut tree rooted at x and meld into root list.

decrease-key of x from 30 to 7

8 29 10 44

30 23 22 48 31 17

45 32 24 50

55
39
Fibonacci heap: decrease key

Intuition for deceasing the key of node x.


・If heap-order is not violated, decrease the key of x.
・Otherwise, cut tree rooted at x and meld into root list.

decrease-key of x from 23 to 5

6 7

8 29 10 44 45 32

23 22 48 31 17 55

24 50

40
Fibonacci heap: decrease key

Intuition for deceasing the key of node x.


・If heap-order is not violated, decrease the key of x.
・Otherwise, cut tree rooted at x and meld into root list.

decrease-key of 22 to 4
decrease-key of 48 to 3
decrease-key of 31 to 2 6 7 5
decrease-key of 17 to 1

8 29 10 44 45 32 24

22 48 31 17 55

50

41
Fibonacci heap: decrease key

Intuition for deceasing the key of node x.


・If heap-order is not violated, decrease the key of x.
・Otherwise, cut tree rooted at x and meld into root list.
・Problem: number of nodes not exponential in rank.

6 7 5 4 3 2 1

8 29 10 44 45 32 24 50

rank = 4, nodes = 5
55

42
Fibonacci heap: decrease key

Intuition for deceasing the key of node x.


・If heap-order is not violated, decrease the key of x.
・Otherwise, cut tree rooted at x and meld into root list.
・Solution: as soon as a node has its second child cut,
cut it off also and meld into root list (and unmark it).

min

7 18 38

marked node: 24 17 23 21 39 41
one child already cut

26 46 30 52

35 88 72
43
Fibonacci heap: decrease key

Case 1. [heap order not violated]


・Decrease key of x.
・Change heap min pointer (if necessary).

decrease-key of x from 46 to 29
min

7 18 38

24 17 23 21 39 41

26 46 30 52

35 88 72
44
Fibonacci heap: decrease key

Case 1. [heap order not violated]


・Decrease key of x.
・Change heap min pointer (if necessary).

decrease-key of x from 46 to 29
min

7 18 38

24 17 23 21 39 41

26 29 30 52

35 88 72
45
Fibonacci heap: decrease key

Case 2a. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 29 to 15
min

7 18 38

24 17 23 21 39 41
p

26 29 30 52

35 88 72
46
Fibonacci heap: decrease key

Case 2a. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 29 to 15
min

7 18 38

24 17 23 21 39 41
p

26 15 30 52

35 88 72
47
Fibonacci heap: decrease key

Case 2a. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 29 to 15
min

7 18 38

24 17 23 21 39 41
p

26 15 30 52

35 88 72
48
Fibonacci heap: decrease key

Case 2a. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 29 to 15
min

x 15 7 18 38

72 24 17 23 21 39 41
p

26 30 52

35 88
49
Fibonacci heap: decrease key

Case 2a. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 29 to 15
min

x 15 7 18 38

72 24 17 23 21 39 41
p

26 30 52

35 88
50
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5
min

15 7 18 38

72 24
24 17 23 21 39 41

p 26 30 52

x 35 88
51
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5
min

15 7 18 38

72 24
24 17 23 21 39 41

p 26 30 52

x 5 88
52
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5

x min
15 5 7 18 38

72 24
24 17 23 21 39 41

p 26 30 52

88
53
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5

x min
15 5 7 18 38

72 24
24 17 23 21 39 41
second child cut

p 26 30 52

88
54
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5

x min p
15 5 26 7 18 38

72 88 24
24 17 23 21 39 41

30 52

55
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5

x min p
15 5 26 7 18 38

72 88 p′ 24
24 17 23 21 39 41

second child cut 30 52

56
Fibonacci heap: decrease key

Case 2b. [heap order violated]


・Decrease key of x.
・Cut tree rooted at x, meld into root list, and unmark.
・If parent p of x is unmarked (hasn’t yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).

decrease-key of x from 35 to 5

x min p p′ p″
15 5 26 24 7 18 38

but don’t
72 88 17 23 21 39 41
mark parent
if it’s a root

30 52

57
Fibonacci heap: decrease key analysis

Actual cost. ci = O(c), where c is the number of cuts.


・O(1) time for changing the key.
・O(1) time for each of c cuts, plus melding into root list.
Change in potential. ∆Φ = O(1) – c.
・trees(H′) = trees(H) + c.
・marks(H′) ≤ marks(H) – c + 2. each cut (except first) unmarks a node

・ΔΦ ≤ c + 2 ⋅ (-c + 2) = 4 – c. last cut may or may not mark a node

Amortized cost. ĉi = ci + ∆Φ = O(1).

Φ(H) = trees(H) + 2 ⋅ marks(H)

58
F IBONACCI H EAPS

‣ preliminaries
‣ insert
‣ extract the minimum
‣ decrease key
‣ bounding the rank
‣ meld and delete

SECTION 19.2, 19.3


Fibonacci heap: meld

Meld. Combine two Fibonacci heaps (destroying old heaps).

Recall. Root lists are circular, doubly-linked lists.

min min

23 24 17 7 3 21

30 26 46
18 52 41

heap H1 heap H2
35
39 44
70
Fibonacci heap: meld

Meld. Combine two Fibonacci heaps (destroying old heaps).

Recall. Root lists are circular, doubly-linked lists.

min

23 24 17 7 3 21

30 26 46
18 52 41

35 heap H
39 44
71
Fibonacci heap: meld analysis

Actual cost. ci = O(1).


Change in potential. ∆Φ = 0.
Amortized cost. ĉi = ci + ∆Φ = O(1).

Φ(H) = trees(H) + 2 ⋅ marks(H)

min

23 24 17 7 3 21

30 26 46
18 52 41

35 heap H
39 44
72
Fibonacci heap: delete

Delete. Given a handle to an element x, delete it from heap H.


・DECREASE-KEY(H, x, -∞).
・EXTRACT-MIN(H).
Amortized cost. ĉi = O(rank(H)).
・O(1) amortized for DECREASE-KEY.
・O(rank(H)) amortized for EXTRACT-MIN.

Φ(H) = trees(H) + 2 ⋅ marks(H)

73
Priority queues performance cost summary

operation linked list binary heap binomial heap Fibonacci heap †

MAKE-HEAP O(1) O(1) O(1) O(1)

IS-EMPTY O(1) O(1) O(1) O(1)

INSERT O(1) O(log n) O(log n) O(1)

EXTRACT-MIN O(n) O(log n) O(log n) O(log n)

DECREASE-KEY O(1) O(log n) O(log n) O(1)

DELETE O(1) O(log n) O(log n) O(log n)

MELD O(1) O(n) O(log n) O(1)

FIND-MIN O(n) O(1) O(log n) O(1)

† amortized

Accomplished. O(1) INSERT and DECREASE-KEY, O(log n) EXTRACT-MIN.

74
P RIORITY Q UEUES

‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
‣ advanced topics
Heaps of heaps

・b-heaps.
・Fat heaps.
・2–3 heaps.
・Leaf heaps.
・Thin heaps.
・Skew heaps.
・Splay heaps.
・Weak heaps.
・Leftist heaps.
・Quake heaps.
・Pairing heaps.
・Violation heaps.
・Run-relaxed heaps.
・Rank-pairing heaps.
・Skew-pairing heaps.
・Rank-relaxed heaps.
・Lazy Fibonacci heaps. 76

You might also like