Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.

Similar presentations


Presentation on theme: "Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore."— Presentation transcript:

1 Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore

2 Priority Queue ADT  Priority Queue is an ADT for maintaining a set S of elements, each with a key value and supports the following operations:  I NSERT (S, x) inserts element x into S (also write as S  S  {x}  M INIMUM (S) returns element in S with min key  E XTRACT -M IN (S) removes and returns element in S with min key  D ECREASE -K EY (S, x, k) decreases the value of element x’s key to a new (smaller) value k.

3 PQ implementations… 1964Binary HeapJ. W. J. Williams 1972Leftist HeapC. A. Crane 1978Binomial HeapJ. Vuillemin 1984Fibonacci Heap M. L. Fredman, R. E. Tarjan 1985Skew Heap D. D. Sleator R. E. Tarjan 1988Relaxed Heap Driscoll, Gabow Shrairman, Tarjan  Many data structures proposed for PQ

4 Different Priority Queues Data StrI NSERT M IN Extract -M IN D-K EY D ELETE Union Binary HO(lg n)O(1)O(lg n) O(n) Binomial HO(lg n) FibonacciO(1) O(lg n)O(1)O(lg n)O(1)  Time Bounds for different PQ implementations  n is the number of items in the PQ

5

6 Binary Min-Heap (as in Heapsort)  Binary min-heap is an array A[1..n] that can be viewed as a nearly complete binary tree  Number the nodes using level order traversal  L EFT (i) = 2i and R IGHT (i) = 2i+1 and  P ARENT (i) =  i/2   Height of tree  lg n  Heap Property: (Each node ≥ its parent node)  A[P ARENT (i)]  A[i]

7 MinHeap – Two views 4 4 6 6 12 8 8 27 11 10 9 9 16 15 1 2 3 4 5 6 7 8 9 10 4 4 6 6 12 8 8 27 11 15 16 9 9 A[1..10] 123456789 10 Left (i) = 2i Right (i) = 2i + 1 Parent (i) =  i/2  Array index ::= level order traversal Heap Property: for all i A[Parent(i)]  A[i]

8 MinHeap – Insert operation (1) 4 4 6 6 10 12 8 8 27 11 15 16 9 9 A[1..10] 123456789 10 5 5 11 H EAP -I NSERT (A, key): insert at end of array S IFT -U P to restore HP Example: H EAP -I NSERT (A, 5) 4 4 6 6 12 8 8 27 11 10 9 9 16 15 1 2 3 4 5 6 7 8 9 10 5 5 Restore heap property with S IFT -U P

9 MinHeap – Insert operation (2) 4 4 6 6 12 5 5 27 11 10 8 8 9 9 16 15 1 2 3 4 5 6 7 8 9 10 4 4 6 6 12 5 5 27 11 15 16 9 9 A[1..10] 123456789 10 8 8 11 H EAP -I NSERT (A, key): insert at end of array S IFT -U P to restore HP Restore heap property with S IFT -U P Example: H EAP -I NSERT (A, 5)

10 MinHeap – Insert operation (3) 4 4 5 5 12 6 6 27 11 10 8 8 9 9 16 15 1 2 3 4 5 6 7 8 9 10 4 4 5 5 12 6 6 27 11 15 16 9 9 A[1..10] 123456789 10 H EAP -I NSERT (A, key): insert at end of array S IFT -U P to restore HP Restore heap property with S IFT -U P Example: H EAP -I NSERT (A, 5) 8 8 11

11 MinHeap – Insert operation (4) 4 4 5 5 12 6 6 27 11 10 8 8 9 9 16 15 1 2 3 4 5 6 7 8 9 10 4 4 5 5 12 6 6 27 11 15 16 9 9 A[1..10] 123456789 10 H EAP -I NSERT (A, key): insert at end of array S IFT -U P to restore HP Done! Example: H EAP -I NSERT (A, 5) 8 8 11

12 MinHeap – Insert operation (5) H EAP -I NSERT (A, key) ⊳ A is a heap (array) heap-size(A)  heap-size(A) + 1 A[heap-size(A)]  key S IFT -U P (A, heap-size(A)) S IFT -U P (A, n) key  A[n] i  n while i > 1 and A[P ARENT (i)] > key do A[i]  A[P ARENT (i)] i  P ARENT (i) A[i]  key T HEAP-INSERT (n) = T SIFT-UP (n) +  (1) = O(lg n) T SIFT-UP (n) = O(h) = O(lg n) (h comparisons, h data-moves)

13 A[1] is the minimum. H EAP -E XTRACT -M IN (A): H EAP -E XTRACT -M IN (A): Exchange A[1] and A[n] S IFT -D OWN to restore HP MinHeap – Extract-Min operation (1) 4 4 6 6 12 8 8 27 11 10 9 9 16 15 1 2 3 4 5 6 7 8 9 10 4 4 6 6 12 8 8 27 11 15 16 9 9 A[1..10] 123456789 10 A[1] is the minimum Exchange A[1] with A[n]

14 H EAP -E XTRACT -M IN (A): Exchange A[1] and A[n] S IFT -D OWN to restore HP MinHeap – Extract-Min operation (2) 9 9 6 6 12 8 8 27 11 10 4 4 16 15 1 2 3 4 5 6 7 8 9 10 9 9 6 6 12 8 8 27 11 15 16 4 4 A[1..10] 123456789 10 S IFT -D OWN to restore heap property return min 4 4

15 H EAP -E XTRACT -M IN (A): Exchange A[1] and A[n] S IFT -D OWN to restore HP MinHeap – Extract-Min operation (3) 6 6 9 9 12 8 8 27 11 10 16 15 1 2 3 4 5 6 7 8 9 6 6 9 9 10 12 8 8 27 11 15 16 A[1..10] 123456789 10 S IFT -D OWN to restore heap property

16 H EAP -E XTRACT -M IN (A): Exchange A[1] and A[n] S IFT -D OWN to restore HP MinHeap – Extract-Min operation (4) 6 6 8 8 12 9 9 27 11 10 16 15 1 2 3 4 5 6 7 8 9 6 6 8 8 10 12 9 9 27 11 15 16 A[1..10] 123456789 10 Done!

17 MinHeap – Extract-Min operation (5) H EAP -E XTRACT- M IN (A, key) ⊳ A is a heap (array) min  A [1] A[1]  A[heap-size(A)] heap-size(A)  heap-size(A)  1 S IFT -D OWN (A, 1) S IFT -D OWN (A, i) while i is not a leaf do let j be the index of min child of i if A[i] > A[j] then exchange A[i]  A[j] i  j else i  heap-size(A)+1 ⊳ make i a leaf T SIFT-DOWN (n) = O(h) = O(lg n) (2h comparisons, h data-moves) T HEAP-EXTRACT-MIN (n) = O(lg n)

18 MinHeap – Decrease-Key (1) 4 4 6 6 10 12 8 8 27 11 15 16 9 9 A[1..10] 123456789 10 H EAP -D ECREASE- K EY (A, i, key): update the key of A[i] S IFT -U P to restore HP H EAP -D ECREASE -K EY (A,9,3) 4 4 6 6 12 8 8 27 11 10 9 9 16 15 1 2 3 4 5 6 7 8 9 10

19 MinHeap – Decrease-Key (2) 4 4 6 6 10 12 8 8 27 11 15 3 3 9 9 A[1..10] 123456789 10 H EAP -D ECREASE- K EY (A, i, key): update the key of A[i] S IFT -U P to restore HP Restore heap property with S IFT -U P H EAP -D ECREASE -K EY (A,9,3) 4 4 6 6 12 8 8 27 11 10 9 9 3 3 15 1 2 3 4 5 6 7 8 9 10

20 MinHeap – Decrease-Key (3) 4 4 6 6 10 3 3 8 8 27 11 15 12 9 9 A[1..10] 123456789 10 H EAP -D ECREASE- K EY (A, i, key): update the key of A[i] S IFT -U P to restore HP H EAP -D ECREASE -K EY (A,9,3) 4 4 6 6 3 3 8 8 27 11 10 9 9 12 15 1 2 3 4 5 6 7 8 9 10

21 MinHeap – Decrease-Key (4) 4 4 3 3 10 6 6 8 8 27 11 15 12 9 9 A[1..10] 123456789 10 H EAP -D ECREASE- K EY (A, i, key): update the key of A[i] S IFT -U P to restore HP H EAP -D ECREASE -K EY (A,9,3) 4 4 3 3 6 6 8 8 27 11 10 9 9 12 15 1 2 3 4 5 6 7 8 9 10

22 MinHeap – Decrease-Key (5) 3 3 4 4 10 6 6 8 8 27 11 15 12 9 9 A[1..10] 123456789 10 H EAP -D ECREASE- K EY (A, i, key): update the key of A[i] S IFT -U P to restore HP DONE ! H EAP -D ECREASE -K EY (A,9,3) 3 3 4 4 6 6 8 8 27 11 10 9 9 12 15 1 2 3 4 5 6 7 8 9 10

23 MinHeap – Insert operation (6) H EAP -D ECREASE- K EY (A, i, key) ⊳ Update A[i] to key A[i]  key S IFT -U P (A, i) T DECREASE-KEY (n) = T SIFT-UP (n) +  (1) = O(lg n) (h comparisons, h data-moves)

24 Binary Heap (Summary)  H EAP -M INIMUM (A)O(1)  return root  H EAP -I NSERT (A, x) O(lg n)  append to end, sift-up  H EAP -E XTRACT -M IN (A)O(lg n)  Replace root, sift-down  H EAP -D ECREASE -K EY (A, i, k)O(lg n)  update key, sift-up  What about  H EAP -D ELETE (A, i)  H EAP -M ELD (A, B)

25 How about Union?  A mergeable heap is any data structure that supports the basic heap operation plus union  Union (H1, H2) creates and returns a new heap

26 Other Heaps

27 Binomial Trees B0B0 B1B1 B2B2 B3B3 B4B4 Some examples: Recursive definition: B0B0 Bk1Bk1 Bk1Bk1 BkBk B1B1 Bk1Bk1 BkBk B2B2 B0B0

28 Properties of Binomial Trees B0B0 B1B1 B2B2 B3B3 B4B4  For a Binomial Tree B k (of order k) 1. there are 2 k nodes, 2. the height of the tree is k, 3. root has degree k and 4. deleting the root gives binomial trees B 0, B 1, …, B k  1 Proof: (DIY, by induction) depth 0 1 2 3 4

29 Defining Property of Binomial Trees B4B4 depth 0 1 2 3 4 Nodes 1 4 6 4 1 There are exactly nodes at depth i, (0  i  k)

30 Binomial Heap (Vuillemin, 1978) B3B3 B0B0 B1B1 37 318 50 483117 44 2910 6 head[H]  A sequence of binomial trees that satisfy  binomial heap property (each tree B k is a min-heap)  0 or 1 binomial tree B k of order k,  There are at most  lg n  + 1 binomial trees.  Eg: A binomial heap H with n = 11 nodes. 11 = (1011) 2

31 Representing Binomial Heaps (1)  Each node x stores  key[x]  degree[x] p[x]p[x]  child[x]  sibling[x]  (3 pointers per node) 101 parent sibling child key degree

32 Representing Binomial Heap (2) 37 3 18 50 483117 44 2910 6 head[H] Each node x has p[x] child[x] sibling[x] degree[x], key[x]

33 The Binomial Heap: It’s actual implementation

34 Operations on a Binomial Heap  M AKE -B INOMIAL -H EAP (H)  Allocate object H, make head[H] = NIL.  (1).  B INOMIAL -H EAP -M INIMUM (H)  Search the root list for minimum. O(lg n). B3B3 B0B0 B1B1 37 318 50 483117 44 2910 6 head[H]

35 Linking Step: Fundamental Op  B INOMIAL -L INK (y, z) Bk1Bk1 y Bk1Bk1 z Bk1Bk1 y Bk1Bk1 z Assume z  y Linking step B INOMIAL -L INK (y, z) ⊳ Assume z  y p[y]  z sibling[y]  child[z] child[z]  y degree[z]  degree[z] + 1 Constant time O(1)

36 Binomial Heap Union 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 +

37 Binomial Heap Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 +

38 Binomial Heap Union + 12 18 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712

39 + 18 25 37 7 3 18 12 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712

40 + 18 25 37 7 3 41 283325 37 157 3 18 12 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712

41 + 18 25 37 7 3 41 283325 37 157 3 18 12 41 283325 37 157 3 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712

42 + 18 25 37 7 3 41 283325 37 157 3 18 12 41 283325 37 157 3 55 45 32 30 24 2322 50 483117 44 82910 6 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712

43 Binomial Heap Union  M AKE -B INOMIAL -H EAP -U NION (H 1, H 2 ) :  Create a heap H that is the union of two heaps H 1 and H 2  Analogous to binary addition of n 1 and n 2  Running time.: O(log n) [n = n 1 + n 2 ]  Prop to # of trees in root lists  2(  log 2 n  + 1). 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26

44 More Operations (1)  B INOMIAL -H EAP -I NSERT (H, x)  Create a one-item (x) binomial heap H 1 and then union H and H 1. O(lg n).  B INOMIAL -H EAP -E XTRACT -M IN (H)  Find minimum, remove root, then union. O(lg n). B3B3 B0B0 B1B1 37 318 50 483117 44 2910 6 head[H]

45 More Operations (2)  B INOMIAL -H EAP -D ECREASE (H, x, k)  B INOMIAL -H EAP -D ELETE (H, x) B3B3 B0B0 B1B1 37 318 50 483117 44 2910 6 head[H]

46 Binomial Heaps (Summary)  M INIMUM (H)O(lg n)  U NION (H 1, H 2 )O(lg n)  I NSERT (H, x)O(lg n)  E XTRACT -M IN (H)O(lg n)  D ECREASE- K EY (H, x, k)O(lg n)  D ELETE (H, x)O(lg n)

47 Binomial  Fibonacci Heaps  Key advantages of Binomial Heap  Elegant and fast – O(lg n)  But, it takes O(lg n) to “maintain” the elegant structure all the time.  Fibonacci Heaps  Want fast I NSERT, D ECREASE -K EY  Be lazy… u Do as little as possible, until there is no choice.  Relax the structure slightly

48 Fibonacci Heap (Fredman & Tarjan)  Modified from Binomial Heaps  With following relaxations…  Set of heap-ordered trees  Any number of trees of each rank  Trees are doubly-linked, but not ordered 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] marked nodes

49 Representing Fibonacci Heaps (1)  Each node x stores  key[x]  degree[x] (also rank(x))  mark[x] p[x]p[x]  child[x]  left[x]  right [x] (4 pointers per node) parent right child key, degree mark left 10 1 FALSE

50 Representing Fibonacci Heaps (2) 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] 1723 30 7 35 2646 24 39 41 1852 3 44 min[H]

51 Properties and Potential Function 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H]  Properties of F IB -H EAP H:  Mark(x) = TRUE (marked, black in my notes), FALSE (yellow in notes)  n[H] = number of nodes in H  t(H) = number of trees in H  m(H) = number of marked notes in H n[H] = 14 t(H) = 5 m(H) = 3 Potential Function for Amortized Analysis  (H) = t(H) + 2 m(H).

52 Operations on a Fibonacci Heap  M AKE -F IB -H EAP (H)  Allocate object H, make min[H] = NIL.  (1).  F IB -H EAP -M INIMUM (H)  min[H] points to minimum element. O(1). 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] n[H] = 14 t(H) = 5 m(H) = 3

53 Lazy Operation: I NSERT (1)  F IB -H EAP -I NSERT (H, x)  Be lazy: Create singleton tree with node x 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] 21 Eg: Insert 21 n[H] = 14 t(H) = 5 m(H) = 3

54 Lazy Operation: I NSERT (2)  F IB -H EAP -I NSERT (H, x)  Be lazy: Create singleton tree with node x  Concatenate x with root list of H  Update min[H], n[H]. 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] 21 Eg: Insert 21 n[H] = 14 t(H) = 5 m(H) = 3 n[H] = 15 t(H) = 6 m(H) = 3

55 Pseudocode for I NSERT F IB- H EAP -I NSERT (H, x) degree[x]  0 p[x]  NIL child[x]  NIL left[x]  x right[x]  x mark[x]  FALSE concatenate the root list containing x with root list H if min[H] = NIL or key[x] < key[min[H]] then min[H]  x n[H]  n[H] + 1 Worst case time = O(1) Amortized time = O(1) Amortized Time of I NSERT : c’(H) = c(H) +  (H’)   (H) = O(1) + 1 = O(1) O(1)

56 Lazy Operations: M ELD  F IB -H EAP -M ELD (H 1, H 2 )  Concatenate the two root lists  Update min[H], n[H] n[H 1 ] = 7 t(H 1 ) = 3 m(H 1 ) = 1 17 39 41 1852 3 44 min[H 2 ] H2H2 23 30 7 35 2646 24 H1H1 min[H 1 ] n[H 2 ] = 7 t(H 2 ) = 2 m(H 2 ) = 2

57 Lazy Operations: M ELD  F IB -H EAP -M ELD (H 1, H 2 )  Concatenate the two root lists  Update min[H], n[H] 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] n[H] = 14 t(H) = 5 m(H) = 3

58 What have we got now? With lazy I NSERT and M ELD, We have a doubly-linked list, with pointer to minimum… ( So, when do we need to do real work? )

59 Linking Step: Fundamental Op  F IB- H EAP -L INK (H, y, x) Linking step F IB- H EAP -L INK (H, y, x) ⊳ Assume x  y remove y from the root list of H make y a child of x, incrementing degree[x] mark[z]  FALSE Constant time O(1) Assume z  y yx y x

60 D ELETE -M IN Operation (1)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] n[H] = 14 t(H) = 5 m(H) = 3

61 D ELETE -M IN Operation (2)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Then what? Where is the new minimum? 1723 30 7 35 2646 24 H 39 41 1852 44 min[H] n[H] = 13 t(H) = 7 m(H) = 3 ?

62 0123 D ELETE -M IN Operation (3)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 1723 30 7 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 7 m(H) = 3 current min

63 0123 D ELETE -M IN Operation (4)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 1723 30 7 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 7 m(H) = 3 current min

64 0123 D ELETE -M IN Operation (5)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 1723 30 7 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 7 m(H) = 3 current min

65 0123 D ELETE -M IN Operation (5)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 1723 30 7 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 7 m(H) = 3 min Link 17 and 23 (degree 0) current

66 0123 D ELETE -M IN Operation (6)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 30 7 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 6 m(H) = 3 current min 17 23 Link 17 and 7 (degree 1)

67 0123 D ELETE -M IN Operation (7)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) 35 2646 24 H 39 41 1852 44 n[H] = 13 t(H) = 5 m(H) = 3 current min Link 7 and 24 (degree 2) 7 3017 23

68 0123 D ELETE -M IN Operation (8)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H 39 41 1852 44 n[H] = 13 t(H) = 4 m(H) = 3 current min 7 30 23 17 35 2646 24

69 0123 D ELETE -M IN Operation (9)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H 39 41 1852 44 n[H] = 13 t(H) = 4 m(H) = 3 current min 7 30 23 17 35 2646 24

70 0123 D ELETE -M IN Operation (10)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H 39 41 1852 44 n[H] = 13 t(H) = 4 m(H) = 3 current min 7 30 23 17 35 2646 24

71 0123 D ELETE -M IN Operation (11)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H 39 41 1852 44 n[H] = 13 t(H) = 4 m(H) = 3 current min 7 30 23 17 35 2646 24 Link 41 and 18 (degree 1)

72 0123 D ELETE -M IN Operation (12)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H 52 n[H] = 13 t(H) = 3 m(H) = 3 current min 7 30 23 17 35 2646 2439 41 18 44 DONE !

73 D ELETE -M IN Operation (13)  F IB -H EAP -D ELETE -M IN (H)  Delete the min and concatenate its children into root list  Consolidate trees (so that no two roots of same degree) H n[H] = 13 t(H) = 3 m(H) = 3 52 min[H] 7 30 23 17 35 2646 24 39 41 18 44 Final outcome

74 Analysis of DELETE-MIN  Notations:  Let H be the heap before D ELETE -M IN op;  t(H) is number of trees in H; D(n) is max degree in any n-node FH  k linking steps are done during consolidation  Steps in DELETE-MIN Cost  Remove min node x O(1)  Concat children of x to root list O(D(n))  Initialize ptr data structure A O(D(n))  Consolidation step…O(D(n)+k)  TOTAL: O(D(n)+k) # trees t(H)t(H) t(H)+D(n)  1 t(H)+D(n)  1  k Amortized Time: c’(H) = c(H) +  = O(D(n))  = D(n)  1  k Show later that D(n) = O(lg n)

75 What have we got now? (2) With Mergeable Heap Operations, i.e., I NSERT and M ELD, D ELETE -M IN can show that all the tree in H are actually Binomial Trees ! ( Now, how about D ECREASE -K EY ? )

76 D ECREASE -K EY Operation (1-1)  F IB -H EAP -D ECREASE -K EY (H, x, k) [1 st example: case 1]  Decrease key of x to k  Cut off x from its parent, 24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 45 35 min[H] n[H] = 16 t(H) = 3 m(H) = 3 Decrease 45 to 16

77 D ECREASE -K EY Operation (1-2)  F IB -H EAP -D ECREASE -K EY (H, x, k) [1 st example: case 1]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H] 24 46 17 30 23 7 88 26 21 52 39 18 41 38 72 16 35 min[H] n[H] = 16 t(H) = 3 m(H) = 3 Decrease 45 to 16

78 D ECREASE -K EY Operation (1-3)  F IB -H EAP -D ECREASE -K EY (H, x, k) [1 st example: case 1]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H]  If p[x] is not marked, mark p[x] n[H] = 16 t(H) = 4 m(H) = 3 46 72 16 Decrease 45 to 16 17 30 23 7 88 26 21 52 39 18 41 38 35 min[H] 24 Mark p[x] 24 Done ! n[H] = 16 t(H) = 4 m(H) = 4

79 D ECREASE -K EY Operation (2-1)  F IB -H EAP -D ECREASE -K EY (H, x, k) [2 nd example: case 2]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H] n[H] = 16 t(H) = 4 m(H) = 4 46 72 16 Decrease 35 to 4 17 30 23 7 88 26 21 52 39 18 41 38 35 min[H] 24

80 D ECREASE -K EY Operation (2-2)  F IB -H EAP -D ECREASE -K EY (H, x, k) [2 nd example: case 2]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H]  If p[x] is marked, then cut off p[x], unmark, insert… n[H] = 16 t(H) = 5 m(H) = 4 46 72 16 Decrease 35 to 4 4 min[H] 17 30 23 7 88 26 21 52 39 18 41 38 24 p[x] is marked

81 D ECREASE -K EY Operation (2-3)  F IB -H EAP -D ECREASE -K EY (H, x, k) [2 nd example: case 2]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H]  If p[x] is marked, then cut off p[x], unmark, insert… REPEAT n[H] = 16 t(H) = 6 m(H) = 3 46 72 16 Decrease 35 to 4 4 88 26 17 30 23 7 21 52 39 18 41 38 24 p[p[x]] is marked min[H]

82 D ECREASE -K EY Operation (2-4)  F IB -H EAP -D ECREASE -K EY (H, x, k) [2 nd example: case 2]  Decrease key of x to k  Cut off x from its parent, insert x into root list, update min[H]  If p[x] is marked, then cut off p[x], unmark, insert… REPEAT n[H] = 16 t(H) = 7 m(H) = 2 46 72 16 Decrease 35 to 4 4 88 26 17 30 23 7 21 52 39 18 41 38 24 Done ! min[H]

83 Analysis of D ECREASE -K EY  Notations:  Let H be the heap before D ECREASE -K EY op;  t(H) is number of trees in H; m(H) is # marked nodes in H  c cascading cuts are done in total  Steps in D ECREASE -K EY Cost  Decrease key of x O(1)  Cascading cuts (unmark) O(c)  Mark final parent node O(1)  TOTAL: O(c) Amortized Time: c’(H) = c(H) +  = O(1)  = c + 2(2  c) = (4  c ) Viola !! # trees t(H) t(H)+c # mark m(H) m(H)  c+1 m(H)  c+2

84 D ELETE Operation  F IB -H EAP -D ELETE (H, x)  D ECREASE -K EY (H, x,  )  D ELETE -M IN (H)  Amortized Time: O(D(n)) 1723 30 7 35 2646 24 H 39 41 1852 3 44 min[H] n[H] = 14 t(H) = 5 m(H) = 3

85 Bounding the Maximum Degree D(n) ::= maximum degree in any F IB -H EAP with n nodes Shall prove that D(n)  lg  n = O(lg n) where  = (1+  5)/2 = 1.61803 [golden ratio] and lg  n = 1.44 lg n

86 Bounding D(n) (1) Lemma 1: Let x be any node in FIB-HEAP with degree[x] = k. Let y 1, y 2,…, y n be children of x in the order in which they are linked to x (from earliest to latest). Then degree[y 1 ]  0 and degree[y i ]  i  2 for i=2, …,k. Proof: When y i was linked to x, degree[x] = i  1 since all of y 1, y 2,…, y i  1 were already children of x. Thus, degree[y i ] = degree[x] = i  1 at the time of linking. In addition, y i could lose at most 1 child, so degree[y i ]  i  2 x … y1y1 y2y2 yiyi … ykyk

87 Bounding D(n) (2) Define s k ::= minimum size of a tree of degree k in a FH. & let f k be such a minimal degree-k tree. To bound D(n), two possible approaches: 1. For a given size n, what is the maximum degree[x] ? 2. For a given degree k, what is the smallest size ? f1f1 f0f0 f2f2 f3f3 f4f4 f2f2 f1f1 f0f0 f0f0 x … y1y1 y2y2 yiyi … ykyk Fibonacci numbers

88 Bounding D(n) (3) Lemma: Let x be node in FH, and degree[x]=k. Then, size(x)  s k  F k+2   k. x … y1y1 y2y2 yiyi … ykyk Proof: First some small values: s 0 = 1, s 1 = 2, s 2 = 3 General Case: f1f1 f0f0 f2f2 Then show: F k+2 = 1 + (F 0 +F 1 +…+F k ) F k+2   k s k  F k+2 Corollary: D(n)  lg  n = 1.44 lg n = O(lg n)

89 Fibonacci Heaps (Summary)  M INIMUM (H)O(1)  U NION (H 1, H 2 )O(1)  I NSERT (H, x)O(1)  E XTRACT -M IN (H)O(lg n)*  D ECREASE- K EY (H, x, k)O(1)*  D ELETE (H, x)O(lg n)*


Download ppt "Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore."

Similar presentations


Ads by Google