what your teachers never told you about fibonacci heaps
play

What your teachers never told you about Fibonacci heaps Jyrki - PowerPoint PPT Presentation

14 November, 2014 ARCO Workshop, Copenhagen What your teachers never told you about Fibonacci heaps Jyrki Katajainen 1 Stefan Edelkamp 2 Jesper Larsson Tr aff 3 1 University of Copenhagen 2 University of Bremen 3 Vienna University of Technology


  1. 14 November, 2014 ARCO Workshop, Copenhagen What your teachers never told you about Fibonacci heaps Jyrki Katajainen 1 Stefan Edelkamp 2 Jesper Larsson Tr¨ aff 3 1 University of Copenhagen 2 University of Bremen 3 Vienna University of Technology These slides are available from my research information system (see http://www.diku.dk/~jyrki/ under Presentations). � Performance Engineering Laboratory c (1)

  2. Said about Fibonacci heaps • Among all the data structures that guarantee constant decrease - key and logarithmic delete - min cost, Fibonacci heaps have re- mained the most popular to teach [Chan 2013] • In spite of the many competitors ..., the original data struc- ture remains one of the simplest to describe and implement [Kaplan, Tarjan, Zwick 2014] • For the analysis , the potential function itself is not compli- cated, but one needs to first establish bounds on the maximum degree of the trees [Chan 2013] • Fibonacci heaps do not perform well in practice , but pairing heaps do [Haeupler, Sen, Tarjan 2013] • One reason Fibonacci heaps perform poorly is that they need an extra pointer per node [Haeupler, Sen, Tarjan 2013] • The decrease - key operation uses a simple “cascading cut” strat- egy, which requires an extra bit per node [Chan 2013] � Performance Engineering Laboratory c (2)

  3. Inspirational source Kaplan, Tarjan, Zwick: Fibonacci heaps revisited, E-print arXiv:1407.5750, arXiv.org (2014) • Simple Fibonacci heaps • Pseudo-code on one page • Beautiful analysis • No experiments • No validation of “simplicity” � Performance Engineering Laboratory c (3)

  4. What does word “simple” mean when we talk about data structures and algorithms? How would you measure “simplicity”? � Performance Engineering Laboratory c (4)

  5. Classification of priority queues Elementary inject , aka insert top , aka minimum input: locator input: none top output: none output: locator inject eject extract - top extract , aka delete extract - top p ← top () input: locator Addressable extract ( p ) output: none top inject elevate elevate , aka decrease - key eject input: locator, value extract output: none Mergeable eject , aka borrow merge , aka union As above + input: none input: two priority queues merge output: locator output: one priority queue � Performance Engineering Laboratory c (5)

  6. Software library vs. algorithm text namespace cphstl { namespace cphstl { template < template < typename V , // value typename E , // element typename C , // comparator typename C , // comparator typename A , // allocator typename N // node typename E , // encapsulator > typename R , // realizator class fibonacci_heap { typename I , // iterator fibonacci_heap ( C const &) ; typename J // iterator const ∼ fibonacci_heap () ; N ∗ begin () const ; > class mergeable_priority_queue { N ∗ end () const ; // 30 + convenience functions } N ∗ top () ; } void inject ( N ∗ ) ; void elevate ( N ∗ , E const &) ; N ∗ eject () ; namespace cphleda { void extract ( N ∗ ) ; template < void merge ( fibonacci_heap &) ; typename K , // key void swap ( fibonacci_heap &) ; typename V , // information } typename C , // comparator } typename R , // realizator typename J // iterator const The interface of our realizators is > class p_queue { similar to that used in a modern // 23 convenience functions } algorithm text. } � Performance Engineering Laboratory c (6)

  7. Where is eject needed? void clear () { while ( n != 0) { I t = eject () ; destroy ( t ) ; } } clear is used by • ∼ mergeable priority queue() • template <typename K> void insert(K, K) • mergeable priority queue(mergeable priority queue const &) • operator=(mergeable priority queue const &) In the last three, clear is needed to achieve exception safety. � Performance Engineering Laboratory c (7)

  8. A collection of heap-ordered multi-ary trees Node x Structure head r x root list L x – element – rank r child list of x – state: unmarked or marked � (a marked node has lost a child) Basic primitive – pointers: parent , (first) child , before , after r +1 r r Heap order fair link − → x element at x ≤ y element at y � Performance Engineering Laboratory c (8)

  9. L Lazy Fibonacci heap eject top • take the first root x • do all fair links on L • catenate its child list and L • find the top in the remaining L • return x inject ( x ) extract ( x ) h • append x to L L z elevate ( x , · ) h L � y 1 z  � y 2 x   • cut x cascading cuts �  y 1 • handle markings as above  • remove x , add its children to L x if x � = h , merge L 1 L 2 • cut x , move it to L • cut y i , unmark, move to L • mark z (if z � = h ) • catenate L 1 and L 2 � Performance Engineering Laboratory c (9)

  10. e e e e L e Analysis � Invariants: Deposit elevate • Give 1 comparison e for the – 1 comparison e at every root node cut – 1 comparison e and 1 work e • Use the work e at a marked at every marked node node to move it to L – 1 work e for each operation • Give 1 comparison and e Fact: max - rank ≤ log ϕ n where ϕ 1 work e for the node marked is the golden ratio eject top • Give 1 comparison e for each • Use comparison at the e created root roots to pay fair links • Give 1 comparison e for each extract node visited in top finding • Handle markings as above • Give 1 comparison e for each inject created root • Give 1 comparison e for the new root merge • No additional money needed � Performance Engineering Laboratory c (10)

  11. Simple Fibonacci heap: Two ingredients 1) A single tree instead of L ; link the remaining roots naively r 2 r 1 r 1 r 2 naive link or − → lucky unlucky 2) Cascading rank decreases instead of cascading cuts if x � = h , h • cut x r z • unmark y i , set its rank r i to max { r i − 1 , 0 } r 2 � • mark z , set its rank r to max { r − 1 , 0 } y 2 • link x and h naively r 1 � y 1 x � Performance Engineering Laboratory c (11)

  12. Lines of code (LOC) Lazy Fibonacci heap realizator 160 node 56 Total 216 Simple Fibonacci heap realizator 121 node 56 Total 177 Pairing heap realizator 131 node 36 Total 167 Addressable multi-ary heap realizator 110 node 24 Total 134 All programs were written in one-statement-per-line style. Comments, empty lines, lines only having a single parenthesis, debugging code, and assertions are not included in the counts. � Performance Engineering Laboratory c (12)

  13. Critical comparison Simple Addressable Fibonacci heap 4-ary heap 2 n + O ( √ n ) words, n elements Space 5 n words, n elements 10 pointer assignments ⌈ lg n ⌉ +2 pointer assignments inject 10 pointer assignments ⌈ lg n ⌉ +2 pointer assignments elevate O (lg n ) amortized time O (1) worst-case time eject 2 . 88 lg n element comparisons 2 lg n element comparisons extract O (1) amortized time O ( m lg n ) worst-case time merge void elevate ( N ∗ x , { { E const & v ) void elevate ( locator_type pair , E const & v ) ( ∗ x ) . element () = v ; N ∗ x = pair . first ; = root ) { ( ∗ x ) . element () = v ; i f ( x =  std : : size_t c = ( ∗ x ) . index () ; return ;  } while ( c > 0) {    ( ∗ root ) . state ( N : : unmarked ) ; std : : size_t p = parent ( c ) ;    decrease_ranks ( x ) ; N ∗ u = sequence [ p ] ;    cut ( x ) ; i f (! comparator (( ∗ u ) . element () , v )) {    root = naive_link ( x , root ) ; break ; siftup } }  set_slot ( c , u ) ;    c = p ;    }     set_slot ( c , x ) ;   } � Performance Engineering Laboratory c (13)

  14. Element-comparison game What is the best solution when handling a request sequence consisting of n inject , m elevate , and n extract - top operations? Simple Fibonacci heap 4 m + 2 . 88 n lg n Lazy Fibonacci heap 2 m + 2 . 88 n lg n Rank-relaxed weak heap 2 m + 1 . 5 n lg n Katajainen’s 3rd conjecture: This request sequence can be handled with 2 m + n lg n + o ( n lg n ) element comparisons in O ( m + n lg n ) worst-case time Folklore: The bound (1 + (1 /k )) m + (2 k/ lg k ) n lg n is known to be achievable (for k ≥ 2). Hint: Let a node loose at most k children. � Performance Engineering Laboratory c (14)

  15. Pointer-assignment game # pointer assignments per operation [ inject increasing sequence; elevate random permutation, priority increase n ] Lazy Simple Pairing Addressable Fibonacci heap Fibonacci heap heap 4-ary heap inject n n = 10 4 10 10 8 15 n = 10 5 10 10 8 18 n = 10 6 10 10 8 22 elevate n n = 10 4 12 10 10 16 n = 10 5 12 10 11 19 n = 10 6 12 10 11 22 extract - top n n = 10 4 239 161 148 11 n = 10 5 304 203 186 13 n = 10 6 368 245 223 14 � Performance Engineering Laboratory c (15)

  16. Running-time game Average running time [ns] per operation [ inject increasing sequence; elevate random permutation, priority increase n ] Lazy Simple Pairing Addressable Fibonacci heap Fibonacci heap heap 4-ary heap inject n n = 10 4 47 49 50 55 n = 10 5 45 50 51 60 n = 10 6 45 45 44 70 elevate n n = 10 4 10 46 24 20 n = 10 5 104 116 98 113 n = 10 6 162 242 254 271 extract - top n n = 10 4 294 242 153 119 n = 10 5 645 581 445 302 n = 10 6 1189 1093 1139 775 � Performance Engineering Laboratory c (16)

Recommend


More recommend