Documentation

Std.Data.UnionFind.Basic

structure Std.UFNode :

Union-find node type

  • parent : Nat

    Parent of node

  • rank : Nat

    Rank of node

Instances For
    def Std.UnionFind.panicWith {α : Type u_1} (v : α) (msg : String) :
    α

    Panic with return value

    Equations
    Instances For
      @[simp]
      theorem Std.UnionFind.panicWith_eq {α : Type u_1} (v : α) (msg : String) :

      Parent of a union-find node, defaults to self when the node is a root

      Equations
      Instances For

        Rank of a union-find node, defaults to 0 when the node is a root

        Equations
        Instances For
          theorem Std.UnionFind.parentD_eq {arr : Array Std.UFNode} {i : Fin (Array.size arr)} :
          Std.UnionFind.parentD arr i = (Array.get arr i).parent
          theorem Std.UnionFind.parentD_eq' {arr : Array Std.UFNode} {i : Nat} (h : i < Array.size arr) :
          Std.UnionFind.parentD arr i = (Array.get arr { val := i, isLt := h }).parent
          theorem Std.UnionFind.rankD_eq {arr : Array Std.UFNode} {i : Fin (Array.size arr)} :
          Std.UnionFind.rankD arr i = (Array.get arr i).rank
          theorem Std.UnionFind.rankD_eq' {arr : Array Std.UFNode} {i : Nat} (h : i < Array.size arr) :
          Std.UnionFind.rankD arr i = (Array.get arr { val := i, isLt := h }).rank
          theorem Std.UnionFind.parentD_set {arr : Array Std.UFNode} {x : Fin (Array.size arr)} {v : Std.UFNode} {i : Nat} :
          Std.UnionFind.parentD (Array.set arr x v) i = if x = i then v.parent else Std.UnionFind.parentD arr i
          theorem Std.UnionFind.rankD_set {arr : Array Std.UFNode} {x : Fin (Array.size arr)} {v : Std.UFNode} {i : Nat} :
          Std.UnionFind.rankD (Array.set arr x v) i = if x = i then v.rank else Std.UnionFind.rankD arr i
          structure Std.UnionFind :

          Union-find data structure #

          The UnionFind structure is an implementation of disjoint-set data structure that uses path compression to make the primary operations run in amortized nearly linear time. The nodes of a UnionFind structure s are natural numbers smaller than s.size. The structure associates with a canonical representative from its equivalence class. The structure can be extended using the push operation and equivalence classes can be updated using the union operation.

          The main operations for UnionFind are:

          • empty/mkEmpty are used to create a new empty structure.
          • size returns the size of the data structure.
          • push adds a new node to a structure, unlinked to any other node.
          • union links two nodes of the data structure, joining their equivalence classes, and performs path compression.
          • find returns the canonical representative of a node and updates the data structure using path compression.
          • root returns the canonical representative of a node without altering the data structure.
          • checkEquiv checks whether two nodes have the same canonical representative and updates the structure using path compression.

          Most use cases should prefer find over root to benefit from the speedup from path-compression.

          The main operations use Fin s.size to represent nodes of the union-find structure. Some alternatives are provided:

          The noncomputable relation UnionFind.Equiv is provided to use the equivalence relation from a UnionFind structure in the context of proofs.

          Instances For
            @[inline, reducible]

            Size of union-find structure.

            Equations
            Instances For

              Create an empty union-find structure with specific capacity

              Equations
              Instances For

                Empty union-find structure

                Equations
                Instances For
                  @[inline, reducible]
                  abbrev Std.UnionFind.parent (self : Std.UnionFind) (i : Nat) :

                  Parent of union-find node

                  Equations
                  Instances For
                    theorem Std.UnionFind.parent'_lt (self : Std.UnionFind) (i : Fin (Std.UnionFind.size self)) :
                    (Array.get self.arr i).parent < Std.UnionFind.size self
                    @[inline, reducible]
                    abbrev Std.UnionFind.rank (self : Std.UnionFind) (i : Nat) :

                    Rank of union-find node

                    Equations
                    Instances For
                      theorem Std.UnionFind.rank'_lt (self : Std.UnionFind) (i : Fin (Std.UnionFind.size self)) :
                      (Array.get self.arr i).parent iStd.UnionFind.rank self i < Std.UnionFind.rank self (Array.get self.arr i).parent
                      noncomputable def Std.UnionFind.rankMax (self : Std.UnionFind) :

                      Maximum rank of nodes in a union-find structure

                      Equations
                      Instances For
                        theorem Std.UnionFind.rank'_lt_rankMax.go {l : List Std.UFNode} {x : Std.UFNode} :
                        x lx.rank List.foldr (fun (x : Std.UFNode) => max x.rank) 0 l
                        theorem Std.UnionFind.push_rankD {i : Nat} (arr : Array Std.UFNode) :
                        Std.UnionFind.rankD (Array.push arr { parent := Array.size arr, rank := 0 }) i = Std.UnionFind.rankD arr i

                        Add a new node to a union-find structure, unlinked with any other nodes

                        Equations
                        Instances For

                          Root of a union-find node.

                          Equations
                          Instances For
                            def Std.UnionFind.rootN {n : Nat} (self : Std.UnionFind) (x : Fin n) (h : n = Std.UnionFind.size self) :
                            Fin n

                            Root of a union-find node.

                            Equations
                            Instances For

                              Root of a union-find node. Panics if index is out of bounds.

                              Equations
                              Instances For

                                Root of a union-find node. Returns input if index is out of bounds.

                                Equations
                                Instances For
                                  theorem Std.UnionFind.parent_root (self : Std.UnionFind) (x : Fin (Std.UnionFind.size self)) :
                                  (Array.get self.arr (Std.UnionFind.root self x)).parent = (Std.UnionFind.root self x)
                                  structure Std.UnionFind.FindAux (n : Nat) :

                                  Auxiliary data structure for find operation

                                  Instances For

                                    Auxiliary function for find operation

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      theorem Std.UnionFind.findAux_s {self : Std.UnionFind} {x : Fin (Std.UnionFind.size self)} :
                                      (Std.UnionFind.findAux self x).s = if (Array.get self.arr x).parent = x then self.arr else Array.modify (Std.UnionFind.findAux self { val := (Array.get self.arr x).parent, isLt := }).s x fun (s : Std.UFNode) => { parent := Std.UnionFind.rootD self x, rank := s.rank }
                                      theorem Std.UnionFind.parentD_findAux {i : Nat} {self : Std.UnionFind} {x : Fin (Std.UnionFind.size self)} :
                                      Std.UnionFind.parentD (Std.UnionFind.findAux self x).s i = if i = x then Std.UnionFind.rootD self x else Std.UnionFind.parentD (Std.UnionFind.findAux self { val := (Array.get self.arr x).parent, isLt := }).s i

                                      Find root of a union-find node, updating the structure using path compression.

                                      Equations
                                      Instances For
                                        def Std.UnionFind.findN {n : Nat} (self : Std.UnionFind) (x : Fin n) (h : n = Std.UnionFind.size self) :

                                        Find root of a union-find node, updating the structure using path compression.

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

                                          Find root of a union-find node, updating the structure using path compression. Panics if index is out of bounds.

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

                                            Find root of a union-find node, updating the structure using path compression. Returns inputs unchanged when index is out of bounds.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              @[simp]
                                              theorem Std.UnionFind.find_root_2 (self : Std.UnionFind) (x : Fin (Std.UnionFind.size self)) :
                                              (Std.UnionFind.find self x).snd.val = Std.UnionFind.rootD self x

                                              Link two union-find nodes

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                theorem Std.UnionFind.setParentBump_rankD_lt {arr' : Array Std.UFNode} {arr : Array Std.UFNode} {x : Fin (Array.size arr)} {y : Fin (Array.size arr)} (hroot : (Array.get arr x).rank < (Array.get arr y).rank (Array.get arr y).parent = y) (H : (Array.get arr x).rank (Array.get arr y).rank) {i : Nat} (rankD_lt : Std.UnionFind.parentD arr i iStd.UnionFind.rankD arr i < Std.UnionFind.rankD arr (Std.UnionFind.parentD arr i)) (hP : Std.UnionFind.parentD arr' i = if x = i then y else Std.UnionFind.parentD arr i) (hR : ∀ {i : Nat}, Std.UnionFind.rankD arr' i = if y = i (Array.get arr x).rank = (Array.get arr y).rank then (Array.get arr y).rank + 1 else Std.UnionFind.rankD arr i) :
                                                theorem Std.UnionFind.setParent_rankD_lt {arr : Array Std.UFNode} {x : Fin (Array.size arr)} {y : Fin (Array.size arr)} (h : (Array.get arr x).rank < (Array.get arr y).rank) {i : Nat} (rankD_lt : Std.UnionFind.parentD arr i iStd.UnionFind.rankD arr i < Std.UnionFind.rankD arr (Std.UnionFind.parentD arr i)) :
                                                let arr' := Array.set arr x { parent := y, rank := (Array.get arr x).rank }; Std.UnionFind.parentD arr' i iStd.UnionFind.rankD arr' i < Std.UnionFind.rankD arr' (Std.UnionFind.parentD arr' i)
                                                @[simp]
                                                def Std.UnionFind.linkN {n : Nat} (self : Std.UnionFind) (x : Fin n) (y : Fin n) (yroot : Std.UnionFind.parent self y = y) (h : n = Std.UnionFind.size self) :

                                                Link a union-find node to a root node.

                                                Equations
                                                Instances For
                                                  def Std.UnionFind.link! (self : Std.UnionFind) (x : Nat) (y : Nat) (yroot : Std.UnionFind.parent self y = y) :

                                                  Link a union-find node to a root node. Panics if either index is out of bounds.

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

                                                    Link two union-find nodes, uniting their respective classes.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      def Std.UnionFind.unionN {n : Nat} (self : Std.UnionFind) (x : Fin n) (y : Fin n) (h : n = Std.UnionFind.size self) :

                                                      Link two union-find nodes, uniting their respective classes.

                                                      Equations
                                                      Instances For

                                                        Link two union-find nodes, uniting their respective classes. Panics if either index is out of bounds.

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

                                                          Check whether two union-find nodes are equivalent, updating structure using path compression.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For
                                                            def Std.UnionFind.checkEquivN {n : Nat} (self : Std.UnionFind) (x : Fin n) (y : Fin n) (h : n = Std.UnionFind.size self) :

                                                            Check whether two union-find nodes are equivalent, updating structure using path compression.

                                                            Equations
                                                            Instances For

                                                              Check whether two union-find nodes are equivalent, updating structure using path compression. Panics if either index is out of bounds.

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

                                                                Check whether two union-find nodes are equivalent with path compression, returns x == y if either index is out of bounds

                                                                Equations
                                                                Instances For
                                                                  def Std.UnionFind.Equiv (self : Std.UnionFind) (a : Nat) (b : Nat) :

                                                                  Equivalence relation from a UnionFind structure

                                                                  Equations
                                                                  Instances For