comparison complexity of priority queue operations
play

Comparison complexity of priority-queue operations Jyrki Katajainen - PowerPoint PPT Presentation

Comparison complexity of priority-queue operations Jyrki Katajainen (University of Copenhagen) Joint work with Amr Elmasry (Max-Planck-Institut f ur Informatik) and Claus Jensen (University of Copenhagen) 16 January 2009 Updated 31 January


  1. Comparison complexity of priority-queue operations Jyrki Katajainen (University of Copenhagen) Joint work with Amr Elmasry (Max-Planck-Institut f¨ ur Informatik) and Claus Jensen (University of Copenhagen) 16 January 2009 Updated 31 January 2009 These slides are available at http://cphstl.dk c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (1)

  2. Priority-queue kernel insert ( Q , p ). Add an element create ( Q ). Create Q . Postcondi- with locator p to Q . tion: Q = ∅ . extract ( Q ). Extract an unspeci- destroy ( Q ). Destroy Q . Precondi- fied element from Q and re- tion: Q = ∅ . turn a locator to that elem- find - min ( Q ). Return a locator to ent. Precondition: Q � = ∅ . an element that, of all elem- delete ( Q , p ). Remove the elem- ents in Q , has a minimum value. Precondition: Q � = ∅ . ent with locator p from Q (without destroying it). size ( Q ). Return the number of meld ( Q , R ). Move all elements elements stored in Q . from Q and R to a new pri- swap ( Q , R ). Make Q refer to the ority queue S , destroy Q and data structure referred to by R , and return S . R , and vice versa. decrease ( Q , p , x ). . . . not relevant for this talk. . . c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (2)

  3. Focus • Comparison complexity • Worst-case efficiency • Constant factors God’s rule 1: Do care about the size of O ! ! Read [Sch¨ onhage et al. 1994] I make no claims about the practical utility of the data structures discussed, even though the development of a library component that provides good practical performance is a challenging task. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (3)

  4. Bug report The binary-heap implementation in LEDA 6.1 is broken. 1. The time bounds are guaranteed in the amortized or average-case sense, not in the worst-case sense, contrary to what is claimed in the documentation. 2. insert is extremely slow; it does not take O expected time as I ! would expect. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (4)

  5. Research question Assumptions: Trivial operations have O ! worst-case running time and perform no element comparisons. The worst-case running time of non-trivial operations is proportional to the number of element comparisons performed. (If not, specify explicitly.) Question: What are the best worst-case bounds for the number of element comparisons performed by the non-trivial operations insert , extract , delete , and meld ? Answer: It is still open whether optimal bounds O ! for insert , extract , and meld ; and lg n + O ! for delete can be achieved or not. n, m : # of elements stored just prior to an operation c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (5)

  6. Biased history Year Inventor insert extract delete meld 1964 Williams lg n + O 2 lg n + O ! lg n lg m ! O ! ! O 1978 Vuillemin lg n + O ! lg n time 2 lg n + O lg n + O O ! ! ! 1988 Driscoll & al. lg n + O 3 lg n + O lg m + O O ! ! ! ! 1995 Brodal ! lg n time 6 lg n + O O ! O ! O ! 1996 Brodal & Okasaki ! lg n time 4 lg n + O O O O ! ! ! 2004 Elmasry – 1 . 44 lg n + O ! lg lg n – O ! Jensen & me lg n + O ! lg lg n – O O ! ! Elmasry lg n + O – O O ! ! ! Now New results lg n + O 2 lg n + 2 lg m + O O ! O ! ! ! lg n + O 3 lg n + O O ! ! ! O ! • O ! ≤ 10; n ≥ m . • Bounds displayed in red are not proved in the original papers. • See our joint paper Multipartite priority queues in ACM Transac- tions on Algorithms 5 ,1 (2008), Article 14. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (6)

  7. Binomial queues n = 1010two min B 0 ≡ 1 3 B k ≡ x B 1 y B k − 1 B 3 B k − 1 • heap-ordered x ≤ y Read [Cormen et al. 2001] • at most ⌊ lg n ⌋ + 1 binomial trees c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (7)

  8. Redundant binary numbers 1 1 1 Binary representation: + 1 ⌊ lg n ⌋ 1 0 0 0 d i 2 i , � n = 1 1 1 i =0 where d i ∈ { 0 , 1 } for all i ∈ + 1 1 1 2 { 0 , 1 , . . . , ⌊ lg n ⌋} . Redundant representation: Assuming a carry stack is avail- ⌊ lg n ⌋ able, ++ is performed as follows: d i 2 i , � n = 1. Fix the topmost carry if the i =0 where d i ∈ { 0 , 1 , 2 } for all i ∈ stack is not empty. { 0 , 1 , . . . , ⌊ lg n ⌋} . 2. Add one as desired. 3. If the least significant digit becomes 2, push this carry Read [Okasaki 1998] onto the stack. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (8)

  9. Bug report The binomial-queue implementation in [Cormen et al. 2001] can be improved: 1. By maintaining a pointer to n = 1010redundant two min the minimum, the running time of find - min can be im- 1 3 proved from O ! lg n to O ! . 2. With the redundant binary representation, the running time of insert can be impro- ved from O ! lg n to O ! . B 1 3. It would be easier to maintain referential integrity if parent pointers were only maintain- B 3 ed for the largest children. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (9)

  10. Prefix-minimum pointers 4 2 5 7 i < j < k B i B j B k The key observation, what I call the ↑→ property , is that when a value in tree B j is updated, the number of element comparisons made to fix B j is j + O and the number of element comparisons made to fix ! the prefix-minimum pointers at higher trees is at most lg n − j + O ! . Read [Elmasry et al. 2008] c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (10)

  11. Delivery buffer 3 7 5 9 Maintain a buffer whose size is between 1 and lg n + O ! . The first node stores the minimum element held in the buffer. The buffer can be used for borrowing and inserting elements. Special handling is necessary when the buffer becomes too small or too large. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (11)

  12. Incremental processing Perform a task taking O ! t time incrementally during the forthcoming t modifying operations ( insert , extract , delete , and meld ) so that each subtask is about equal in size. It is important that there is at most one incremental process running at a time. The simplest way of implementing this idea is to divide the task into primitive steps and execute a fixed number of primitive steps in connection with each subtask. An alternative way is to use coroutines in languages that support them. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (12)

  13. Multipartite priority queues doubly-linked list Upper store at most lg n + O ! prefix-minimum pointers binomial queue (binary) Main store at most n elements doubly-linked list Buffer 1..lg n + O ! elements binomial queue (redundant) 0.. 1 2 lg n + O ! elements Recall Floating tree where the overall binomial tree minimum at most n elements is! c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (13)

  14. Priority-queue operations (Part I) extract : insert : – Remove a node from the buf- – Insert the given node into the buffer. fer. – If the buffer is larger than – If the buffer is smaller than 1 7 8 lg n + O ! , construct B k , where 8 lg n + O ! , take the smallest tree ! ≤ 2 k < 1 1 from the main store and split it 4 lg n + O 2 lg n + O ! , and (if possible). If the smallest tree unite it with the main store. is of size 1, move it to the buffer. [Done incrementally during the next 1 After such a move, update the 8 lg n + O ! operations!] upper store accordingly. Do this splitting repeatedly until the buf- fer contains more than 1 4 lg n + O ! elements. [Done incrementally during the next 1 8 lg n + O ! operations!] c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (14)

  15. Priority-queue operations (Part II) delete : meld ( Q , R ): (if size ( Q ) ≤ size ( R )) – If the removed node is in the – Throw away both upper stores. buffer, remove it and update the – Concatenate the two buffers. buffer minimum. Stop. – If Q had a floating tree under – Swap the removed node with construction, complete this. its parent until it reaches the root – If R had a floating tree under of its tree B k . construction, continue this. – Borrow a node from the buffer. – If the buffer is still too large, – Remove the root of B k . start the creation of a new float- – Rebuild B k using the root’s ing tree. subtrees and the borrowed node. – Unite the two main stores, the – If the removed node was in two new trees (if any), and the the main store, update the prefix- two floating trees (if any). minimum pointers for trees larger – Create an upper store for the than B k , if necessary. new main store. c � Performance Engineering Laboratory Visit at the Max-Planck-Institut f¨ ur Informatik (15)

Recommend


More recommend