Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fibonacci Heap 1. 2 3 Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.

Similar presentations


Presentation on theme: "Fibonacci Heap 1. 2 3 Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they."— Presentation transcript:

1 Fibonacci Heap 1

2 2

3 3

4 Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they have the same asymptotic running times for the remaining operations. Note, however, that the running times for Fibonacci heaps in Figure 19.1 are amortized time bounds, not worst-case per-operation time bounds. The UNION operation takes only constant amortized time in a Fibonacci heap, which is significantly better than the linear worst-case time required in a binary heap (assuming, of course, that an amortized time bound suffices). 4

5 5

6 Structure of Fibonacci heaps We access a given Fibonacci heap H by a pointer H:min to the root of a tree containing the minimum key; we call this node the minimum node of the Fibonacci heap. If more than one root has a key with the minimum value, then any such root may serve as the minimum node. When a Fibonacci heap H is empty, H:min is NIL. The roots of all the trees in a Fibonacci heap are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. The pointer H:min thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. We rely on one other attribute for a Fibonacci heap H: H:n, the number of nodes currently in H. 6

7 7

8 8 723 30 17 35 2646 24 Heap H 39 41 1852 3 44 Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. roots heap-ordered tree each parent larger than its children

9 9 723 30 17 35 2646 24 Heap H 39 41 1852 3 44 Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min find-min takes O(1) time

10 10 723 30 17 35 2646 24 Heap H 39 41 1852 3 44 Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min use to keep heaps flat (stay tuned) marked

11 11 Fibonacci Heaps: Notation Notation. n = number of nodes in heap. rank(x) =degree D(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. 723 30 17 35 2646 24 39 41 1852 3 44 rank = 3 min Heap H trees(H) = 5 marks(H) = 3 marked n = 14

12 12

13 13 Fibonacci Heaps: Potential Function 723 30 17 35 2646 24  (H) = 5 + 2  3 = 11 39 41 1852 3 44 min Heap H  (H) = trees(H) + 2  marks(H) potential of heap H trees(H) = 5 marks(H) = 3 marked

14 Mergeable-heap operations The mergeable-heap operations on Fibonacci heaps delay work as long as possible. The various operations have performance trade-offs. For example, we insert a node by adding it to the root list, which takes just constant time. If we were to start with an empty Fibonacci heap and then insert k nodes, the Fibonacci heap would consist of just a root list of k nodes. 14

15 15

16 16 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary). 723 30 17 35 2646 24 39 41 1852 3 44 21 insert 21 min Heap H

17 17 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary). 39 41 723 1852 3 30 17 35 2646 24 44 21 min Heap H insert 21

18 18 Fibonacci Heaps: Insert Analysis Actual cost. O(1) Change in potential. +1 Amortized cost. O(1) 39 41 7 1852 3 30 17 35 2646 24 44 21 23 min Heap H  (H) = trees(H) + 2  marks(H) potential of heap H

19 19 Union

20 20 Fibonacci Heaps: Union. 39 41 717 1852 3 30 23 35 2646 24 44 21 min Heap H'Heap H''

21 21

22 22 Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists. 39 41 717 1852 3 30 23 35 2646 24 44 21 min Heap H

23 23 Fibonacci Heaps: Union Actual cost. O(1) Change in potential. 0 Amortized cost. O(1)  (H) = trees(H) + 2  marks(H) potential function 39 41 717 1852 3 30 23 35 2646 24 44 21 min Heap H

24 24 Extracting the minimum node

25 25

26 26

27 27 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 411852 3 44 1723 30 7 35 2646 24 min

28 28 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 min

29 29 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 min current

30 30 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 0123 current min rank

31 31 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 0123 min current rank

32 32 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 0123 min current rank

33 33 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117231852 30 7 35 2646 24 44 0123 min current rank link 23 into 17

34 34 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 4117 23 1852 30 7 35 2646 24 44 0123 min current rank link 17 into 7

35 35 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 417 30 1852 17 35 2646 24 44 0123 23 current min rank link 24 into 7

36 36 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 417 30 1852 23 17 35 2646 2444 0123 min current rank

37 37 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 417 30 1852 23 17 35 2646 2444 0123 min current rank

38 38 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 417 30 1852 23 17 35 2646 2444 0123 min current rank

39 39 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 417 30 1852 23 17 35 2646 2444 0123 min current rank link 41 into 18

40 40 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 39 41 7 30 1852 23 17 35 2646 24 44 0123 min current rank

41 41 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 7 30 52 23 17 35 2646 24 0123 min rank 39 41 18 44 current

42 42 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank. 7 30 52 23 17 35 2646 24 min 39 41 18 44 stop

43 43 Fibonacci Heaps: Extract Delete 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. Amortized cost. O(rank(H))= O(D(H))  (H) = trees(H) + 2  marks(H) potential function

44 44 Decrease Key

45 45

46 46 Intuition for decreasing 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. n 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). 24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 Fibonacci Heaps: Decrease Key 35 min marked node: one child already cut

47 47 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary). 24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 Fibonacci Heaps: Decrease Key 29 35 min x decrease-key of x from 46 to 29

48 48 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary). 24 29 17 30 23 7 88 26 21 52 39 18 41 38 72 Fibonacci Heaps: Decrease Key 35 min x decrease-key of x from 46 to 29

49 49 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). 24 29 17 30 23 7 88 26 21 52 39 18 41 38 72 Fibonacci Heaps: Decrease Key 15 35 min decrease-key of x from 29 to 15 p x

50 50 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). 24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x

51 51 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). 2417 30 23 7 88 26 21 52 39 18 41 38 Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p 15 72 x

52 52 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). 2417 30 23 7 88 26 21 52 39 18 41 38 Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p 15 72 x mark parent 24

53 53 35 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). 24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 24 Fibonacci Heaps: Decrease Key 5 min x p decrease-key of x from 35 to 5

54 54 5 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). 24 15 17 30 23 7 88 26 21 52 39 18 41 38 72 24 Fibonacci Heaps: Decrease Key min x p decrease-key of x from 35 to 5

55 55 Fibonacci Heaps: Decrease Key 2417 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 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).

56 56 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). 2417 30 23 7 26 21 52 39 18 41 38 24 5 Fibonacci Heaps: Decrease Key 88 15 72 decrease-key of x from 35 to 5 x p second child cut min

57 57 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). 24 26 17 30 23 7 21 52 39 18 41 38 88 24 5 Fibonacci Heaps: Decrease Key 15 72 decrease-key of x from 35 to 5 xp min

58 58 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). 24 26 17 30 23 7 21 52 39 18 41 38 88 24 5 Fibonacci Heaps: Decrease Key 15 72 decrease-key of x from 35 to 5 xp p' second child cut min

59 59 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). 26 17 30 23 7 21 52 39 18 41 38 88 5 Fibonacci Heaps: Decrease Key 1524 72 decrease-key of x from 35 to 5 xpp' min don't mark parent if it's a root p''

60 60 Decrease-key. Actual cost. O(c) n 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. Amortized cost. O(1) Fibonacci Heaps: Decrease Key Analysis  (H) = trees(H) + 2  marks(H) potential function

61 61

62 62 Delete

63 63

64 64 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. Fibonacci Heaps: Delete  (H) = trees(H) + 2  marks(H) potential function


Download ppt "Fibonacci Heap 1. 2 3 Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they."

Similar presentations


Ads by Google