TExcelTAD
Home › Linear Programming & Duality for Algorithms › Weak and Strong Duality

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

(P)  max⁡{cTx:Ax≤b, x≥0}(D)  min⁡{bTy:ATy≥c, y≥0}.(P)\ \ \max \{ c^T x : Ax \le b,\ x \ge 0 \} \qquad\qquad (D)\ \ \min \{ b^T y : A^T y \ge c,\ y \ge 0 \} .

Weak duality

Theorem (weak duality). If xx is feasible for (P)(P) and yy is feasible for (D)(D), then cTx≤bTyc^T x \le b^T y.

Proof. Using x≥0x \ge 0 with ATy≥cA^T y \ge c, then y≥0y \ge 0 with Ax≤bAx \le b:

cTx≤(ATy)Tx=yT(Ax)≤yTb.c^T x \le (A^T y)^T x = y^T (A x) \le y^T b .

Each inequality multiplies a vector inequality by a non-negative vector, which is why the sign conditions in the recipe are what they are. □\square

Three corollaries are used constantly:

  1. If cTx=bTyc^T x = b^T y for feasible xx and yy, both are optimal (a certificate of optimality).
  2. If (P)(P) is unbounded, (D)(D) is infeasible (any dual feasible yy would bound (P)(P)), and vice versa.
  3. Any dual feasible yy bounds OPT of (P)(P), even if nobody knows OPT. Approximation algorithms prove "my solution costs at most α\alpha 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.

python · try it yourself
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 (75,15,0)(\frac{7}{5}, \frac{1}{5}, 0) is a single corner of a three-dimensional region, which uniform samples rarely approach. Neither side crosses 17. Checking all 200022000^2 pairs is unnecessary: comparing the best primal with the best dual sample is enough.

Strong duality

Theorem (strong duality). If (P)(P) has an optimal solution, so does (D)(D), and the optimal values are equal.

Proof sketch from Farkas' lemma

Let γ\gamma be the optimal value of (P)(P) and fix ε>0\varepsilon > 0. The system Ax≤bAx \le b, −cTx≤−(γ+ε)-c^T x \le -(\gamma + \varepsilon), x≥0x \ge 0 has no solution. A version of Farkas' lemma (next lesson) says that a system {Mx≤d, x≥0}\{ Mx \le d,\ x \ge 0 \} is infeasible exactly when some u≥0u \ge 0 has MTu≥0M^T u \ge 0 and dTu<0d^T u < 0. With M=(A−cT)M = \binom{A}{-c^T} and d=(b−γ−ε)d = \binom{b}{-\gamma - \varepsilon}, this gives y≥0y \ge 0 and λ≥0\lambda \ge 0 with

ATy≥λcandbTy<λ(γ+ε).A^T y \ge \lambda c \qquad \text{and} \qquad b^T y < \lambda (\gamma + \varepsilon).

If λ=0\lambda = 0, then ATy≥0A^T y \ge 0 and bTy<0b^T y < 0, which by Farkas again would mean that {Ax≤b, x≥0}\{ Ax \le b,\ x \ge 0 \} is infeasible; but (P)(P) has an optimum. So λ>0\lambda > 0, and y/λy / \lambda is dual feasible with value below γ+ε\gamma + \varepsilon. By weak duality the dual values are at least γ\gamma, so the dual infimum is exactly γ\gamma. A feasible, bounded LP attains its optimum at a vertex (fundamental theorem), so the dual has an optimal solution of value γ\gamma.

A second proof is constructive: when the simplex method stops, the reduced costs of the slack columns, negated, form a dual feasible yy with bTyb^T y equal to the primal value. The next program shows this.

The four possible outcomes

For a primal-dual pair, only these combinations can occur:

(D)(D) has an optimum (D)(D) unbounded (D)(D) infeasible
(P)(P) has an optimum yes, equal values impossible impossible
(P)(P) unbounded impossible impossible possible
(P)(P) 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.

python · try it yourself
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 0≤−10 \le -1, and the dual constraints y1−y2≥2y_1 - y_2 \ge 2 and −y1+y2≥−1-y_1 + y_2 \ge -1 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 z=z∗+∑j∈Ncˉjxjz = z^* + \sum_{j \in N} \bar c_j x_j with cˉj≤0\bar c_j \le 0. For a slack column sis_i, the reduced cost is cˉsi=0−yi\bar c_{s_i} = 0 - y_i where yT=cBTAB−1y^T = c_B^T A_B^{-1}, so y=−(reduced costs of the slacks)y = -(\text{reduced costs of the slacks}). Optimality (cˉ≤0\bar c \le 0) is then exactly dual feasibility: y≥0y \ge 0 from the slack columns and ATy≥cA^T y \ge c from the original columns. The program runs the Bland simplex of the earlier lesson on the LP max⁡x1+2x2+4x3\max x_1 + 2x_2 + 4x_3 and reads off yy.

python · try it yourself
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 y=(2,0,0)y = (2, 0, 0) says that the first constraint alone, multiplied by 2, proves the bound: 2(x1+x2+2x3)≤82(x_1 + x_2 + 2x_3) \le 8 dominates x1+2x2+4x3x_1 + 2x_2 + 4x_3 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?

  1. Only Ax≤bAx \le b
  2. x≥0x \ge 0 together with ATy≥cA^T y \ge c, and y≥0y \ge 0 together with Ax≤bAx \le b
  3. Strong duality
  4. That AA is square

The primal (a maximization) is unbounded. What can you say about its dual?

  1. It is unbounded too
  2. It is infeasible
  3. It has an optimum equal to +∞+\infty
  4. Nothing

Can a primal LP and its dual both be infeasible?

  1. No, strong duality forbids it
  2. Yes
  3. Only if AA is the zero matrix
  4. Only for equality constraints

Where does the simplex method's final tableau store an optimal dual solution?

  1. In the right-hand side column
  2. In the negated reduced costs of the slack columns
  3. In the pivot elements
  4. It does not contain one