Pairing Heaps: Experiments and Analysis
Total Page:16
File Type:pdf, Size:1020Kb
RESEARCHCONlRlWlIONS Algorithms and Data Structures Pairing Heaps: G. Scott Graham Editor Experiments and Analysis JOHN T. STASKO and JEFFREY SCOTT VlllER ABSTRACT: The pairing heap has recently been and practical importance from their use in solving a introduced as a new data structure for priority queues. wide range of combinatorial problems, including job Pairing heaps are extremely simple to implement and scheduling, minimal spanning tree, shortest path, seem to be very efficient in practice, but they are difficult and graph traversal. to analyze theoretically, and open problems remain. It Priority queues support the operations insert, has been conjectured that they achieve the same find-min, and delete-min; additional operations often amortized time bounds as Fibonacci heaps, namely, include decrease-key and delete. The insert(t, v) opera- O(log n) time for delete and delete-min and O(1) for tion adds item t with key value v to the priority all other operations, where n is the size of the priority queue. The find-min operation returns the item queue at the time of the operation. We provide empirical with minimum key value. The delete-min operation evidence that supports this conjecture. The most returns the item with minimum key value and promising algorithm in our simulations is a new variant removes it from the priority queue. The decrease- of the twopass method, called auxiliary twopass. We key(t, d) operation reduces item t’s key value by d. prove that, assuming no decrease-key operations are The delete(t) operation removes item t from the performed, it achieves the same amortized time bounds as priority queue. The decrease-key and delete opera- Fibonacci heaps. tions require that a pointer to the location in the priority queue of item t be supplied explicitly, since 1. INTRODUCTION priority queues do not support searching for arbi- A priority queue is an abstract data type for main- trary items by value. Some priority queues also sup- taining and manipulating a set of items based on port the merge operation, which combines two item- priority [I]. Prio’rity queues derive great theoretical disjoint priority queues. We will concentrate on the insert, delete-min, and Support was provided in part by NSF research grant DCR-84-03613, an NSF Presidential Young Investigator Award, an IBM Faculty Development Award, decrease-key operations because they are the opera- and a Guggenheim Fellowship. tions that primarily distinguish priority queues from Part of this research was performed at Mathematical Sciences Research Insti- tute. Berkeley, Calif., and the Institut National de Recherche en Informatique other set manipulation algorithms and because they et en Automatique, Rocquencourt. France. are the critical operations as far as the time bounds 0 1987 ACM OOOl-0782/87/0300-0234 75a: are concerned. Communications of the ACM March 1987 Volume 30 Number 3 Research Contributions Several implementations of priority queues, such quences of commands to make the pairing heaps as implicit heaps [lo], leftist heaps [3, 71, and bino- perform as poorly as we could. The results were mial heaps [2, 91 have been shown to exhibit an positive in that the pairing heaps always performed O(log n) worst-case time bound for all operations, extremely well. This does not prove that the desired where n is the size of the priority queue at the time time bounds do hold, but it is reassuring and makes of the operation. Fibonacci heaps [4] provide a dra- us optimistic that the conjecture is true. matic improvement on the general logarthmic bound In this article we study the “twopass” and “multi- by achieving amortized time bounds of O(1) for pass” versions of pairing heaps; the names arise from insert, decrease-key, and find-min and O(log n) for the method used to do the delete-min in each version delete-min and delete. This greatly improves the [5]. We also introduce new variants called “auxiliary best known theoretical bounds for the time required twopass” and “auxiliary multipass.” All versions are to solve several combinatorial problems.* Follow- described in the next section. In Section 3, we dis- ing the approach of [8], a sequence of operations cuss our simulations and the empirical data. Auxil- op,, opz,. ., opk is said to have amortized time bounds iary twopass performed best in the simulations, b,, bz, . , bk if based on our measure of performance. In Section 4, we provide a partial theoretical analysis of pairing Esj ti 5 C bit for all 1 5 j I k, lsisj heaps by introducing the concept of “batched poten- tial.” We show, for example, that auxiliary twopass where ti is the actual time used by opi. Intuitively, if uses O(1) amortized time per insert and find-min and operation opi uses less time than its allotted bi units, O(log n) amortized time for the other operations. then the leftover time may be held in reserve to be Conjectures and open problems follow in Section 5. used by later operations. Fibonacci heaps achieve their time bounds by 2. PAIRING HEAP ALGORITHMS complicated invariants with significant overhead, so A comprehensive description of pairing heaps ap- they are not the method of choice in practice. Re- pears in [5]. A summary is given below. Our studies cently, a self-adjusting data structure called the pair- involve the twopass algorithm, which was the sub- ing heap was proposed [5]. Pairing heaps are much ject of most of the analysis in [5], and the multipass simpler than Fibonacci heaps, both conceptually and algorithm. in implementation; and they have less overhead per Pairing heaps are represented by heap-ordered operation. The best amortized bound proved so far trees and forests. The key value of each node in the for pairing heaps is O(log n) time per operation. It heap is less than or equal to those of its children. is conjectured that pairing heaps achieve the same Consequently, the node with minimum value (for amortized time bounds as Fibonacci heaps, namely, simplicity, we will stop referring to a key value, and O(1) per operation except O(log n) for delete-min and just associate the value directly with the heap node) delete. is the root of its tree. Groups of siblings, such as tree To test whether the conjecture is true, we per- roots in a forest, have no intrinsic ordering. formed several simulations of the pairing heap algo- In the general sense, pairing heaps are represented rithms. These simulations differed significantly from by multiway trees with no restriction on the number the ones independently done in [6], since the latter of children that a node may have. Because this mul- ones did not address the conjecture. In our simula- tiple child representation is difficult to implement tions, we tested several different pairing heaps and directly, the child-sibling binary tree representation used “greedy” heuristics and the appropriate se- of a multiway tree is used, as illustrated in Figure 1 (p. 236). In this representation, the left pointer of a ‘For example, a standard implementation of Dijkstra’s algorithm (which finds node accesses its first child, and the right pointer of the shortest path from a specified vertex x to all other vertices in a graph with nonnegative edge lengths) uses a priority queue as follows: Let us denote the a node accesses its next sibling. In terms of the bi- number of vertices in the graph by V and the number of edges by E. The key value of each item y in the priority queue represents the length of the shortest nary tree representation, it then follows that the path from vertex x to vertex y using only the edges in the graph already value of a node is less than or equal to all the values processed. Initially, no edges are processed, and the priority queue contains V items: the key value of item x is 0 and all other items have key value m. The of nodes in its left subtree. A third pointer, to the algorithm successively performs delete-mins until the priority queue is empty. previous sibling, is also included in each node in Each time a delehe-min is performed (say, the vertex y is deleted), the algo- rithm outputs the shortest path between x and y, and each unprocessed edge order to facilitate the decrease-key and delete opera- (y, z) incident toy in the graph is processed; this may require that a decrease- key be performed in order to lower the key value of z in the priority queue. tions. The number of pointers can be reduced from Thus. there are at most V inserfs, V delete-mins, and E decrense-keys during the three to two, as explained in [5] at the expense of a ccwrse of the algorithm. If a Fibonacci heap is used to implemenithe priority V V1. constant factor increase in running time. Unless aueue. the resultine- runnineI time is OfE + lee- ,. which is a sienificant improvement over O((E + V) log V) using the other heap representations. stated otherwise, the terms “child,” “parent,” and Other examples of how Fibonacci heaps can improve worst-case running times are given in 141. “subtree” will be used in the multiway tree sense; March 1987 Volume 30 Number 3 Communications of the ACM 235 Research Contributions tree representation, the root node always has a null right pointer. The insert(t, V) operation performs a comparison-link between t and the tree root; the node with smaller value becomes the root of the resulting tree. The decrease-key(t, d) operation begins by reducing t’s value by d.