0-1 knapsack problem: an integer programming example, solved and checked
The 0-1 knapsack problem is a classic integer programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
Say it or snap it
You can give ProvenDecisions this problem out loud or as a photo. The app wrote down both, solved them, and got the same checked answer as the typed version.
What the app heard: “5 items have values 10, 13, 7, 8 and 5, and weights 5, 6, 3, 4 and 2. The napsack can hold a total weight of 10. Each item is either taken whole or left behind, which items should be taken to maximize the total value.”

The problem
Five items have values 10, 13, 7, 8, 5 and weights 5, 6, 3, 4, 2. The knapsack holds weight 10. Which items maximize total value? Let t1, ..., t5 be 1 if item 1, ..., 5 is taken and 0 if not.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
The model
Each line has a plain-language note on the right.
| make as large as possible: total value of the items in the knapsack (value units) | |
| limit 1: left side cannot exceed 10 | |
| yes (1) or no (0) |
In matrix form
With its numbers:
What is being decided
There are 5 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| t1 | binary (0 or 1) | 1 if item 1 is taken, 0 otherwise (decision) |
| t2 | binary (0 or 1) | 1 if item 2 is taken, 0 otherwise (decision) |
| t3 | binary (0 or 1) | 1 if item 3 is taken, 0 otherwise (decision) |
| t4 | binary (0 or 1) | 1 if item 4 is taken, 0 otherwise (decision) |
| t5 | binary (0 or 1) | 1 if item 5 is taken, 0 otherwise (decision) |
Goal and limits
Maximize total value of the items in the knapsack (value units) subject to 1 constraint.
Problem type
ILP, complexity level 5: integer variables with linear objective and constraints. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): (1) 5*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 10. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | 5*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 10 | 10 | 10 | 0 | yes |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (and A x = b for equalities); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
t1 = cp.Variable(name='t1', boolean=True) # 1 if item 1 is taken, 0 otherwise (decision)
t2 = cp.Variable(name='t2', boolean=True) # 1 if item 2 is taken, 0 otherwise (decision)
t3 = cp.Variable(name='t3', boolean=True) # 1 if item 3 is taken, 0 otherwise (decision)
t4 = cp.Variable(name='t4', boolean=True) # 1 if item 4 is taken, 0 otherwise (decision)
t5 = cp.Variable(name='t5', boolean=True) # 1 if item 5 is taken, 0 otherwise (decision)
objective = cp.Maximize(10*t1 + 13*t2 + 7*t3 + 8*t4 + 5*t5)
constraints = [
5*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 10,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('t1 =', t1.value)
print('t2 =', t2.value)
print('t3 =', t3.value)
print('t4 =', t4.value)
print('t5 =', t5.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Integer variables are relaxed: this is the dual of the continuous (LP) relaxation, which gives a bound on the integer optimum, not its exact value. Only weak duality: the dual of the continuous relaxation gives an upper bound of 22.8333 on the integer optimum 22, a duality gap of 0.833333. Integer problems generally have a gap.
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint '5*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 10' (objective change per extra unit of its right-hand side) | 2.16667 |
| y2 | price of constraint 't1 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint 't2 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y4 | price of constraint 't3 <= 1' (objective change per extra unit of its right-hand side) | 0.5 |
| y5 | price of constraint 't4 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y6 | price of constraint 't5 <= 1' (objective change per extra unit of its right-hand side) | 0.666667 |
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
maximize 10*t1 + 13*t2 + 7*t3 + 8*t4 + 5*t5
subject to
5*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 10
binary: t1, t2, t3, t4, t5
# t1 = 1 if item 1 is taken, 0 otherwise (decision)
# t2 = 1 if item 2 is taken, 0 otherwise (decision)
# t3 = 1 if item 3 is taken, 0 otherwise (decision)
# t4 = 1 if item 4 is taken, 0 otherwise (decision)
# t5 = 1 if item 5 is taken, 0 otherwise (decision)
# objective = total value of the items in the knapsack (value units)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, SCIP) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.