PairingHeap (priority_queue v1.1.0)

View Source

Pairing Heap implementation see:

http://en.wikipedia.org/wiki/Pairing_heap

A Pairing Heap is a type of heap structure with relatively simple implementation and excellent practical amortised performance.

Determining the precise asymptotic running time of pairing heaps has proved difficult, see the Wikipedia page referenced above for a more complete discussion. In particular practical performance of decrease-key is excellent (and initially conjectured to be O(1)), but at present it's known to be "no worse" then O(log n). However, no tight bound is known.

Operation find-min: Θ(1) delete-min: Θ(log n) insert: Θ(1) decrease-key: Θ(log n) - however, tight bound not known merge: Θ(1)

Guts: pairing heaps A pairing heap is either nil or a term {key, value, [sub_heaps]} where sub_heaps is a list of heaps.

TODO: Allow the comparison function to be specified

  Implement decrease_key

Summary

Functions

return the heap with the min item removed

True iff argument is an empty priority queue

Merge (meld) two heaps

Merge (meld) two heaps

min item in the heap

Create new empty heap. Optionally pass in initial key, value

Returns the min item, as well as the heap without the min item Equivalent to: {min(heap), delete_min(heap)}

Add element X to priority queue

Types

element()

@type element() :: {key(), value()}

key()

@type key() :: any()

t()

@type t() :: {key(), value(), list()} | nil

value()

@type value() :: any()

Functions

delete_min(arg1)

@spec delete_min(t()) :: t()

return the heap with the min item removed

iex> PairingHeap.new(1, "first") |> PairingHeap.delete_min |> PairingHeap.empty?
true

iex> PairingHeap.new(1, "first") |> PairingHeap.delete_min |> PairingHeap.delete_min |> PairingHeap.empty?
true

empty?(arg1)

@spec empty?(t()) :: boolean()

True iff argument is an empty priority queue

iex> PairingHeap.new |> PairingHeap.empty?
true

iex> PairingHeap.new(1, "first") |> PairingHeap.empty?
false

iex> PairingHeap.new(1, "first") |> PairingHeap.delete_min |> PairingHeap.empty?
true

meld(heap, heap)

@spec meld(t(), t()) :: t()

Merge (meld) two heaps

merge(h1, h2)

@spec merge(t(), t()) :: t()

Merge (meld) two heaps

min(heap, default \\ {nil, nil})

@spec min(t(), element()) :: element()

min item in the heap

new()

@spec new() :: t()

Create new empty heap. Optionally pass in initial key, value

new(key, value)

@spec new(key(), value()) :: t()

pop(heap, default \\ {nil, nil})

@spec pop(t(), element()) :: {element(), t()}

Returns the min item, as well as the heap without the min item Equivalent to: {min(heap), delete_min(heap)}

iex> PairingHeap.new(1, "first") |> PairingHeap.pop |> elem(0)
{1, "first"}

put(heap, key, value)

@spec put(t(), key(), value()) :: t()

Add element X to priority queue

iex> PairingHeap.new |> PairingHeap.put(1, "first") |> PairingHeap.pop |> elem(0)
{1, "first"}