Worst-Case Ef fi cient External-Memory Priority Queues Gerth Stølting Brodal Max–Planck–Institut f¨ ur Informatik Saarbr¨ ucken Germany Jyrki Katajainen Datalogisk Institut Københavns Universitet Denmark July 1998
Worst-Case Ef fi cient External-Memory Priority Queues Priority Queues Maintain a set of N elements from a totally ordered universe under I NSERT � x � – Insert element x into the set D ELETE M IN �� – Delete and return the minimum of the set I NSERT and D ELETE M IN can be implemented with ��log N � � comparisons – Williams, 1964 G. S. Brodal, J. Katajainen 2
Worst-Case Ef fi cient External-Memory Priority Queues External Memory Model – Aggarwal, Vitter, 1988 � � � � � � M = Size of the internal memory A � A � B = I/O block size registers A � � N = # elements in the priority queue � � CPU Complexity measures Internal memory Internal : # comparisons Y I/O � � � � External : # I/Os � � � Assumptions External External memory memory N �� B , M � B � log M �B M G. S. Brodal, J. Katajainen 3
Worst-Case Ef fi cient External-Memory Priority Queues External-Memory Merge Sorting � � N Theorem N elements stored in blocks can be sorted with O B � � � comparisons and N N I/Os. �� N log N � log M �B � B B Algorithm: (1) Sort in internal memory all runs of M elements � � (2) In M iterations apply a N M -ary merging algorithm log � M �B B � � i . M to obtain lists of length � M B The sorting algorithm is optimal w.r.t. both comparisons and I/Os. – Aggarwal, Vitter, 1988 G. S. Brodal, J. Katajainen 4
Worst-Case Ef fi cient External-Memory Priority Queues Worst-Case Effeciency Internal Each priority queue operation should require ��log N � � comparisons in the worst-case External The I/Os should be divided evenly among the operations, i.e., one � � I/O for every N th operation. � B � log M �B B G. S. Brodal, J. Katajainen 5
Worst-Case Ef fi cient External-Memory Priority Queues External-Memory Priority Queues Binary heap – Williams, 1964 – Random memory accesses imply one I/O every ��� CPU operation. O Fishspear – Fischer, Patterson, 1994 � � – N I/Os for N operations. Uses ��� push down stacks. O log N O � B Heap + B elements/node – Wegner, Teuhola, 1989 � � – N I/Os for every B th operation. Optimal for � . O log M � O � B � B Buffer tree * – Arge, 1995 � � – N N I/Os for N operations. O log M �B B M Heap with buffers * – Fadel et al., 1997 – Similar bounds as for buffer trees. * No worst-case guarantee on the individual operations G. S. Brodal, J. Katajainen 6
Worst-Case Ef fi cient External-Memory Priority Queues New Result Theorem D ELETE M IN and I NSERT can be done with ��log N � comparisons and one I/O for every N � th operation. �� B � log M �B B Outline of the Data Structure � � � � � � Internal part Work area � MIN � � NEW � � � External part � � � � � � Q k Sorted lists of length Q i � � M � � M B MIN stores the overall � smallest elements � fast D ELETE M IN . O � M NEW stores the most recently inserted � elements � fast I NSERT . O � M [ small elements are inserted directly into MIN ] The external part stores exactly B elements per block [ all elemenst are larger than the elements in MIN ] G. S. Brodal, J. Katajainen 7
Operations K � �� M � I NSERT � x � – If max MIN then swap x and max MIN � x – Insert x into NEW j NEW j – If K then perform B ATCH I NSERT � with K elements from NEW . D ELETE M IN �� – If MIN � then perfrom B ATCH D ELETE � Merge the result with NEW Move the K smallest elements to MIN – Delete and return min MIN B ATCH I NSERT – Create a new list of length K and assign it rank 1 – i �� � – while #list of rank M B do i � Merge the lists with rank i Assign the resulting list rank i � � i �� i � � B ATCH D ELETE – Fetch the K smallest elements from external memory to internal memory j L j M N rank � lists b log c � L � � log M �B M �B B B M G. S. Brodal, J. Katajainen 8
Worst-Case Ef fi cient External-Memory Priority Queues Amortized Result Lemma The described data structure supports I NSERT and D ELETE M IN � � with amortized � comparisons and N I/Os. � ��log N � log M �B B B [ Identical bounds follow by using buffer trees and external heaps ] Worst-Case Bounds? � Incremental list merging – Thorup, 1996 � Incremental batch operations � Start B ATCH D ELETE before MIN gets empty. G. S. Brodal, J. Katajainen 9
Incremental List Merging Lists of rank i n � � i L L L L � � � i i i i � � � z �� � � � � � � n i L L L � � � i i i M ERGE S TEP � i � – Perform K steps of the list merging at rank i . – If the merging is fi nished, then make the resulting � list i a list of rank � (list promotion) L i � B ATCH I NSERT – Create a new list with rank one – � M ERGE S TEP � i � i � G. S. Brodal, J. Katajainen 10
Deletions B ATCH D ELETE deletes and returns the K least elements of the lists. � The length of the lists decreases. � The maximum rank increases. Possible solution: Incremental global rebuilding. Our solution: 1) If the incremental merging of rank i fi nishes, i , then we do not promote � � � � but M j j L � K L i i B to rank � . i � 2) Let R be the maximum rank of a list. � � R � � , � If the resulting list M L � K R B � then R gets rank � . � L R � 3) If K elements are deleted from i , we perform L M ERGE S TEP � i � . 4) Always perform M ERGE S TEP � after � R B ATCH D ELETE � � � N O R � log M �B M G. S. Brodal, J. Katajainen 11
Worst-Case Ef fi cient External-Memory Priority Queues Conclusion � The fi rst worst case external-memory priority-queue implementation. Open Problems � Is the de fi nition of “worst-case” reasonable in practice ? � Experimental studies. � Can buffer trees be deamortized, i.e., how can buffer trees support � � operations with worst-case N I/Os for B operations ? O log M �B B G. S. Brodal, J. Katajainen 12
Recommend
More recommend