Pairing heap


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.
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, and support the following operations :
The analysis of pairing heaps' time complexity was initially inspired by that of splay trees.
The amortized time per delete-min is, and the operations find-min, meld, and insert run in amortized 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 for some sequences of operations.
Using a different amortization argument, Pettie then proved that insert, meld, and decrease-key all run in amortized time, which is.
Elmasry later introduced elaborations of pairing heaps for which decrease-key runs in amortized time and other operations have optimal amortized bounds, but no tight bound is known for the original data structure.
Although this is worse than other priority queue algorithms such as Fibonacci heaps, which perform decrease-key in amortized time, the performance in practice is excellent. Stasko and Vitter,
Moret and Shapiro,
and Larkin, Sen, and Tarjan
conducted experiments on pairing heaps and other heap data structures. They concluded that pairing heaps are often faster in practice than array-based binary heaps and d-ary heaps, and almost always faster in practice than other pointer-based heaps, including data structures like Fibonacci heaps that are theoretically more efficient.

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 = Heap
type PairingHeap = Empty | PairingTree
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 singly-linked list: a pointer to the node's first child, one to its next sibling, and one to its previous 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.

Operations

find-min

The function find-min simply returns the root element of the heap:
function find-min -> Elem
if heap is Empty
error
else
return heap.elem

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 -> PairingHeap
if heap1 is Empty
return heap2
elsif heap2 is Empty
return heap1
elsif heap1.elem < heap2.elem
return Heap
else
return Heap

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 -> PairingHeap
return meld

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 from left to right and then melds the resulting list of heaps from right to left:
function delete-min -> PairingHeap
if heap is Empty
error
else
return merge-pairs
This uses the auxiliary function merge-pairs:
function merge-pairs -> PairingHeap
if length 0
return Empty
elsif length 1
return list
else
return meld, merge-pairs)
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
=> meld, merge-pairs)
# meld H1 and H2 to H12, then the rest of the list
=> meld, merge-pairs))
# meld H3 and H4 to H34, then the rest of the list
=> meld, merge-pairs)))
# meld H5 and H6 to H56, then the rest of the list
=> meld
# switch direction, meld the last two resulting heaps, giving H567
=> meld
# meld the last two resulting heaps, giving H34567
=> meld
# finally, meld the first pair with the result of merging the rest
=> H1234567

Summary of running times