fibonacci heaps

49
Fibonacci Heaps 1 Created by: Naseeba P P

Upload: naseeba-p-p

Post on 14-Apr-2017

687 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Fibonacci Heaps

1

Fibonacci Heaps

Created by:Naseeba P P

Page 2: Fibonacci Heaps

2

What is a Fibonacci Heap?• Heap ordered Trees.• Rooted, but unordered.• Children of a node are linked together in a Circular,

doubly linked List.• Collection of unordered Binomial Trees.• Support Mergeable heap operations such as Insert,

Minimum, Extract Min, and Union in constant time O(1).• Desirable when the number of Extract Min and Delete

operations are small relative to the number of other operations.

Page 3: Fibonacci Heaps

3

Cont..

Node Pointers - left [x] - right [x] - degree [x] - number of children in the child list of x - - mark [x]

Page 4: Fibonacci Heaps

4

Fibonacci Heap Operations

• Insertion• Linking operation• Extract Minimum Node• Decrease key• Delete

Page 5: Fibonacci Heaps

5

Insert

• Create a new singleton tree.

Page 6: Fibonacci Heaps

6

Cont..

• Add to root list; update min pointer (if necessary).• Increment the total number of nodes in the Heap,

n[H].

Page 7: Fibonacci Heaps

7

Insert: Algorithm

Page 8: Fibonacci Heaps

8

Insert Analysis

• Actual cost. O(1)• Change in potential. +1• Amortized cost. O(1)• Potential of heap H

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

Page 9: Fibonacci Heaps

9

Union

• Combine two Fibonacci heaps

Page 10: Fibonacci Heaps

10

Cont..

Page 11: Fibonacci Heaps

11

Union: Algorithm

Page 12: Fibonacci Heaps

12

Union Analysis

• Potential function

•Actual cost. O(1) • Change in potential. 0• Amortized cost. O(1)

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

Page 13: Fibonacci Heaps

13

Delete or Extract Min

• Delete minimum

Page 14: Fibonacci Heaps

14

Cont..

• Meld its children into root list.• Update minimum.

Page 15: Fibonacci Heaps

15

Cont..

• Consolidate trees so that no two roots have same rank.

Page 16: Fibonacci Heaps

16

Cont..

current

Page 17: Fibonacci Heaps

17

Cont..

current

Page 18: Fibonacci Heaps

18

Cont..

current

Page 19: Fibonacci Heaps

19

Cont..

current

Page 20: Fibonacci Heaps

20

Cont..

current

Link 23 into 17

Page 21: Fibonacci Heaps

21

Cont..

Link 17 into 7

Page 22: Fibonacci Heaps

22

Cont..

Link 24 into 7

Page 23: Fibonacci Heaps

23

Cont..

current

Page 24: Fibonacci Heaps

24

Cont..

current

Page 25: Fibonacci Heaps

25

Cont..

current

Page 26: Fibonacci Heaps

26

Cont..

current

Link 41 into 18

Page 27: Fibonacci Heaps

27

Cont..

Page 28: Fibonacci Heaps

28

Cont..

Page 29: Fibonacci Heaps

29

Cont..

Stop

Page 30: Fibonacci Heaps

30

Delete Min: Algorithm

Page 31: Fibonacci Heaps

31

Cont..

Page 32: Fibonacci Heaps

32

Cont..

Page 33: Fibonacci Heaps

33

Delete Min AnalysisDelete min.

Actual cost. O(rank(H)) + O(trees(H)) O(rank(H)) to meld min's children into root list.O(rank(H)) + O(trees(H)) to update min.O(rank(H)) + O(trees(H)) to consolidate trees.

Change in potential. O(rank(H)) - trees(H)trees(H' ) rank(H) + 1 since no two trees have same rank.(H) rank(H) + 1 - trees(H).

Amortized cost. O(rank(H))

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

Potential function

Page 34: Fibonacci Heaps

34

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

Page 35: Fibonacci Heaps

35

Cont..

Case 1. [heap order not violated]– Decrease key of x.– Change heap min pointer (if necessary).

Page 36: Fibonacci Heaps

36

Cont..Case 2a. [heap order violated]– Decrease key of x.

Page 37: Fibonacci Heaps

37

Cont..

Cut tree rooted at x, meld into root list, and unmark.

Page 38: Fibonacci Heaps

38

Cont..

If parent p of x is unmarked (hasn't yet lost a child), mark it.

Page 39: Fibonacci Heaps

39

Cont..

Case 2b. [heap order violated]– Decrease key of x.

Page 40: Fibonacci Heaps

40

Cont..

Cut tree rooted at x, meld into root list, and unmark.

Page 41: Fibonacci Heaps

41

Cont..

If parent p of x is marked

Page 42: Fibonacci Heaps

42

Cont..

Cut p, meld into root list, and unmark

Page 43: Fibonacci Heaps

43

Cont..

Do so recursively for all ancestors that lose a second child.

Page 44: Fibonacci Heaps

44

Cont..

Do so recursively for all ancestors that lose a second child.

Page 45: Fibonacci Heaps

45

Decrease key: Algorithm

Page 46: Fibonacci Heaps

46

Cont..

Page 47: Fibonacci Heaps

47

Decrease Key AnalysisDecrease-key.

Actual cost. O(c)– 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.– c + 2 (-c + 2) = 4 - c.

Amortized cost. O(1)

(H)  = trees(H) + 2 marks(H)Potential function

Page 48: Fibonacci Heaps

48

Delete

Delete node x.– decrease-key of x to -.– delete-min element in heap.

Amortized cost. O(rank(H))– O(1) amortized for decrease-key.– O(rank(H)) amortized for delete-min.

Page 49: Fibonacci Heaps

49

THANK YOU