Towards Ultimate Binary Heaps Jyrki Katajainen 1 , 2 Amr Elmasry 3 1 University of Copenhagen 2 Jyrki Katajainen and Company 3 Alexandria University c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (1)
Binary heaps 0 8 1 2 26 10 construct () 3 4 5 6 for ( i = parent ( n − 1); i ≥ 0; −− i ) 75 46 12 75 sift - down ( i ) 7 minimum () 80 return a 0 n = 8 insert ( x ) a 8 26 10 75 46 12 75 80 a n = x 5 6 7 0 1 2 3 4 sift - up ( n ) n += 1 left - child ( i ) return 2 i + 1 extract - min () min = a 0 right - child ( i ) n − = 1 return 2 i + 2 a 0 = a n parent ( i ) sift - down (0) return ⌊ ( i − 1) / 2 ⌋ return min c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (2)
Model of computation Available • An infinite array a suitable for storing elements • O (1) other memory locations for storing elements • O (1) variables (counters, indices, bit strings of length ⌈ lg(1+ n ) ⌉ ) workspace n = 8 a 0 1 2 3 4 5 6 7 Requirement • If the data structure stores n elements, these elements must be kept in the first n locations of a . c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (3)
2014: The 50th anniversary of binary heaps [Williams 1964] Operation Worst-case time # element comparisons O (1) 0 minimum O (lg n ) lg n + O (1) insert extract - min O (lg n ) 2 lg n + O (1) [Gonnet & Munro 1986] Operation # element comparisons insert lg lg n ± O (1) sufficient and necessary lg n + log ∗ n ± O (1) sufficient and necessary extract - min Assumptions 1. Single heap 2. Perfectly heap ordered before and after each operation 3. Operations are memoryless. c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (4)
Multi-ary heaps 0 k = 4 [Johnson 1975] 8 3 4 1 2 12 10 75 46 Operation Worst-case time 5 6 7 O (1) minimum 26 80 75 O (log k n ) insert extract - min O ( k log k n ) n = 8 n : # elements a 8 12 10 75 46 26 75 80 k : arity 5 6 7 0 1 2 3 4 first - child ( i ) return k × i + 1 parent ( i ) return ⌊ ( i − 1) /k ⌋ c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (5)
Open—but solved later (see the last slide) Operation Worst-case time # element comparisons O (1) 0 minimum O (1) O (1) insert extract - min O (lg n ) lg n + O (1) in-place as binary heaps Sorting Operation Worst-case time # element comparisons O (1) 0 minimum O (1) O (1) insert extract - min Ω(lg n ) lg n − O (1) necessary n : # elements c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (6)
Related work [Carlsson, Munro & Poblete 1988] queue of pennants extract - min : 3 lg n + log ∗ n element comparisons [Elmasry, Jensen & Katajainen 2008] multipartite priority queue O ( n ) extra words [Edelkamp, Elmasry & Katajainen 2012] engineered weak heaps n + O ( w ) extra bits n : # elements w : word size in bits c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (7)
Our main result The objective can be achieved in the amortized sense ℓ ≤ lg 2 ( n 0 ) / 4 r ≤ n 0 n = n 0 + ℓ + r partial heap components: buffer backlog a 0 a 1 . . . a n − 1 elements: sizes: n 0 ℓ r What is special? backlog some large elements kept outside partial heap partial heap buffer heap-order violations heap-order violations c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (8)
Insert ℓ ≤ lg 2 ( n 0 ) / 4 r ≤ n 0 n = n 0 + ℓ + r partial heap components: buffer backlog a 0 a 1 . . . a n − 1 elements: sizes: n 0 ℓ r insert ( x ) a n ← a n 0 + ℓ a n 0 + ℓ ← x if ( ℓ = 0) k ← ⌈ lg( n 0 ) / 2 ⌉ k - ary - heap - insert ( a, n 0 , ℓ, a n 0 + ℓ ) ℓ ++ n ++ if ( ℓ = k 2 ) bulk - insert ( a, n, n 0 , ℓ ) c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (9)
Bulk-insert Use Floyd’s heap-construction algorithm in the red area: call II part sift - down at each node I part: O ( ℓ ) nodes; O ( ℓ ) total I part work as for Floyd’s algorithm II part: O (lg( n 0 )) sift - down calls ⇒ O (lg 2 ( n 0 )) total work ℓ ≤ lg 2 ( n 0 ) / 4 ∴ amortized O (1) work per insert c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (10)
Extract-min ℓ ≤ lg 2 ( n 0 ) / 4 r ≤ n 0 n = n 0 + ℓ + r partial heap components: buffer backlog a 0 a 1 . . . a n − 1 elements: sizes: n 0 ℓ r extract - root ( a, n, n 0 , ℓ ) Minimum in the buffer if ( n 0 + ℓ = n ) rebuild - structure ( a, n, n 0 , ℓ ) k - ary - heap - extract - min ( a, n 0 , ℓ ) i ← 0 a n 0 + ℓ ← a n while ( i ≤ parent ( n 0 − 1)) ℓ -- if ( a left - child ( i ) < a right - child ( i ) ) n -- a i ← a left - child ( i ) i ← left - child ( i ) Minimum in the heap else a i ← a right - child ( i ) extract - root ( a, n, n 0 , ℓ ) i ← right - child ( i ) n -- a i ← a n − 1 c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (11)
Deamortization of insertions The objective can almost be achieved in the worst case Operation Worst-case time # element comparisons minimum O (1) O (1) insert O (1) O (1) lg n + log ∗ n + O (1) extract - min O (lg n ) Role of the buffers ℓ ≤ lg 2 ( n 0 ) / 4 • buffer 2 takes care of the in- m ≤ lg 2 ( n 0 + ℓ ) / 4 serted elements n = n 0 + ℓ + m • buffer 1 is incrementally sub- binary heap components: buffer 1 buffer 2 merged into the heap by per- a 0 a 1 . . . a n − 1 elements: forming a constant amount sizes: n 0 ℓ m of work per insert c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (12)
Online sorting Operation Worst-case time # element comparisons O ( n ) O ( n ) construct extract - min O (lg n ) lg n + O (1) → Read the details from the paper [Elmasry 2003] O ( n ) extra bits n : # elements c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (13)
Note added on 15 April, 2013 Stefan Edelkamp came with the missing link that helped us to solve the problem. We are in the process of writing the paper “Optimal in-place heaps”. The new construction shows that the both lower bounds proved by Gonnet & Munro can be bypassed. c � Performance Engineering Laboratory ARCO meeting at SDU on 5 April, 2013 (14)
Recommend
More recommend