1 / 53

Fibonacci Heaps

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. Fibonacci Heaps. History. [Fredman and Tarjan, 1986]

ewendt
Download Presentation

Fibonacci Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Fibonacci Heaps History. [Fredman and Tarjan, 1986] • Ingenious data structure and analysis. • Original motivation: improve Dijkstra's shortest path algorithmfrom O(E log V) to O(E + V log V). 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. V insert, V extract-min, E decrease-key

  3. Fibonacci Heaps: Structure each parent smaller than its children Fibonacci heap. • Set of heap-ordered trees. • Maintain pointer to minimum element. • Set of marked nodes. roots heap-ordered tree 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39

  4. Fibonacci Heaps: Structure Fibonacci heap. • Set of heap-ordered trees. • Maintain pointer to minimum element. • Set of marked nodes. find-min takes O(1) time min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39

  5. Fibonacci Heaps: Structure Fibonacci heap. • Set of heap-ordered trees. • Maintain pointer to minimum element. • Set of marked nodes. use to keep heaps flat (stay tuned) min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39

  6. Fibonacci Heaps: Notation Notation. • n = number of nodes in heap. • degree(x) = number of children of node x. • D(n) = upper bound on the maximum degree of any node. In fact, D(n)= O(logn). The proof (omitted) uses Fibonacci numbers. • t(H) = number of trees in heap H. • m(H) = number of marked nodes in heap H. m(H) = 3 n = 14 t(H) = 5 degree = 3 min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39

  7. Fibonacci Heaps: Potential Function (H) = t(H) + 2m(H) potential of heap H m(H) = 3 t(H) = 5 min (H) = 5 + 23 = 11 17 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39

  8. Insert

  9. Fibonacci Heaps: Insert Insert. • Create a new singleton tree. • Add to root list; update min pointer (if necessary). insert 21 21 min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39

  10. Fibonacci Heaps: Insert Insert. • Create a new singleton tree. • Add to root list; update min pointer (if necessary). insert 21 min 17 24 23 7 3 21 30 26 46 18 52 41 35 Heap H 44 39

  11. Fibonacci Heaps: Insert Analysis • Actual cost. O(1) • Change in potential. • (t(H’)+2m(H’))-(t(H)+2m(H))= • (t(H)+1+2m(H))-(t(H)+2m(H))=1 • Amortized cost. O(1) (H) = t(H) + 2m(H) potential of heap H min 17 24 23 7 3 21 30 26 46 18 52 41 35 Heap H 44 39

  12. Extract-Min

  13. Linking Operation Linking operation. Make larger root be a child of smaller root. larger root still heap-ordered smaller root 3 3 15 15 18 52 41 18 52 41 56 24 39 44 39 44 56 24 77 tree T1 tree T2 77 tree T'

  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. min 7 24 23 17 3 30 26 46 18 52 41 35 39 44

  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. min 7 24 23 17 18 52 41 39 44 30 26 46 35

  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. min current 7 24 23 17 18 52 41 39 44 30 26 46 35

  17. 0 1 2 3 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. degree min current 7 24 23 17 18 52 41 39 44 30 26 46 35

  18. 0 1 2 3 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. degree min current 7 24 23 17 18 52 41 39 44 30 26 46 35

  19. 0 1 2 3 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. degree min 7 24 23 17 18 52 41 39 44 current 30 26 46 35

  20. 0 1 2 3 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. degree min 7 24 23 17 18 52 41 39 44 current 30 26 46 35 link 23 into 17

  21. 0 1 2 3 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. degree min 7 24 17 18 52 41 23 39 44 current 30 26 46 35 link 17 into 7

  22. 0 1 2 3 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. degree current min 24 7 18 52 41 17 30 39 44 26 46 35 23 link 24 into 7

  23. 0 1 2 3 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. degree current min 7 18 52 41 24 17 30 39 44 26 46 23 35

  24. 0 1 2 3 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. degree current min 7 18 52 41 24 17 30 39 44 26 46 23 35

  25. 0 1 2 3 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. degree current min 7 18 52 41 24 17 30 39 44 26 46 23 35

  26. 0 1 2 3 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. degree current min 7 18 52 41 24 17 30 39 44 26 46 23 link 41 into 18 35

  27. 0 1 2 3 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. degree current min 7 52 18 41 24 17 30 39 26 46 23 44 35

  28. 0 1 2 3 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. degree current min 7 52 18 41 24 17 30 39 26 46 23 44 35

  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. min 7 52 18 41 24 17 30 39 26 46 23 44 stop 35

  30. Fibonacci Heaps: Extract-Min Analysis Extract-Min. Actual cost. O(D(n)) + O(t(H)) • O(D(n)) to meld min's children into root list.(at most D(n) children of min) • O(D(n)) + O(t(H)) to update min.(the size of the root list is at most D(n)+t(n)-1) • O(D(n)) + O(t(H)) 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. O(D(n)) - t(H) • (H') = D(n)+ 1 +2m(H) (at most D(n)+1 roots with distinct degrees remain and no nodes become marked during the operation) Amortized cost. O(D(n)) (H) = t(H) + 2m(H) potential function

  31. Decrease Key

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

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

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

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

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

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

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

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

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

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

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

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

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

  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). min x p p' p'' 15 5 26 24 7 18 38 88 17 23 21 39 41 72 don't markparent ifit's a root 30 52 decrease-key of x from 35 to 5

  46. Fibonacci Heaps: Decrease Key Analysis Decrease-key. Actual cost. O(c), where c is the number of cascading cuts • 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 • t(H') = t(H) + c. (the original t(H) trees and c trees produced by cascading cuts) • m(H') m(H) - c + 2. (c-1 nodes were unmarked by the first c-1 cascading cuts and the last cut may have marked a node) •   c + 2  (-c + 2) = 4 - c. Amortized cost. O(1) (H) = t(H) + 2m(H) potential function

  47. Union

  48. Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists. min min 23 24 17 7 3 21 30 26 46 18 52 41 35 Heap H' Heap H'' 44 39

  49. Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists. min 23 24 17 7 3 21 30 26 46 18 52 41 35 Heap H 44 39

  50. Fibonacci Heaps: Union Actual cost. O(1) Change in potential. 0 (t(H) and m(H) remain the same) Amortized cost. O(1) (H) = t(H) + 2m(H) potential function min 23 24 17 7 3 21 30 26 46 18 52 41 35 Heap H 44 39

More Related