TExcelTAD
Home › Optimization › Duality in Linear Programming

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

Primal:min⁡ cTxs.t. Ax≥b, x≥0Dual:max⁡ bTys.t. ATy≤c, y≥0\begin{aligned} &\textbf{Primal:} && \min\ \mathbf{c}^T\mathbf{x} && \text{s.t. } A\mathbf{x} \ge \mathbf{b}, \ \mathbf{x} \ge \mathbf{0} \\ &\textbf{Dual:} && \max\ \mathbf{b}^T\mathbf{y} && \text{s.t. } A^T\mathbf{y} \le \mathbf{c}, \ \mathbf{y} \ge \mathbf{0} \end{aligned}

The recipe for other forms:

Primal (min) Dual (max)
constraint aiTx≥bi\mathbf{a}_i^T\mathbf{x} \ge b_i variable yi≥0y_i \ge 0
constraint aiTx=bi\mathbf{a}_i^T\mathbf{x} = b_i variable yiy_i free
variable xj≥0x_j \ge 0 constraint a(j)Ty≤cj\mathbf{a}^{(j)T}\mathbf{y} \le c_j
variable xjx_j free constraint a(j)Ty=cj\mathbf{a}^{(j)T}\mathbf{y} = c_j

The dual of the dual is the primal.

The duality theorems

  • Weak duality: for feasible x\mathbf{x}, y\mathbf{y}: bTy≤(Ax)Ty=xTATy≤cTx\mathbf{b}^T\mathbf{y} \le (A\mathbf{x})^T\mathbf{y} = \mathbf{x}^TA^T\mathbf{y} \le \mathbf{c}^T\mathbf{x}.
  • Strong duality: if either problem has a finite optimum, so does the other, with equal values.
  • Complementary slackness: feasible x\mathbf{x}, y\mathbf{y} are both optimal iff yi(aiTx−bi)=0y_i(\mathbf{a}_i^T\mathbf{x} - b_i) = 0 for all ii and xj(cj−a(j)Ty)=0x_j(c_j - \mathbf{a}^{(j)T}\mathbf{y}) = 0 for all jj.
Primal \ Dual finite optimum unbounded infeasible
finite optimum possible (equal values) impossible impossible
unbounded impossible impossible possible
infeasible impossible possible possible

Checking a certificate

python · try it yourself
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 x\mathbf{x} and y\mathbf{y} 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 x\mathbf{x} at prices c\mathbf{c} to meet requirements b\mathbf{b}, the dual is a competitor who sells the requirements directly at unit prices y\mathbf{y}, as high as possible (max⁡bTy\max \mathbf{b}^T\mathbf{y}) but without any input being cheaper to buy than to assemble from the competitor's prices (ATy≤cA^T\mathbf{y} \le \mathbf{c}). 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 ss to tt equals the minimum total capacity of edges whose removal disconnects tt from ss.

python · try it yourself
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.marginals in 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 cj−ajTyc_j - \mathbf{a}_j^T\mathbf{y} is worth adding.

Exercises

Exercise 1: Write the dual

Write the dual of: minimize 4x1+6x24x_1 + 6x_2 s.t. x1+3x2≥2x_1 + 3x_2 \ge 2, 2x1+x2≥32x_1 + x_2 \ge 3, x≥0\mathbf{x} \ge 0.

Answer

Maximize 2y1+3y22y_1 + 3y_2 s.t. y1+2y2≤4y_1 + 2y_2 \le 4, 3y1+y2≤63y_1 + y_2 \le 6, y≥0\mathbf{y} \ge 0.

Solution

Transpose AA, swap the roles of b\mathbf{b} and c\mathbf{c}, and turn min into max with ≤\le constraints and non-negative variables.

Exercise 2: Use complementary slackness

In Exercise 1, the primal optimum is x=(1.4,0.2)\mathbf{x} = (1.4, 0.2). Which dual constraints must be tight, and what is y\mathbf{y}?

Answer

Both, giving y=(1.6,1.2)\mathbf{y} = (1.6, 1.2) and value 6.8.

Solution

Both xj>0x_j > 0, so both dual constraints are equalities: y1+2y2=4y_1 + 2y_2 = 4, 3y1+y2=63y_1 + y_2 = 6, giving y1=1.6y_1 = 1.6, y2=1.2y_2 = 1.2. Check: primal value 4(1.4)+6(0.2)=6.84(1.4) + 6(0.2) = 6.8 and dual value 2(1.6)+3(1.2)=6.82(1.6) + 3(1.2) = 6.8.

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
python · try it yourself
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…

  1. unbounded
  2. infeasible
  3. optimal with the same value
  4. always feasible

Complementary slackness says that if xj>0x_j > 0 at the optimum, then…

  1. yj=0y_j = 0
  2. the jj-th dual constraint is tight
  3. cj=0c_j = 0
  4. the primal is degenerate

Max-flow min-cut is an instance of…

  1. LP strong duality
  2. Newton's method
  3. the central limit theorem
  4. Jensen's inequality