Shortest path as a linear program: a linear programming example, solved and checked
The shortest path as a linear program is a classic linear 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
The problem
Road lengths in km: s-a 1, s-b 4, a-b 2, a-t 6, b-t 1 (one-way, in that direction). Find the shortest route from s to t.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- xsa = 1 means the route uses the road s → a, and xsa = 0 means it does not. The objective adds up the length of every road used, in km.
- xsa + xsb = 1: the route leaves s once.
- xsa − xab − xat = 0: a route that enters a must leave it (in = out). The next line is the same rule at b.
- xat + xbt = 1: the route arrives at t once.
- The model does not force x to be 0 or 1, but in a network problem like this the best corner of the linear program is whole numbers anyway, as the answer shows.
The model
Each line has a plain-language note on the right.
| make as small as possible: total route length from s to t (km) | |
| limit 1: left side must equal 1 | |
| limit 2: left side must equal 0 | |
| limit 3: left side must equal 0 | |
| limit 4: left side must equal 1 | |
| flow on road s-a (1 if used) cannot be negative | |
| flow on road s-b (1 if used) cannot be negative | |
| flow on road a-b (1 if used) cannot be negative | |
| flow on road a-t (1 if used) cannot be negative | |
| flow on road b-t (1 if used) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 5 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x_sa | real number | flow on road s-a (1 if used) |
| x_sb | real number | flow on road s-b (1 if used) |
| x_ab | real number | flow on road a-b (1 if used) |
| x_at | real number | flow on road a-t (1 if used) |
| x_bt | real number | flow on road b-t (1 if used) |
Goal and limits
Minimize total route length from s to t (km) subject to 9 constraints, of which 5 are simple bounds on a single variable.
Problem type
LP, complexity level 2: a linear program. 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) x_sa + x_sb = 1; (2) -x_ab - x_at + x_sa = 0; (3) x_ab - x_bt + x_sb = 0; (4) x_at + x_bt = 1; (6) x_sb >= 0; (8) x_at >= 0. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x_sa + x_sb = 1 | 1 | 1 | 0 | yes |
| 2 | -x_ab - x_at + x_sa = 0 | 0 | 0 | 0 | yes |
| 3 | x_ab - x_bt + x_sb = 0 | 0 | 0 | 0 | yes |
| 4 | x_at + x_bt = 1 | 1 | 1 | 0 | yes |
| 5 | x_sa >= 0 | 1 | 0 | 1 | |
| 6 | x_sb >= 0 | 0 | 0 | 0 | yes |
| 7 | x_ab >= 0 | 1 | 0 | 1 | |
| 8 | x_at >= 0 | 0 | 0 | 0 | yes |
| 9 | x_bt >= 0 | 1 | 0 | 1 |
Why this is optimal
A proof you can check by hand. Multiply each constraint in the table by its weight and add them up: the result is a lower limit on the objective that holds for every plan meeting the constraints. Our answer reaches that limit exactly, so no plan does better. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 1 | x_sa + x_sb = 1 | 1 |
| 3 | x_ab - x_bt + x_sb = 0 | 2 |
| 4 | x_at + x_bt = 1 | 3 |
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 (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
x_sa = cp.Variable(name='x_sa') # flow on road s-a (1 if used)
x_sb = cp.Variable(name='x_sb') # flow on road s-b (1 if used)
x_ab = cp.Variable(name='x_ab') # flow on road a-b (1 if used)
x_at = cp.Variable(name='x_at') # flow on road a-t (1 if used)
x_bt = cp.Variable(name='x_bt') # flow on road b-t (1 if used)
objective = cp.Minimize(2*x_ab + 6*x_at + x_bt + x_sa + 4*x_sb)
constraints = [
x_sa + x_sb == 1,
-x_ab - x_at + x_sa == 0,
x_ab - x_bt + x_sb == 0,
x_at + x_bt == 1,
x_sa >= 0,
x_sb >= 0,
x_ab >= 0,
x_at >= 0,
x_bt >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x_sa =', x_sa.value)
print('x_sb =', x_sb.value)
print('x_ab =', x_ab.value)
print('x_at =', x_at.value)
print('x_bt =', x_bt.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (4).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'x_sa + x_sb = 1' (objective change per extra unit of its right-hand side) | 1 |
| y2 | price of constraint '-x_ab - x_at + x_sa = 0' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint 'x_ab - x_bt + x_sb = 0' (objective change per extra unit of its right-hand side) | 2 |
| y4 | price of constraint 'x_at + x_bt = 1' (objective change per extra unit of its right-hand side) | 3 |
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 1*x_sa + 4*x_sb + 2*x_ab + 6*x_at + 1*x_bt
subject to
x_sa + x_sb = 1
x_sa - x_ab - x_at = 0
x_sb + x_ab - x_bt = 0
x_at + x_bt = 1
x_sa, x_sb, x_ab, x_at, x_bt >= 0
# x_sa = flow on road s-a (1 if used)
# x_sb = flow on road s-b (1 if used)
# x_ab = flow on road a-b (1 if used)
# x_at = flow on road a-t (1 if used)
# x_bt = flow on road b-t (1 if used)
# objective = total route length from s to t (km)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, CLARABEL) 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.