Fibonacci Heaps

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Fibonacci Heaps as PDF for free.

More details

  • Words: 3,031
  • Pages: 12
Priority Queues

Fibonacci Heaps Heaps

These lecture slides are adapted from CLRS, Chapter 20.

Binomial

Fibonacci †

Relaxed

1

1

1

1

log N

log N

1

1

1

log N

1

1

N

log N

log N

log N

log N

1

N

log N

1

1

decrease-key

1

log N

log N

1

1

delete

N

log N

log N

log N

log N

is-empty

1

1

1

1

1

Operation

Linked List

make-heap

1

insert

1

find-min

N

delete-min union

Binary

† amortized

this time Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne

2

Fibonacci Heaps

Fibonacci Heaps: Structure

Fibonacci heap history. Fredman and Tarjan (1986) ■





Fibonacci heap.

Ingenious data structure and analysis.



Set of min-heap ordered trees.

Original motivation: O(m + n log n) shortest path algorithm. – also led to faster algorithms for MST, weighted bipartite matching Still ahead of its time.

Fibonacci heap intuition. ■

Similar to binomial heaps, but less structured.



Decrease-key and union run in O(1) time.



"Lazy" unions.

min

17 30

24

26 35

3

23

46

7

marked

H

3

18

39

52

41 44 4

Fibonacci Heaps: Implementation

Fibonacci Heaps: Potential Function

Implementation. ■





Key quantities.

Represent trees using left-child, right sibling pointers and circular, doubly linked list. – can quickly splice off subtrees Roots of trees connected with circular doubly linked list. – fast union



Degree[x] = degree of node x.



Mark[x] = mark of node x (black or gray).



t(H) = # trees.



m(H) = # marked nodes.



Φ(H) = t(H) + 2m(H) = potential function.

Pointer to root of tree with min element. – fast find-min

t(H) = 5, m(H) = 3 Φ(H) = 11

min

17 30

24

26

23

7

46

18

35

17

3

H

52

30

41

26

23

7

min

3

46

18

35

44

39

24

degree = 3

H

52

41 44

39

5

6

Fibonacci Heaps: Insert

Fibonacci Heaps: Insert

Insert.

Insert.



Create a new singleton tree.



Create a new singleton tree.



Add to left of min pointer.



Add to left of min pointer.



Update min pointer.



Update min pointer.

Insert 21

Insert 21

21

min

17 30

24

26 35

23

46

7

17

3

18

H

min

39

52

30

41

24

26 35

44 7

23

46

7

3

21

18

H

39

52

41 44 8

Fibonacci Heaps: Insert

Fibonacci Heaps: Union

Insert.

Union.



Create a new singleton tree.



Concatenate two Fibonacci heaps.



Add to left of min pointer.



Root lists are circular, doubly linked lists.



Update min pointer.

Running time. O(1) amortized Insert 21



Actual cost = O(1).



Change in potential = +1.



Amortized cost = O(1).

17

24

min

min

23

7

23

3

21

24

min

17

7

3

21

H’ 30

26

46

35

18

H

52

H’’ 30

41

26

18

35

44

39

46

52

41 44

39

9

10

Fibonacci Heaps: Union

Fibonacci Heaps: Delete Min

Union.

Delete min.



Concatenate two Fibonacci heaps.



Delete min and concatenate its children into root list.



Root lists are circular, doubly linked lists.



Consolidate trees so that no two roots have same degree.

Running time. O(1) amortized ■

Actual cost = O(1).



Change in potential = 0.



Amortized cost = O(1).

min min 7

23

24

17

7

24

30

26

H’’ 26 35

46

18

39

52

17

3

21

3

H’ 30

23

41

35

46

18 39

52

41 44

44 11

12

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

current

1

2

3

current

min

min 7

30

24

26

23

17

18

52

39

46

41

7

44

30

35

24

26

23

17

18

52

39

46

41

44

35

13

14

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

0

1

2

3

current min

min 7

30

24

26

46

23

17

18

39

52

41

7

44

30

35

26

24

23

46

current

17

18

39

52

41

44

35

15

16

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

0

min

1

2

3

current

min 7

30

24

26

23

17

46

18

current

52

39

41

7

44

30

35

26

24

17

18

46

23

39

52

41

44

35

Merge 17 and 23 trees.

Merge 7 and 17 trees. 17

18

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

current

min 24

26

35

46

17

0

7

18

30

39

1

2

3

current

min 52

41

44

23

26

24

17

46

23

7

18

30

39

52

41

44

Merge 7 and 24 trees. 35 19

20

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

current

min

26

24

17

46

23

0

7

18

30

39

1

2

3

current

min 52

41

44

26

35

24

17

46

23

7

18

30

39

52

41

44

35 21

22

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

0

current

min

26

24

17

46

23

7

18

30

39

52

1

2

3

current

min 7

41

44

26

24

17

46

23

30

18

52

41

39

44

Merge 41 and 18 trees. 35

35 23

24

Fibonacci Heaps: Delete Min

Fibonacci Heaps: Delete Min

Delete min.

Delete min.



Delete min and concatenate its children into root list.



Delete min and concatenate its children into root list.



Consolidate trees so that no two roots have same degree.



Consolidate trees so that no two roots have same degree.

0

1

2

3

current

min 7

26

24

17

46

23

18

52

30

min

41

7

39

44

26

24

17

46

23

18

52

41

30

39

44

Stop. 35

35 25

26

Fibonacci Heaps: Delete Min Analysis

Fibonacci Heaps: Delete Min Analysis

Notation. ■

D(n) = max degree of any node in Fibonacci heap with n nodes.



t(H) = # trees in heap H.



Is amortized cost of O(D(n)) good?



Φ(H) = t(H) + 2m(H).

Actual cost. O(D(n) + t(H)) ■



O(D(n)) work adding min’s children into root list and updating min. – at most D(n) children of min node



O(D(n) + t(H)) work consolidating trees. – work is proportional to size of root list since number of roots decreases by one after each merging – ≤ D(n) + t(H) - 1 root nodes at beginning of consolidation

Yes, if only Insert, Delete-min, and Union operations supported. – in this case, Fibonacci heap contains only binomial trees since we only merge trees of equal root degree – this implies D(n) ≤ log2 N Yes, if we support Decrease-key in clever way. – we’ll show that D(n) ≤ logφ N, where φ is golden ratio – φ2 = 1 + φ – φ = (1 + √5) / 2 = 1.618… – limiting ratio between successive Fibonacci numbers!

Amortized cost. O(D(n)) ■



t(H’) ≤ D(n) + 1 since no two trees have same degree. ∆Φ(H) ≤ D(n) + 1 - t(H). 27

28

Fibonacci Heaps: Decrease Key

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. ■

Decrease key of element x to k.

Case 0: min-heap property not violated. – decrease key of x to k – change heap min pointer if necessary



Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer

min

min

7

35

24

17

26

46 45

30

88

72

23

21

18

38

39

41

7

52

Decrease 46 to 45.

35

24

17

26

45 15

30

88

72

23

21

18

38

39

41

52

Decrease 45 to 15.

29

30

Fibonacci Heaps: Decrease Key

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. ■

Decrease key of element x to k.

Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer



Case 1: parent of x is unmarked. – decrease key of x to k – cut off link between x and its parent – mark parent – add tree rooted at x to root list, updating heap min pointer

min

min

7

35

24

17

26

15

30

88

72

23

21

18

38

15

39

41

72

7

24

26

52

Decrease 45 to 15.

35 31

88

17

30

23

21

18

38

39

41

52

Decrease 45 to 15. 32

Fibonacci Heaps: Decrease Key

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. ■

Decrease key of element x to k.

Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list ! If p[p[x]] unmarked, then mark it. ! If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.



Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list ! If p[p[x]] unmarked, then mark it. ! If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

min 15

min

7

72

24

26

35 5

17

23

30

21

18

38

15

39

41

72

5

7

24

26

parent marked

52

Decrease 35 to 5.

88

17

23

30

21

18

38

39

41

52

Decrease 35 to 5.

88 33

34

Fibonacci Heaps: Decrease Key

Fibonacci Heaps: Decrease Key

Decrease key of element x to k. ■

Decrease key of element x to k.

Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list ! If p[p[x]] unmarked, then mark it. ! If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.



Case 2: parent of x is marked. – decrease key of x to k – cut off link between x and its parent p[x], and add x to root list – cut off link between p[x] and p[p[x]], add p[x] to root list ! If p[p[x]] unmarked, then mark it. ! If p[p[x]] marked, cut off p[p[x]], unmark, and repeat.

min 15

72

5

26

88

min

7

24

17

parent marked30

23

21

18

38

15

39

41

72

52

5

26

88

24

7

17

30

Decrease 35 to 5.

23

21

18

38

39

41

52

Decrease 35 to 5. 35

36

Fibonacci Heaps: Decrease Key Analysis

Fibonacci Heaps: Delete

Notation.

Delete node x.



t(H)



m(H) = # marked nodes in heap H.



= # trees in heap H.



Decrease key of x to -∞.



Delete min element in heap.

Φ(H) = t(H) + 2m(H). Amortized cost. O(D(n))

Actual cost. O(c)



O(1) for decrease-key.



O(1) time for decrease key.



O(D(n)) for delete-min.



O(1) time for each of c cascading cuts, plus reinserting in root list.



D(n) = max degree of any node in Fibonacci heap.

Amortized cost. O(1) ■





t(H’) = t(H) + c m(H’) ≤ m(H) - c + 2 – each cascading cut unmarks a node – last cascading cut could potentially mark a node ∆Φ ≤ c + 2(-c + 2) = 4 - c.

37

38

Fibonacci Heaps: Bounding Max Degree

Fibonacci Heaps: Bounding Max Degree

Definition. D(N) = max degree in Fibonacci heap with N nodes. Key lemma. D(N) ≤ logφ N, where φ = (1 + √5) / 2. Corollary. Delete and Delete-min take O(log N) amortized time.

Key lemma. In a Fibonacci heap with N nodes, the maximum degree of any node is at most logφ N, where φ = (1 + √5) / 2. Proof of key lemma.

Lemma. Let x be a node with degree k, and let y1, . . . , yk denote the children of x in the order in which they were linked to x. Then:



if i = 1 0 degree ( y i ) ≥  − 2 if i ≥ 1 i 



Proof. ■





When yi is linked to x, y1, . . . , yi-1 already linked to x, ⇒ degree(x) = i - 1 ⇒ degree(yi) = i - 1 since we only link nodes of equal degree



Since then, yi has lost at most one child – otherwise it would have been cut from x

For any node x, we show that size(x) ≥ φdegree(x) . – size(x) = # node in subtree rooted at x – taking base φ logs, degree(x) ≤ logφ (size(x)) ≤ logφ N. Let sk be min size of tree rooted at any degree k node. – trivial to see that s0 = 1, s1 = 2 sk = size ( x*) – sk monotonically increases with k k Let x* be a degree k node of size sk, and let y1, . . . , yk be children in order that they were linked to x*. Assume k ≥ 2

Thus, degree(yi) = i - 1 or i - 2

= 2 + ∑ size ( yi ) i =2 k

≥ 2 + ∑ sdeg[ y ] i =2 k

i

≥ 2 + ∑ si − 2 i =2 k −2

= 2 + ∑ si 39

i =0

40

Fibonacci Facts Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21, . . . • Slightly nonstandard definition.



Golden Ratio

1  Fk =  2  F +F k -2  k -1

if k = 0 if k = 1

Definition. The Fibonacci sequence is: 1, 2, 3, 5, 8, 13, 21, . . . Definition. The golden ratio φ = (1 + √5) / 2 = 1.618…

if k ≥ 2



Divide a rectangle into a square and smaller rectangle such that the smaller rectangle has the same ratio as original one.

Fact F1. Fk ≥ φk, where φ = (1 + √5) / 2 = 1.618…

Fact F2.

k −2

For k ≥ 2, Fk = 2 + ∑ Fi i =0

sk

= size ( x*) k

= 2 + ∑ size ( yi ) Consequence. sk ≥ Fk ≥ φk. ■

This implies that size(x) ≥ φdegree(x) for all nodes x.

i =2 k

≥ 2 + ∑ sdeg[ y ] i =2 k

i

≥ 2 + ∑ si − 2 i =2 k −2

= 2 + ∑ si i =0

Parthenon, Athens Greece

41

Fibonacci Facts

42

Fibonacci Numbers and Nature

Pinecone Cauliflower

43

44

Fibonacci Proofs φk.

Fact F1. Fk ≥ Proof. (by induction on k) ■



Fk + 2

Base cases: – F0 = 1, F1 = 2 ≥ φ. Inductive hypotheses: – Fk ≥ φk and Fk+1 ≥ φk+1

= ≥ = =

On Complicated Algorithms "Once you succeed in writing the programs for [these] complicated algorithms, they usually run extremely fast. The computer doesn’t need to understand the algorithm, its task is only to run the programs."

Fk + Fk +1 ϕ k + ϕ k +1 ϕ k (1 + ϕ ) ϕ k (ϕ 2 )

φ2 = φ + 1

= ϕ k+2 k −2

Fact F2. For k ≥ 2, Fk = 2 + ∑ Fi i =0 Proof. (by induction on k) ■



Fk + 2

Base cases: – F2 = 3, F3 = 5

= Fk + Fk +1 k −2

= 2 + ∑ Fi + Fk +1

Inductive hypotheses: k −2

Fk = 2 + ∑ Fi i =0

i =0 k

R. E. Tarjan

= 2 + ∑ Fk i =0

45

46

Related Documents

Fibonacci Heaps
July 2020 3
Fibonacci
November 2019 39
Fibonacci
May 2020 32
Fibonacci
October 2019 39
Fibonacci
May 2020 26
Fibonacci
December 2019 32