Introduction
Linear programming (LP) is the general name for methods that optimize a linear objective subject to linear constraints. It is a branch of operations research with applications in many areas. Special cases such as network flow and multicommodity flow may appear in programming contests. Problems that can only be solved with linear programming are rare; most can be solved more efficiently by methods such as network-flow modeling.
A Simple Example
For a problem to be expressed as a linear program, it must have several linear constraints and a linear objective function.
Consider the following example:
Example
A breakfast cook makes steamed buns and fried dough sticks, both of which are popular with customers. To maximize profit, the cook wants to make as many as possible, but is limited by ingredients, time, and other resources. The ingredient usage, preparation time, and profit for one serving are recorded below:
| Breakfast | Cooking oil | Flour | Time | Profit |
|---|---|---|---|---|
| Steamed buns | ||||
| Fried dough sticks |
Suppose the cook can buy at most units of oil and units of flour each day, and can spend at most units of preparation time. How should the numbers of buns and dough sticks be chosen to maximize daily profit?
In mathematical language, let and be the numbers of buns and dough sticks. “The total amount of oil required is at most units” becomes
Similarly, “the total flour is at most units” and “the total time is at most units” become
The cook cannot produce a negative number of servings, so we also require
The cook must maximize profit subject to these constraints:
This is a typical linear programming problem. Its objective is linear in the decision variables, and its constraints are linear equalities or inequalities in those variables.
Graphical Method
A linear program with only two decision variables can be solved intuitively by the graphical method.
Consider the problem in this section
The corresponding geometric picture is shown below. The last constraint says that every candidate point lies in the first quadrant. The other three constraints say that it lies below the lines , , and . The intersection of these regions, shown in green, is the set of all choices:
We next maximize . Treating this equation as the line , varying gives a family of parallel lines; larger moves the line farther toward the upper right. Move the line until it reaches a critical position where any further movement makes it miss the depicted region. The corresponding is the maximum.
As shown, this occurs at the red point, the intersection of and . Solving the two equations gives . This is the unique optimum, and the cook's maximum profit is .
When there are more than two decision variables, the graphical method no longer applies. Some observations from this example remain valid: each inequality constraint describes a half-plane, and the feasible set is their intersection, hence a convex polygon. An optimum can always be attained at a vertex of this polygon, whose coordinates can be found by solving equations for the boundaries of the half-planes. Extending these observations to higher dimensions leads to the simplex method, an efficient method for solving linear programs and the one most often used in programming contests.
Another point is that, in principle, the numbers of buns and dough sticks are integers rather than infinitely divisible quantities. The problem does not explicitly impose this, but its optimum happens to be integral, so adding integrality does not change the answer. In many programs, however, an optimum need not occur at an integer point; such problems are integer programs rather than ordinary linear programs. This class is briefly discussed at the end.
Basic Concepts
This section introduces basic concepts of linear programming.
Linear Programs
A linear program usually consists of two parts:
-
A linear objective function of the form
where are constants;
-
Linear constraints of the form
where are constants.
A linear program maximizes or minimizes its objective subject to the given constraints. A solution satisfying the constraints is a feasible solution; among feasible solutions, one attaining the optimum objective value is an optimal solution.
Standard Form
For convenient description and further processing, we choose a standard form for linear programs. Different references use different conventions; here the standard form is:
In other words, this is a minimization problem in which all decision variables are nonnegative and the only other constraints are equalities with nonnegative right-hand sides. Using matrices gives a more concise expression:
Here is the decision vector, while and are constants in the constraints. The size of a linear program means the number of decision variables and constraints.
Vector Inequalities
Vector inequalities such as occur repeatedly in this article. In general, for vectors , the inequality means , comparing real values coordinate by coordinate. This relation is a partial order on the vector space, so some pairs of vectors are incomparable.
The choice of standard form is only for convenience. It is not special, because every linear program can equivalently be written in one of the following six forms:
The following operations convert any linear program equivalently into one of these six forms:
- Multiplying by a negative sign, replacing with , converts maximization to minimization and vice versa.
- Multiplying by a negative sign, replacing with , reverses an inequality or makes the right-hand side of an equality nonnegative.
- Every equality can be replaced by the two opposite inequalities and .
- Every inequality can be converted by adding a nonnegative slack variable into the equality together with .
- If a decision variable is unrestricted in sign, replace it by the difference of two nonnegative variables: with .
The transformed program has size at most twice that of the original, and feasible and optimal solutions can be converted easily in both directions. Thus a general linear program can first be converted to standard form (or one of the six forms) before being solved.
Example
Consider the linear program
Operations 1, 2, and 3 convert it into the form , namely
Operations 4 and 5 convert it into the form , namely
Feasible Regions and Solutions
The set of all feasible solutions is the feasible region of . Geometrically, each inequality describes a half-space , and each equality describes a hyperplane . Thus the feasible region is an intersection of finitely many half-spaces and hyperplanes. In optimization1,such a geometric object is called a polyhedron in . A polyhedron is always closed and convex but need not be bounded. A bounded polyhedron is also called a polytope. A polytope generalizes a polygon to higher dimensions, while a polyhedron further allows unboundedness.
Examples of Polyhedra
Here are some common polyhedra:
- The empty set , also called a nullitope, with dimension defined as .
- An affine subspace, the intersection of several hyperplanes . It is the solution set of the linear system : if the system is inconsistent, it is empty; otherwise, it can always be written as , where and is an -dimensional linear subspace. In particular, a hyperplane is also an affine subspace.
- A polyhedral cone, the set of all nonnegative combinations of finitely many points , namely . It is a convex cone with its apex at the origin. Equivalently, it is the polyhedron bounded by hyperplanes through the origin, . In particular, a half-space is also a polyhedral cone.
- A polytope, namely a bounded polyhedron. In particular, the -, -, -, -, and -dimensional polytopes are the familiar empty set, point, segment, polygon, and (in the usual sense) polyhedron. A set is a polytope if and only if it is the convex hull of finitely many points , namely . A -dimensional polytope is generated by at least points.
- A simplex, namely a polytope generated by exactly points and of dimension . It is the simplest -dimensional polytope. In particular, the -, -, -, -, and -dimensional polytopes are the empty set, point, segment, triangle, and tetrahedron. The simplest -dimensional simplex is , and every -dimensional simplex can be transformed into this special case by an affine transformation (translation and scaling). Note that the simplex method is not actually performed on a simplex.
Every polyhedron can be viewed as the Minkowski sum: the former describes the unbounded part and the latter the shape of the bounded part. This polyhedral cone is unique: the polyhedral cone in the decomposition of is always .
Solutions of linear programs are closely related to polyhedral structure. For a polyhedron and a vector , consider the program below (the minimization case is analogous):
Geometrically, given the hyperplane and the feasible region , move in the direction of the hyperplane while keeping it intersecting the region, and make as large as possible. There are three possibilities:
-
The feasible region is empty. Then has no feasible solution because some constraints contradict one another. is infeasible, and its optimum is defined as .
-
The feasible region is nonempty but contains a ray in direction : some satisfies for every . Because along direction the hyperplane can keep moving, and remains nonempty, the objective can be arbitrarily large. Then is unbounded, and its optimum is defined as .
-
The feasible region is nonempty and contains no ray in direction . Then problem is bounded. Let be the optimum of problem . The hyperplane is critical: it intersects the polyhedron , and lies in the half-space . This hyperplane is a supporting hyperplane of the polyhedron . The optimal set of is . As the intersection of a supporting hyperplane and a polyhedron, is a polyhedron contained in the boundary of . It is called a face of . Informally, the polyhedron is bounded by these faces. In addition to faces formed this way, a polyhedron generally has two more faces: the empty set and the polyhedron itself. Ordered by inclusion, all faces form a lattice structure.
A face of a -dimensional polyhedron has an integer dimension between and . A -dimensional face (a point) is a vertex or corner point of polyhedron ; a -dimensional face is an edge of ; and a -dimensional face is a facet of . Not every polyhedron has vertices. Since a face of a face is still a face, and only affine subspaces have no strictly smaller nonempty face, all minimal faces of are affine subspaces. The minimal faces of the same polyhedron have the same dimension; in particular, the minimal faces of have dimension .
A face of a polyhedron is the solution set of a bounded linear program, so we need to determine the equations of its faces. Suppose the polyhedron is described by constraints , and is a face of . If a constraint is an equality for every , it is tight on . Points of satisfy the equations obtained from these tight constraints, and the intersection of the resulting affine subspace with is . Conversely, choosing any subset of the constraints of , setting them to equality, and intersecting the resulting affine subspace with gives a face of . The more tight constraints are chosen, the smaller the face under inclusion.
In particular, the feasible region of standard form has coefficient matrix of rank , so its minimal faces are vertices. Thus, if the problem is bounded, an optimum can be chosen at a vertex, obtained by solving linearly independent tight constraints. This is a convenience of standard form.
Example
In the figure, is the feasible region. Objective coefficients give, respectively, a unique optimum, multiple optima, and an unbounded problem. In the first two cases, the corresponding thick red line is a supporting hyperplane of ; the optimal sets are the vertex and edge . In the third case, contains a ray in direction , so a hyperplane with normal can keep moving in the direction of , and the problem is unbounded.
The discussion above ignores . Then the program cannot be unbounded: it is either infeasible, or its optimum is and its optimal set is all of . This special type is called feasibility linear programming.
It is worth noting that deciding feasibility or boundedness of a linear program, and finding a feasible solution to a system of inequalities, are just as difficult as solving the linear program itself2. For example, the proof of the strong duality theorem below shows that solving a bounded linear program is equivalent to finding a feasible solution to a system of inequalities. Therefore, for tasks such as deciding whether a system of inequalities has a solution or whether a system of equations has a nonnegative solution, the most effective approach is to solve the corresponding feasibility linear program3.
If a constraint is tight on no face of the feasible region, it is redundant. In the cook's example at the beginning, the time constraint is redundant. To determine whether is redundant in a system, solve and compare it with .
Common Algorithms
In programming contests, few problems can only be solved by a linear-programming algorithm. Most problems solvable by linear programming can usually be solved more efficiently by specialized algorithms such as network flow.
Common algorithms for linear programming are:
- Simplex method
- Ellipsoid method
- Interior-point method
Although the simplex method has exponential worst-case complexity and interior-point methods have polynomial complexity, both perform extremely well in most practical problems. The ellipsoid method also has polynomial theoretical complexity, but is usually slow and impractical.
It is not currently known whether linear programming has a strongly polynomial-time algorithm.
Dual Problems
Every linear program has a corresponding dual problem. Their solutions are closely related. The dual can reveal the structure of the original problem and often improve the efficiency of solving it.
For the linear program (lowercase variables here are vectors)
its dual problem is the linear program
The dual variables are the Lagrange multipliers for the three types of constraints in the original problem. Conversely, are the Lagrange multipliers for the three types of constraints in the dual. It is easy to verify that the dual of the dual is the original problem.
The correspondence between and is:
| Minimization | Maximization |
|---|---|
| Greater-than-or-equal constraint | Nonnegative variable |
| Less-than-or-equal constraint | Nonpositive variable |
| Equality constraint | Unrestricted variable |
| Nonnegative variable | Less-than-or-equal constraint |
| Nonpositive variable | Greater-than-or-equal constraint |
| Unrestricted variable | Equality constraint |
| Objective coefficient | Right-hand-side constant |
| Right-hand-side constant | Objective coefficient |
In particular, the dual of the standard-form program
is
Duality Principle
The original and dual problems are not only mirror images in form; their solutions are also closely related. This is the duality principle. For convenience, the theorems and proofs below use the standard-form primal problem.
First, the weak duality theorem says that the maximum of the dual is no greater than the minimum of the primal.
Weak duality theorem
For every , , and , we have
Proof
If either the primal or dual is infeasible, the inequality is trivial. Assume both are feasible. For every feasible and ,
Taking the extrema on both sides proves weak duality.
Weak duality leaves only four possible situations for the primal and dual:
- Both are infeasible, namely ;
- The primal is infeasible and the dual is unbounded, namely ;
- The primal is unbounded and the dual is infeasible, namely ;
- Both are bounded.
Weak duality has many corollaries. For example, it gives a way to detect primal unboundedness from primal and dual feasibility.
Corollary
A linear program is unbounded if and only if it is feasible and its dual is infeasible.
Applying weak duality to feasibility linear programs gives Farkas' lemma and its variants.
Farkas' lemma
For and , exactly one of the following holds:
- There is an with and ;
- There is a with and .
Proof
Consider , whose dual is . The dual is feasible because is feasible. By weak duality, either the primal is feasible or the dual is unbounded, but not both. These are exactly cases 1 and 2 of Farkas' lemma.
Farkas' lemma is in fact a hyperplane separation theorem. Case 1 says that the point lies in the polyhedral cone generated by the columns of , ; therefore, Farkas' lemma says that, if and only if the point is not in this convex cone , there is a hyperplane with normal vector , , through the origin that strictly separates the point from the cone .
In the fourth case allowed by weak duality, a stronger conclusion holds: the primal and dual optima are equal. Combining this with the other three cases gives strong duality: whenever either the primal or the dual is feasible, their optimal values are equal.
Strong duality theorem
For every , , and ,
provided at least one of the two sets is nonempty.
Proof
The only case not covered by weak duality is when both primal and dual are feasible. Consider the following feasibility program :
If has a feasible solution , weak duality and optimality give
But , so all inequalities are equalities. Thus strong duality holds, and and are optimal primal and dual solutions.
It remains to prove that is feasible. Suppose not. As in the proof of Farkas' lemma, consider problem 's dual :
Since is feasible for , weak duality says that infeasibility of implies unboundedness of . Thus some satisfies
If , these inequalities make feasible for the preceding problem, a contradiction. Hence , so
However, since both the primal and dual in the theorem are assumed feasible, some satisfies
and therefore
This is a contradiction. Hence is feasible, and strong duality follows.
The proof of strong duality also gives:
Corollary
If feasible primal and dual solutions and satisfy strong duality, , then they are optimal solutions to the primal and dual, respectively.
Strong duality says that for a feasible linear program, solving the dual is enough to obtain the primal optimum.
Complementary Slackness
As in other optimization problems, complementary slackness is part of the optimality conditions. Since the objective is linear, for linear programs it is also necessary and sufficient for feasible solutions to be optimal.
The complementary slackness condition says that a variable in the dual (primal) can be nonzero only when the corresponding constraint in the primal (dual) is an equality, or tight. If a nonzero variable is viewed as a slack constraint, this says that corresponding variables and constraints in the primal and dual cannot both be slack.
For a standard-form linear program:
Theorem
Suppose and are feasible solutions to the primal and dual Then they are both optimal if and only if complementary slackness holds, namely
then and are optimal solutions to the primal and dual, respectively.
Proof
Since and are feasible,
Thus complementary slackness holds if and only if . By the strong-duality corollary, this is equivalent to and being optimal.
Standard form may be too restrictive. A slightly more general version is:
Theorem
Suppose and are feasible solutions to the primal and dual Then they are both optimal if and only if complementary slackness holds, namely
Then and are optimal solutions to the primal and dual, respectively.
Proof
The proof is essentially the same, except that the difference is written as
Complementary slackness provides a simple test for optimality of feasible solutions to a linear program.
Primal-Dual Method
The dual can assist in solving the primal. A common approach is the primal-dual method, which solves a sequence of relatively simple auxiliary problems to improve a dual solution and eventually obtain an optimal primal solution.
For the standard-form primal
and its dual
The previous section showed that it suffices to find feasible solutions to and satisfying complementary slackness . Consider the following procedure:
-
Start from a feasible dual solution to , namely , and compute the set of tight dual constraints
-
By complementary slackness, if there is a feasible solution to , denoted , with only when , we have found optimal solutions. Thus consider the linear program
-
If the minimum of is , then the optimum has as an optimum of the primal . Otherwise, solve the dual for :
Strong duality gives .
-
Use the solution of to improve the feasible solution of . Let with . Then . Choose as large as possible while keeping feasible for .
For ,
so these constraints of remain satisfied.
For the remaining constraints, , take
This improves the dual as much as possible while preserving feasibility; then return to step 1. If the set in the formula is empty, so , the dual is unbounded and the primal is infeasible.
Only truly needs to be solved; strong duality links problem to . Problem supplies a direction for improving the dual solution and has a simpler form than the dual . Farkas' lemma guarantees feasibility of problem , while the constraint is a normalization condition that makes problem bounded.
In programming contests, the primal-dual method is widely used in combinatorial optimization. Examples include the Hungarian algorithm, the cycle-canceling algorithm for minimum-cost flow, the SSP algorithm (primal-dual algorithm), Dijkstra's algorithm for shortest paths, and the Ford–Fulkerson augmenting algorithm for maximum flow; all can be viewed as direct applications of the primal-dual method.
Integer Programming
Integer programming usually means integer linear programming (ILP). Its standard form is:
Here , , and . An ILP is obtained by adding the requirement that the decision variables of a linear program be integers.
Integer constraints substantially increase complexity. Many combinatorial optimization problems, such as knapsack, satisfiability, and numerous graph-optimization problems, can be modeled as ILPs, and most of them are known to be NP-hard.
Totally Unimodular Matrices
For this reason, large integer optimization problems are sometimes relaxed by dropping integrality and solving an LP instead. For minimization, the relaxed LP optimum is generally only a lower bound for the ILP optimum. If the relaxed optimum happens to be integral, it is also an optimum of the original ILP.
A natural question is whether some condition guarantees that every LP optimum is integral. Totally unimodular matrices provide such a condition.
Totally unimodular matrix
A matrix is totally unimodular if every square submatrix has determinant or . The matrix is therefore called totally unimodular.
In particular, every entry of a totally unimodular matrix is or . This gives the following result:
Theorem
For a totally unimodular matrix , , and , the LP and its dual
have integer optima whenever they are bounded.
Proof
Earlier we saw that an LP optimum can be chosen on a minimal face, which is the solution set of the equations from several linearly independent tight constraints:
Write this system as , with , where is a full-rank square matrix with determinant . By Cramer's rule, the solution
is an integer solution on the minimal face.
In common graph models, the coefficient matrices of the LPs for network flow, shortest path, and bipartite graphs are totally unimodular. If the parameters are integral, their optima can therefore be chosen integral, with no fractional flows or matchings. Thus maximum flow, minimum cut, minimum-cost flow, shortest path, difference constraints, and maximum (weight) matching and minimum vertex cover in bipartite graphs can all be formulated as linear programs. Moreover, maximum flow and minimum cut, shortest path and difference constraints, and maximum bipartite matching and minimum vertex cover are pairwise dual problems.
Some other graph models have feasible solutions exactly equal to all vertices of a polytope whose vertices are integral. By choosing constraints carefully, the corresponding combinatorial optimum is an LP optimum. General-graph matching and spanning-tree models are examples, so general-graph maximum (weight) matching and minimum spanning tree can likewise be formulated as linear programs.
References and Notes
- Schrijver, Alexander. Theory of linear and integer programming. John Wiley & Sons, 1998.
- Papadimitriou, Christos H., and Kenneth Steiglitz. Combinatorial optimization: algorithms and complexity. Courier Corporation, 1998.
- Duality in linear programming. Part 1—definition and construction. by adamant - Codeforces blog
- Duality in linear programming. Part 2—in competitive programming. by adamant - Codeforces blog
Footnotes
-
Different references define these terms differently: some call the bounded case a “polyhedron” and the unbounded case a “polytope”; some do not assume convexity; and some use “polyhedron” for a three-dimensional polytope. This article follows the definitions in Schrijver (1998), Boyd and Vandenberghe (2004), and similar references. ↩
-
More precisely, they are mutually reducible in polynomial time. ↩
-
Other methods for systems of inequalities include Fourier–Motzkin elimination and the Agmon–Motzkin–Schoenberg relaxation method. They are more direct but often inefficient. ↩