Documentation

Provenance.HavingComplexity

NP-completeness of deciding non-zero HAVING provenance #

Deciding whether the provenance of a HAVING SUM query over an โ„•[X]- or ๐”น[X]-instance is non-๐Ÿ˜ is NP-complete, already in data complexity: the query SELECT DISTINCT 1 FROM R GROUP BY b HAVING SUM(v) = b is fixed and only the instance varies.

The development is in three parts.

That last half is not bookkeeping. Values written in unary would make the problem tractable โ€“ Provenance.Algorithms.SumDP is the very dynamic program that solves it, and DescriptiveComplexity.no_unary_encoding shows the size bounds reject that reading โ€“ so the representation is part of the statement, and the encoding is where it is pinned down.

Hardness is a padding interpretation: keep the instance and add one weight-zero item per element of the universe. Those items change no reachable total, but they make a solution non-empty, which is what the possible-world semantics requires: Having.havingProv sums over the non-empty sub-worlds of a group only.

Main results #

Step 1: the provenance of a HAVING SUM predicate #

noncomputable def Provenance.Complexity.havingSumProv {ฮน K : Type} [DecidableEq ฮน] [CommSemiringWithMonus K] (ฮฑ : ฮน โ†’ K) (U : Finset ฮน) (t : ฮน โ†’ โ„•) (B : โ„•) :
K

Provenance of HAVING SUM(t) = B on the group of occurrences U, in the possible-world semantics: the โŠ•-sum, over the non-empty worlds whose aggregate equals B, of the world annotations Having.T.

Equations
Instances For
    Dependency graph

    Step 2: โ„•[X] with distinct variables โ€“ no cancellation #

    noncomputable def Provenance.Complexity.expo {ฮน X : Type} (x : ฮน โ†’ X) (W : Finset ฮน) :

    The exponent vector of a world: with pairwise distinct variables, the annotation A_W is the monomial with this exponent.

    Equations
    Instances For
      Dependency graph
      theorem Provenance.Complexity.expo_apply {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (hx : Function.Injective x) (W : Finset ฮน) (j : ฮน) :
      (expo x W) (x j) = if j โˆˆ W then 1 else 0
      Dependency graph
      theorem Provenance.Complexity.expo_inj {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (hx : Function.Injective x) :

      Distinct worlds carry distinct exponent vectors.

      Dependency graph
      theorem Provenance.Complexity.A_eq_monomial {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (W : Finset ฮน) :
      Having.A (fun (i : ฮน) => MvPolynomial.X (x i)) W = (MvPolynomial.monomial (expo x W)) 1

      With pairwise distinct variables, A_W is a single monomial.

      Dependency graph
      theorem Provenance.Complexity.T_eq_A {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (hx : Function.Injective x) (U W : Finset ฮน) :
      Having.T (fun (i : ฮน) => MvPolynomial.X (x i)) U W = Having.A (fun (i : ฮน) => MvPolynomial.X (x i)) W

      The monus factor of a world annotation collapses: the subtracted one-step extensions of W are monomials of degree |W| + 1, so they do not meet the support of A_W.

      Dependency graph
      theorem Provenance.Complexity.havingSumProv_ne_zero_iff {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (hx : Function.Injective x) (U : Finset ฮน) (t : ฮน โ†’ โ„•) (B : โ„•) :
      havingSumProv (fun (i : ฮน) => MvPolynomial.X (x i)) U t B โ‰  0 โ†” โˆƒ W โІ U, W.Nonempty โˆง โˆ‘ i โˆˆ W, t i = B

      The bridge. Over โ„•[X] with pairwise distinct variables as annotations, the possible-world provenance of SUM(t) = B on a group U is non-๐Ÿ˜ exactly when some non-empty sub-world of U has aggregate B.

      Dependency graph

      Step 2': ๐”น[X] with distinct variables โ€“ the same characterization #

      theorem Provenance.Complexity.havingSumProvBool_ne_zero_iff {ฮน X : Type} [DecidableEq ฮน] [DecidableEq X] (x : ฮน โ†’ X) (hx : Function.Injective x) (U : Finset ฮน) (t : ฮน โ†’ โ„•) (B : โ„•) :
      havingSumProv (fun (i : ฮน) => BoolFunc.var (x i)) U t B โ‰  0 โ†” โˆƒ W โІ U, W.Nonempty โˆง โˆ‘ i โˆˆ W, t i = B

      The bridge, over ๐”น[X]. With pairwise distinct variables as annotations, the possible-world provenance of SUM(t) = B on a group U is non-๐Ÿ˜ in ๐”น[X] exactly when some non-empty sub-world of U has aggregate B: the valuation making true exactly the variables of a witnessing world satisfies precisely that world's annotation. The combinatorial characterization is the same as over โ„•[X] (havingSumProv_ne_zero_iff), so the NP-completeness of the underlying decision problem covers both semirings.

      Dependency graph

      Step 3: the decision problem and its membership in NP #

      Some non-empty set of items has weights summing to the target.

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

        The decision problem behind the NP-completeness of non-zero HAVING SUM provenance: the isomorphism-invariant bundling of HasNonemptySubsetSum.

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

          The target is non-zero.

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

            Some item has weight zero.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              Dependency graph
              Dependency graph
              Dependency graph
              Dependency graph
              theorem Provenance.Complexity.weight_le_sum {A : Type} [FirstOrder.Language.binWeights.Structure A] [Finite A] {S : A โ†’ Prop} {iโ‚€ : A} (hiโ‚€ : S iโ‚€) :

              A single selected item's weight is at most the total.

              Dependency graph

              HavingSumNonzero is Knapsack cut down by one first-order sentence: a solution can be taken non-empty exactly when the target is non-zero (any solution is then non-empty) or some item has weight zero (which can be added to a solution).

              Dependency graph

              HavingSumNonzero is in NP: it is Knapsack, whose ฮฃโ‚ definition is the binary adder of knapsackKernel, conjoined with a first-order sentence.

              Dependency graph

              Step 4: NP-hardness, by FO reduction from Knapsack #

              Tags of the padding interpretation: the original instance, plus a copy of the universe turned into items of weight zero.

              Instances For
                Dependency graph
                @[instance_reducible]
                Equations
                Dependency graph
                Dependency graph
                Dependency graph
                Dependency graph
                Dependency graph
                Dependency graph
                Equations
                Instances For
                  Dependency graph
                  Dependency graph

                  The padding interpretation: keep the instance, and add one weight-zero item per element of the universe.

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

                    A point of the interpreted universe.

                    Equations
                    Instances For
                      Dependency graph
                      theorem Provenance.Complexity.HRed.hPt_surj {A : Type} (q : hInterp.Map A) :
                      โˆƒ (t : HTag) (a : A), q = hPt t a
                      Dependency graph
                      theorem Provenance.Complexity.HRed.hPt_inj {A : Type} {t t' : HTag} {a a' : A} :
                      hPt t a = hPt t' a' โ†” t = t' โˆง a = a'
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph
                      Dependency graph

                      Padded items weigh nothing, so a selection's total weight is that of its original part.

                      Dependency graph

                      Correctness of the reduction. Padding with weight-zero items makes a solution non-empty without changing which totals are reachable.

                      Dependency graph

                      Knapsack FO-reduces to HavingSumNonzero, so the latter is NP-hard in data complexity: the reduction outputs an instance, the query being fixed.

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

                        NP-completeness of non-zero HAVING SUM provenance, in data complexity: membership and FO-hardness from Knapsack. Read through the bridges havingSumProv_ne_zero_iff and havingSumProvBool_ne_zero_iff, and through the faithful encoding of Step 5 below, this is the NP-completeness of deciding non-๐Ÿ˜ provenance of a concrete group over โ„•[X]- and ๐”น[X]-instances, the values written in binary.

                        Dependency graph

                        Step 5: the concrete groups the theorem is about #

                        A Language.binWeights structure is not what a user has in hand: a HAVING SUM group is a list of aggregate values and the constant the predicate compares against. That is exactly DescriptiveComplexity.SubsetSumInstance, and DescriptiveComplexity.binarySubsetSumEncoding encodes it โ€“ with the two size obligations, no padding and no compression, discharged at construction against a declared size that counts bit length. What remains is the semantic obligation, DescriptiveComplexity.Encoding.Faithful: that the decision problem above computes non-zero provenance on every encoded group. Both semirings get it from the same encoding, the bridges having shown they have the same combinatorial content.

                        @[reducible, inline]

                        A concrete HAVING SUM group: the aggregate values of its occurrences, and the constant the predicate compares against. This is literally DescriptiveComplexity.SubsetSumInstance; naming it here records what the components mean on this side of the bridge.

                        Equations
                        Instances For
                          Dependency graph

                          Some non-empty sub-world of the group aggregates to the constant: the combinatorial content the two bridges give the provenance, on concrete data. Non-emptiness is the possible-world semantics' exclusion of the empty world, not a technicality.

                          Equations
                          Instances For
                            Dependency graph

                            Non-zero HAVING SUM provenance of a concrete group over โ„•[X], its occurrences annotated by pairwise distinct variables.

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

                              Non-zero HAVING SUM provenance of a concrete group over ๐”น[X], its occurrences annotated by pairwise distinct variables.

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

                                The problem, read along an indexing of the items. A selection of items is a set of indices, and conversely, with the same total and the same emptiness โ€“ the two halves of selection_toIndex/selection_ofIndex. This serves both the encoding (indexing by itemPt) and the decoding (indexing by a listing of a presented structure's items).

                                Dependency graph
                                Dependency graph

                                The encoding is faithful, over โ„•[X]: on every encoded group, HavingSumNonzero computes exactly non-zero provenance of the concrete group. With the size bounds discharged at construction, this is what makes havingSumNonzero_NP_complete a statement about lists of binary-written aggregate values โ€“ and not about the unary reading, which Provenance.Algorithms.SumDP solves.

                                Dependency graph

                                The encoding is faithful, over ๐”น[X]: the same encoding serves the Boolean-function semiring, the two bridges having given the same combinatorial characterization.

                                Dependency graph

                                Step 6: reading the hardness back on concrete groups #

                                Faithfulness carries membership to concrete groups; hardness needs the converse โ€“ that the problem is not hard only on structures no group encodes. The decoder of DescriptiveComplexity.bwDecode is problem-independent, so it serves here unchanged: only its soundness has to be restated against HavingSumNonzero. There is again no junk to exclude, whence exists_concreteNonemptySubsetSum_iff for every nonempty finite structure.

                                theorem Provenance.Complexity.concreteNonemptySubsetSum_map {ฮฑ : Type} (l : List ฮฑ) (g : ฮฑ โ†’ โ„•) (t : โ„•) :
                                ConcreteNonemptySubsetSum (List.map g l, t) โ†” โˆƒ (J : Finset (Fin l.length)), J.Nonempty โˆง โˆ‘ j โˆˆ J, g (l.get j) = t

                                Reindexing a decoded weight list, for the non-empty variant.

                                Dependency graph

                                A group with no occurrences cannot aggregate to a positive constant โ€“ what the decoder returns on a structure that is a no-instance for lack of a linear order.

                                Dependency graph
                                Dependency graph

                                The computable decoding of binary-weighted structures, for non-zero HAVING SUM provenance. Every nonempty finite structure decodes, so the NP-hardness above is nowhere hardness on junk alone.

                                Equations
                                Instances For
                                  Dependency graph

                                  Hardness reads back to concrete groups: every nonempty finite binary-weighted structure is decided by HavingSumNonzero exactly as some concrete group is by the non-empty subset-sum condition โ€“ equivalently, by the two bridges, as some concrete group's provenance is non-๐Ÿ˜.

                                  Dependency graph