∇ProvenDecisions
Ctrl K

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.

Answer. The smallest objective value is 4, at x_sa = 1, x_sb = 0, x_ab = 1, x_at = 0, x_bt = 1. All 9 constraints are satisfied exactly. Exactly proven optimal.

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:

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.

VariableTypeMeaning
x_sareal numberflow on road s-a (1 if used)
x_sbreal numberflow on road s-b (1 if used)
x_abreal numberflow on road a-b (1 if used)
x_atreal numberflow on road a-t (1 if used)
x_btreal numberflow 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.

x_sa1x_sb0x_ab1x_at0x_bt1

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.

#ConstraintValueLimitSlackBinding
1x_sa + x_sb = 1110yes
2-x_ab - x_at + x_sa = 0000yes
3x_ab - x_bt + x_sb = 0000yes
4x_at + x_bt = 1110yes
5x_sa >= 0101
6x_sb >= 0000yes
7x_ab >= 0101
8x_at >= 0000yes
9x_bt >= 0101

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.

#ConstraintWeight
1x_sa + x_sb = 11
3x_ab - x_bt + x_sb = 02
4x_at + x_bt = 13

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 variablePricesValue
y1price of constraint 'x_sa + x_sb = 1' (objective change per extra unit of its right-hand side)1
y2price of constraint '-x_ab - x_at + x_sa = 0' (objective change per extra unit of its right-hand side)0
y3price of constraint 'x_ab - x_bt + x_sb = 0' (objective change per extra unit of its right-hand side)2
y4price 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)

Change the numbers and solve it

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

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: