TExcelTAD

Farkas' Lemma

How do you prove that a system of linear inequalities has no solution? For a solvable system the proof is easy: show a solution. Farkas' lemma (Farkas, 1902) says that unsolvable systems also have short proofs: a single vector of multipliers that combines the constraints into an obvious contradiction such as 0≤−10 \le -1. It is the theorem of the alternative behind strong duality, behind solvers' infeasibility certificates, and, read on combinatorial LPs, behind theorems like Hall's marriage theorem. This lesson states it, draws its geometric picture, sketches the proof, and checks certificates in exact arithmetic.

The statement

Farkas' lemma. Let A∈Rm×nA \in \mathbb{R}^{m \times n} and b∈Rmb \in \mathbb{R}^m. Exactly one of the following holds:

  1. there is x∈Rnx \in \mathbb{R}^n with Ax=bAx = b and x≥0x \ge 0;
  2. there is y∈Rmy \in \mathbb{R}^m with ATy≥0A^T y \ge 0 and bTy<0b^T y < 0.

They cannot both hold: if Ax=bAx = b with x≥0x \ge 0 and ATy≥0A^T y \ge 0, then bTy=xT(ATy)≥0b^T y = x^T (A^T y) \ge 0. The content of the lemma is that one of them always holds.

There are several equivalent variants, each obtained from the first by the conversions of Standard Form, Slack Form and Combinatorial LPs:

System Has no solution exactly when some yy satisfies
Ax=bAx = b, x≥0x \ge 0 ATy≥0A^T y \ge 0, bTy<0b^T y < 0
Ax≤bAx \le b, x≥0x \ge 0 y≥0y \ge 0, ATy≥0A^T y \ge 0, bTy<0b^T y < 0
Ax≤bAx \le b (xx free) y≥0y \ge 0, ATy=0A^T y = 0, bTy<0b^T y < 0

The last row reads naturally: a non-negative combination of the rows of Ax≤bAx \le b that cancels every variable produces 0≤bTy<00 \le b^T y < 0, a contradiction. The lemma says that every infeasible system can be refuted this way. (For equations without sign constraints, the corresponding statement is the familiar one from linear algebra: Ax=bAx = b has no solution exactly when some yy has ATy=0A^T y = 0 and bTy≠0b^T y \ne 0.)

The geometric picture

The set cone⁡(A)={Ax:x≥0}\operatorname{cone}(A) = \{ Ax : x \ge 0 \} is the cone generated by the columns a1,…,ana_1, \ldots, a_n of AA: all their non-negative combinations. Alternative 1 says b∈cone⁡(A)b \in \operatorname{cone}(A). Alternative 2 says that the hyperplane {z:yTz=0}\{ z : y^T z = 0 \} through the origin has every column on its non-negative side (ajTy≥0a_j^T y \ge 0) and bb strictly on the negative side. So Farkas' lemma is a separation theorem: a point outside a finitely generated cone can be separated from it by a hyperplane through the origin.

text
         a2 (1,1)
          /
         /   cone(A) = everything between a1 and a2
        /
  -----o--------- a1 (1,0)
        \
         \   b = (1,-1) lies below a1: outside the cone
          \  separating line y^T z = 0 with y = (0,1):
              a1.y = 0 >= 0,  a2.y = 1 >= 0,  b.y = -1 < 0

The program tests two right-hand sides against the cone of a1=(1,0)a_1 = (1, 0) and a2=(1,1)a_2 = (1, 1). For a 2×22 \times 2 invertible AA the decision is a matter of solving Ax=bAx = b: either the solution is non-negative, or a negative coordinate xk<0x_k < 0 yields a certificate, the kk-th row of A−1A^{-1} (it is orthogonal to the other column, positive on aka_k, and negative on bb).

python · try it yourself
from fractions import Fraction as F

a1, a2 = (F(1), F(0)), (F(1), F(1))
A = [[a1[0], a2[0]], [a1[1], a2[1]]]                 # columns a1, a2
det = A[0][0] * A[1][1] - A[0][1] * A[1][0]
Ainv = [[A[1][1] / det, -A[0][1] / det], [-A[1][0] / det, A[0][0] / det]]

for b in [(F(3), F(1)), (F(1), F(-1))]:
    x = [Ainv[i][0] * b[0] + Ainv[i][1] * b[1] for i in range(2)]
    if min(x) >= 0:
        print(f"b = {tuple(map(str, b))}: in the cone, x = {[str(v) for v in x]}")
    else:
        k = 0 if x[0] < 0 else 1
        y = Ainv[k]                                   # row k of A^-1 separates
        ATy = [y[0] * col[0] + y[1] * col[1] for col in (a1, a2)]
        print(f"b = {tuple(map(str, b))}: outside, certificate y = {[str(v) for v in y]}, "
              f"A^T y = {[str(v) for v in ATy]}, b^T y = {y[0] * b[0] + y[1] * b[1]}")
b = ('3', '1'): in the cone, x = ['2', '1']
b = ('1', '-1'): outside, certificate y = ['0', '1'], A^T y = ['0', '1'], b^T y = -1

The certificate y=(0,1)y = (0, 1) for b=(1,−1)b = (1, -1) is the line of the picture: both columns have ajTy≥0a_j^T y \ge 0, and bTy=−1<0b^T y = -1 < 0.

Proof sketch

Suppose b∉C=cone⁡(A)b \notin C = \operatorname{cone}(A). Two facts give the separating yy:

  1. CC is closed. A finitely generated cone is a finite union of cones generated by linearly independent subsets of the columns (a Carathéodory-type argument, the same support-shrinking step as in the fundamental theorem), and each of those is the image of the closed orthant under an injective linear map, hence closed.
  2. Project bb onto CC. Let p∈Cp \in C be the point of CC closest to bb (it exists because CC is closed and non-empty). Put y=p−by = p - b. The optimality of pp gives yT(z−p)≥0y^T (z - p) \ge 0 for all z∈Cz \in C; using z=0z = 0 and z=2pz = 2p gives yTp=0y^T p = 0, so yTz≥0y^T z \ge 0 for all z∈Cz \in C, in particular ATy≥0A^T y \ge 0. And bTy=(p−y)Ty=−∥y∥2<0b^T y = (p - y)^T y = -\lVert y \rVert^2 < 0.

The analytic step (closedness, a nearest point) is what makes Farkas a genuine theorem and not just linear algebra. Purely algebraic proofs exist too, for example by Fourier-Motzkin elimination of one variable at a time, which also shows that a certificate with rational entries exists whenever the data are rational.

Certificates of infeasibility in practice

Solvers return a Farkas certificate (a "dual ray") when they report infeasibility, and you can check it with exact arithmetic without trusting the solver. The program takes the system x1+x2≤1x_1 + x_2 \le 1, x1−x2≤0x_1 - x_2 \le 0, −2x1≤−3-2x_1 \le -3 (with xx free) and checks two candidate certificates for the third variant of the table.

python · try it yourself
from fractions import Fraction as F

A = [[1, 1], [1, -1], [-2, 0]]
b = [1, 0, -3]

def check(y):
    """Is y a certificate that {x : Ax <= b} is empty?  Needs y >= 0, y^T A = 0, y^T b < 0."""
    yA = [sum(F(y[i]) * A[i][j] for i in range(3)) for j in range(2)]
    yb = sum(F(y[i]) * b[i] for i in range(3))
    ok = all(v >= 0 for v in y) and all(v == 0 for v in yA) and yb < 0
    print(f"y = {y}: y^T A = {[str(v) for v in yA]}, y^T b = {yb} -> {'valid certificate' if ok else 'not a proof'}")

check((2, 0, 1))      # a careless guess
check((1, 1, 1))      # rows 1 + 2 give 2 x1 <= 1; adding row 3 gives 0 <= -2
y = (2, 0, 1): y^T A = ['0', '2'], y^T b = -1 -> not a proof
y = (1, 1, 1): y^T A = ['0', '0'], y^T b = -2 -> valid certificate

The careless guess leaves 2x22x_2 in the combination, and the check catches it. The valid certificate adds the first two rows (2x1≤12x_1 \le 1) to the third (−2x1≤−3-2x_1 \le -3), which gives 0≤−20 \le -2.

A combinatorial reading: Hall's theorem

A bipartite graph with sides UU and WW of equal size has a perfect matching exactly when the system "∑e∋vxe=1\sum_{e \ni v} x_e = 1 for every vertex vv, x≥0x \ge 0" is feasible; the polytope is integral (Totally Unimodular Matrices), so a fractional solution implies an integral one. Farkas' lemma says that otherwise there is yy with yu+yw≥0y_u + y_w \ge 0 on every edge uwuw and ∑vyv<0\sum_v y_v < 0. A Hall violator, a set S⊆US \subseteq U with fewer than ∣S∣\lvert S \rvert neighbours, gives one: y=−1y = -1 on SS, +1+1 on its neighbourhood N(S)N(S), 00 elsewhere. Every edge leaving SS ends in N(S)N(S), so yu+yw≥0y_u + y_w \ge 0, and ∑vyv=∣N(S)∣−∣S∣<0\sum_v y_v = \lvert N(S) \rvert - \lvert S \rvert < 0. The program finds a violator by brute force and checks the certificate.

python · try it yourself
from itertools import combinations

U, W = ["u1", "u2", "u3", "u4"], ["w1", "w2", "w3", "w4"]
edges = [("u1", "w1"), ("u1", "w2"), ("u2", "w1"), ("u3", "w1"), ("u3", "w2"), ("u4", "w3"), ("u4", "w4")]
N = lambda S: {w for u, w in edges if u in S}

for k in range(1, len(U) + 1):
    viol = next((S for S in combinations(U, k) if len(N(S)) < len(S)), None)
    if viol:
        break
y = {v: 0 for v in U + W}
for u in viol:
    y[u] = -1
for w in N(viol):
    y[w] = 1
print("Hall violator S =", viol, " N(S) =", sorted(N(viol)))
print("y_u + y_w >= 0 on every edge:", all(y[u] + y[w] >= 0 for u, w in edges))
print("sum of y =", sum(y.values()), "< 0, so no perfect matching exists")
Hall violator S = ('u1', 'u2', 'u3')  N(S) = ['w1', 'w2']
y_u + y_w >= 0 on every edge: True
sum of y = -1 < 0, so no perfect matching exists

The three vertices u1,u2,u3u_1, u_2, u_3 share only two neighbours, and the certificate proves that no perfect matching, fractional or integral, exists. Hall's theorem is the converse: when no violator exists, there is a perfect matching. That converse is the hard direction, and it follows from Farkas together with integrality (or from König's theorem, later in this section).

Test yourself

Which statement is Farkas' lemma for Ax=bAx = b, x≥0x \ge 0?

  1. The system always has a solution
  2. Either it has a solution, or some yy has ATy≥0A^T y \ge 0 and bTy<0b^T y < 0, but not both
  3. It has a solution iff AA is invertible
  4. It has a solution iff b≥0b \ge 0

What is the geometric meaning of a Farkas certificate yy?

  1. A vertex of the polytope
  2. A hyperplane through the origin with every column of AA on one side and bb strictly on the other
  3. The projection of bb onto the columns
  4. An eigenvector of AA

For a system Ax≤bAx \le b with free xx, what does a certificate of infeasibility look like?

  1. y≥0y \ge 0 with ATy=0A^T y = 0 and bTy<0b^T y < 0
  2. yy free with ATy≥0A^T y \ge 0
  3. x≥0x \ge 0 with Ax=bAx = b
  4. y≤0y \le 0 with bTy>0b^T y > 0