Virtual laboratory/Mathematics/Graph theory/The knapsack problem — optimisation

The knapsack problem — optimisation

An extension of the topic of optimisation and combinatorics. We pick items of the greatest total value that fit in the knapsack (sum of weights ≤ capacity). The greedy strategy does not always give the optimum, and checking all 2ⁿ subsets grows exponentially — this is an NP-hard problem, tied to the P vs NP question (a Millennium Prize Problem of the Clay Institute, 2000).

The knapsack problem — optimisation
Mathematics · Graph theory · The knapsack problem — optimisation (2000)
Item-selection strategy
Knapsack capacity WW4 weight units
Always taking the most valuable item (greedily) gives…
capacity W
4
weight used
3 ≤ 4
value of choice
5
loss vs optimum
−1 (opt 6)
🎯 Challenge: greedy = optimum?
streak: 0

Items: A(w3,v5), B(w2,v3), C(w2,v3). I’ll give you a capacity W — decide whether taking the most valuable item (greedily) yields the maximum value.

What just happened?

We have three items — A (weight 3, value 5), B (weight 2, value 3) and C (weight 2, value 3) — and a knapsack of capacity W=4W=4. The greedy strategy reaches for the most valuable item A (value 5), but it takes up 3 of the 4 weight units and there is no room left for anything else. Meanwhile giving up A in favour of two cheaper items B + C gives value 6 with the knapsack exactly full (weight 4).

The knapsack problem — what is it?

It is a classic problem of combinatorial optimisation: given items of weights wiw_i and values viv_i and a knapsack of capacity WW, we pick a subset of maximum total value that fits inside it:

max⁡∑ivisubject to∑iwi≤W\max \sum_{i} v_i \qquad \text{subject to} \qquad \sum_{i} w_i \le W

The constraint ∑iwi≤W\sum_i w_i \le W is hard — the total weight of the chosen items can never exceed the capacity. It is tempting to solve the task “greedily” (always take the most valuable item, or the locally best one with the largest ratio vi/wiv_i/w_i), but — as seen above — this strategy does not guarantee the optimum.

Why is it hard? The class NP and “P vs NP”

Finding a guaranteed optimal solution requires considering many combinations of items, and the number of all subsets 2n2^n grows exponentially with the number of items: for 3 items that is 8 subsets, for 20 already over a million, for 50 — more than the number of atoms in many galaxies. That is why the knapsack problem belongs to the class of NP-hard problems, closely tied to one of the most important unsolved questions in mathematics and computer science: “P versus NP”. It is one of the 7 Millennium Prize Problems of the Clay Mathematics Institute (2000), and a prize of USD 1 million awaits its solution.

Where will you meet this in practice?

The knapsack problem has huge applications: packing cargo of limited mass, investment budgeting (which projects to fund within a fixed budget?), resource allocation, even cryptography. It shows that a seemingly simple task — “what to pack to carry away the most value?” — can be computationally very hard.

Why this matters for your exams:

It is a model example that the greedy strategy is not always optimal — the intuition “take the best now” can be misleading. The same caution helps in problems on optimisation, combinatorics and probability: before you declare the locally best move globally best, check for a counterexample.

Remember: in the knapsack problem the sum of the weights of the chosen items must satisfy ∑iwi≤W\sum_i w_i \le W, and the goal is max⁡∑ivi\max \sum_i v_i — yet the greedy strategy does not guarantee the maximum value. Only an exhaustive search of the combinations gives a guarantee, and its cost grows exponentially. That is why the knapsack problem is NP-hard, and the question “P versus NP” still awaits an answer.

Illustrative model · NexTutor Virtual Laboratory

Enjoyed the experiment? Share it.

It's free — send it to friends from school.

Theory & formulas

The task

Choose items of weights wiw_i and values viv_i so as to maximise the value subject to the capacity constraint WW: max⁡∑ivixisubject to∑iwixi≤W,  xi∈{0,1}\max\sum_i v_i x_i\quad\text{subject to}\quad \sum_i w_i x_i\le W,\ \ x_i\in\{0,1\}

Why it is hard

The number of subsets 2n2^n grows exponentially, and the greedy strategy (take the most valuable) does not guarantee the optimum. This is an NP-hard problem.

Density ratio

It is worth looking greedily at viwi\dfrac{v_i}{w_i}, but even that can fail in the 00-11 version.

Notation: wiw_i weight, viv_i value, WW capacity, xix_i the choice (00 or 11).

Frequently asked questions

›

Does the greedy strategy give the optimum in the knapsack problem?

Not always — sometimes a different set of items is better than taking the locally most valuable one.

›

Why is the knapsack problem hard?

Because the number of possible subsets (2ⁿ) grows exponentially — it is an NP-hard problem.

›

What great question is the knapsack problem tied to?

With the P vs NP question — one of the 7 Millennium Prize Problems of the Clay Institute (a USD 1 million prize).

Teaching? Embed this experiment on your site.

Free widget — paste it on your school site, blog or notes. It links to the full version.

Teaching a class or tutoring? Guide students in NexTutor →