Weak and Strong Duality
The dual was built to give upper bounds on a maximization. Weak duality says that it always does, with a two-line proof. Strong duality says that the best bound is exact: the primal maximum equals the dual minimum. Weak duality is what algorithm analyses use every day (a dual feasible solution bounds OPT); strong duality is what turns LP pairs into min-max theorems. This lesson proves the first, proves the second from Farkas' lemma (in a sketch whose remaining step is the next lesson), lists the four possible outcomes of a primal-dual pair, and shows where the simplex method hides an optimal dual solution. The convex-analysis versions (Slater's condition, the duality gap of non-linear problems) are in Weak and Strong Duality.
Throughout, the pair is
Weak duality
Theorem (weak duality). If is feasible for and is feasible for , then .
Proof. Using with , then with :
Each inequality multiplies a vector inequality by a non-negative vector, which is why the sign conditions in the recipe are what they are.
Three corollaries are used constantly:
- If for feasible and , both are optimal (a certificate of optimality).
- If is unbounded, is infeasible (any dual feasible would bound ), and vice versa.
- Any dual feasible bounds OPT of , even if nobody knows OPT. Approximation algorithms prove "my solution costs at most times OPT" by comparing it with a dual feasible solution.
The program samples feasible points on both sides of the workshop LP and checks that every primal value lies below every dual value.
import random
A = [[2, 1], [1, 3], [1, 0]]
b = [10, 15, 4]
c = [3, 2]
rng = random.Random(1947)
def primal_feasible(x):
return all(v >= 0 for v in x) and all(sum(a * v for a, v in zip(row, x)) <= bi for row, bi in zip(A, b))
def dual_feasible(y):
return all(v >= 0 for v in y) and all(sum(A[i][j] * y[i] for i in range(3)) >= c[j] for j in range(2))
xs, ys = [], []
while len(xs) < 2000:
x = (rng.uniform(0, 5), rng.uniform(0, 5))
if primal_feasible(x):
xs.append(x)
while len(ys) < 2000:
y = (rng.uniform(0, 3), rng.uniform(0, 3), rng.uniform(0, 3))
if dual_feasible(y):
ys.append(y)
best_primal = max(c[0] * x[0] + c[1] * x[1] for x in xs)
best_dual = min(sum(bi * yi for bi, yi in zip(b, y)) for y in ys)
print("largest sampled primal value:", round(best_primal, 3))
print("smallest sampled dual value: ", round(best_dual, 3))
print("weak duality held for all pairs:", best_primal <= best_dual)largest sampled primal value: 16.97
smallest sampled dual value: 18.863
weak duality held for all pairs: True
The primal samples come within a few hundredths of the common optimum 17. The dual samples stay further above it: the dual optimum is a single corner of a three-dimensional region, which uniform samples rarely approach. Neither side crosses 17. Checking all pairs is unnecessary: comparing the best primal with the best dual sample is enough.
Strong duality
Theorem (strong duality). If has an optimal solution, so does , and the optimal values are equal.
Proof sketch from Farkas' lemma
Let be the optimal value of and fix . The system , , has no solution. A version of Farkas' lemma (next lesson) says that a system is infeasible exactly when some has and . With and , this gives and with
If , then and , which by Farkas again would mean that is infeasible; but has an optimum. So , and is dual feasible with value below . By weak duality the dual values are at least , so the dual infimum is exactly . A feasible, bounded LP attains its optimum at a vertex (fundamental theorem), so the dual has an optimal solution of value .
A second proof is constructive: when the simplex method stops, the reduced costs of the slack columns, negated, form a dual feasible with equal to the primal value. The next program shows this.
The four possible outcomes
For a primal-dual pair, only these combinations can occur:
| has an optimum | unbounded | infeasible | |
|---|---|---|---|
| has an optimum | yes, equal values | impossible | impossible |
| unbounded | impossible | impossible | possible |
| infeasible | impossible | possible | possible |
The last cell surprises people: both LPs can be infeasible at once. The program solves one example of each possible case with SciPy.
from scipy.optimize import linprog
def status(c, A, b, kind):
# kind "P": max c^T x, Ax <= b, x >= 0 kind "D": min b^T y, A^T y >= c, y >= 0
if kind == "P":
r = linprog([-v for v in c], A_ub=A, b_ub=b, bounds=[(0, None)] * len(c), method="highs")
val = -r.fun if r.status == 0 else None
else:
At = [[-A[i][j] for i in range(len(A))] for j in range(len(c))]
r = linprog(b, A_ub=At, b_ub=[-v for v in c], bounds=[(0, None)] * len(A), method="highs")
val = r.fun if r.status == 0 else None
name = {0: "optimal", 2: "infeasible", 3: "unbounded"}[r.status]
return name + (f" ({round(float(val), 6)})" if val is not None else "")
cases = {
"workshop": ([3, 2], [[2, 1], [1, 3], [1, 0]], [10, 15, 4]),
"primal unbounded": ([1, 1], [[1, -1]], [1]),
"primal infeasible": ([1, 1], [[1, 1], [-1, -1]], [1, -2]),
"both infeasible": ([2, -1], [[1, -1], [-1, 1]], [1, -2]),
}
for name, (c, A, b) in cases.items():
print(f"{name:18} P: {status(c, A, b, 'P'):16} D: {status(c, A, b, 'D')}")workshop P: optimal (17.0) D: optimal (17.0)
primal unbounded P: unbounded D: infeasible
primal infeasible P: infeasible D: unbounded
both infeasible P: infeasible D: infeasible
In the last case the primal rows add up to , and the dual constraints and contradict each other: neither side has a feasible point.
The dual hidden in the final tableau
When the simplex method stops, the objective row reads with . For a slack column , the reduced cost is where , so . Optimality () is then exactly dual feasibility: from the slack columns and from the original columns. The program runs the Bland simplex of the earlier lesson on the LP and reads off .
from fractions import Fraction as F
def simplex(c, A, b):
m, n = len(A), len(c)
T = [[F(v) for v in A[i]] + [F(int(i == k)) for k in range(m)] + [F(b[i])] for i in range(m)]
z = [F(v) for v in c] + [F(0)] * (m + 1)
basis = [n + i for i in range(m)]
while True:
improving = [j for j in range(n + m) if z[j] > 0]
if not improving:
x = [F(0)] * n
for i, j in enumerate(basis):
if j < n:
x[j] = T[i][-1]
return x, [-z[n + i] for i in range(m)], -z[-1]
e = min(improving)
l = min((i for i in range(m) if T[i][e] > 0), key=lambda i: (T[i][-1] / T[i][e], basis[i]))
T[l] = [v / T[l][e] for v in T[l]]
for i in range(m):
if i != l and T[i][e] != 0:
f = T[i][e]
T[i] = [a - f * p for a, p in zip(T[i], T[l])]
f = z[e]
z = [a - f * p for a, p in zip(z, T[l])]
basis[l] = e
c, A, b = [1, 2, 4], [[1, 1, 2], [2, 0, 1], [2, 1, 3]], [4, 5, 7]
x, y, value = simplex(c, A, b)
print("primal x =", [str(v) for v in x], " value", value)
print("dual y =", [str(v) for v in y], " b^T y =", sum(bi * yi for bi, yi in zip(b, y)))
print("A^T y >= c:", [str(sum(A[i][j] * y[i] for i in range(3))) + " >= " + str(c[j]) for j in range(3)])primal x = ['0', '0', '2'] value 8
dual y = ['2', '0', '0'] b^T y = 8
A^T y >= c: ['2 >= 1', '2 >= 2', '4 >= 4']
The dual solution says that the first constraint alone, multiplied by 2, proves the bound: dominates coefficient by coefficient. The other two constraints are not needed for the proof, and indeed they are not tight at the optimum, which is the content of complementary slackness.
Test yourself
Which facts does the proof of weak duality use?
- Only
- together with , and together with
- Strong duality
- That is square
The primal (a maximization) is unbounded. What can you say about its dual?
- It is unbounded too
- It is infeasible
- It has an optimum equal to
- Nothing
Can a primal LP and its dual both be infeasible?
- No, strong duality forbids it
- Yes
- Only if is the zero matrix
- Only for equality constraints
Where does the simplex method's final tableau store an optimal dual solution?
- In the right-hand side column
- In the negated reduced costs of the slack columns
- In the pivot elements
- It does not contain one