Learn
Lesson · Integer Programming · Modelling with binaries

Knapsack and Set Covering

One capacity with chosen items, and every requirement covered by at least one chosen set: these are the two models most practical binary programmes are assembled from. Their relaxations are fractional in characteristic ways — one splits a single item, the other spreads halves across overlapping sets — and that is exactly why a relaxation's value is a bound rather than an answer.

knapsack max Σ v_j y_j s.t. Σ w_j y_j ≤ W, y_j ∈ {0, 1} covering min Σ c_j y_j s.t. Σ y_j ≥ 1 over the sets covering i relaxed knapsack take in v/w order and split exactly one item relaxed covering spread halves over a ring of overlapping sets 240 ≥ 220 ≥ 160 bound ≥ optimum ≥ what greed got covering ≥ 1 is not partitioning = 1: one instance, two answers
Two models, and three numbers for each

Core ideas

What this lesson establishes

Two models and one idea about both of them: the way a relaxation goes fractional is a property of the model's shape, not an accident of the data.

1

One capacity, chosen items

Maximise Σ v_j y_j subject to one row Σ w_j y_j ≤ W. Every selection problem with a single budget is this model: which features to ship in a release, which repairs to fund this year, which crates to load. The binaries make it hard; the single row makes it the smallest hard model there is.

2

Every requirement covered by some chosen set

Minimise Σ c_j y_j subject to one row per requirement saying that at least one set containing it is chosen. Depots covering districts, shifts covering hours, tests covering code paths. The rows outnumber the variables and each one is an “at least one of these” from the previous lesson.

3

Each relaxation goes fractional in its own way

The relaxed knapsack fills in value-per-weight order and splits exactly one item, because one row can only be tight in one place. The relaxed covering problem does something else entirely: on a ring of overlapping sets it puts 1/2 on every one of them, satisfying each row with two halves and paying half the price.

The material

The two atoms, and the shape of each relaxation

Both models from data, both relaxations solved, and the three numbers each one produces.

Definition · The 0/1 knapsack model

Items 1, …, n with values v_j and weights w_j, and a capacity W. Choose y_j ∈ {0, 1} to maximise Σ v_j y_j subject to Σ w_j y_j ≤ W. The name is the picture: one bag, a weight limit, and each item either in or out.

Definition · The set-covering model

Requirements 1, …, m and sets 1, …, n with costs c_j, where set j covers some of the requirements. Choose y_j ∈ {0, 1} to minimise Σ c_j y_j subject to, for each requirement i, the row Σ y_j ≥ 1 taken over the sets that cover i. Replacing that ≥ by = gives set partitioning, which is a different problem.

Take three items worth 60, 100 and 120 weighing 10, 20 and 30, with a capacity of 50. Their values per unit weight are 6, 5 and 4, so the relaxation loads the first two whole — thirty units of weight, 160 of value — and then takes 2/3 of the third to fill the bag, for 240. That is the bound.

Example · Three answers on one instance

The relaxation is worth 240 and gets there by splitting the third item. Greed by value per weight takes the first two, cannot fit the third in the remaining twenty units, and stops at 160. The best whole choice, found by trying all eight subsets, is the second and third items together: weight exactly 50, value 220. So 240 ≥ 220 ≥ 160, and the middle number is the only one that is an answer.

Discrete Mathematics’ “Greedy Algorithms” closes by showing the value-per-weight rule optimal for the fractional problem and defeated by the 0/1 version, which is precisely the gap between 240 and 160 here. The rule is not a heuristic for this model; it is the exact solution to the relaxation, and the two facts are easy to confuse because the arithmetic is the same.

The covering relaxation spreads halves

Five districts in a ring, each reachable from two of five depots, every depot costing 2. Each row says “at least one of these two depots”. The relaxation satisfies all five rows by opening every depot to the extent of 1/2, and pays 5. No whole choice does: three depots are needed, costing 6. The 1 between them is the price of not being able to open half a depot.

Example · Covering is not partitioning

On that same ring, require each district to be covered exactly once instead of at least once. Nothing in the data changed and only the relation did, and now there is no feasible choice of depots at all: an odd ring cannot be partitioned by adjacent pairs. The relaxation of the partitioning version is still solvable and still worth 5, which is the cruellest part — a relaxation being feasible says nothing about whether the integer problem is.

Both atoms above were solved exactly by enumerating every subset, and that is where this lesson's method ends: 2¹² is 4096, which is instant, and 2⁵⁰ is not a number of subsets anybody enumerates. What is this lesson's own is the pair of models and the shape of their relaxations. Four results about them are used here and proved elsewhere, each by its owner.

  • The exact method for 0/1 knapsack beyond enumeration is a pseudo-polynomial dynamic programme — the Algorithms path, “0/1 Knapsack and Pseudo-Polynomial Time”.
  • The approximation with a proved ratio is the fully polynomial scheme of “A Fully Polynomial Approximation for Knapsack”, in the same subject.
  • The ln n ratio for greedy set cover is “Greedy Set Cover and the ln n Ratio”, which proves what greed costs in the worst case rather than measuring it on one instance.
  • Why both problems are hard in the technical sense belongs to Discrete Mathematics’ “P, NP and NP-Completeness”, which is also where the word “hard” acquires its meaning.

None of those four is derived here, and none of them is needed to write the models down. What is needed is the reflex of solving the relaxation first and looking at what it made fractional, because the split item and the spread halves each point at the row doing the work — which is the variable to branch on when the search lesson arrives.

Interactive

Knapsack and set covering

Two atoms of binary modelling, and the characteristic shape of each relaxation

One capacity, three answersa relaxation is a bound, greed is a guess, and only one of the three is the optimum
the relaxationgreedthe exact optimum

Method

Building either atom from data

The same five steps for both models. The fourth is where a relaxation earns its keep.

1

Decide what one binary stands for

An item taken, or a set opened. If the answer is “how many of them”, this is not one of these two models and a binary is the wrong variable.

2

Write the rows, one per constraint you actually have

A knapsack has exactly one row, and a second row means a second model. A covering problem has one row per requirement, and each row lists the sets that cover it — which is read straight off the incidence table.

3

Solve the relaxation and look at what is fractional

One split item, or a scatter of halves. This is the diagnostic step: it tells you which row is binding and, later, which variable to branch on.

4

Get a whole answer, and a second whole answer

Greed by value per weight, or greed by cost per newly covered requirement, gives one. Enumeration at this size gives the best one. Two whole numbers are worth more than one, because their difference is what greed cost.

5

State the bound relation

z_LP ≥ z_IP ≥ z_greedy for a maximisation, reversed for a minimisation. Write all three and label them; a covering result reported as one number cannot be told apart from a guess.

Two atoms and their relaxations

Score 0 / 0

Three items at a capacity of 50, and five districts in a ring

Both atoms on one page, each solved three ways. Every figure is the lab's, on its opening presets.

KNAPSACK v = 60, 100, 120 w = 10, 20, 30 W = 50 density 60/10 = 6 100/20 = 5 120/30 = 4 relaxation item 1 whole, item 2 whole → w = 30, v = 160 20 of capacity left, so 20/30 = 2/3 of item 3 z_LP = 160 + (2/3)(120) = 160 + 80 = 240 greed items 1 and 2; item 3 does not fit z = 160 exact all 2³ = 8 subsets: {1,2} w 30 v 160 {1,3} w 40 v 180 {2,3} w 50 v 220 {1,2,3} w 60 too heavy z_IP = 220 at {2, 3} 240 ≥ 220 ≥ 160 gap 20 above, gap 60 below COVERING 5 districts in a ring, 5 depots, each cost 2 district i is covered by depot i and depot i+1 relaxation every y_j = 1/2, each row 1/2 + 1/2 = 1 ≥ 1 z_LP = 5(2)(1/2) = 5 exact 32 subsets, 11 of them feasible; cheapest is 3 depots z_IP = 6 greed cost per newly covered district: also 6 here with “exactly once” instead: no feasible choice at all its relaxation: still 5

The knapsack's two gaps say different things. The 20 above is what integrality costs on this instance; the 60 below is what greed costs. Only the first is a property of the model — the second would change if greed were replaced by anything better, and the bound would not.

The covering relaxation's halves are worth a second look. Every one of the five rows is satisfied with nothing to spare, and the total is 5 against an answer of 6. That is the characteristic shape: a covering relaxation pays for the rows and not for the indivisibility, and on an odd ring the indivisibility is the whole cost.

For a faded rehearsal, drop the knapsack capacity to 40 and redo all three numbers. The supplied first move is that the density order does not change, because the items did not — so the relaxation is still items 1 and 2 whole and then a fraction of item 3, and only the fraction moves. Work out the fraction, then the exact optimum, and say what happened to each of the two gaps.

Common mistakes

1
Using the density rule as though it solved the 0/1 problemValue per weight is exactly optimal when items may be split and merely plausible when they may not. Here it returns 160 against an optimum of 220, because it commits the capacity to two light items and then cannot fit the third. The rule is how you solve the relaxation, not how you solve the model.
2
Turning ≥ 1 into = 1 to tidy the modelIt looks like the same requirement stated more precisely and it is a different problem: the ring instance loses every feasible solution. If double coverage is genuinely forbidden, the equality is right and the infeasibility is real information; if it is merely untidy, the ≥ was correct.
3
Reading the relaxation's fractional take as a plan“Two-thirds of the third crate” and “half a depot” are arithmetic, not instructions. The value of such a solution is a bound and the solution itself is not a solution — which is the same lesson as the first lesson of this course, arriving in a model where it is harder to see.

Check your understanding

✓
Finish when you can write either atom from a table of data and predict what its relaxation will make fractional.

You should be able to formulate a knapsack and a covering instance from data, solve both relaxations, name the single split item in one and the spread halves in the other, produce a greedy answer and an exact one by enumeration at this size, state both gaps, and say what changes when a covering row becomes an equality.

Note: Enumeration stops being an option somewhere around a dozen binaries, and the two lessons that close this course are what replaces it. Before that, “Fixed Charges, Facility Location and Big-M” adds the one piece these two atoms are missing: a cost that is paid only if an activity happens at all, which needs a binary tied to a continuous quantity rather than standing alone.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.