Pairing heap explained

A pairing heap is a type of heap data structure with relatively simple implementation and excellent practical amortized performance, introduced by Michael Fredman, Robert Sedgewick, Daniel Sleator, and Robert Tarjan in 1986.[1] Pairing heaps are heap-ordered multiway tree structures, and can be considered simplified Fibonacci heaps. They are considered a "robust choice" for implementing such algorithms as Prim's MST algorithm,[2] and support the following operations (assuming a min-heap):

The analysis of pairing heaps' time complexity was initially inspired by that of splay trees.[1] The amortized time per delete-min is, and the operations find-min, meld, and insert run in 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, but Fredman proved that the amortized time per decrease-key is at least

\Omega(loglogn)

for some sequences of operations.[3] Using a different amortization argument, Pettie then proved that insert, meld, and decrease-key all run in

O(22\sqrt{loglog

}) amortized time, which is

o(logn)

.Elmasry later introduced elaborations of pairing heaps (lazy, consolidate) for which decrease-key runs in

O(loglogn)

amortized time and other operations have optimal amortized bounds,[4] but no tight

\Theta(loglogn)

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[5] and Larkin, Sen, and Tarjanconducted 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.[6] 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.

type PairingTree[Elem] = Heap(elem: Elem, subheaps: List[PairingTree[Elem]]) type 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.[1]

Operations

Meld

Melding with an empty heap returns the other heap, otherwise a new heap is returned that has the minimum of the two root elements as its root element and just adds the heap with the larger root to the list of subheaps:

function meld(heap1, heap2: PairingHeap[Elem]) -> PairingHeap[Elem] if heap1 is Empty return heap2 elsif heap2 is Empty return heap1 elsif heap1.elem < heap2.elem return Heap(heap1.elem, heap2 :: heap1.subheaps) else return Heap(heap2.elem, heap1 :: heap2.subheaps)

Insert

The easiest way to insert an element into a heap is to meld the heap with a new heap containing just this element and an empty list of subheaps:

function insert(elem: Elem, heap: PairingHeap[Elem]) -> PairingHeap[Elem] return meld(Heap(elem, []), heap)

Find-min

The function find-min simply returns the root element of the heap:

function find-min(heap: PairingHeap[Elem]) -> Elem if heap is Empty error else return heap.elem

Delete-min

The only non-trivial fundamental operation is the deletion of the minimum element from the heap. This requires performing repeated melds of its children until only one tree remains. The standard strategy first melds the subheaps in pairs (this is the step that gave this data structure its name) from left to right and then melds the resulting list of heaps from right to left:

function delete-min(heap: PairingHeap[Elem]) -> PairingHeap[Elem] if heap is Empty error else return merge-pairs(heap.subheaps)

This uses the auxiliary function merge-pairs:

function merge-pairs(list: List[PairingTree[Elem]]) -> PairingHeap[Elem] if length(list)

0 return Empty elsif length(list)

1 return list[0] else return meld(meld(list[0], list[1]), merge-pairs(list[2..]))

That this does indeed implement the described two-pass left-to-right then right-to-left merging strategy can be seen from this reduction:

merge-pairs([H1, H2, H3, H4, H5, H6, H7]) => meld(meld(H1, H2), merge-pairs([H3, H4, H5, H6, H7])) # meld H1 and H2 to H12, then the rest of the list => meld(H12, meld(meld(H3, H4), merge-pairs([H5, H6, H7]))) # meld H3 and H4 to H34, then the rest of the list => meld(H12, meld(H34, meld(meld(H5, H6), merge-pairs([H7])))) # meld H5 and H6 to H56, then the rest of the list => meld(H12, meld(H34, meld(H56, H7))) # switch direction, meld the last two resulting heaps, giving H567 => meld(H12, meld(H34, H567)) # meld the last two resulting heaps, giving H34567 => meld(H12, H34567) # finally, meld the first pair with the result of merging the rest => H1234567

External links

Notes and References

  1. Fredman . Michael L. . Michael Fredman . Sedgewick . Robert . Robert Sedgewick (computer scientist) . Sleator . Daniel D. . Daniel Sleator . Tarjan . Robert E. . Robert Tarjan . 10.1007/BF01840439 . 1–4 . Algorithmica . 111–129 . The pairing heap: a new form of self-adjusting heap . 1 . 1986. 23664143 .
  2. Book: Mehlhorn . Kurt. Kurt Mehlhorn . Peter . Sanders. Peter Sanders (computer scientist) . Algorithms and Data Structures: The Basic Toolbox . Springer . 2008 . 231.
  3. Fredman . Michael L. . Michael Fredman . 10.1145/320211.320214 . 4 . Journal of the ACM . 473–501 . On the efficiency of pairing heaps and related data structures . 46 . 1999 . 16115266 . 2011-05-03 . https://web.archive.org/web/20110721051135/http://wwwens.uqac.ca/azinflou/Fichiers840/EfficiencyPairingHeap.pdf . 2011-07-21 . dead .
  4. Elmasry . Amr . Toward Optimal Self-Adjusting Heaps . 10.1145/3147138 . ACM Transactions on Algorithms . 13 . 4 . 1–14 . November 2017. 1182235 .
  5. Jones . Douglas W. . 1986 . An empirical comparison of priority-queue and event-set implementations . Communications of the ACM . 29 . 4 . 300–311 . 10.1.1.117.9380 . 10.1145/5684.5686. 43650389 .
  6. Chen . Mo . Chowdhury. Rezaul Alam . Ramachandran . Vijaya . Roche . David Lan . Tong . Lingling . Priority Queues and Dijkstra's Algorithm . TR-07-54 . University of Texas . 12 October 2007 . 2007.