Travelling salesman problem: an integer programming example, solved and checked
The travelling salesman 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 cities are numbered 1 to 5, distances are in kilometers. The distance from city 1 to city 2 is 3 kilometers, 1 to 3 is 4, 1 to 4 is 2 and 1 to 5 is 7. From city 2 to city 3 it is 4 kilometers, 2 to 4 is 6 and 2 to 5 is 3. From city 3 to city 4 it is 5 kilometers and 3 to 5 is 8. From city 4 to city 5 it is 6 kilometers. Distances are the same in both directions. Find the shortest tour in kilometers that visits every city once and returns to the start.”

The problem
Distances in km between cities 1-5 (symmetric): d12=3, d13=4, d14=2, d15=7, d23=4, d24=6, d25=3, d34=5, d35=8, d45=6. Find the shortest tour (in km) visiting every city once and returning to the start.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
Assumptions:
- the tour is undirected, so each city has exactly two tour edges; with 5 cities this already rules out subtours, since a 3-city loop plus a 2-city loop would need an edge used twice
The model
Each line has a plain-language note on the right.
| make as small as possible: total tour length (km) | |
| limit 1: left side must equal 2 | |
| limit 2: left side must equal 2 | |
| limit 3: left side must equal 2 | |
| limit 4: left side must equal 2 | |
| limit 5: left side must equal 2 | |
| yes (1) or no (0) |
In matrix form
With its numbers:
What is being decided
There are 10 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| y12 | binary (0 or 1) | 1 if the tour uses the road between cities 1 and 2 (else 0); likewise y13, y14, y15, y23, y24, y25, y34, y35, y45 |
| y13 | binary (0 or 1) | see y12 |
| y14 | binary (0 or 1) | see y12 |
| y15 | binary (0 or 1) | see y12 |
| y23 | binary (0 or 1) | see y12 |
| y24 | binary (0 or 1) | see y12 |
| y25 | binary (0 or 1) | see y12 |
| y34 | binary (0 or 1) | see y12 |
| y35 | binary (0 or 1) | see y12 |
| y45 | binary (0 or 1) | see y12 |
Goal and limits
Minimize total tour length (km) subject to 5 constraints.
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) y12 + y13 + y14 + y15 = 2; (2) y12 + y23 + y24 + y25 = 2; (3) y13 + y23 + y34 + y35 = 2; (4) y14 + y24 + y34 + y45 = 2; (5) y15 + y25 + y35 + y45 = 2. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | y12 + y13 + y14 + y15 = 2 | 2 | 2 | 0 | yes |
| 2 | y12 + y23 + y24 + y25 = 2 | 2 | 2 | 0 | yes |
| 3 | y13 + y23 + y34 + y35 = 2 | 2 | 2 | 0 | yes |
| 4 | y14 + y24 + y34 + y45 = 2 | 2 | 2 | 0 | yes |
| 5 | y15 + y25 + y35 + y45 = 2 | 2 | 2 | 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
y12 = cp.Variable(name='y12', boolean=True) # 1 if the tour uses the road between cities 1 and 2 (else 0); likewise y13, y14, y15, y23, y24, y25, y34, y35, y45
y13 = cp.Variable(name='y13', boolean=True) # see y12
y14 = cp.Variable(name='y14', boolean=True) # see y12
y15 = cp.Variable(name='y15', boolean=True) # see y12
y23 = cp.Variable(name='y23', boolean=True) # see y12
y24 = cp.Variable(name='y24', boolean=True) # see y12
y25 = cp.Variable(name='y25', boolean=True) # see y12
y34 = cp.Variable(name='y34', boolean=True) # see y12
y35 = cp.Variable(name='y35', boolean=True) # see y12
y45 = cp.Variable(name='y45', boolean=True) # see y12
objective = cp.Minimize(3*y12 + 4*y13 + 2*y14 + 7*y15 + 4*y23 + 6*y24 + 3*y25 + 5*y34 + 8*y35 + 6*y45)
constraints = [
y12 + y13 + y14 + y15 == 2,
y12 + y23 + y24 + y25 == 2,
y13 + y23 + y34 + y35 == 2,
y14 + y24 + y34 + y45 == 2,
y15 + y25 + y35 + y45 == 2,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('y12 =', y12.value)
print('y13 =', y13.value)
print('y14 =', y14.value)
print('y15 =', y15.value)
print('y23 =', y23.value)
print('y24 =', y24.value)
print('y25 =', y25.value)
print('y34 =', y34.value)
print('y35 =', y35.value)
print('y45 =', y45.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 lower bound of 19 on the integer optimum 19, a duality gap of 0. Integer problems generally have a gap.
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'y12 + y13 + y14 + y15 = 2' (objective change per extra unit of its right-hand side) | 0.5 |
| y2 | price of constraint 'y12 + y23 + y24 + y25 = 2' (objective change per extra unit of its right-hand side) | 0.5 |
| y3 | price of constraint 'y13 + y23 + y34 + y35 = 2' (objective change per extra unit of its right-hand side) | 3.5 |
| y4 | price of constraint 'y14 + y24 + y34 + y45 = 2' (objective change per extra unit of its right-hand side) | 1.5 |
| y5 | price of constraint 'y15 + y25 + y35 + y45 = 2' (objective change per extra unit of its right-hand side) | 4.5 |
| y6 | price of constraint 'y12 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y7 | price of constraint 'y13 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y8 | price of constraint 'y14 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y9 | price of constraint 'y15 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y10 | price of constraint 'y23 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y11 | price of constraint 'y24 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y12 | price of constraint 'y25 <= 1' (objective change per extra unit of its right-hand side) | -2 |
| y13 | price of constraint 'y34 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y14 | price of constraint 'y35 <= 1' (objective change per extra unit of its right-hand side) | 0 |
| y15 | price of constraint 'y45 <= 1' (objective change per extra unit of its right-hand side) | 0 |
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).
minimize 3*y12 + 4*y13 + 2*y14 + 7*y15 + 4*y23 + 6*y24 + 3*y25 + 5*y34 + 8*y35 + 6*y45
subject to
y12 + y13 + y14 + y15 = 2
y12 + y23 + y24 + y25 = 2
y13 + y23 + y34 + y35 = 2
y14 + y24 + y34 + y45 = 2
y15 + y25 + y35 + y45 = 2
binary: y12, y13, y14, y15, y23, y24, y25, y34, y35, y45
# y12 = 1 if the tour uses the road between cities 1 and 2 (else 0); likewise y13, y14, y15, y23, y24, y25, y34, y35, y45
# objective = total tour length (km)
# assume: the tour is undirected, so each city has exactly two tour edges; with 5 cities this already rules out subtours, since a 3-city loop plus a 2-city loop would need an edge used twiceChecked: 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.