Maximum flow problem: a linear programming example, solved and checked
The maximum flow problem 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
Arcs and capacities (units per hour): s->a 10, s->b 5, a->b 15, a->t 5, b->t 10. What is the maximum flow from s to t, in units per hour?
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- f is the total flow from s to t, in units per hour; fsa is the flow on the arc s → a, and so on.
- f = fsa + fsb: everything that reaches t starts at s, along s → a or s → b.
- fsa = fab + fat: what flows into node a flows out again (nothing is stored there). fab + fsb = fbt is the same rule at node b.
- f = fat + fbt: the whole flow arrives at t along a → t or b → t.
- fsa ≤ 10 and the lines after it: no arc carries more than its capacity.
The model
Each line has a plain-language note on the right.
| make as large as possible: total flow from s to t (units per hour) | |
| limit 1: left side must equal the right side | |
| limit 2: left side must equal the right side | |
| limit 3: left side must equal the right side | |
| limit 4: left side must equal the right side | |
| flow on arc s->a (units per hour) is at most 10 | |
| flow on arc s->b (units per hour) is at most 5 | |
| flow on arc a->b (units per hour) is at most 15 | |
| flow on arc a->t (units per hour) is at most 5 | |
| flow on arc b->t (units per hour) is at most 10 | |
| total flow from s to t (units per hour) cannot be negative | |
| flow on arc s->a (units per hour) cannot be negative | |
| flow on arc s->b (units per hour) cannot be negative | |
| flow on arc a->b (units per hour) cannot be negative | |
| flow on arc a->t (units per hour) cannot be negative | |
| flow on arc b->t (units per hour) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 6 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| f | real number | total flow from s to t (units per hour) |
| f_sa | real number | flow on arc s->a (units per hour) |
| f_sb | real number | flow on arc s->b (units per hour) |
| f_at | real number | flow on arc a->t (units per hour) |
| f_bt | real number | flow on arc b->t (units per hour) |
| f_ab | real number | flow on arc a->b (units per hour) |
Goal and limits
Maximize total flow from s to t (units per hour) subject to 15 constraints, of which 11 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): constraints 1, 2, 3, 4, 5, 6, 8, 9 (see the table). Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | f = f_sa + f_sb | 15 | 15 | 0 | yes |
| 2 | f = f_at + f_bt | 15 | 15 | 0 | yes |
| 3 | f_sa = f_ab + f_at | 10 | 10 | 0 | yes |
| 4 | f_ab + f_sb = f_bt | 10 | 10 | 0 | yes |
| 5 | f_sa <= 10 | 10 | 10 | 0 | yes |
| 6 | f_sb <= 5 | 5 | 5 | 0 | yes |
| 7 | f_ab <= 15 | 5 | 15 | 10 | |
| 8 | f_at <= 5 | 5 | 5 | 0 | yes |
| 9 | f_bt <= 10 | 10 | 10 | 0 | yes |
| 10 | f >= 0 | 15 | 0 | 15 | |
| 11 | f_sa >= 0 | 10 | 0 | 10 | |
| 12 | f_sb >= 0 | 5 | 0 | 5 | |
| 13 | f_ab >= 0 | 5 | 0 | 5 | |
| 14 | f_at >= 0 | 5 | 0 | 5 | |
| 15 | f_bt >= 0 | 10 | 0 | 10 |
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 an upper 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 | f = f_sa + f_sb | 1 |
| 5 | f_sa ≤ 10 | 1 |
| 6 | f_sb ≤ 5 | 1 |
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
f = cp.Variable(name='f') # total flow from s to t (units per hour)
f_sa = cp.Variable(name='f_sa') # flow on arc s->a (units per hour)
f_sb = cp.Variable(name='f_sb') # flow on arc s->b (units per hour)
f_at = cp.Variable(name='f_at') # flow on arc a->t (units per hour)
f_bt = cp.Variable(name='f_bt') # flow on arc b->t (units per hour)
f_ab = cp.Variable(name='f_ab') # flow on arc a->b (units per hour)
objective = cp.Maximize(f)
constraints = [
f == f_sa + f_sb,
f == f_at + f_bt,
f_sa == f_ab + f_at,
f_ab + f_sb == f_bt,
f_sa <= 10,
f_sb <= 5,
f_ab <= 15,
f_at <= 5,
f_bt <= 10,
f >= 0,
f_sa >= 0,
f_sb >= 0,
f_ab >= 0,
f_at >= 0,
f_bt >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('f =', f.value)
print('f_sa =', f_sa.value)
print('f_sb =', f_sb.value)
print('f_at =', f_at.value)
print('f_bt =', f_bt.value)
print('f_ab =', f_ab.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 (15).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'f = f_sa + f_sb' (objective change per extra unit of its right-hand side) | 1 |
| y2 | price of constraint 'f = f_at + f_bt' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint 'f_sa = f_ab + f_at' (objective change per extra unit of its right-hand side) | 0 |
| y4 | price of constraint 'f_ab + f_sb = f_bt' (objective change per extra unit of its right-hand side) | 0 |
| y5 | price of constraint 'f_sa <= 10' (objective change per extra unit of its right-hand side) | 1 |
| y6 | price of constraint 'f_sb <= 5' (objective change per extra unit of its right-hand side) | 1 |
| y7 | price of constraint 'f_ab <= 15' (objective change per extra unit of its right-hand side) | 0 |
| y8 | price of constraint 'f_at <= 5' (objective change per extra unit of its right-hand side) | 0 |
| y9 | price of constraint 'f_bt <= 10' (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).
maximize f
subject to
f = f_sa + f_sb
f = f_at + f_bt
f_sa = f_ab + f_at
f_sb + f_ab = f_bt
f_sa <= 10
f_sb <= 5
f_ab <= 15
f_at <= 5
f_bt <= 10
f, f_sa, f_sb, f_ab, f_at, f_bt >= 0
# f = total flow from s to t (units per hour)
# f_sa = flow on arc s->a (units per hour)
# f_sb = flow on arc s->b (units per hour)
# f_ab = flow on arc a->b (units per hour)
# f_at = flow on arc a->t (units per hour)
# f_bt = flow on arc b->t (units per hour)
# objective = total flow from s to t (units per hour)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.