






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































Many of the problems for which we want algorithms are optimization tasks: the shortest path, the cheapest spanning tree, the longest increasing subsequence, and so on. In such cases, we seek a solution that (1) satisfies certain constraints (for instance, the path must use edges of the graph and lead from s to t, the tree must touch all nodes, the subsequence must be increasing); and (2) is the best possible, with respect to some well-defined criterion, among all solutions that satisfy these constraints.
Linear programming describes a broad class of optimization tasks in which both the con- straints and the optimization criterion are linear functions. It turns out an enormous number of problems can be expressed in this way.
Given the vastness of its topic, this chapter is divided into several parts, which can be read separately subject to the following dependencies.
Duality
matchings
Flows and
Games
Simplex
Introduction to linear programming and reductions
In a linear programming problem we are given a set of variables, and we want to assign real values to them so as to (1) satisfy a set of linear equations and/or linear inequalities involving these variables and (2) maximize or minimize a given linear objective function.
201
202 Algorithms
Figure 7.1 (a) The feasible region for a linear program. (b) Contour lines of the objective function: x 1 + 6x 2 = c for different values of the profit c.
(a)
100 200 300 400
100
200
300
400
0
x 2
x 1
(b)
100 200 300 400
100
200
300
400
0
c = 1500 c = 1200
c = 600
x 2
x 1
Optimum point Profit = $
A boutique chocolatier has two products: its flagship assortment of triangular chocolates, called Pyramide , and the more decadent and deluxe Pyramide Nuit. How much of each should it produce to maximize profits? Let’s say it makes x 1 boxes of Pyramide per day, at a profit of $1 each, and x 2 boxes of Nuit, at a more substantial profit of $6 apiece; x 1 and x 2 are unknown values that we wish to determine. But this is not all; there are also some constraints on x 1 and x 2 that must be accommodated (besides the obvious one, x 1 , x 2 ≥ 0 ). First, the daily demand for these exclusive chocolates is limited to at most 200 boxes of Pyramide and 300 boxes of Nuit. Also, the current workforce can produce a total of at most 400 boxes of chocolate per day. What are the optimal levels of production? We represent the situation by a linear program , as follows.
Objective function max x 1 + 6x 2 Constraints x 1 ≤ 200 x 2 ≤ 300 x 1 + x 2 ≤ 400 x 1 , x 2 ≥ 0
A linear equation in x 1 and x 2 defines a line in the two-dimensional (2D) plane, and a linear inequality designates a half-space , the region on one side of the line. Thus the set of all feasible solutions of this linear program, that is, the points (x 1 , x 2 ) which satisfy all constraints, is the intersection of five half-spaces. It is a convex polygon, shown in Figure 7.1. We want to find the point in this polygon at which the objective function—the profit—is maximized. The points with a profit of c dollars lie on the line x 1 + 6x 2 = c, which has a slope of − 1 / 6 and is shown in Figure 7.1 for selected values of c. As c increases, this “profit line” moves parallel to itself, up and to the right. Since the goal is to maximize c, we must move
204 Algorithms
Figure 7.2 The feasible polyhedron for a three-variable linear program.
x 1
x 3
x 2
Optimum
More products
Encouraged by consumer demand, the chocolatier decides to introduce a third and even more exclusive line of chocolates, called Pyramide Luxe. One box of these will bring in a profit of $13. Let x 1 , x 2 , x 3 denote the number of boxes of each chocolate produced daily, with x 3 referring to Luxe. The old constraints on x 1 and x 2 persist, although the labor restriction now extends to x 3 as well: the sum of all three variables can be at most 400. What’s more, it turns out that Nuit and Luxe require the same packaging machinery, except that Luxe uses it three times as much, which imposes another constraint x 2 + 3x 3 ≤ 600. What are the best possible levels of production? Here is the updated linear program.
max x 1 + 6x 2 + 13x 3 x 1 ≤ 200 x 2 ≤ 300 x 1 + x 2 + x 3 ≤ 400 x 2 + 3x 3 ≤ 600 x 1 , x 2 , x 3 ≥ 0
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 205
The space of solutions is now three-dimensional. Each linear equation defines a 3D plane, and each inequality a half-space on one side of the plane. The feasible region is an intersection of seven half-spaces, a polyhedron (Figure 7.2). Looking at the figure, can you decipher which inequality corresponds to each face of the polyhedron? A profit of c corresponds to the plane x 1 + 6x 2 + 13x 3 = c. As c increases, this profit-plane moves parallel to itself, further and further into the positive orthant until it no longer touches the feasible region. The point of final contact is the optimal vertex: (0, 300 , 100), with total profit $3100. How would the simplex algorithm behave on this modified problem? As before, it would move from vertex to vertex, along edges of the polyhedron, increasing profit steadily. A possi- ble trajectory is shown in Figure 7.2, corresponding to the following sequence of vertices and profits:
(0, 0 , 0) $
Finally, upon reaching a vertex with no better neighbor, it would stop and declare this to be the optimal point. Once again by basic geometry, if all the vertex’s neighbors lie on one side of the profit-plane, then so must the entire polyhedron.
Here is why you should believe that (0, 300 , 100), with a total profit of $3100, is the optimum: Look back at the linear program. Add the second inequality to the third, and add to them the fourth multiplied by 4. The result is the inequality x 1 + 6x 2 + 13x 3 ≤ 3100. Do you see? This inequality says that no feasible solution (values x 1 , x 2 , x 3 satisfying the constraints) can possibly have a profit greater than 3100. So we must indeed have found the optimum! The only question is, where did we get these mysterious multipliers (0, 1 , 1 , 4) for the four inequalities? In Section 7.4 we’ll see that it is always possible to come up with such multipliers by solving another LP! Except that (it gets even better) we do not even need to solve this other LP, because it is in fact so intimately connected to the original one—it is called the dual — that solving the original LP solves the dual as well! But we are getting far ahead of our story.
What if we add a fourth line of chocolates, or hundreds more of them? Then the problem becomes high-dimensional, and hard to visualize. Simplex continues to work in this general setting, although we can no longer rely upon simple geometric intuitions for its description and justification. We will study the full-fledged simplex algorithm in Section 7.6. In the meantime, we can rest assured in the knowledge that there are many professional, industrial-strength packages that implement simplex and take care of all the tricky details like numeric precision. In a typical application, the main task is therefore to correctly express the problem as a linear program. The package then takes care of the rest. With this in mind, let’s look at a high-dimensional application.
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 207
Finally, what is the objective function? It is to minimize the total cost:
min 2000
i
wi + 320
i
hi + 400
i
fi + 8
i
si + 180
i
oi,
a linear function of the variables. Solving this linear program by simplex should take less than a second and will give us the optimum business strategy for our company.
Well, almost. The optimum solution might turn out to be fractional ; for instance, it might involve hiring 10. 6 workers in the month of March. This number would have to be rounded to either 10 or 11 in order to make sense, and the overall cost would then increase correspond- ingly. In the present example, most of the variables take on fairly large (double-digit) values, and thus rounding is unlikely to affect things too much. There are other LPs, however, in which rounding decisions have to be made very carefully in order to end up with an integer solution of reasonable quality.
In general, there is a tension in linear programming between the ease of obtaining frac- tional solutions and the desirability of integer ones. As we shall see in Chapter 8, finding the optimum integer solution of an LP is an important but very hard problem, called integer linear programming.
Next we turn to a miniaturized version of the kind of problem a network service provider might face.
Suppose we are managing a network whose lines have the bandwidths shown in Fig- ure 7.3, and we need to establish three connections: between users A and B, between B and C, and between A and C. Each connection requires at least two units of bandwidth, but can be assigned more. Connection A–B pays $3 per unit of bandwidth, and connections B–C and A–C pay $2 and $4, respectively.
Each connection can be routed in two ways, a long path and a short path, or by a combina- tion: for instance, two units of bandwidth via the short route, one via the long route. How do we route these connections to maximize our network’s revenue?
This is a linear program. We have variables for each connection and each path (long or short); for example, xAB is the short-path bandwidth allocated to the connection between A and B, and x′ AB the long-path bandwidth for this same connection. We demand that no edge’s bandwidth is exceeded and that each connection gets a bandwidth of at least 2 units.
208 Algorithms
Figure 7.3 A communications network between three users A, B, and C. Bandwidths are shown.
12
10
6
13
11
8
user
user
user
max 3 xAB + 3x′ AB + 2xBC + 2x′ BC + 4xAC + 4x′ AC xAB + x′ AB + xBC + x′ BC ≤ 10 [edge (b, B)] xAB + x′ AB + xAC + x′ AC ≤ 12 [edge (a, A)] xBC + x′ BC + xAC + x′ AC ≤ 8 [edge (c, C)] xAB + x′ BC + x′ AC ≤ 6 [edge (a, b)] x′ AB + xBC + x′ AC ≤ 13 [edge (b, c)] x′ AB + x′ BC + xAC ≤ 11 [edge (a, c)] xAB + x′ AB ≥ 2 xBC + x′ BC ≥ 2 xAC + x′ AC ≥ 2 xAB , x′ AB , xBC , x′ BC , xAC , x′ AC ≥ 0
Even a tiny example like this one is hard to solve on one’s own (try it!), and yet the optimal solution is obtained instantaneously via simplex:
xAB = 0, x′ AB = 7, xBC = x′ BC = 1. 5 , xAC = 0. 5 , x′ AC = 4. 5.
This solution is not integral, but in the present application we don’t need it to be, and thus no rounding is required. Looking back at the original network, we see that every edge except a–c is used at full capacity. One cautionary observation: our LP has one variable for every possible path between the users. In a larger network, there could easily be exponentially many such paths, and therefore
210 Algorithms
As evidenced in our examples, a general linear program has many degrees of freedom.
We will now show that these various LP options can all be reduced to one another via simple transformations. Here’s how.
2a. To turn an inequality constraint like
∑n i=1 aixi^ ≤^ b^ into an equation, introduce a new variable s and use ∑^ n
i=
aixi + s = b
s ≥ 0.
This s is called the slack variable for the inequality. As justification, observe that a vector (x 1 ,... , xn) satisfies the original inequality constraint if and only if there is some s ≥ 0 for which it satisfies the new equality constraint.
2b. To change an equality constraint into inequalities is easy: rewrite ax = b as the equiva- lent pair of constraints ax ≤ b and ax ≥ b.
This way, x can take on any real value by appropriately adjusting the new variables. More precisely, any feasible solution to the original LP involving x can be mapped to a feasible solution of the new LP involving x+, x−, and vice versa.
By applying these transformations we can reduce any LP (maximization or minimization, with both inequalities and equations, and with both nonnegative and unrestricted variables) into an LP of a much more constrained kind that we call the standard form , in which the variables are all nonnegative, the constraints are all equations, and the objective function is to be minimized. For example, our first linear program gets rewritten thus:
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 211
max x 1 + 6x 2 x 1 ≤ 200 x 2 ≤ 300 x 1 + x 2 ≤ 400 x 1 , x 2 ≥ 0
min −x 1 − 6 x 2 x 1 + s 1 = 200 x 2 + s 2 = 300 x 1 + x 2 + s 3 = 400 x 1 , x 2 , s 1 , s 2 , s 3 ≥ 0
The original was also in a useful form: maximize an objective subject to certain inequalities. Any LP can likewise be recast in this way, using the reductions given earlier.
A linear function like x 1 + 6x 2 can be written as the dot product of two vectors
c =
and x =
x 1 x 2
denoted c · x or cT^ x. Similarly, linear constraints can be compiled into matrix-vector form:
x 1 ≤ 200 x 2 ≤ 300 x 1 + x 2 ≤ 400
x 1 x 2
A x ≤ b
Here each row of matrix A corresponds to one constraint: its dot product with x is at most the value in the corresponding row of b. In other words, if the rows of A are the vectors a 1 ,... , am, then the statement Ax ≤ b is equivalent to
ai · x ≤ bi for all i = 1,... , m.
With these notational conveniences, a generic LP can be expressed simply as
max cT^ x Ax ≤ b x ≥ 0.
Figure 7.4(a) shows a directed graph representing a network of pipelines along which oil can be sent. The goal is to ship as much oil as possible from the source s to the sink t. Each pipeline has a maximum capacity it can handle, and there are no opportunities for storing oil
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 213
Figure 7.5 An illustration of the max-flow algorithm. (a) A toy network. (b) The first path chosen. (c) The second path chosen. (d) The final flow. (e) We could have chosen this path first. (f) In which case, we would have to allow this second path.
(a)
(^11)
1 1 1
(b)
(c)
(d)
(^11)
1 1 0
(e)
1 1 1
(f)
1
1
1
All we know so far of the simplex algorithm is the vague geometric intuition that it keeps making local moves on the surface of a convex feasible region, successively improving the objective function until it finally reaches the optimal solution. Once we have studied it in more detail (Section 7.6), we will be in a position to understand exactly how it handles flow LPs, which is useful as a source of inspiration for designing direct max-flow algorithms. It turns out that in fact the behavior of simplex has an elementary interpretation:
Start with zero flow. Repeat: choose an appropriate path from s to t, and increase flow along the edges of this path as much as possible.
Figure 7.5(a)–(d) shows a small example in which simplex halts after two iterations. The final flow has size 2 , which is easily seen to be optimal.
214 Algorithms
There is just one complication. What if we had initially chosen a different path, the one in Figure 7.5(e)? This gives only one unit of flow and yet seems to block all other paths. Simplex gets around this problem by also allowing paths to cancel existing flow. In this particular case, it would subsequently choose the path of Figure 7.5(f). Edge (b, a) of this path isn’t in the original network and has the effect of canceling flow previously assigned to edge (a, b). To summarize, in each iteration simplex looks for an s − t path whose edges (u, v) can be of two types:
If the current flow is f , then in the first case, edge (u, v) can handle up to cuv − fuv additional units of flow, and in the second case, upto fvu additional units (canceling all or part of the existing flow on (v, u)). These flow-increasing opportunities can be captured in a residual network Gf^ = (V, Ef^ ), which has exactly the two types of edges listed, with residual capacities cf^ : (^) { cuv − fuv if (u, v) ∈ E and fuv < cuv fvu if (v, u) ∈ E and fvu > 0
Thus we can equivalently think of simplex as choosing an s − t path in the residual network. By simulating the behavior of simplex, we get a direct algorithm for solving max-flow. It proceeds in iterations, each time explicitly constructing Gf^ , finding a suitable s − t path in Gf^ by using, say, a linear-time breadth-first search, and halting if there is no longer any such path along which flow can be increased. Figure 7.6 illustrates the algorithm on our oil example.
Now for a truly remarkable fact: not only does simplex correctly compute a maximum flow, but it also generates a short proof of the optimality of this flow! Let’s see an example of what this means. Partition the nodes of the oil network (Figure 7.4) into two groups, L = {s, a, b} and R = {c, d, e, t}:
s
a
b
c
d
e
3 t
3
4
10 1
2
1
5
1
2
5
Any oil transmitted must pass from L to R. Therefore, no flow can possibly exceed the total capacity of the edges from L to R, which is 7. But this means that the flow we found earlier, of size 7, must be optimal!
216 Algorithms
using a breadth-first search, which finds the path with the fewest edges—then the number of iterations is at most O(|V | · |E|), no matter what the capacities are. This latter bound gives an overall running time of O(|V | · |E|^2 ) for maximum flow.
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 217
Figure 7.6 The max-flow algorithm applied to the network of Figure 7.4. At each iteration, the current flow is shown on the left and the residual network on the right. The paths chosen are shown in bold.
Current flow Residual graph
(a)
s
a
b
c
d
e
t s
a
b
c
d
e
3 t
3
4
10 1
2
1
5
1
2
5
(b)
s
a
b
c
d
e
t
1 1
1
1 1
s
a
b
c
d
e
3 t 4
10 1 1 1
1 2 2
1 1
1 4
1 4
(c)
s
a
b
c
d
e
t
1
1 1
2
2
2
s
a
b
c
d
e
3 t 4
10 1 1 1
2
1 4
2 2 1
3
2
(d)
s
a
b
c
d
e
1 1 t
2
2
5 4
3
s
a
b
c
d
e
3 t
10 1 1 1
2
2 2 1
3
1
1
4
5
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 219
Figure 7.7 An edge between two people means they like each other. Is it possible to pair everyone up happily?
Alice
Beatrice
Carol
Danielle
Chet
Dan
Bob
Al
Figure 7.7 shows a graph with four nodes on the left representing boys and four nodes on the right representing girls.^1 There is an edge between a boy and girl if they like each other (for instance, Al likes all the girls). Is it possible to choose couples so that everyone has exactly one partner, and it is someone they like? In graph-theoretic jargon, is there a perfect matching? This matchmaking game can be reduced to the maximum-flow problem, and thereby to linear programming! Create a new source node, s, with outgoing edges to all the boys; a new sink node, t, with incoming edges from all the girls; and direct all the edges in the original bipartite graph from boy to girl (Figure 7.8). Finally, give every edge a capacity of 1. Then there is a perfect matching if and only if this network has a flow whose size equals the number of couples. Can you find such a flow in the example? Actually, the situation is slightly more complicated than just stated: what is easy to see is that the optimum integer-valued flow corresponds to the optimum matching. We would be at a bit of a loss interpreting a flow that ships 0. 7 units along the edge Al–Carol, for instance!
(^1) This kind of graph, in which the nodes can be partitioned into two groups such that all edges are between the groups, is called bipartite.
Figure 7.8 A matchmaking network. Each edge has a capacity of one.
s (^) t
Dan
Bob
Chet
Danielle
Beatrice
Alice
Carol
Al
220 Algorithms
Fortunately, the maximum-flow problem has the following property: if all edge capacities are integers, then the optimal flow found by our algorithm is integral. We can see this directly from the algorithm, which in such cases would increment the flow by an integer amount on each iteration. Hence integrality comes for free in the maximum-flow problem. Unfortunately, this is the exception rather than the rule: as we will see in Chapter 8, it is a very difficult problem to find the optimum solution (or for that matter, any solution) of a general linear program, if we also demand that the variables be integers.
We have seen that in networks, flows are smaller than cuts, but the maximum flow and mini- mum cut exactly coincide and each is therefore a certificate of the other’s optimality. Remark- able as this phenomenon is, we now generalize it from maximum flow to any problem that can be solved by linear programming! It turns out that every linear maximization problem has a dual minimization problem, and they relate to each other in much the same way as flows and cuts. To understand what duality is about, recall our introductory LP with the two types of chocolate:
max x 1 + 6x 2 x 1 ≤ 200 x 2 ≤ 300 x 1 + x 2 ≤ 400 x 1 , x 2 ≥ 0
Simplex declares the optimum solution to be (x 1 , x 2 ) = (100, 300), with objective value 1900. Can this answer be checked somehow? Let’s see: suppose we take the first inequality and add it to six times the second inequality. We get
x 1 + 6x 2 ≤ 2000.
This is interesting, because it tells us that it is impossible to achieve a profit of more than
x 1 + 6x 2 ≤ 1900.
So 1900 must indeed be the best possible value! The multipliers (0, 5 , 1) magically constitute a certificate of optimality! It is remarkable that such a certificate exists for this LP—and even if we knew there were one, how would we systematically go about finding it?