Fibonacci Heaps Lecture slides adapted from: Chapter 20 of - - PowerPoint PPT Presentation

fibonacci heaps
SMART_READER_LITE
LIVE PREVIEW

Fibonacci Heaps Lecture slides adapted from: Chapter 20 of - - PowerPoint PPT Presentation

Fibonacci Heaps Lecture slides adapted from: Chapter 20 of Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein. Chapter 9 of The Design and Analysis of Algorithms by Dexter Koze COS 423 Theory of Algorithms Kevin


slide-1
SLIDE 1

COS 423 Theory of Algorithms • Kevin Wayne • Spring 2007 Adapted by Cheng Li and Virgil Pavlu

Fibonacci Heaps

Lecture slides adapted from:

  • Chapter 20 of Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein.
  • Chapter 9 of The Design and Analysis of Algorithms by Dexter Koze
slide-2
SLIDE 2

Fibonacci Heaps

  • History. [Fredman and Tarjan, 1986]

 Ingenious data structure and analysis.  Original motivation: improve Dijkstra's shortest path algorithm

(module 12) from to Basic idea.

 Similar to binomial heaps, but less rigid structure.  Binomial heap: eagerly consolidate trees after each insert.  Fibonacci heap: lazily defer consolidation until next extract-min.

2

V insert, V extract-min, E decrease-key

slide-3
SLIDE 3

Fibonacci Heaps: Structure

Fibonacci heap.

 Set of heap-ordered trees.  Maintain pointer to minimum element.  Set of marked nodes.

3

7 23 30 17 35 26 46 24 Heap H 39 41 18 52 3 44

roots heap-ordered tree each parent smaller than its children

slide-4
SLIDE 4

Fibonacci Heaps: Structure

Fibonacci heap.

 Set of heap-ordered trees.  Maintain pointer to minimum element.  Set of marked nodes.

4

7 23 30 17 35 26 46 24 Heap H 39 41 18 52 3 44

min find-min takes O(1) time

slide-5
SLIDE 5

Fibonacci Heaps: Structure

Fibonacci heap.

 Set of heap-ordered trees.  Maintain pointer to minimum element.  Set of marked nodes.

5

7 23 30 17 35 26 46 24 Heap H 39 41 18 52 3 44

min use to keep heaps flat (stay tuned) marked

slide-6
SLIDE 6

Fibonacci Heaps: Notation

Notation.

 = number of nodes in heap.  degree(x) = number of children of node x.  = upper bound on the maximum degree of any node.

In fact, . The proof (omitted) uses Fibonacci numbers.

 = number of trees in heap H.  = number of marked nodes in heap H.

6

7 23 30 17 35 26 46 24 39 41 18 52 3 44 degree = 3

min

Heap H t(H) = 5 m(H) = 3

marked

n = 14

slide-7
SLIDE 7

Fibonacci Heaps: Potential Function

7

7 23 30 17 35 26 46 24 Φ(H) = 5 + 2⋅3 = 11 39 41 18 52 3 44

min

Heap H

potential of heap H :

t(H) = 5 m(H) = 3

marked This lecture is not a complete treatment of Fibonacci heaps; in order to implement (code) and use them, more details are necessary (see book). Our main purpose here is to understand how the potential function works. Next: analyze change in potential and amortized costs for heaps operations:

  • Insert (easy, required)
  • Extract min (medium, required)
  • Decrease Key (difficult, optional)
  • Union (easy, required)
  • Delete (medium, required)
slide-8
SLIDE 8

8

Insert

slide-9
SLIDE 9

Fibonacci Heaps: Insert

Insert.

 Create a new singleton tree.  Add to root list; update min pointer (if necessary).

9

7 23 30 17 35 26 46 24 39 41 18 52 3 44 21

insert 21

min

Heap H

slide-10
SLIDE 10

Fibonacci Heaps: Insert

Insert.

 Create a new singleton tree.  Add to root list; update min pointer (if necessary).

10

39 41 7 23 18 52 3 30 17 35 26 46 24 44 21

min

Heap H

insert 21

slide-11
SLIDE 11

Fibonacci Heaps: Insert Analysis

Actual cost.

 H’ = the heap after insert

Change in potential. Amortized cost.

11

39 41 7 18 52 3 30 17 35 26 46 24 44 21 23

min

Heap H

potential of heap H

slide-12
SLIDE 12

12

Extract-Min

slide-13
SLIDE 13

Linking Operation

Linking operation. Make larger root be a child of smaller root.

13

39 41 18 52 3 44 77 56 24 15 tree T1 tree T2 39 41 18 52 3 44 77 56 24 15 tree T'

smaller root larger root still heap-ordered

slide-14
SLIDE 14

Fibonacci Heaps: Extract-Min

Extract-min.

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

14

39 41 18 52 3 44 17 23 30 7 35 26 46 24

min

slide-15
SLIDE 15

Fibonacci Heaps: Extract-Min

Extract-Min.

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

15

39 41 17 23 18 52 30 7 35 26 46 24 44

min

slide-16
SLIDE 16

Fibonacci Heaps: Extract-Min

Extract-Min.

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

16

39 41 17 23 18 52 30 7 35 26 46 24 44

min

current

slide-17
SLIDE 17

Fibonacci Heaps: Extract-Min

Extract-Min.

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

17

39 41 17 23 18 52 30 7 35 26 46 24 44 1 2 3 current

min degree

slide-18
SLIDE 18

Fibonacci Heaps: Extract-Min

Extract-Min.

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

18

39 41 17 23 18 52 30 7 35 26 46 24 44 1 2 3

min

current

degree

slide-19
SLIDE 19

Fibonacci Heaps: Extract-Min

Extract-Min.

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

19

39 41 17 23 18 52 30 7 35 26 46 24 44 1 2 3

min

current

degree

slide-20
SLIDE 20

Fibonacci Heaps: Extract-Min

Extract-Min.

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

20

39 41 17 23 18 52 30 7 35 26 46 24 44 1 2 3

min

current

degree

link 23 into 17

slide-21
SLIDE 21

Fibonacci Heaps: Extract-Min

Extract-Min.

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

21

39 41 17 23 18 52 30 7 35 26 46 24 44 1 2 3

min

current

degree

link 17 into 7

slide-22
SLIDE 22

Fibonacci Heaps: Extract-Min

Extract-Min.

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

22

39 41 7 30 18 52 17 35 26 46 24 44 1 2 3 23 current

min degree

link 24 into 7

slide-23
SLIDE 23

Fibonacci Heaps: Extract-Min

Extract-Min.

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

23

39 41 7 30 18 52 23 17 35 26 46 24 44 1 2 3

min

current

degree

slide-24
SLIDE 24

Fibonacci Heaps: Extract-Min

Extract-Min.

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

24

39 41 7 30 18 52 23 17 35 26 46 24 44 1 2 3

min

current

degree

slide-25
SLIDE 25

Fibonacci Heaps: Extract-Min

Extract-Min.

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

25

39 41 7 30 18 52 23 17 35 26 46 24 44 1 2 3

min

current

degree

slide-26
SLIDE 26

Fibonacci Heaps: Extract-Min

Extract-Min.

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

26

39 41 7 30 18 52 23 17 35 26 46 24 44 1 2 3

min

current

degree

link 41 into 18

slide-27
SLIDE 27

Fibonacci Heaps: Extract-Min

Extract-Min.

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

27

39 41 7 30 18 52 23 17 35 26 46 24 44 1 2 3

min

current

degree

slide-28
SLIDE 28

Fibonacci Heaps: Extract-Min

Extract-Min.

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

28

7 30 52 23 17 35 26 46 24 1 2 3

min degree

39 41 18 44 current

slide-29
SLIDE 29

Fibonacci Heaps: Extract-Min

Extract-Min.

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

29

7 30 52 23 17 35 26 46 24

min

39 41 18 44

stop

slide-30
SLIDE 30

Fibonacci Heaps: Extract-Min Analysis

Extract-Min. Actual cost.

 to meld min's children into root list. (at most

children of min)

 to update min.(the size of the root list is at most

)

 to consolidate trees.(one of the roots is linked to

another in each merging, and thus the total number of iterations is at most the number of roots in the root list.) Change in potential:

 (at most roots with distinct

degrees remain and no nodes become marked during the

  • peration)

Amortized cost:

30

potential function

slide-31
SLIDE 31

31

Decrease Key

slide-32
SLIDE 32

Fibonacci Heaps: Decrease Key

Intuition for deceasing the key of node x.

 If heap-order is not violated, just decrease the key of x.  Otherwise, cut tree rooted at x and meld into root list.  To keep trees flat: as soon as a node has its second child cut,

cut it off and meld into root list (and unmark it).

32

24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 35

min marked node:

  • ne child already cut
slide-33
SLIDE 33

Fibonacci Heaps: Decrease Key

Case 1. [heap order not violated]

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

33

24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 29 35

min x

decrease-key of x from 46 to 29

slide-34
SLIDE 34

Fibonacci Heaps: Decrease Key

Case 1. [heap order not violated]

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

34

24 29 17 30 23 7 88 26 21 52 39 18 41 38 72 35

min x

decrease-key of x from 46 to 29

slide-35
SLIDE 35

Fibonacci Heaps: 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).

35

24 29 17 30 23 7 88 26 21 52 39 18 41 38 72 15 35

min

decrease-key of x from 29 to 15

p x

slide-36
SLIDE 36

Fibonacci Heaps: 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).

36

24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 35

min

decrease-key of x from 29 to 15

p x

slide-37
SLIDE 37

Fibonacci Heaps: 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).

37

24 17 30 23 7 88 26 21 52 39 18 41 38 35

min

decrease-key of x from 29 to 15

p

15 72

x

slide-38
SLIDE 38

Fibonacci Heaps: 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).

38

24 17 30 23 7 88 26 21 52 39 18 41 38 35

min

decrease-key of x from 29 to 15

p

15 72

x mark parent

24

slide-39
SLIDE 39

Fibonacci Heaps: 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).

39

35 24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 24 5

min x p

decrease-key of x from 35 to 5

slide-40
SLIDE 40

Fibonacci Heaps: 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).

40

5 24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 24

min x p

decrease-key of x from 35 to 5

slide-41
SLIDE 41

Fibonacci Heaps: 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).

41

24 17 30 23 7 26 21 52 39 18 41 38 24 5 88 15 72

decrease-key of x from 35 to 5

x p min

slide-42
SLIDE 42

Fibonacci Heaps: 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).

42

24 17 30 23 7 26 21 52 39 18 41 38 24 5 88 15 72

decrease-key of x from 35 to 5

x p second child cut min

slide-43
SLIDE 43

Fibonacci Heaps: 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).

43

24 26 17 30 23 7 21 52 39 18 41 38 88 24 5 15 72

decrease-key of x from 35 to 5

x p min

slide-44
SLIDE 44

Fibonacci Heaps: 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).

44

24 26 17 30 23 7 21 52 39 18 41 38 88 24 5 15 72

decrease-key of x from 35 to 5

x p p' second child cut min

slide-45
SLIDE 45

Fibonacci Heaps: 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).

45

26 17 30 23 7 21 52 39 18 41 38 88 5 15 24 72

decrease-key of x from 35 to 5

x p p' min don't mark parent if it's a root p''

slide-46
SLIDE 46

Fibonacci Heaps: Decrease Key Analysis

Decrease-key. Actual cost. , where c is the number of cascading cuts

 time for changing the key.  time for each of c cuts, plus melding into root list.

Change in potential.

 (the original trees and c trees produced by

cascading cuts)

 (c-1 nodes were unmarked by the first c-1

cascading cuts and the last cut may have marked a node)

 Difference in potential .

Amortized cost.

46

potential function

slide-47
SLIDE 47

47

Union

slide-48
SLIDE 48

Fibonacci Heaps: Union

  • Union. Combine two Fibonacci heaps.
  • Representation. Root lists are circular, doubly linked lists.

48

39 41 7 17 18 52 3 30 23 35 26 46 24 44 21

min min

Heap H' Heap H''

slide-49
SLIDE 49

Fibonacci Heaps: Union

  • Union. Combine two Fibonacci heaps.
  • Representation. Root lists are circular, doubly linked lists.

49

39 41 7 17 18 52 3 30 23 35 26 46 24 44 21

min

Heap H

slide-50
SLIDE 50

Fibonacci Heaps: Union

Actual cost: Change in potential: 0 ( and remain the same) Amortized cost:

50

potential function

39 41 7 17 18 52 3 30 23 35 26 46 24 44 21

min

Heap H

slide-51
SLIDE 51

51

Delete

slide-52
SLIDE 52

Fibonacci Heaps: Delete

Delete node x.

 decrease-key of x to -∞.  extract-min element in heap.

Amortized cost.

 amortized for decrease-key.  amortized for extract-min.

52

potential function

slide-53
SLIDE 53

Priority Queues Performance Cost Summary

53

make-heap Operation insert find-min extract-min union decrease-key delete 1 Binary Heap log n 1 log n n log n log n 1 Binomial Heap log n log n log n log n log n log n 1 Fibonacci Heap † 1 1 log n 1 1 log n 1 Relaxed Heap 1 1 log n 1 1 log n 1 Linked List 1 n n 1 n n is-empty 1 1 1 1 1

† amortized n = number of elements in priority queue