Lably
Skip to content

Pairing heap

Variant of heap data structure

  • 5 min read
  • 3 chapters

Related on Lably

Through Data structure and Big O notation

  • find-min: simply return the top element of the heap.
  • meld: compare the two root elements, the smaller remains the root of the result, the larger element and its subtree is appended as a child of this root.
  • insert: create a new heap for the inserted element and meld into the original heap.
  • decrease-key (optional): remove the subtree rooted at the key to be decreased, replace the key with a smaller key, then meld the result back into the heap.
  • delete-min: remove the root and do repeated melds of its subtrees until one tree remains. Various merging strategies are employed.

The analysis of pairing heaps' time complexity was initially inspired by that of splay trees. The amortized time per delete-min is O(log n), and the operations find-min, meld, and insert run in O(1) time.

When a decrease-key operation is added as well, determining the precise asymptotic running time of pairing heaps has turned out to be difficult. Initially, the time complexity of this operation was conjectured on empirical grounds to be O(1), but Fredman proved that the amortized time per decrease-key is at least Ω(log⁡log⁡n) for some sequences of operations. Using a different amortization argument, Pettie then proved that insert, meld, and decrease-key all run in O(22log⁡log⁡n) amortized time, which is o(log⁡n). Elmasry later introduced elaborations of pairing heaps (lazy, consolidate) for which decrease-key runs in O(log⁡log⁡n) amortized time and other operations have optimal amortized bounds, but no tight Θ(log⁡log⁡n) bound is known for the original data structure.

Although the asymptotic performance of pairing heaps is worse than other priority queue algorithms such as Fibonacci heaps, which perform decrease-key in O(1) amortized time, the performance in practice is excellent. Jones and Larkin, Sen, and Tarjan conducted experiments on pairing heaps and other heap data structures. They concluded that d-ary heaps such as binary heaps are faster than all other heap implementations when the decrease-key operation is not needed (and hence there is no need to externally track the location of nodes in the heap), but that when decrease-key is needed pairing heaps are often faster than d-ary heaps and almost always faster than other pointer-based heaps, including data structures like Fibonacci heaps that are theoretically more efficient. Chen et al. examined priority queues specifically for use with Dijkstra's algorithm and concluded that in normal cases using a d-ary heap without decrease-key (instead duplicating nodes on the heap and ignoring redundant instances) resulted in better performance, despite the inferior theoretical performance guarantees.

Structure

A pairing heap is either an empty heap, or a pairing tree consisting of a root element and a possibly empty list of pairing trees. The heap ordering property requires that parent of any node is no greater than the node itself. The following description assumes a purely functional heap that does not support the decrease-key operation.

<b id="mwfQ">type</b> PairingTree[Elem] = Heap(elem: Elem, subheaps: List[PairingTree[Elem]]) <b id="mwfg">type</b> PairingHeap[Elem] = Empty | PairingTree[Elem]

A pointer-based implementation for RAM machines, supporting decrease-key, can be achieved using three pointers per node, by representing the children of a node by a doubly-linked list: a pointer to the node's first child, one to its next sibling, and one to its previous sibling (or, for the leftmost sibling, to its parent). It can also be viewed as a variant of a Left-child right-sibling binary tree with an additional pointer to a node's parent (which represents its previous sibling or actual parent for the leftmost sibling). Alternatively, the previous-pointer can be omitted by letting the last child point back to the parent, if a single Boolean flag is added to indicate "end of list". This achieves a more compact structure at the expense of a constant overhead factor per operation.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication