∇ProvenDecisions
Ctrl K

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.

Answer. The largest objective value is 22, at t1 = 1, t2 = 0, t3 = 1, t4 = 0, t5 = 1. The constraint is satisfied exactly. Optimal within gap tolerance.

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.

🎙️ Spoken (16 seconds)

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

📷 Photographed
Photo of the 0-1 knapsack problem written on a note

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.

VariableTypeMeaning
t1binary (0 or 1)1 if item 1 is taken, 0 otherwise (decision)
t2binary (0 or 1)1 if item 2 is taken, 0 otherwise (decision)
t3binary (0 or 1)1 if item 3 is taken, 0 otherwise (decision)
t4binary (0 or 1)1 if item 4 is taken, 0 otherwise (decision)
t5binary (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.

t11t20t31t40t51

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.

#ConstraintValueLimitSlackBinding
15*t1 + 6*t2 + 3*t3 + 4*t4 + 2*t5 <= 1010100yes

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 variablePricesValue
y1price 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
y2price of constraint 't1 <= 1' (objective change per extra unit of its right-hand side)0
y3price of constraint 't2 <= 1' (objective change per extra unit of its right-hand side)0
y4price of constraint 't3 <= 1' (objective change per extra unit of its right-hand side)0.5
y5price of constraint 't4 <= 1' (objective change per extra unit of its right-hand side)0
y6price 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)

Change the numbers and solve it

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

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: