Towards a Final Analysis of Pairing Heaps
Total Page:16
File Type:pdf, Size:1020Kb
Towards a Final Analysis of Pairing Heaps Seth Pettie£ Max Planck Institut f¨ur Informatik Abstract perimentally that the cost of decreasekey is virtually, if not asymptotically, constant, and Moret & Shapiro [10] con- Fredman, Sedgewick, Sleator, and Tarjan proposed the cluded that in Prim’s minimum spanning tree algorithm pairing heap as a self-adjusting, streamlined version of the pairing heaps are faster than the alternatives. These in- Fibonacci heap. It provably supports all priority queue cluded binary heaps, splay heaps, Fibonacci heaps, and oth- operations in logarithmic time and is known to be extremely ers. The pairing heap is now included in implementations efficient in practice. However, despite its simplicity and of the GNU C++ library and the LEDA library [9]. empirical superiority, the pairing heap is one of the few Fredman [4] proved the remarkable result that on a spe- popular data structures whose basic complexity remains cific distribution of operation sequences, no (generalized) open. In this paper we prove that pairing heaps support pairing heap can perform optimally. Specifically, there Ò Ò ÐÓ Ò the deletemin operation in optimal logarithmic time and are sequences of Ò insertions, deletions, and de- Ò ÐÓ Ò ÐÓ ÐÓ Òµ all other operations (insert, meld, and decreasekey) in time ª´ Ô creasekeys that take time to execute. ¾ ÐÓ ÐÓ Ò Ç ´¾ µ. This result gives the first sub-logarithmic He concluded that the complexity of the decreasekey op- Òµ Ç ´ÐÓ Òµ time bound for decreasekey and comes close to the lower eration lies somewhere between ª´ÐÓ ÐÓ and . Òµ bound of ª´ÐÓ ÐÓ established by Fredman. The difference between optimality and suboptimality is not as stark as one might think. Because the user of Pairing heaps have a well known but poorly understood re- a priority queue has already resigned himself to paying lationship to splay trees and, to date, the transfer of ideas Ò ÐÓ Òµ Ò ¢´ time for deletemins, a non-constant cost for has flowed in one direction: from splaying to pairing. One decreasekey will only be detectable for sufficiently dense contribution of this paper is a new analysis that reasons ex- input sequences. (Density is the ratio of decreasekeys plicitly with information-theoretic measures. Whether these to deletemins.) For example, if Fredman’s lower bound ideas could contribute to the analysis of splay trees is an turns out to be tight, this would imply that in the stan- open question. dard graph applications (shortest paths, weighted match- ings, minimum spanning trees, etc.) pairing heaps are no worse than Fibonacci heaps when the number of edges is ´Ò ÐÓ Ò ÐÓ ÐÓ Òµ 1 Introduction Ç . Fredman’s lower bound only estab- lishes that the pairing heap is suboptimal in the density ´ÐÓ Ò ÐÓ ÐÓ Òµ Ó´ÐÓ Ò ÐÓ ÐÓ Òµ Twenty years ago Fredman et al. [5] proposed the pair- range to ; for other ing heap, a simple priority queue data structure that they ranges its complexity remains open. conjectured to be as efficient as Fibonacci heaps [6]. In In this paper we provide the first proof that pairing heaps their original analysis Fredman et al. proved that all priority support decreasekeys in sub-logarithmic time. queue operations (insert, deletemin, meld, and decreasekey) ´ÐÓ Òµ Ò were supported in Ç amortized time, where is the Theorem 1 The pairing heap executes a mixed sequence size of the heap. Although logarithmic time is optimal for Ò of Ñ priority queue operations, including deletemins, in deletemin, theoretically efficient priority queues like the Fi- Ô ¾ ÐÓ ÐÓ Ò Ç ´Ò ÐÓ Ò · Ѿ µ time. ´½µ bonacci heap enjoy Ç amortized time for insert, meld, and decreasekey. Ô ¾ ÐÓ ÐÓ Ò In the intervening years pairing heaps have become the The ¾ function lies in the asymptotic spectrum ¯ ´ ´ÐÓ ÐÓ Òµµ Ó´ÐÓ Òµ priority queue of choice in applications requiring the de- between poly and . It is an ugly creasekey operation. Stasko & Vitter [8] demonstrated ex- bound and undoubtedly not tight. Nonetheless, we can now £ claim with certainty that the pairing heap is asymptotically Supported by an Alexander von Humboldt Postdoctoral Fellowship. Ô ¾ ÐÓ ÐÓ Ò Ç ´ÐÓ Ò¾ Email: [email protected]. optimal for any density µ. The Pairing Heap. The structure consists of a single rectly. rooted tree where the children of a node are assigned some left-to-right ordering. Each node is identified with a key and Overview. In Section 2 we discuss the original analysis of the key of a parent is no larger than the key of any child. (It pairing heaps in order to provide a reference point for our is heap ordered.) During the execution of an operation there approach. In Section 3 we introduce some key ideas and, as Ô Ü Ü ¾ may be multiple rooted trees. If ½ are two roots then ´ ÐÓ Òµ a warmup, sketch a proof that decreasekeys take Ç Ü Ü Ü Ü ¾ ½ ¾ linking ½ and either makes the leftmost child of or time. The full proof of Theorem 1 appears in Section 4. vice versa, depending on which node’s key is smaller. The In Section 5 we discuss some open problems and make a operations are implemented as follows. An insert creates conjecture on the actual complexity of pairing heaps. a new node with a given key and links it with the existing root. A meld of two given heaps simply links their roots. A decreasekey operation is given both a node and the heap to 2 Pairing Heaps, Entropy, and Potentials which it belongs. It detaches the subtree rooted at the given node, reduces the node’s key appropriately, then links the The original analysis of pairing heaps [5] was a stroke of given node with the existing heap root. elegance. However, it was achieved by drawing a slightly By the heap-order property the minimum node is neces- unnatural correspondence between pairing heaps and splay sarily at the root. A deletemin proceeds by detaching ev- trees. In particular, Fredman et al. [5] map a general rooted ery child of the root then linking children in a prescribed tree to a binary tree: a node’s left child in the binary rep- fashion until one tree remains. Several linking strategies resentation corresponds to its leftmost child in the general have been proposed [5, 8, 3]. In this paper we consider the tree and its right child corresponds to its right sibling in the Ü Ü Ü ¾ standard 2-pass version given in [5]. Let ½ be general tree. They observe that in the binary representation the children of the root in left-to-right order. In the cou- a deletemin operation looks very similar to a splay up the ´Ü Ü µ ´Ü Ü µ ´Ü Ü µ ¾ ¿ pling pass we link the pairs ½ . right shoulder of the tree. By borrowing the same poten- Ý Ý Ý ¾ Let ½ be the winners of these linkings, where tial function and analysis from [11] they derive amortized Ý Ü Ý if is odd. In the accumulation pass we link logarithmic bounds on deletemins and all other operations. Ý Ý ½ ¾ with , the winner of this comparison with ,andso Ý Ý Ý ·½ on; in general, is linked with the winner of . Previous Work. Fredmanetal.[5]provedthatthe ´ÐÓ Òµ 2-pass version of deletemin takes Ç amortized time. They also considered arbitrary coupling, multi- pass,andbackward 2-pass.1 It was shown that arbi- Ô Òµ trary coupling takes ¢´ time and that multipass takes Ô ´ÐÓ Ò ÐÓ ÐÓ Ò ÐÓ ÐÓ ÐÓ Òµ Ó´ Òµ Ç time. No time bound is known for backward 2-pass. Stasko and Vitter [8] Figure 1. The general trees are marked with and Elmasry [2] proposed several variants of these linking circular nodes, the binary representation with strategies. In [8] it was observed that with a small inser- square nodes. On the left is a tree with en- tion buffer the amortized cost of insertion can be reduced tropy zero and on the right is a tree with en- ´½µ ÐÓ´ µ to Ç . Iacono [7] proved that even the standard 2-pass tropy . ´½µ version supports insert and meld in Ç time. Bringing the ´½µ insertion cost to Ç is significant in applications where the heap is likely to end in a non-empty state, for instance, The analysis of [5] uses the potential function ¨ È ×´Üµ ״ܵ when computing single-source, single-destination shortest ÐÓ where is the size of the subtree rooted Ü paths with Dijkstra’s algorithm. Iacono [7] and Elmasry at Ü in the binary representation. One property of this func- [2] studied the distribution-sensitive properties of pairing tion is that general trees with completely different charac- heaps. Before the pairing heap was introduced Sleator and teristics can get assigned the same potential. In Figure 1 Tarjan [12] invented the skew-heap, a self-adjusting data we have, among rooted 5-node trees, the two with mini- structure that supported insert, deletemin, and meld in log- mum and maximum entropy, respectively. Here entropy is arithmic time; however, it did not support decreasekeys di- the logarithm of the number of linear extentions consistent with the heap-order. Since both trees get assigned a poten- 1Arbitrary coupling partitions the roots into couplets arbitrarily and ac- tial of ÐÓ´ µ one wonders if there is an interpretation of cumulates the couplet winners in no specific order. Multipass repeatedly executes the coupling pass of 2-pass and backward 2-pass accumulates the this potential that makes sense in the context of a priority Ý Ý ½ roots from left-to-right rather than right-to-left.