Pairing heap on:  
[Wikipedia]  
[Google]  
[Amazon]
A pairing heap is a type of
heap data structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
with relatively simple implementation and excellent practical
amortized
In computer science, amortized analysis is a method for analyzing a given algorithm's complexity, or how much of a resource, especially time or memory, it takes to execute. The motivation for amortized analysis is that looking at the worst-case ...
performance, introduced by
Michael Fredman
Michael Lawrence Fredman is an emeritus professor at the Computer Science Department at Rutgers University, United States. He earned his Ph.D. degree from Stanford University in 1972 under the supervision of Donald Knuth. He was a member of the m ...
,
Robert Sedgewick,
Daniel Sleator
Daniel Dominic Kaplan Sleator (born 10 December 1953) is a Professor of Computer Science at Carnegie Mellon University, Pittsburgh, United States. In 1999, he won the ACM Paris Kanellakis Award (jointly with Robert Tarjan) for the splay tree da ...
, and
Robert Tarjan
Robert Endre Tarjan (born April 30, 1948) is an American computer scientist and mathematician. He is the discoverer of several graph algorithms, including Tarjan's off-line lowest common ancestors algorithm, and co-inventor of both splay trees a ...
in 1986.
Pairing heaps are
heap-ordered multiway
tree structures, and can be considered simplified
Fibonacci heap
In computer science, a Fibonacci heap is a data structure for priority queue operations, consisting of a collection of heap-ordered trees. It has a better amortized running time than many other priority queue data structures including the binar ...
s. They are considered a "robust choice" for implementing such algorithms as
Prim's MST algorithm,
and support the following operations (assuming a min-heap):
* ''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 , 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 (lazy, consolidate) 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 the asymptotic performance of pairing heaps is worse than other priority queue algorithms such as Fibonacci heap
In computer science, a Fibonacci heap is a data structure for priority queue operations, consisting of a collection of heap-ordered trees. It has a better amortized running time than many other priority queue data structures including the binar ...
s, which perform ''decrease-key'' in 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 heap
In computer science, a Fibonacci heap is a data structure for priority queue operations, consisting of a collection of heap-ordered trees. It has a better amortized running time than many other priority queue data structures including the binar ...
s 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.
type PairingTreelem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
= Heap(elem: Elem, subheaps: List airingTree[Elem)
_type_PairingHeaplem_
Lem_may_refer_to:
_Places
*__3836_Lem,_an_asteroid_named_after_Stanisław_Lem
*_,_a_municipality_in_Jutland
_People_Given_name_or_nickname
(Alphabetical_by_surname)
*_Lemuel_Lem_Barney_(born_1945),_American_football_player
*__Lem_Billings_(1916–_...
=_Empty_.html" ;"title="lem.html" ;"title="airingTree[Elem">airingTree[Elem)
type PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
= 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 singly-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). 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(heap: PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
-> 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(heap1, heap2: PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
-> PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
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
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
-> PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
return meld(Heap(elem, []), heap)
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
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
-> PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
if heap is Empty
error
else
return merge-pairs(heap.subheaps)
This uses the auxiliary function ''merge-pairs'':
function merge-pairs(list: List
airingTree[Elem) -> PairingHeap
lem
Lem may refer to:
Places
* 3836 Lem, an asteroid named after Stanisław Lem
* , a municipality in Jutland
People Given name or nickname
(Alphabetical by surname)
* Lemuel Lem Barney (born 1945), American football player
* Lem Billings (1916– ...
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(
1, H2, H3, H4, H5, H6, H7
Onekama ( ) is a village in Manistee County in the U.S. state of Michigan. The population was 411 at the 2010 census. The village is located on the shores of Portage Lake and is surrounded by Onekama Township. The town's name is derived from "On ...
=> meld(meld(H1, H2), merge-pairs(
3, H4, H5, H6, H7)
# meld H1 and H2 to H12, then the rest of the list
=> meld(H12, meld(meld(H3, H4), merge-pairs(
5, H6, H7))
# meld H3 and H4 to H34, then the rest of the list
=> meld(H12, meld(H34, meld(meld(H5, H6), merge-pairs(
7)))
# 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
Summary of running times
References
{{reflist, 30em
External links
* Louis Wasserman discusses pairing heaps and their implementation in
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
i
The Monad Reader, Issue 16(pp. 37–52).
Sartaj Sahni
Professor Sartaj Kumar Sahni (born July 22, 1949, in Pune, India) is a computer scientist based in the United States, and is one of the pioneers in the field of data structures. He is a distinguished professor in the Department of Computer and I ...
Heaps (data structures)
Amortized data structures