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 . 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 and . Exactly one of the following holds:
- there is with and ;
- there is with and .
They cannot both hold: if with and , then . 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 satisfies |
|---|---|
| , | , |
| , | , , |
| ( free) | , , |
The last row reads naturally: a non-negative combination of the rows of that cancels every variable produces , 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: has no solution exactly when some has and .)
The geometric picture
The set is the cone generated by the columns of : all their non-negative combinations. Alternative 1 says . Alternative 2 says that the hyperplane through the origin has every column on its non-negative side () and 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.
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 < 0The program tests two right-hand sides against the cone of and . For a invertible the decision is a matter of solving : either the solution is non-negative, or a negative coordinate yields a certificate, the -th row of (it is orthogonal to the other column, positive on , and negative on ).
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 for is the line of the picture: both columns have , and .
Proof sketch
Suppose . Two facts give the separating :
- 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.
- Project onto . Let be the point of closest to (it exists because is closed and non-empty). Put . The optimality of gives for all ; using and gives , so for all , in particular . And .
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 , , (with free) and checks two candidate certificates for the third variant of the table.
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 <= -2y = (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 in the combination, and the check catches it. The valid certificate adds the first two rows () to the third (), which gives .
A combinatorial reading: Hall's theorem
A bipartite graph with sides and of equal size has a perfect matching exactly when the system " for every vertex , " is feasible; the polytope is integral (Totally Unimodular Matrices), so a fractional solution implies an integral one. Farkas' lemma says that otherwise there is with on every edge and . A Hall violator, a set with fewer than neighbours, gives one: on , on its neighbourhood , elsewhere. Every edge leaving ends in , so , and . The program finds a violator by brute force and checks the certificate.
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 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 , ?
- The system always has a solution
- Either it has a solution, or some has and , but not both
- It has a solution iff is invertible
- It has a solution iff
What is the geometric meaning of a Farkas certificate ?
- A vertex of the polytope
- A hyperplane through the origin with every column of on one side and strictly on the other
- The projection of onto the columns
- An eigenvector of
For a system with free , what does a certificate of infeasibility look like?
- with and
- free with
- with
- with