Documentation

DescriptiveComplexity.Problems.Knapsack.Hardness

Knapsack is NP-hard #

The reduction is Karp's, from EXACT COVER: give the set s the weight ∑_{e ∈ s} B ^ rank e, one digit block per ground element, and give the target the digit 1 in every block. A subfamily then has the right weight exactly when it covers every element once – provided that the digits never carry, which is what choosing the base B above the number of sets buys.

The gadget, in the shape an interpretation can build #

The base is B = 2 ^ |A|, so a block is |A| bit positions, and the bit positions are pairs: the position (e, x) is the x-th bit of the block of the ground element e. The order on positions is lexicographic, so the block of e is a contiguous range of |A| positions and the rank of its lowest position is |A| * rank e (DescriptiveComplexity.KnapRed.bitRank_kLow) – place value (2 ^ |A|) ^ rank e, as wanted.

Only the lowest position of a block ever carries a bit: the weight of s has a 1 at (e, ⊥) for e ∈ s, and the target has a 1 at (e, ⊥) for every e. The digit of the block of e in the sum over a subfamily G is therefore the number of sets of G containing e, at most the number of sets, hence at most |A| < 2 ^ |A| = B. Uniqueness of base-B expansions (DescriptiveComplexity.digitNum_inj) turns the equation into “every digit is 1”, which is exactly exactness of the cover.

The order is needed twice – to say “the lowest position of a block” and to pin the padding of the items down to a single representative – so this is an DescriptiveComplexity.OrderedFOReduction, and being one has the pleasant side effect of supplying the finiteness that the counting needs.

Formula builders over the ordered expansion of set systems #

@[reducible, inline]

The ordered expansion of the language of set systems.

Equations
Instances For
    Dependency graph
    @[reducible, inline]

    The ground-element symbol in the ordered expansion.

    Equations
    Instances For
      Dependency graph
      @[reducible, inline]

      The family symbol in the ordered expansion.

      Equations
      Instances For
        Dependency graph
        @[reducible, inline]

        The incidence symbol in the ordered expansion.

        Equations
        Instances For
          Dependency graph
          Dependency graph
          Dependency graph
          Dependency graph
          Dependency graph
          Dependency graph
          Dependency graph
          noncomputable def DescriptiveComplexity.KnapRed.minF {α : Type} (x : α) :

          x holds a minimum of the order, as a formula.

          Equations
          Instances For
            Dependency graph
            Dependency graph
            Dependency graph
            @[simp]
            theorem DescriptiveComplexity.KnapRed.realize_memF {α A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {v : αA} {x f : α} :
            (memF x f).Realize v SSMem (v x) (v f)
            Dependency graph
            @[simp]
            theorem DescriptiveComplexity.KnapRed.realize_leF {α A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {v : αA} {x y : α} :
            (leF x y).Realize v v x v y
            Dependency graph
            @[simp]
            theorem DescriptiveComplexity.KnapRed.realize_eqF {α A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {v : αA} {x y : α} :
            (eqF x y).Realize v v x = v y
            Dependency graph
            @[simp]
            theorem DescriptiveComplexity.KnapRed.realize_ltF {α A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {v : αA} {x y : α} :
            (ltF x y).Realize v v x < v y
            Dependency graph
            Dependency graph

            Lexicographic order on pairs #

            The lexicographic order on pairs of elements, as the interpretation sees them: coordinates of a 2-tuple.

            Equations
            Instances For
              Dependency graph
              theorem DescriptiveComplexity.KnapRed.tuple₂_ext {A : Type} {w w' : Fin 2A} (h0 : w 0 = w' 0) (h1 : w 1 = w' 1) :
              w = w'

              Two 2-tuples with the same coordinates are equal.

              Dependency graph
              Dependency graph
              theorem DescriptiveComplexity.KnapRed.lex2_trans {A : Type} [LinearOrder A] {u v w : Fin 2A} (h₁ : Lex2 u v) (h₂ : Lex2 v w) :
              Lex2 u w
              Dependency graph
              theorem DescriptiveComplexity.KnapRed.lex2_antisymm {A : Type} [LinearOrder A] {u v : Fin 2A} (h₁ : Lex2 u v) (h₂ : Lex2 v u) :
              u = v
              Dependency graph
              Dependency graph

              The interpretation #

              Tags of the reduction: the items (one per set of the family) and the bit positions (one per pair “ground element, index inside its block”).

              • itm : KTag

                The item of a set of the family.

              • pos : KTag

                A bit position.

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

                Defining formula for the bits of the weights: the weight of the set s has a 1 at the lowest position of the block of e exactly when e ∈ s.

                Equations
                Instances For
                  Dependency graph
                  Dependency graph

                  The lexicographic comparison of the two argument tuples, as a formula.

                  Equations
                  Instances For
                    Dependency graph
                    Dependency graph

                    The interpretation of a binary-weighted instance in a set system: one item per set, one bit position per pair “ground element, index”.

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

                      The points of the interpreted structure #

                      def DescriptiveComplexity.KnapRed.kPt {A : Type} (t : KTag) (w : Fin 2A) :

                      The point of tag t over the pair w.

                      Equations
                      Instances For
                        Dependency graph
                        theorem DescriptiveComplexity.KnapRed.kPt_surj {A : Type} (q : kInterp.Map A) :
                        ∃ (t : KTag) (w : Fin 2A), q = kPt t w
                        Dependency graph
                        theorem DescriptiveComplexity.KnapRed.kPt_eq_iff {A : Type} {t t' : KTag} {w w' : Fin 2A} :
                        kPt t w = kPt t' w' t = t' w = w'
                        Dependency graph
                        @[simp]
                        theorem DescriptiveComplexity.KnapRed.kPt_snd {A : Type} (t : KTag) (w : Fin 2A) (j : Fin 2) :
                        (kPt t w).2 j = w j
                        Dependency graph

                        The item of the set s, padded with the minimum a₀.

                        Equations
                        Instances For
                          Dependency graph

                          The bit position (e, x): the x-th position of the block of e.

                          Equations
                          Instances For
                            Dependency graph

                            The lowest position of the block of e.

                            Equations
                            Instances For
                              Dependency graph
                              @[simp]
                              theorem DescriptiveComplexity.KnapRed.kItem_fst {A : Type} (a₀ s : A) :
                              (kItem a₀ s).2 0 = s
                              Dependency graph
                              @[simp]
                              theorem DescriptiveComplexity.KnapRed.kLow_fst {A : Type} (a₀ e : A) :
                              (kLow a₀ e).2 0 = e
                              Dependency graph
                              @[simp]
                              theorem DescriptiveComplexity.KnapRed.kLow_snd {A : Type} (a₀ e : A) :
                              (kLow a₀ e).2 1 = a₀
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              theorem DescriptiveComplexity.KnapRed.eq_kLow_iff {A : Type} {a₀ e : A} {t : KTag} {w : Fin 2A} :
                              kPt t w = kLow a₀ e t = KTag.pos w = ![e, a₀]

                              Being the lowest position of a block, coordinate by coordinate.

                              Dependency graph

                              Characterization of the interpreted relations #

                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              @[simp]
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph
                              Dependency graph

                              Comparison with the lowest position of a block.

                              Dependency graph
                              Dependency graph

                              The interpreted order is a linear order: the two tags in order, each carrying the lexicographic order on pairs.

                              Dependency graph

                              The place values #

                              theorem DescriptiveComplexity.KnapRed.eq_kItem {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {a₀ : A} (ha₀ : IsBot a₀) {i : kInterp.Map A} (h : BWItem i) :
                              i = kItem a₀ (i.2 0) SSFam (i.2 0)

                              An item is the padded item of the set it carries.

                              Dependency graph
                              theorem DescriptiveComplexity.KnapRed.bitRank_kLow {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] {a₀ : A} (ha₀ : IsBot a₀) (e : A) :
                              bitRank BWLe BWPosn (kLow a₀ e) = Nat.card A * bitRank (fun (x1 x2 : A) => x1 x2) SSElem e

                              The block structure: the rank of the lowest position of the block of e is |A| times the rank of e, so its place value is (2 ^ |A|) ^ rank e – one digit of base 2 ^ |A| per ground element.

                              Dependency graph

                              Weights and target, as base-2 ^ |A| numbers #

                              noncomputable def DescriptiveComplexity.KnapRed.base (A : Type) [Finite A] :

                              The base: one digit per ground element, |A| bits wide, which is above the number of sets, so digits never carry into the next block.

                              Equations
                              Instances For
                                Dependency graph
                                Dependency graph
                                Dependency graph
                                theorem DescriptiveComplexity.KnapRed.binNum_eq_digit {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] [Finite A] {a₀ : A} (ha₀ : IsBot a₀) {p : kInterp.Map AProp} {sub : AProp} (hsub : ∀ (e : A), sub eSSElem e) (hp : ∀ (q : kInterp.Map A), BWPosn q p q ∃ (e : A), sub e q = kLow a₀ e) :
                                binNum BWLe BWPosn p = digitNum (fun (x1 x2 : A) => x1 x2) SSElem (base A) fun (e : A) => if sub e then 1 else 0

                                A binary number whose bits sit on the lowest positions of the blocks selected by sub is the base-2 ^ |A| number with digit 1 there.

                                Dependency graph
                                theorem DescriptiveComplexity.KnapRed.bwTarget_eq {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] [Finite A] {a₀ : A} (ha₀ : IsBot a₀) :
                                BWTarget (kInterp.Map A) = digitNum (fun (x1 x2 : A) => x1 x2) SSElem (base A) fun (e : A) => if SSElem e then 1 else 0

                                The target is the number with digit 1 in every block.

                                Dependency graph
                                theorem DescriptiveComplexity.KnapRed.bwWeight_eq {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] [Finite A] {a₀ : A} (ha₀ : IsBot a₀) {i : kInterp.Map A} (h : BWItem i) :
                                BWWeight i = digitNum (fun (x1 x2 : A) => x1 x2) SSElem (base A) fun (e : A) => if SSElem e SSMem e (i.2 0) then 1 else 0

                                The weight of an item is the number with digit 1 in the blocks of the elements of its set.

                                Dependency graph

                                The counting step #

                                theorem DescriptiveComplexity.KnapRed.sum_weights_eq {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] [Finite A] {a₀ : A} (ha₀ : IsBot a₀) {S : kInterp.Map AProp} (hS : ∀ (i : kInterp.Map A), S iBWItem i) :
                                ∑ᶠ (i : kInterp.Map A) (_ : i {i : kInterp.Map A | S i}), BWWeight i = digitNum (fun (x1 x2 : A) => x1 x2) SSElem (base A) fun (e : A) => {i : kInterp.Map A | S i SSElem e SSMem e (i.2 0)}.ncard

                                The sum of the selected weights, read digit by digit: the digit of the block of e is the number of selected items whose set contains e.

                                Dependency graph
                                theorem DescriptiveComplexity.KnapRed.count_lt_base {A : Type} [FirstOrder.Language.setSystem.Structure A] [LinearOrder A] [Finite A] {S : kInterp.Map AProp} (hS : ∀ (i : kInterp.Map A), S iBWItem i) {a₀ : A} (ha₀ : IsBot a₀) (e : A) :
                                {i : kInterp.Map A | S i SSElem e SSMem e (i.2 0)}.ncard < base A

                                Only items are selected, and there are at most |A| of them, so the digits of the sum stay below the base.

                                Dependency graph

                                Correctness #

                                Correctness of the reduction: the set system has an exact cover iff the interpreted binary-weighted instance has a set of items summing to the target.

                                Dependency graph

                                Exact Cover FO-reduces to Knapsack, over any linear order on the input: one item per set, one digit block of |A| bits per ground element, weights carrying a 1 in the block of each of their elements and the target a 1 in every block.

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