Documentation

Std.Data.PairingHeap

inductive Std.PairingHeapImp.Heap (α : Type u) :

A Heap is the nodes of the pairing heap. Each node have two pointers: child going to the first child of this node, and sibling goes to the next sibling of this tree. So it actually encodes a forest where each node has children node.child, node.child.sibling, node.child.sibling.sibling, etc.

Each edge in this forest denotes a le a b relation that has been checked, so the root is smaller than everything else under it.

Instances For
instance Std.PairingHeapImp.instReprHeap :
{α : Type u_1} → [inst : Repr α] → Repr (Std.PairingHeapImp.Heap α)
Equations
  • Std.PairingHeapImp.instReprHeap = { reprPrec := Std.PairingHeapImp.reprHeap✝ }

A node containing a single element a.

Equations

O(1). Is the heap empty?

Equations
@[specialize #[]]

O(1). Merge two heaps. Ignore siblings.

Equations
  • One or more equations did not get rendered due to their size.
@[specialize #[]]

Auxiliary for Heap.deleteMin: merge the forest in pairs.

Equations
@[inline]

O(1). Get the smallest element in the heap, including the passed in value a.

Equations
@[inline]

O(1). Get the smallest element in the heap, if it has an element.

Equations
@[inline]

Amortized O(log n). Find and remove the the minimum element from the heap.

Equations
@[inline]

Amortized O(log n). Get the tail of the pairing heap after removing the minimum element.

Equations
@[inline]

Amortized O(log n). Remove the minimum element of the heap.

Equations

A predicate says there is no more than one tree.

Instances For
Equations
  • One or more equations did not get rendered due to their size.
@[specialize #[]]
def Std.PairingHeapImp.Heap.foldM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (le : ααBool) (s : Std.PairingHeapImp.Heap α) (init : β) (f : βαm β) :
m β

O(n log n). Monadic fold over the elements of a heap in increasing order, by repeatedly pulling the minimum element out of the heap.

Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def Std.PairingHeapImp.Heap.fold {α : Type u_1} {β : Type u_2} (le : ααBool) (s : Std.PairingHeapImp.Heap α) (init : β) (f : βαβ) :
β

O(n log n). Fold over the elements of a heap in increasing order, by repeatedly pulling the minimum element out of the heap.

Equations
@[inline]
def Std.PairingHeapImp.Heap.toArray {α : Type u_1} (le : ααBool) (s : Std.PairingHeapImp.Heap α) :

O(n log n). Convert the heap to an array in increasing order.

Equations
@[inline]
def Std.PairingHeapImp.Heap.toList {α : Type u_1} (le : ααBool) (s : Std.PairingHeapImp.Heap α) :
List α

O(n log n). Convert the heap to a list in increasing order.

Equations
@[specialize #[]]
def Std.PairingHeapImp.Heap.foldTreeM {m : Type u_1 → Type u_2} {β : Type u_1} {α : Type u_3} [Monad m] (nil : β) (join : αββm β) :

O(n). Fold a monadic function over the tree structure to accumulate a value.

Equations
@[inline]
def Std.PairingHeapImp.Heap.foldTree {β : Type u_1} {α : Type u_2} (nil : β) (join : αβββ) (s : Std.PairingHeapImp.Heap α) :
β

O(n). Fold a function over the tree structure to accumulate a value.

Equations

O(n). Convert the heap to a list in arbitrary order.

Equations

O(n). Convert the heap to an array in arbitrary order.

Equations
def Std.PairingHeapImp.Heap.NodeWF {α : Type u_1} (le : ααBool) (a : α) :

The well formedness predicate for a heap node. It asserts that:

  • If a is added at the top to make the forest into a tree, the resulting tree is a le-min-heap (if le is well-behaved)
Equations
inductive Std.PairingHeapImp.Heap.WF {α : Type u_1} (le : ααBool) :

The well formedness predicate for a pairing heap. It asserts that:

  • There is no more than one tree.
  • It is a le-min-heap (if le is well-behaved)
theorem Std.PairingHeapImp.Heap.WF.merge_node :
∀ {α : Type u_1} {le : ααBool} {a₁ : α} {c₁ : Std.PairingHeapImp.Heap α} {a₂ : α} {c₂ s₁ s₂ : Std.PairingHeapImp.Heap α}, Std.PairingHeapImp.Heap.NodeWF le a₁ c₁Std.PairingHeapImp.Heap.NodeWF le a₂ c₂Std.PairingHeapImp.Heap.WF le (Std.PairingHeapImp.Heap.merge le (Std.PairingHeapImp.Heap.node a₁ c₁ s₁) (Std.PairingHeapImp.Heap.node a₂ c₂ s₂))
def Std.PairingHeap (α : Type u) (le : ααBool) :

A pairing heap is a data structure which supports the following primary operations:

The first two operations are known as a "priority queue", so this could be called a "mergeable priority queue". The standard choice for a priority queue is a binary heap, which supports insert and deleteMin in O(log n), but merge is O(n). With a PairingHeap, insert and merge are O(1), deleteMin is amortized O(log n).

Note that deleteMin may be O(n) in a single operation. So if you need an efficient persistent priority queue, you should use other data structures with better worst-case time.

Equations
Instances For
@[inline]
def Std.mkPairingHeap (α : Type u) (le : ααBool) :

O(1). Make a new empty pairing heap.

Equations
@[inline]
def Std.PairingHeap.empty {α : Type u} {le : ααBool} :

O(1). Make a new empty pairing heap.

Equations
instance Std.PairingHeap.instInhabitedPairingHeap {α : Type u} {le : ααBool} :
Equations
  • Std.PairingHeap.instInhabitedPairingHeap = { default := Std.PairingHeap.empty }
@[inline]
def Std.PairingHeap.isEmpty {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

O(1). Is the heap empty?

Equations
@[inline]
def Std.PairingHeap.size {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

O(n). The number of elements in the heap.

Equations
@[inline]
def Std.PairingHeap.singleton {α : Type u} {le : ααBool} (a : α) :

O(1). Make a new heap containing a.

Equations
@[inline]
def Std.PairingHeap.merge {α : Type u} {le : ααBool} :

O(1). Merge the contents of two heaps.

Equations
@[inline]
def Std.PairingHeap.insert {α : Type u} {le : ααBool} (a : α) (h : Std.PairingHeap α le) :

O(1). Add element a to the given heap h.

Equations
def Std.PairingHeap.ofList {α : Type u} (le : ααBool) (as : List α) :

O(n log n). Construct a heap from a list by inserting all the elements.

Equations
def Std.PairingHeap.ofArray {α : Type u} (le : ααBool) (as : Array α) :

O(n log n). Construct a heap from a list by inserting all the elements.

Equations
@[inline]
def Std.PairingHeap.deleteMin {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

Amortized O(log n). Remove and return the minimum element from the heap.

Equations
@[inline]
def Std.PairingHeap.head? {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

O(1). Returns the smallest element in the heap, or none if the heap is empty.

Equations
@[inline]
def Std.PairingHeap.head! {α : Type u} {le : ααBool} [Inhabited α] (b : Std.PairingHeap α le) :
α

O(1). Returns the smallest element in the heap, or panics if the heap is empty.

Equations
@[inline]
def Std.PairingHeap.headI {α : Type u} {le : ααBool} [Inhabited α] (b : Std.PairingHeap α le) :
α

O(1). Returns the smallest element in the heap, or default if the heap is empty.

Equations
@[inline]
def Std.PairingHeap.tail? {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

Amortized O(log n). Removes the smallest element from the heap, or none if the heap is empty.

Equations
@[inline]
def Std.PairingHeap.tail {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

Amortized O(log n). Removes the smallest element from the heap, if possible.

Equations
@[inline]
def Std.PairingHeap.toList {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :
List α

O(n log n). Convert the heap to a list in increasing order.

Equations
@[inline]
def Std.PairingHeap.toArray {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

O(n log n). Convert the heap to an array in increasing order.

Equations
@[inline]
def Std.PairingHeap.toListUnordered {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :
List α

O(n). Convert the heap to a list in arbitrary order.

Equations
@[inline]
def Std.PairingHeap.toArrayUnordered {α : Type u} {le : ααBool} (b : Std.PairingHeap α le) :

O(n). Convert the heap to an array in arbitrary order.

Equations