Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick

Similar presentations


Presentation on theme: "Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick"— Presentation transcript:

1 Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
December 2013

2 Heaps / Priority queues
Fibonacci Heaps Lazy Binomial Heaps Binomial Heaps Binary Heaps O(1) O(log n) Insert Find-min Delete-min Decrease-key Meld Worst case Amortized Delete can be implemented using Decrease-key + Delete-min Decrease-key in O(1) time important for Dijkstra and Prim

3 Binomial Heaps [Vuillemin (1978)]

4 Binomial Trees B0 B1 B2 B3 B4

5 Binomial Trees B0 B1 B2 B3 B4

6 Binomial Trees B0 B1 B2 B3 Bk−1 Bk−2 Bk Bk Bk−1

7 Binomial Trees Bk−1 Bk−2 Bk Bk Bk−1 B0

8 Min-heap Ordered Binomial Trees
2 15 5 17 25 40 20 35 18 11 38 45 58 31 45 67 key of child  key of parent

9 Tournaments  Binomial Trees
D G H F E A B D C D F A D F G A B C D E F G H The children of x are the items that lost matches with x, in the order in which the matches took place.

10 Binomial Heap A list of binomial trees, at most one of each rank
Pointer to root with minimal key 23 9 33 45 67 40 58 20 31 15 35 Each number n can be written in a unique way as a sum of powers of 2 At most log2 (n+1) trees 11 = (1011)2 = 8+2+1

11 Ordered forest  Binary tree
23 9 15 x 33 40 20 35 info key child next 45 58 31 67 child – leftmost child 2 pointers per node next – next “sibling”

12 Heap order  “half ordered”
Forest  Binary tree 40 45 20 67 58 31 35 9 15 33 23 23 9 33 45 67 40 58 20 31 15 35 Heap order  “half ordered”

13 Binomial heap representation
Q 2 pointers per node n first No explicit rank information min How do we determine ranks? x 23 9 15 33 40 20 35 info key child next 45 58 31 “Structure V” [Brown (1978)] 67

14 Alternative representation
Q Reverse sibling pointers n first Make lists circular min Avoids reversals during meld 23 9 15 33 40 20 35 info key child next 45 58 31 “Structure R” [Brown (1978)] 67

15 Linking binomial trees
a ≤ b x Bk a Bk−1 y b Bk−1 O(1) time

16 Linking binomial trees
Linking in first representation Linking in second representation

17 Melding binomial heaps
Link trees of same degree Q1: Q2:

18 Melding binomial heaps
Link trees of same degree B1 B2 B3 Q1: B0 B1 B3 Q2: B0 B2 B3  B3 B4 Like adding binary numbers Maintain a pointer to the minimum O(log n) time

19 Insert O(log n) time New item is a one tree binomial heap
11 23 15 33 9 40 20 35 45 58 31 67 New item is a one tree binomial heap Meld it to the original heap O(log n) time

20 When we delete the minimum, we get a binomial heap
Delete-min 23 15 33 9 40 20 35 When we delete the minimum, we get a binomial heap 45 58 31 67

21 Delete-min O(log n) time
23 15 33 40 20 35 When we delete the minimum, we get a binomial heap 45 58 31 67 Meld it to the original heap (Need to reverse list of roots in first representation) O(log n) time

22 Decrease-key using “sift-up”
2 I 15 5 17 25 40 20 35 18 11 38 45 58 31 45 67 Decrease-key(Q,I,7)

23 Decrease-key using “sift-up”
2 I 15 5 17 25 40 20 35 18 11 38 45 58 7 45 Need parent pointers (not needed before) 67

24 Decrease-key using “sift-up”
2 I 15 5 17 25 40 7 35 18 11 38 45 58 20 45 67 Need to update the node pointed by I

25 Decrease-key using “sift-up”
2 I 15 5 17 25 40 7 35 18 11 38 45 58 20 45 67 Need to update the node pointed by I

26 Decrease-key using “sift-up”
2 I 7 5 17 25 40 15 35 18 11 38 45 58 20 45 67 How can we do it?

27 Adding parent pointers
Q Adding parent pointers n first min 23 9 15 33 40 20 35 45 58 31 67

28 Adding a level of indirection
Q Adding a level of indirection n “Endogenous vs. exogenous” first min 23 9 15 item child next x parent node info key I 33 40 20 35 45 58 31 Nodes and items are distinct entities 67

29 Heaps / Priority queues
Fibonacci Heaps Lazy Binomial Heaps Binomial Heaps Binary Heaps O(1) O(log n) Insert Find-min Delete-min Decrease-key Meld Worst case Amortized

30 Lazy Binomial Heaps

31 Binomial Heaps Lazy Binomial Heaps
A list of binomial trees, at most one of each rank, sorted by rank (at most O(log n) trees) Pointer to root with minimal key Lazy Binomial Heaps An arbitrary list of binomial trees (possibly n trees of size 1) Pointer to root with minimal key

32 Lazy Binomial Heaps An arbitrary list of binomial trees
Pointer to root with minimal key 10 29 9 33 59 45 67 40 58 20 31 15 35 87 19 20

33 Lazy Meld Lazy Insert Concatenate the two lists of trees
Update the pointer to root with minimal key O(1) worst case time Lazy Insert Add the new item to the list of roots Update the pointer to root with minimal key O(1) worst case time

34 Lazy Delete-min ? Remove the minimum root and meld ?
10 29 15 33 59 9 19 20 40 20 35 45 58 31 67 May need (n) time to find the new minimum

35 Consolidating / Successive Linking
10 29 15 59 40 20 35 19 33 45 58 31 20 67 1 2 3

36 Consolidating / Successive Linking
29 15 59 40 20 35 19 33 45 58 31 20 67 10 1 2 3

37 Consolidating / Successive Linking
15 59 40 20 35 19 33 45 58 31 20 67 10 29 1 2 3

38 Consolidating / Successive Linking
59 40 20 35 19 45 58 31 20 67 10 15 29 33 1 2 3

39 Consolidating / Successive Linking
40 20 35 19 45 58 31 20 67 10 15 29 59 33 1 2 3

40 Consolidating / Successive Linking
20 35 19 31 20 45 67 40 58 15 33 10 29 59 1 2 3

41 Consolidating / Successive Linking
35 19 20 45 67 40 58 15 33 10 29 20 59 31 1 2 3

42 Consolidating / Successive Linking
19 20 35 45 67 40 58 15 33 10 29 59 20 31 1 2 3

43 Consolidating / Successive Linking
19 20 45 67 40 58 15 33 10 29 20 35 31 59 1 2 3

44 Consolidating / Successive Linking
At the end of the process, we obtain a non-lazy binomial heap containing at most log (n+1) trees, at most one of each rank 45 67 40 58 15 33 10 29 20 35 31 19 20 59 1 2 3

45 Consolidating / Successive Linking
At the end of the process, we obtain a non-lazy binomial heap containing at most log n trees, at most one of each degree Worst case cost – O(n) Amortized cost – O(log n) Potential = Number of Trees

46 Cost of Consolidating T0 – Number of trees before
T1 – Number of trees after L – Number of links T1 = T0−L (Each link reduces the number of tree by 1) Total number of trees processed – T0+L (Each link creates a new tree) Putting trees into buckets or finding trees to link with Linking Handling the buckets Total cost = O( (T0+L) + L + log2n ) = O( T0+ log2n ) As L ≤ T0

47 Amortized Cost of Consolidating
(Scaled) actual cost = T0 + log2n Change in potential =  = T1−T0 Amortized cost = (T0 + log2n ) + (T1−T0)  = T1 + log2n  ≤ log2n As T1 ≤ log2n Another view: A link decreases the potential by 1. This can pay for handling all the trees involved in the link. The only “unaccounted” trees are those that were not the input nor the output of a link operation.

48 Lazy Binomial Heaps O(1) 1 Insert Find-min O(log n) Delete-min
Amortized cost Change in potential Actual cost O(1) 1 Insert Find-min O(log n) k1+T1−T0 O(k+T0+log n) Delete-min Decrease-key Meld Rank of deleted root

49 Heaps / Priority queues
Fibonacci Heaps Lazy Binomial Heaps Binomial Heaps Binary Heaps O(1) O(log n) Insert Find-min Delete-min Decrease-key Meld Worst case Amortized

50 One-pass successive linking
A tree produced by a link is immediately put in the output list and not linked again Worst case cost – O(n) Amortized cost – O(log n) Potential = Number of Trees Exercise: Prove it!

51 One-pass successive Linking
10 29 15 59 40 20 35 19 33 45 58 31 20 67 1 2 3

52 One-pass successive Linking
29 15 59 40 20 35 19 33 45 58 31 20 67 10 1 2 3

53 One-pass successive Linking
15 59 40 20 35 19 33 45 58 31 20 67 1 2 3 10 Output list: 29

54 Fibonacci Heaps [Fredman-Tarjan (1987)]

55 Decrease-key in O(1) time?
2 x 15 5 17 25 40 20 35 18 11 38 45 58 31 45 Decrease-key(Q,x,30) 67

56 Decrease-key in O(1) time?
2 x 15 5 17 25 30 20 35 18 11 38 45 58 31 45 Decrease-key(Q,x,10) 67

57 Decrease-key in O(1) time?
2 x 15 5 17 25 10 20 35 18 11 38 45 58 31 45 67 Heap order violation

58 Decrease-key in O(1) time?
2 x 15 5 17 25 10 20 35 18 11 38 45 58 31 45 67 Cut the edge

59 Decrease-key in O(1) time?
2 x 15 5 17 25 10 20 35 18 11 38 45 58 31 45 67 Tree no longer binomial

60 Fibonacci Heaps A list of heap-ordered trees
Pointer to root with minimal key 10 29 9 59 87 15 19 25 22 33 40 20 35 58 31 Not all trees may appear in Fibonacci heaps

61 Fibonacci heap representation
Q min 1 3 23 9 15 2 33 40 20 35 Explicit ranks = degrees Doubly linked lists of children 1 45 58 Arbitrary order of children child points to an arbitrary child 67 4 pointers + rank + mark bit per node

62 Fibonacci heap representation
Q min 1 x info rank child next prev parent mark key 3 23 9 15 2 33 40 20 35 1 45 58 67 4 pointers + rank + mark bit per node

63 Are simple cuts enough? A binomial tree of rank k contains at least 2k
We may get trees of rank k containing only k+1 nodes Ranks not necessarily O(log n) Analysis breaks down

64 Cascading cuts Invariant: Each node looses at most one child after becoming a child itself To maintain the invariant, use a mark bit Each node is initially unmarked. When a non-root node looses its first child, it becomes marked When a marked node looses a second child, it is cut from its parent

65 Our convention: Roots are unmarked
Cascading cuts Invariant: Each node looses at most one child after becoming a child itself When xy is cut: x becomes unmarked If y is unmarked, it becomes marked If y is marked, it is cut from its parent Our convention: Roots are unmarked

66 Perform a cascading-cut process starting at x
Cascading cuts Perform a cascading-cut process starting at x Cut x from its parent y

67 Cascading cuts

68 Cascading cuts

69 Cascading cuts

70 Cascading cuts

71 Cascading cuts

72 Cascading cuts

73 Cascading cuts

74 Cascading cuts

75 Cascading cuts

76 Number of cuts Potential = Number of marked nodes
A decrease-key operation may trigger many cuts Lemma 1: The first d decrease-key operations trigger at most 2d cuts Proof in a nutshell: Number of times a second child is lost is at most the number of times a first child is lost Potential = Number of marked nodes

77 Number of cuts Potential = Number of marked nodes c cuts
Amortized number of cuts ≤ c + (1(c1)) = 2

78 Trees formed by cascading cuts
Lemma 2: Let x be a node of rank k and let y1,y2,…,yk be the current children of x, in the order in which they were linked to x. Then, the rank of yi is at least i2. Proof: When yi was linked to x, y1,…yi1 were already children of x. At that time, the rank of x and yi was at least i1. As yi is still a child of x, it lost at most one child.

79 Trees formed by cascading cuts
Lemma 3: A node of rank k in a Fibonacci Heap has at least Fk+2 ≥ k descendants, including itself. n 1 2 3 4 5 6 7 8 9 Fn 13 21 34

80 Trees formed by cascading cuts
Lemma 3: A node of rank k in a Fibonacci Heap has at least Fk+2 ≥ k descendants, including itself. Let Sk be the minimum number of descendants of a node of rank at least k k k3 k2 1

81 Trees formed by cascading cuts
Lemma 3: A node of rank k in a Fibonacci Heap has at least Fk+2 ≥ k descendants, including itself. Corollary: In a Fibonacci heap containing n items, all ranks are at most logn ≤ log2n Ranks are again O(log n) Are we done?

82 Putting it all together
Are we done? A cut increases the number of trees… We need a potential function that gives good amortized bounds on both successive linking and cascading cuts Potential = #trees + 2 #marked

83 Cost of Consolidating T0 – Number of trees before
T1 – Number of trees after L – Number of links T1 = T0−L (Each link reduces the number of tree by 1) Total number of trees processed – T0+L (Each link creates a new tree) Putting trees into buckets or finding trees to link with Linking Handling the buckets Total cost = O( (T0+L) + L + logn ) = O( T0+ logn ) As L ≤ T0

84 Cost of Consolidating T0 – Number of trees before
T1 – Number of trees after L – Number of links T1 = T0−L (Each link reduces the number of tree by 1) Total number of trees processed – T0+L (Each link creates a new tree) Only change: logn instead of log2n Total cost = O( (T0+L) + L + logn ) = O( T0+ logn ) As L ≤ T0

85 Number of cuts performed
Fibonacci heaps Amortized cost  Marks  Trees Actual cost O(1) 1 Insert Find-min O(log n) ≤ 0 k1+T1−T0 O(k+T0+log n) Delete-min ≤ 2c c O(c) Decrease-key Meld Rank of deleted root Number of cuts performed

86 Heaps / Priority queues
Fibonacci Heaps Lazy Binomial Heaps Binomial Heaps Binary Heaps O(1) O(log n) Insert Find-min Delete-min Decrease-key Meld Worst case Amortized

87 Consolidating / Successive linking


Download ppt "Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick"

Similar presentations


Ads by Google