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).

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 . 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 and values and a knapsack of capacity , we pick a subset of maximum total value that fits inside it:
The constraint 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 ), 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 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 , and the goal is — 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.
Choose items of weights and values so as to maximise the value subject to the capacity constraint :
The number of subsets grows exponentially, and the greedy strategy (take the most valuable) does not guarantee the optimum. This is an NP-hard problem.
It is worth looking greedily at , but even that can fail in the - version.
Notation: weight, value, capacity, the choice ( or ).
Not always — sometimes a different set of items is better than taking the locally most valuable one.
Because the number of possible subsets (2ⁿ) grows exponentially — it is an NP-hard problem.
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.