Documentation

CslibFrontier.Computability.Complexity.Problems.Knapsack

The 0-1 knapsack problem #

Knapsack is the decision version of the 0-1 knapsack problem: an instance x : KnapsackInstance (Cslib.Complexity.encKnapsack) gives a list of items x.items (Cslib.Complexity.Knapsack.Item), each with a weight and a value, a capacity x.capacity, and a goal x.goal, and asks whether some sub-multiset of the items has total weight at most x.capacity and total value at least x.goal [GJ79]. All numbers are encoded in binary (encNat); the verifier compares the sums in binary (ComputableInPolyTime.binary_le).

Giving every number of a subset sum instance equal weight and value, and taking the target as both capacity and goal, reduces SubsetSum to Knapsack. Karp's KNAPSACK [Kar72] is the subset sum form of the problem.

Main definitions #

Main results #

References #

An item in Knapsack, with a natural-number weight and value.

  • weight : ℕ

    The weight of the item.

  • value : ℕ

    The value of the item.

Instances For
    theorem Cslib.Complexity.Knapsack.Item.ext {x y : Item} (weight : x.weight = y.weight) (value : x.value = y.value) :
    x = y
    Instances For

      Binary encoding of a Knapsack.Item as (weight, value).

      Instances For
        theorem Cslib.Complexity.Knapsack.encItem_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget x0 x1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn Turing.MultiTapeTM.encNat budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn Turing.MultiTapeTM.encNat budget) :
        Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { weight := x0 x, value := x1 x }) encIn encItem budget
        @[reducible, inline]

        The tuple of the fields of encItem.

        Instances For
          theorem Cslib.Complexity.Knapsack.encItem_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 T0 S0 x1 T1 S1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encNat T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encNat T1 S1) :
          Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { weight := x0 x, value := x1 x }) encIn encItem (fun (x : γ) => T0 x + T1 x) fun (x : γ) => S0 x + S1 x
          theorem Cslib.Complexity.Knapsack.encItem_value_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → Item} :
          theorem Cslib.Complexity.Knapsack.encItem_weight_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → Item} :

          An instance of the 0-1 knapsack problem Garey and Johnson: a list of items items, a weight capacity capacity, and a value goal goal.

          • The available items, each with a weight and a value.

          • capacity : ℕ

            The maximum total weight allowed.

          • goal : ℕ

            The minimum total value required.

          Instances For
            theorem Cslib.Complexity.KnapsackInstance.ext {x y : KnapsackInstance} (items : x.items = y.items) (capacity : x.capacity = y.capacity) (goal : x.goal = y.goal) :
            x = y
            theorem Cslib.Complexity.encKnapsack_capacity_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → KnapsackInstance} :
            @[reducible, inline]

            The tuple of the fields of encKnapsack.

            Instances For

              Binary encoding of KnapsackInstance as (items, (capacity, goal)).

              Instances For
                theorem Cslib.Complexity.encKnapsack_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → List Knapsack.Item} {T0 S0 x1 T1 S1 x2 T2 S2 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn (Turing.MultiTapeTM.encList Knapsack.encItem) T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encNat T1 S1) (h2 : Turing.MultiTapeTM.ComputableInO x2 encIn Turing.MultiTapeTM.encNat T2 S2) :
                Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { items := x0 x, capacity := x1 x, goal := x2 x }) encIn encKnapsack (fun (x : γ) => T0 x + (T1 x + T2 x)) fun (x : γ) => S0 x + (S1 x + S2 x)
                theorem Cslib.Complexity.encKnapsack_goal_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → KnapsackInstance} :
                theorem Cslib.Complexity.encKnapsack_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget : γ → ℕ} {x0 : γ → List Knapsack.Item} {x1 x2 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn (Turing.MultiTapeTM.encList Knapsack.encItem) budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn Turing.MultiTapeTM.encNat budget) (h2 : Turing.MultiTapeTM.ComputableInSpace x2 encIn Turing.MultiTapeTM.encNat budget) :
                Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { items := x0 x, capacity := x1 x, goal := x2 x }) encIn encKnapsack budget
                theorem Cslib.Complexity.encKnapsack_items_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → KnapsackInstance} :

                The 0-1 knapsack problem: instances x : KnapsackInstance for which some sub-multiset of x.items has total weight at most x.capacity and total value at least x.goal Garey and Johnson.

                Instances For

                  The verifier for Knapsack, a mask verifier (Cslib.Complexity.maskVerifier): the items selected by the certificate mask fit the capacity and reach the goal.

                  Instances For

                    The reduction from SubsetSum to Knapsack: each number becomes an item with equal weight and value, and the target becomes both the capacity and the goal.

                    Instances For

                      The 0-1 knapsack problem is NP-complete, with all numbers in binary Garey and Johnson.