Duality in Linear Programming
Every LP comes paired with a dual LP. The pair have the same optimal value (unless both are infeasible), each primal constraint corresponds to a dual variable and vice versa, and complementary slackness links their solutions. LP duality gives optimality certificates, shadow prices, and the dual simplex method, and it explains game theory's minimax theorem and the max-flow min-cut theorem.
The dual pair
The recipe for other forms:
| Primal (min) | Dual (max) |
|---|---|
| constraint | variable |
| constraint | variable free |
| variable | constraint |
| variable free | constraint |
The dual of the dual is the primal.
The duality theorems
- Weak duality: for feasible , : .
- Strong duality: if either problem has a finite optimum, so does the other, with equal values.
- Complementary slackness: feasible , are both optimal iff for all and for all .
| Primal \ Dual | finite optimum | unbounded | infeasible |
|---|---|---|---|
| finite optimum | possible (equal values) | impossible | impossible |
| unbounded | impossible | impossible | possible |
| infeasible | impossible | possible | possible |
Checking a certificate
import numpy as np
from scipy.optimize import linprog
# diet-style primal: min c^T x s.t. A x >= b, x >= 0
c = np.array([3.0, 2.0, 4.0]); A = np.array([[1.0, 1.0, 2.0], [2.0, 1.0, 1.0]]); b = np.array([4.0, 5.0])
p = linprog(c, A_ub=-A, b_ub=-b, bounds=[(0, None)] * 3, method="highs")
d = linprog(-b, A_ub=A.T, b_ub=c, bounds=[(0, None)] * 2, method="highs")
x, y = p.x, d.x
print("primal x =", np.round(x, 4) + 0, " value", round(p.fun, 6))
print("dual y =", np.round(y, 4) + 0, " value", round(-d.fun, 6))
print("y_i * (A x - b)_i:", np.round(y * (A @ x - b), 10) + 0)
print("x_j * (c - A^T y)_j:", np.round(x * (c - A.T @ y), 10) + 0)primal x = [1. 3. 0.] value 9.0
dual y = [1. 1.] value 9.0
y_i * (A x - b)_i: [0. 0.]
x_j * (c - A^T y)_j: [0. 0. 0.]
Anyone given and can verify optimality with two matrix-vector products: feasibility of both plus equal objective values. No trust in the solver is needed.
Economic reading
If the primal buys inputs at prices to meet requirements , the dual is a competitor who sells the requirements directly at unit prices , as high as possible () but without any input being cheaper to buy than to assemble from the competitor's prices (). At the optimum, both cost the same. The dual variables are the shadow prices.
Max-flow min-cut
The dual of the maximum-flow LP is a minimum-cut LP. Strong duality gives the famous theorem: the maximum flow from to equals the minimum total capacity of edges whose removal disconnects from .
import numpy as np
from scipy.optimize import linprog
from itertools import combinations
edges = [("s", "a", 3), ("s", "b", 2), ("a", "b", 1), ("a", "t", 2), ("b", "t", 3)]
nodes = ["s", "a", "b", "t"]
n_e = len(edges)
# variables: flow on each edge + total flow F; maximize F
Aeq = np.zeros((2, n_e + 1))
for j, (u, v, _) in enumerate(edges):
for k, node in enumerate(["a", "b"]):
Aeq[k, j] += (v == node) - (u == node) # inflow - outflow = 0 at inner nodes
Aout = np.zeros((1, n_e + 1)); Aout[0, -1] = -1
for j, (u, v, _) in enumerate(edges):
Aout[0, j] += (u == "s") # outflow of s equals F
res = linprog(np.r_[np.zeros(n_e), -1.0], A_eq=np.vstack([Aeq, Aout]), b_eq=np.zeros(3),
bounds=[(0, cap) for _, _, cap in edges] + [(0, None)], method="highs")
print("max flow:", -res.fun)
cuts = []
for k in range(0, 3):
for S in combinations(["a", "b"], k):
side = {"s", *S}
cuts.append(sum(cap for u, v, cap in edges if u in side and v not in side))
print("min cut capacity:", min(cuts))max flow: 5.0
min cut capacity: 5
In software
- Solvers return dual values with every LP solution (
res.ineqlin.marginalsin SciPy). The dual simplex method, which keeps the dual feasible, is the default re-optimizer in MIP solvers. - Column generation (cutting-stock, airline crew scheduling) prices new columns with the dual variables: a column with negative reduced cost is worth adding.
Exercises
Exercise 1: Write the dual
Write the dual of: minimize s.t. , , .
Answer
Maximize s.t. , , .
Solution
Transpose , swap the roles of and , and turn min into max with constraints and non-negative variables.
Exercise 2: Use complementary slackness
In Exercise 1, the primal optimum is . Which dual constraints must be tight, and what is ?
Answer
Both, giving and value 6.8.
Solution
Both , so both dual constraints are equalities: , , giving , . Check: primal value and dual value .
Exercise 3: Verify with a solver (code)
Solve both LPs of Exercise 1 with linprog and compare.
Answer
Both have optimal value 6.8.
Solution
from scipy.optimize import linprog
p = linprog([4, 6], A_ub=[[-1, -3], [-2, -1]], b_ub=[-2, -3], bounds=[(0, None)] * 2, method="highs")
d = linprog([-2, -3], A_ub=[[1, 2], [3, 1]], b_ub=[4, 6], bounds=[(0, None)] * 2, method="highs")
print([round(float(v), 4) for v in p.x], round(p.fun, 4), [round(float(v), 4) for v in d.x], round(-d.fun, 4))[1.4, 0.2] 6.8 [1.6, 1.2] 6.8
Going further: Complementary Slackness and Optimality Certificates and Max-Flow Min-Cut as LP Duality show duality at work on combinatorial problems.
Test yourself
If the primal LP is unbounded, the dual is…
- unbounded
- infeasible
- optimal with the same value
- always feasible
Complementary slackness says that if at the optimum, then…
- the -th dual constraint is tight
- the primal is degenerate
Max-flow min-cut is an instance of…
- LP strong duality
- Newton's method
- the central limit theorem
- Jensen's inequality