Documentation

Std.Data.BinomialHeap.Basic

A HeapNode is one of the internal nodes of the binomial heap. It is always a perfect binary tree, with the depth of the tree stored in the Heap. However the interpretation of the two pointers is different: we view the child as 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
    Equations
    • Std.BinomialHeap.Imp.instReprHeapNode = { reprPrec := Std.BinomialHeap.Imp.reprHeapNode✝ }

    The "real size" of the node, counting up how many values of type α are stored. This is O(n) and is intended mainly for specification purposes. For a well formed HeapNode the size is always 2^n - 1 where n is the depth.

    Equations
    Instances For

      A node containing a single element a.

      Equations
      Instances For

        O(log n). The rank, or the number of trees in the forest. It is also the depth of the forest.

        Equations
        Instances For
          inductive Std.BinomialHeap.Imp.Heap (α : Type u) :

          A Heap is the top level structure in a binomial heap. It consists of a forest of HeapNodes with strictly increasing ranks.

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

            O(n). The "real size" of the heap, counting up how many values of type α are stored. This is intended mainly for specification purposes. Prefer Heap.size, which is the same for well formed heaps.

            Equations
            Instances For

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

              Equations
              Instances For
                @[inline]

                O(1). Is the heap empty?

                Equations
                Instances For
                  @[inline]

                  O(1). The heap containing a single value a.

                  Equations
                  Instances For

                    O(1). Auxiliary for Heap.merge: Is the minimum rank in Heap strictly larger than n?

                    Equations
                    Instances For
                      Equations
                      • One or more equations did not get rendered due to their size.

                      O(log n). The number of trees in the forest.

                      Equations
                      Instances For
                        @[inline]
                        def Std.BinomialHeap.Imp.combine {α : Type u_1} (le : ααBool) (a₁ : α) (a₂ : α) (n₁ : Std.BinomialHeap.Imp.HeapNode α) (n₂ : Std.BinomialHeap.Imp.HeapNode α) :

                        O(1). Auxiliary for Heap.merge: combines two heap nodes of the same rank into one with the next larger rank.

                        Equations
                        Instances For
                          @[specialize #[]]

                          Merge two forests of binomial trees. The forests are assumed to be ordered by rank and merge maintains this invariant.

                          Equations
                          Instances For

                            O(log n). Convert a HeapNode to a Heap by reversing the order of the nodes along the sibling spine.

                            Equations
                            Instances For
                              @[specialize #[]]
                              def Std.BinomialHeap.Imp.Heap.headD {α : Type u_1} (le : ααBool) (a : α) :

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

                              Equations
                              Instances For
                                @[inline]
                                def Std.BinomialHeap.Imp.Heap.head? {α : Type u_1} (le : ααBool) :

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

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  structure Std.BinomialHeap.Imp.FindMin (α : Type u_1) :
                                  Type u_1

                                  The return type of FindMin, which encodes various quantities needed to reconstruct the tree in deleteMin.

                                  Instances For
                                    @[specialize #[]]

                                    O(log n). Find the minimum element, and return a data structure FindMin with information needed to reconstruct the rest of the binomial heap.

                                    Equations
                                    Instances For

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

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[inline]

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

                                        Equations
                                        Instances For
                                          @[inline]

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

                                          Equations
                                          Instances For
                                            @[specialize #[]]
                                            def Std.BinomialHeap.Imp.Heap.foldM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (le : ααBool) (s : Std.BinomialHeap.Imp.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 Std.BinomialHeap.Imp.Heap.fold {α : Type u_1} {β : Type u_2} (le : ααBool) (s : Std.BinomialHeap.Imp.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 Std.BinomialHeap.Imp.Heap.toArray {α : Type u_1} (le : ααBool) (s : Std.BinomialHeap.Imp.Heap α) :

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

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

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

                                                  Equations
                                                  Instances For
                                                    @[specialize #[]]
                                                    def Std.BinomialHeap.Imp.HeapNode.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
                                                    Instances For
                                                      @[specialize #[]]
                                                      def Std.BinomialHeap.Imp.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
                                                      Instances For
                                                        @[inline]
                                                        def Std.BinomialHeap.Imp.Heap.foldTree {β : Type u_1} {α : Type u_2} (nil : β) (join : αβββ) (s : Std.BinomialHeap.Imp.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 Std.BinomialHeap.Imp.HeapNode.WF {α : 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)
                                                              • When interpreting child and sibling as left and right children of a binary tree, it is a perfect binary tree with depth r
                                                              Equations
                                                              Instances For
                                                                def Std.BinomialHeap.Imp.Heap.WF {α : Type u_1} (le : ααBool) (n : Nat) :

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

                                                                • It consists of a list of well formed trees with the specified ranks
                                                                • The ranks are in strictly increasing order, and all are at least n
                                                                Equations
                                                                Instances For
                                                                  theorem Std.BinomialHeap.Imp.Heap.WF.nil :
                                                                  ∀ {α : Type u_1} {le : ααBool} {n : Nat}, Std.BinomialHeap.Imp.Heap.WF le n Std.BinomialHeap.Imp.Heap.nil
                                                                  theorem Std.BinomialHeap.Imp.Heap.WF.of_le {n : Nat} {n' : Nat} :
                                                                  ∀ {α : Type u_1} {le : ααBool} {s : Std.BinomialHeap.Imp.Heap α}, n n'Std.BinomialHeap.Imp.Heap.WF le n' sStd.BinomialHeap.Imp.Heap.WF le n s
                                                                  structure Std.BinomialHeap.Imp.FindMin.WF {α : Type u_1} (le : ααBool) (res : Std.BinomialHeap.Imp.FindMin α) :

                                                                  The well formedness predicate for a FindMin value. This is not actually a predicate, as it contains an additional data value rank corresponding to the rank of the returned node, which is omitted from findMin.

                                                                  Instances For

                                                                    The conditions under which findMin is well-formed.

                                                                    Equations
                                                                    Instances For
                                                                      def Std.BinomialHeap (α : Type u) (le : ααBool) :

                                                                      A binomial 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 BinomialHeap, all three operations are O(log n).

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

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

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

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

                                                                          Equations
                                                                          Instances For
                                                                            Equations
                                                                            • Std.BinomialHeap.instEmptyCollectionBinomialHeap = { emptyCollection := Std.BinomialHeap.empty }
                                                                            instance Std.BinomialHeap.instInhabitedBinomialHeap {α : Type u} {le : ααBool} :
                                                                            Equations
                                                                            • Std.BinomialHeap.instInhabitedBinomialHeap = { default := Std.BinomialHeap.empty }
                                                                            @[inline]
                                                                            def Std.BinomialHeap.isEmpty {α : Type u} {le : ααBool} (b : Std.BinomialHeap α le) :

                                                                            O(1). Is the heap empty?

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

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

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

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

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

                                                                                  O(log n). Merge the contents of two heaps.

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

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

                                                                                    Equations
                                                                                    Instances For
                                                                                      def Std.BinomialHeap.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 Std.BinomialHeap.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 Std.BinomialHeap.deleteMin {α : Type u} {le : ααBool} (b : Std.BinomialHeap α le) :

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

                                                                                          Equations
                                                                                          Instances For
                                                                                            instance Std.BinomialHeap.instStreamBinomialHeap {α : Type u} {le : ααBool} :
                                                                                            Equations
                                                                                            • Std.BinomialHeap.instStreamBinomialHeap = { next? := Std.BinomialHeap.deleteMin }
                                                                                            def Std.BinomialHeap.forIn {α : Type u} {le : ααBool} {m : Type u_1 → Type u_2} {β : Type u_1} [Monad m] (b : Std.BinomialHeap α le) (x : β) (f : αβm (ForInStep β)) :
                                                                                            m β

                                                                                            O(n log n). Implementation of for x in (b : BinomialHeap α le) ... notation, which iterates over the elements in the heap in increasing order.

                                                                                            Equations
                                                                                            Instances For
                                                                                              instance Std.BinomialHeap.instForInBinomialHeap {α : Type u} {le : ααBool} {m : Type u_1 → Type u_2} :
                                                                                              ForIn m (Std.BinomialHeap α le) α
                                                                                              Equations
                                                                                              • Std.BinomialHeap.instForInBinomialHeap = { forIn := fun {β : Type u_1} [Monad m] => Std.BinomialHeap.forIn }
                                                                                              @[inline]
                                                                                              def Std.BinomialHeap.head? {α : Type u} {le : ααBool} (b : Std.BinomialHeap α le) :

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

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

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

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

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

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

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

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

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

                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        @[inline]
                                                                                                        def Std.BinomialHeap.foldM {α : Type u} {le : ααBool} {m : Type u_1 → Type u_2} {β : Type u_1} [Monad m] (b : Std.BinomialHeap α le) (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
                                                                                                        Instances For
                                                                                                          @[inline]
                                                                                                          def Std.BinomialHeap.fold {α : Type u} {le : ααBool} {β : Type u_1} (b : Std.BinomialHeap α le) (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 Std.BinomialHeap.toList {α : Type u} {le : ααBool} (b : Std.BinomialHeap α le) :
                                                                                                            List α

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

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

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

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

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

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

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

                                                                                                                  Equations
                                                                                                                  Instances For