Documentation

Batteries.Data.PairingHeap

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.

  • nil {α : Type u} : Heap α

    An empty forest, which has depth 0.

  • node {α : Type u} (a : α) (child sibling : Heap α) : Heap α

    A forest consists of a root a, a forest child elements greater than a, and another forest sibling.

Instances For
    def Batteries.PairingHeapImp.instReprHeap.repr {α✝ : Type u_1} [Repr α✝] :
    Heap α✝ → Nat → Std.Format
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

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

      Equations
      Instances For

        O(1). Is the heap empty?

        Equations
        Instances For
          @[specialize #[]]
          def Batteries.PairingHeapImp.Heap.merge {α : Type u_1} (le : α → α → Bool) :
          Heap α → Heap α → Heap α

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

          Equations
          Instances For
            @[specialize #[]]
            def Batteries.PairingHeapImp.Heap.combine {α : Type u_1} (le : α → α → Bool) :
            Heap α → Heap α

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

            Equations
            Instances For
              @[inline]
              def Batteries.PairingHeapImp.Heap.headD {α : Type u_1} (a : α) :
              Heap α → α

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

              Equations
              Instances For
                @[inline]

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

                Equations
                Instances For
                  @[inline]
                  def Batteries.PairingHeapImp.Heap.deleteMin {α : Type u_1} (le : α → α → Bool) :
                  Heap α → Option (α × Heap α)

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

                  Equations
                  Instances For
                    @[inline]
                    def Batteries.PairingHeapImp.Heap.tail? {α : Type u_1} (le : α → α → Bool) (h : Heap α) :

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

                    Equations
                    Instances For
                      @[inline]
                      def Batteries.PairingHeapImp.Heap.tail {α : Type u_1} (le : α → α → Bool) (h : Heap α) :
                      Heap α

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

                      Equations
                      Instances For

                        A predicate says there is no more than one tree.

                        Instances For
                          theorem Batteries.PairingHeapImp.Heap.noSibling_merge {α : Type u_1} (le : α → α → Bool) (s₁ s₂ : Heap α) :
                          (merge le s₁ s₂).NoSibling
                          theorem Batteries.PairingHeapImp.Heap.noSibling_combine {α : Type u_1} (le : α → α → Bool) (s : Heap α) :
                          theorem Batteries.PairingHeapImp.Heap.noSibling_deleteMin {α : Type u_1} {le : α → α → Bool} {a : α} {s' s : Heap α} (eq : deleteMin le s = some (a, s')) :
                          theorem Batteries.PairingHeapImp.Heap.noSibling_tail? {α : Type u_1} {le : α → α → Bool} {s' s : Heap α} :
                          tail? le s = some s' → s'.NoSibling
                          theorem Batteries.PairingHeapImp.Heap.noSibling_tail {α : Type u_1} (le : α → α → Bool) (s : Heap α) :
                          theorem Batteries.PairingHeapImp.Heap.size_merge_node {α : Type u_1} (le : α → α → Bool) (a₁ : α) (c₁ s₁ : Heap α) (a₂ : α) (c₂ s₂ : Heap α) :
                          (merge le (node a₁ c₁ s₁) (node a₂ c₂ s₂)).size = c₁.size + c₂.size + 2
                          theorem Batteries.PairingHeapImp.Heap.size_merge {α : Type u_1} (le : α → α → Bool) {s₁ s₂ : Heap α} (h₁ : s₁.NoSibling) (h₂ : s₂.NoSibling) :
                          (merge le s₁ s₂).size = s₁.size + s₂.size
                          @[irreducible]
                          theorem Batteries.PairingHeapImp.Heap.size_combine {α : Type u_1} (le : α → α → Bool) (s : Heap α) :
                          (combine le s).size = s.size
                          theorem Batteries.PairingHeapImp.Heap.size_deleteMin {α : Type u_1} {le : α → α → Bool} {a : α} {s' s : Heap α} (h : s.NoSibling) (eq : deleteMin le s = some (a, s')) :
                          s.size = s'.size + 1
                          theorem Batteries.PairingHeapImp.Heap.size_tail? {α : Type u_1} {le : α → α → Bool} {s' s : Heap α} (h : s.NoSibling) :
                          tail? le s = some s' → s.size = s'.size + 1
                          theorem Batteries.PairingHeapImp.Heap.size_tail {α : Type u_1} (le : α → α → Bool) {s : Heap α} (h : s.NoSibling) :
                          (tail le s).size = s.size - 1
                          theorem Batteries.PairingHeapImp.Heap.size_deleteMin_lt {α : Type u_1} {le : α → α → Bool} {a : α} {s' s : Heap α} (eq : deleteMin le s = some (a, s')) :
                          s'.size < s.size
                          theorem Batteries.PairingHeapImp.Heap.size_tail?_lt {α : Type u_1} {le : α → α → Bool} {s' s : Heap α} :
                          tail? le s = some s' → s'.size < s.size
                          @[irreducible, specialize #[]]
                          def Batteries.PairingHeapImp.Heap.foldM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (le : α → α → Bool) (s : 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.
                          Instances For
                            @[inline]
                            def Batteries.PairingHeapImp.Heap.fold {α : Type u_1} {β : Type u_2} (le : α → α → Bool) (s : 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
                            Instances For
                              @[inline]
                              def Batteries.PairingHeapImp.Heap.toArray {α : Type u_1} (le : α → α → Bool) (s : Heap α) :

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

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

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

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

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

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

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

                                    Equations
                                    Instances For

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

                                      Equations
                                      Instances For

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

                                        Equations
                                        Instances For
                                          def Batteries.PairingHeapImp.Heap.NodeWF {α : Type u_1} (le : α → α → Bool) (a : α) :
                                          Heap α → Prop

                                          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
                                          Instances For
                                            inductive Batteries.PairingHeapImp.Heap.WF {α : Type u_1} (le : α → α → Bool) :
                                            Heap α → Prop

                                            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)
                                            Instances For
                                              theorem Batteries.PairingHeapImp.Heap.WF.singleton {α✝ : Type u_1} {a : α✝} {le : α✝ → α✝ → Bool} :
                                              theorem Batteries.PairingHeapImp.Heap.WF.merge_node {α✝ : Type u_1} {le : α✝ → α✝ → Bool} {a₁ : α✝} {c₁ : Heap α✝} {a₂ : α✝} {c₂ s₁ s₂ : Heap α✝} (h₁ : NodeWF le a₁ c₁) (h₂ : NodeWF le a₂ c₂) :
                                              WF le (Heap.merge le (Heap.node a₁ c₁ s₁) (Heap.node a₂ c₂ s₂))
                                              theorem Batteries.PairingHeapImp.Heap.WF.merge {α✝ : Type u_1} {le : α✝ → α✝ → Bool} {s₁ s₂ : Heap α✝} (h₁ : WF le s₁) (h₂ : WF le s₂) :
                                              WF le (Heap.merge le s₁ s₂)
                                              theorem Batteries.PairingHeapImp.Heap.WF.combine {α✝ : Type u_1} {le : α✝ → α✝ → Bool} {s : Heap α✝} {a : α✝} (h : NodeWF le a s) :
                                              WF le (Heap.combine le s)
                                              theorem Batteries.PairingHeapImp.Heap.WF.deleteMin {α : Type u_1} {le : α → α → Bool} {a : α} {s' s : Heap α} (h : WF le s) (eq : Heap.deleteMin le s = some (a, s')) :
                                              WF le s'
                                              theorem Batteries.PairingHeapImp.Heap.WF.tail? {α : Type u_1} {s : Heap α} {le : α → α → Bool} {tl : Heap α} (hwf : WF le s) :
                                              Heap.tail? le s = some tl → WF le tl
                                              theorem Batteries.PairingHeapImp.Heap.WF.tail {α : Type u_1} {s : Heap α} {le : α → α → Bool} (hwf : WF le s) :
                                              WF le (Heap.tail le s)
                                              theorem Batteries.PairingHeapImp.Heap.deleteMin_fst {α : Type u_1} {s : Heap α} {le : α → α → Bool} :
                                              Option.map (fun (x : α × Heap α) => x.fst) (deleteMin le s) = s.head?
                                              def Batteries.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 Batteries.mkPairingHeap (α : Type u) (le : α → α → Bool) :

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

                                                Equations
                                                Instances For
                                                  @[inline]
                                                  def Batteries.PairingHeap.empty {α : Type u} {le : α → α → Bool} :

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

                                                  Equations
                                                  Instances For
                                                    @[implicit_reducible]
                                                    instance Batteries.PairingHeap.instInhabited {α : Type u} {le : α → α → Bool} :
                                                    Equations
                                                    @[inline]
                                                    def Batteries.PairingHeap.isEmpty {α : Type u} {le : α → α → Bool} (b : PairingHeap α le) :

                                                    O(1). Is the heap empty?

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

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

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

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

                                                        Equations
                                                        Instances For
                                                          @[inline]
                                                          def Batteries.PairingHeap.merge {α : Type u} {le : α → α → Bool} :
                                                          PairingHeap α le → PairingHeap α le → PairingHeap α le

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

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

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

                                                            Equations
                                                            Instances For
                                                              def Batteries.PairingHeap.ofList {α : Type u} (le : α → α → Bool) (as : List α) :

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

                                                              Equations
                                                              Instances For
                                                                def Batteries.PairingHeap.ofArray {α : Type u} (le : α → α → Bool) (as : Array α) :

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

                                                                Equations
                                                                Instances For
                                                                  @[inline]
                                                                  def Batteries.PairingHeap.deleteMin {α : Type u} {le : α → α → Bool} (b : PairingHeap α le) :
                                                                  Option (α × PairingHeap α le)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                                                    Equations
                                                                                    Instances For