linear programming [1ex] dpv chapter 7, part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of...

27
Linear Programming DPV Chapter 7, Part 1 Jim Royer March 20, 2019 Uncredited diagrams are from DPV or homemade. DPV Chapter 7, Part 1 Linear Programming 1/1

Upload: others

Post on 18-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Linear ProgrammingDPV Chapter 7, Part 1

Jim Royer

March 20, 2019

Uncredited diagrams are from DPV or homemade.

DPV Chapter 7, Part 1 Linear Programming 1 / 1

Page 2: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Linear Programming: Introduction, 1

Ingredients of Linear ProgrammingI a set of variablesI a set of linear equations/inequalities over these variablesI a linear objective function to min/max

An Example Problem: Maximizing Profits

Item # made per day profit per unit max # per day1 x1 $1 2002 x2 $6 300

We also cannot make more than a total of 400 units per day of items 1 and 2 combined.Q: What should x1 and x2 be to maximize profits?

DPV Chapter 7, Part 1 Linear Programming 2 / 1

Page 3: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Sample Problem, Continued

An Example Problem: Maximizing Profits

Item # made per day profit per unit max # per day1 x1 $1 2002 x2 $6 300

We also cannot make more than a total of 400 units per day of items 1 and 2 combined.

The problem expressed as a linear program

Objective function max: x1 + 6x2Constraints x1 ≤ 200

x2 ≤ 300x1 + x2 ≤ 400

x1, x2 ≥ 0

DPV Chapter 7, Part 1 Linear Programming 3 / 1

Page 4: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Sample Problem, Continued

The problemexpressed as alinear program

Objective functionmax: x1 + 6x2

Constraintsx1 ≤ 200x2 ≤ 300

x1 + x2 ≤ 400x1, x2 ≥ 0

202 Algorithms

Figure 7.1 (a) The feasible region for a linear program. (b) Contour lines of the objectivefunction: x1 + 6x2 = c for different values of the profit c.

(a)

100 200 300 400

100

200

300

400

0

x2

x1

(b)

100 200 300 400

100

200

300

400

0

c = 1500

c = 1200

c = 600

x2

x1

Optimum pointProfit = $1900

7.1.1 Example: profit maximizationA 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 shouldit produce to maximize profits? Let’s say it makes x1 boxes of Pyramide per day, at a profit of$1 each, and x2 boxes of Nuit, at a more substantial profit of $6 apiece; x1 and x2 are unknownvalues that we wish to determine. But this is not all; there are also some constraints on x1 andx2 that must be accommodated (besides the obvious one, x1, x2 ! 0). First, the daily demandfor these exclusive chocolates is limited to at most 200 boxes of Pyramide and 300 boxes ofNuit. 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 x1 + 6x2

Constraints x1 " 200

x2 " 300

x1 + x2 " 400

x1, x2 ! 0

A linear equation in x1 and x2 defines a line in the two-dimensional (2D) plane, and alinear inequality designates a half-space, the region on one side of the line. Thus the setof all feasible solutions of this linear program, that is, the points (x1, x2) which satisfy allconstraints, 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 x1 + 6x2 = c, which has a slopeof #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

DPV Chapter 7, Part 1 Linear Programming 4 / 1

Page 5: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Linear Programming, 2

I The constraints describe a convex polygon. (Why?)

Generally . . .The optimum is achieved at a vertex of the feasible region. (Why?)

. . . except when there is no optimum. E.g.:I the linear program is infeasible, meaning the constraints are so tight it is impossible to

satisfy them all. E.g.: x ≤ 1, x ≥ 2.I the feasible region is unbounded, meaning the constraints are so loose that the objective

function takes on arbitrarily large values. E.g.: maximize x1 + x2 under x1, x2 ≥ 0.

DPV Chapter 7, Part 1 Linear Programming 5 / 1

Page 6: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

How to Solve Linear ProgramsThe Simplex MethodI Warning: Far from the whole story!!I The feasible region is a polygon.I Suppose p is a vertex of the feasible

region polygon.I p is a local maximum when the value of

the objective function is larger at p andat any of its neighbors.

I The method hill-climbs.p← the originwhile p is not a local max do

p← the neighbor at which theobj. function is larger

return p

?? Why is the local max the true max?

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 203

the line as far up as possible, while still touching the feasible region. The optimum solutionwill be the very last feasible point that the profit line sees and must therefore be a vertex ofthe polygon, as shown in the figure. If the slope of the profit line were different, then its lastcontact with the polygon could be an entire edge rather than a single vertex. In this case, theoptimum solution would not be unique, but there would certainly be an optimum vertex.

It is a general rule of linear programs that the optimum is achieved at a vertex of thefeasible region. The only exceptions are cases in which there is no optimum; this can happenin two ways:1. The linear program is infeasible; that is, the constraints are so tight that it is impossibleto satisfy all of them. For instance,

x ! 1, x " 2.

2. The constraints are so loose that the feasible region is unbounded, and it is possible toachieve arbitrarily high objective values. For instance,

max x1 + x2

x1, x2 " 0

Solving linear programsLinear programs (LPs) can be solved by the simplex method, devised by George Dantzig in1947. We shall explain it in more detail in Section 7.6, but briefly, this algorithm starts at avertex, in our case perhaps (0, 0), and repeatedly looks for an adjacent vertex (connected byan edge of the feasible region) of better objective value. In this way it does hill-climbing onthe vertices of the polygon, walking from neighbor to neighbor so as to steadily increase profitalong the way. Here’s a possible trajectory.

100

300

200

100 2000

Profit $1900

$0 $200

$1400

Upon reaching a vertex that has no better neighbor, simplex declares it to be optimal andhalts. Why does this local test imply global optimality? By simple geometry—think of theprofit line passing through this vertex. Since all the vertex’s neighbors lie below the line, therest of the feasible polygon must also lie below this line.

DPV Chapter 7, Part 1 Linear Programming 6 / 1

Page 7: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Sample Problem, Continued

An Example Problem: Maximizing Profits

Item # made per day profit per unit max # per day1 x1 $1 2002 x2 $6 3003 x3 $13

Other production constraints: x1 + x2 + x3 ≤ 400 and x2 + 3x3 ≤ 600.

The problem expressed as a linear program

Objective function max: x1 + 6x2 + 13x3Constraints x1 ≤ 200

x2 ≤ 300x1 + x2 + x3 ≤ 400

x2 + 3x3 ≤ 400x1, x2, x3 ≥ 0

DPV Chapter 7, Part 1 Linear Programming 7 / 1

Page 8: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Sample Problem, Continued

The problem expressed as a linear program

Objective function max: x1 + 6x2 + 13x3Constraints x1 ≤ 200

x2 ≤ 300x1 + x2 + x3 ≤ 400

x2 + 3x3 ≤ 400x1, x2, x3 ≥ 0

204 Algorithms

Figure 7.2 The feasible polyhedron for a three-variable linear program.

x1

x3

x2

Optimum

More productsEncouraged by consumer demand, the chocolatier decides to introduce a third and even moreexclusive line of chocolates, called Pyramide Luxe. One box of these will bring in a profit of $13.Let x1, x2, x3 denote the number of boxes of each chocolate produced daily, with x3 referring toLuxe. The old constraints on x1 and x2 persist, although the labor restriction now extends tox3 as well: the sum of all three variables can be at most 400. What’s more, it turns out thatNuit and Luxe require the same packaging machinery, except that Luxe uses it three timesas much, which imposes another constraint x2 + 3x3 ! 600. What are the best possible levelsof production?Here is the updated linear program.

max x1 + 6x2 + 13x3

x1 ! 200

x2 ! 300

x1 + x2 + x3 ! 400

x2 + 3x3 ! 600

x1, x2, x3 " 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 intersectionof seven half-spaces, a polyhedron (Figure 7.2). Looking at the figure, can you decipher whichinequality corresponds to each face of the polyhedron?A profit of c corresponds to the plane x1 + 6x2 + 13x3 = c. As c increases, this profit-plane

moves parallel to itself, further and further into the positive orthant until it no longer touchesthe feasible region. The point of final contact is the optimal vertex: (0, 300, 100), with totalprofit $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 andprofits:

(0, 0, 0)$0

!" (200, 0, 0)$200

!" (200, 200, 0)$1400

!" (200, 0, 200)$2800

!" (0, 300, 100)$3100

Finally, upon reaching a vertex with no better neighbor, it would stop and declare this to bethe optimal point. Once again by basic geometry, if all the vertex’s neighbors lie on one sideof the profit-plane, then so must the entire polyhedron.

A magic trick called dualityHere 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 themthe fourth multiplied by 4. The result is the inequality x1 + 6x2 + 13x3 # 3100.Do you see? This inequality says that no feasible solution (values x1, x2, x3 satisfying the

constraints) can possibly have a profit greater than 3100. So we must indeed have found theoptimum! The only question is, where did we get these mysterious multipliers (0, 1, 1, 4) forthe 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 otherLP, 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 ourstory.

What if we add a fourth line of chocolates, or hundreds more of them? Then the problembecomes high-dimensional, and hard to visualize. Simplex continues to work in this generalsetting, although we can no longer rely upon simple geometric intuitions for its descriptionand 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 detailslike numeric precision. In a typical application, the main task is therefore to correctly expressthe 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.

DPV Chapter 7, Part 1 Linear Programming 8 / 1

Page 9: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Notes

I At higher dimensions, things are harder to visualize, but the math still works!I There are lots of professional packages that quickly and accurately solve linear

programming problems.I So the key thing to know is: how to precisely express a problem as a LP-problem.

DPV Chapter 7, Part 1 Linear Programming 9 / 1

Page 10: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bandwidth Allocation, 1

I We have a network as shown in the figure.I The numbers on edges are max-bandwidth capacities.I We need connections between users A&B, A&C, and

B&C.I Each connection requires at least two units of bandwidth,

but more can be used.I Connection A–B pays $3 per bandwidth unit; A–C pays

$4 and B–C pays $2.I Each connection can be routed via the short route, long

route, or a combination of the two.

?? How do we route connections to maximize profit?

208 Algorithms

Figure 7.3 A communications network between three users A,B, and C. Bandwidths areshown.

a

cb

12

10

6

13

11

8

userA

userB

userC

max 3xAB + 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 optimalsolution 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 norounding is required. Looking back at the original network, we see that every edge except a–cis 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 exponentiallymany such paths, and therefore

DPV Chapter 7, Part 1 Linear Programming 10 / 1

Page 11: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bandwidth Allocation, 2I Each connection needs at least two units of bandwidth.I Connection A–B pays $3 per bandwidth unit; A–C pays $4 and

B–C pays $2.

?? How do we route connections to maximize profit?

208 Algorithms

Figure 7.3 A communications network between three users A,B, and C. Bandwidths areshown.

a

cb

12

10

6

13

11

8

userA

userB

userC

max 3xAB + 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 optimalsolution 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 norounding is required. Looking back at the original network, we see that every edge except a–cis 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 exponentiallymany such paths, and therefore

208 Algorithms

Figure 7.3 A communications network between three users A,B, and C. Bandwidths areshown.

a

cb

12

10

6

13

11

8

userA

userB

userC

max 3xAB + 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 optimalsolution 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 norounding is required. Looking back at the original network, we see that every edge except a–cis 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 exponentiallymany such paths, and therefore

Solution via Simplex:

xAB = 0.0

x′AB = 7.0

xBC = 1.5

x′BC = 1.5

xAC = 0.5

x′AC = 4.5

? xUV = the short path allocation for U-V ? x′UV = the long path allocation for U-VDPV Chapter 7, Part 1 Linear Programming 11 / 1

Page 12: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Variations on Linear Programming, 1

LP in Standard FormI the objective function is minimized

I the variables are constrained to be ≥ 0I the other constraints are equations

LP in GeneralI the objective function is minimized or

maximizedI the variables may take on negative valuesI the other constraints are equations or

inequalities

Reduction to Standard Formmax : f ⇐⇒ min : (−f )

x positive/negative ⇐⇒ x+, x− ≥ 0 & x = x+ − x−

a1x1 + · · ·+ anxn ≤ b ⇐⇒ (∑ aixi) + s = b & s ≥ 0

s as above is called a slack variable.

DPV Chapter 7, Part 1 Linear Programming 12 / 1

Page 13: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Variations on Linear Programming, 2

Reduction to Standard Formmax : f ⇐⇒ min : (−f )

x positive/negative ⇐⇒ x+, x− ≥ 0 & x = x+ − x−

a1x1 + · · ·+ anxn ≤ b ⇐⇒ (∑ aixi) + s = b & s ≥ 0

Example

max: x1 + 6x2 min: −x1 − 6x2x1 ≤ 200 x1 + s1 = 200x2 ≤ 300 =⇒ x2 + s2 = 300

x1 + x2 ≤ 400 x1 + x2 + s3 = 400x1, x2 ≥ 0 x1, x2, s1, s2, s3 ≥ 0

DPV Chapter 7, Part 1 Linear Programming 13 / 1

Page 14: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Linear Programming in Matrix Form

Example

max: x1 + 6x2 max:~cT ·~xx1 ≤ 200 =⇒ A~x ≤~bx2 ≤ 300 ~x ≥ 0

x1 + x2 ≤ 400x1, x2 ≥ 0

where ~c =(

16

), ~x =

(x1x2

), ~b =

200300400

, A =

1 00 11 1

DPV Chapter 7, Part 1 Linear Programming 14 / 1

Page 15: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Network Flows212 Algorithms

Figure 7.4 (a) A network with edge capacities. (b) A flow in the network.(a)

s

a

b

c

d

e

t3

3

4

10

1

2

5

5

2

1

1

(b)

s

a

b

c

d

e

t

5

2

0

102

1

4

5

2

1

en route. Figure 7.4(b) shows a possible flow from s to t, which ships 7 units in all. Is this thebest that can be done?

7.2.2 Maximizing flowThe networks we are dealing with consist of a directed graph G = (V,E); two special nodess, t ! V , which are, respectively, a source and sink of G; and capacities ce > 0 on the edges.We would like to send as much oil as possible from s to t without exceeding the capacities

of any of the edges. A particular shipping scheme is called a flow and consists of a variable fe

for each edge e of the network, satisfying the following two properties:

1. It doesn’t violate edge capacities: 0 " fe " ce for all e ! E.

2. For all nodes u except s and t, the amount of flow entering u equals the amount leavingu: !

(w,u)!E

fwu =!

(u,z)!E

fuz.

In other words, flow is conserved.The size of a flow is the total quantity sent from s to t and, by the conservation principle,

is equal to the quantity leaving s:

size(f) =!

(s,u)!E

fsu.

In short, our goal is to assign values to {fe : e ! E} that will satisfy a set of linearconstraints and maximize a linear objective function. But this is a linear program! Themaximum-flow problem reduces to linear programming.For example, for the network of Figure 7.4 the LP has 11 variables, one per edge. It seeks

to maximize fsa + fsb + fsc subject to a total of 27 constraints: 11 for nonnegativity (such asfsa # 0), 11 for capacity (such as fsa " 3), and 5 for flow conservation (one for each node ofthe graph other than s and t, such as fsc + fdc = fce). Simplex would take no time at all tocorrectly solve the problem and to confirm that, in our example, a flow of 7 is in fact optimal.

(a) A network of oil pipelines.The number on each edge is its maximum capacity.No storage in nodes. s = source node. t = target node.Goal: Send as much oil from s to t as the network allows.

(b) A flow in the network, shipping 7 units.Can we do better? How do we find out?

DPV Chapter 7, Part 1 Linear Programming 15 / 1

Page 16: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Network Flow as an LP Problem

I A variable for each edge: fe = the flow on that edge.0 ≤ fe ≤ ce = the capacity of edge e.

I Flow is conserved at each node except s and t:

∑(w,u)∈E

fwu = ∑(u,z)∈E

fuz

I The size of the flow is the amount sent from s to t. By conservation:

size(f ) = ∑(s,u)∈E

fsu.

I Goal: Assign values to the fuv’s that satisfy the constraints and maximize size(f ).

∴ This is a linear programming problem,so Simplex can solve these problems for us.

DPV Chapter 7, Part 1 Linear Programming 16 / 1

Page 17: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

From Simplex to a Direct Network Flow Algorithm, 1If you analyze how Simplex solvesNetwork Flow Problems, it is roughlyequivalent to:

Start with zero flow.Repeat

Choose an appropriate path from s to tIncrease the flow along the path’s edges

as much as possibleuntil there is no path that improves the flow

(a) Initial network.

(b) First path chosen.

(c) Second path chosen.

(d) Final flow.(e) Alternative first path.

(f) Alternative second path.The b→ a edge cancels flow.

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 pathchosen. (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)

s

b

a

t11

1 1

1

(b)

s

a

t

(c)

s

b

t

(d)

s

b

a

t11

1 1

0

(e)

s

b

a

t1

1

1

(f)

s

b

a

t1

1

1

7.2.3 A closer look at the algorithmAll we know so far of the simplex algorithm is the vague geometric intuition that it keepsmaking local moves on the surface of a convex feasible region, successively improving theobjective function until it finally reaches the optimal solution. Once we have studied it inmore detail (Section 7.6), we will be in a position to understand exactly how it handles flowLPs, 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 edgesof this path as much as possible.

Figure 7.5(a)–(d) shows a small example in which simplex halts after two iterations. Thefinal flow has size 2, which is easily seen to be optimal.

DPV Chapter 7, Part 1 Linear Programming 17 / 1

Page 18: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

From Simplex to a Direct Network Flow Algorithm, 2I Appropriate paths consists of two sorts of edges (u, v):

1. (u, v) is in the network and not yet at full capacity.(u, v) can handle up to cuv − fuv more flow.

2. (v, u) is in the network with flow along it.(u, v) can handle up to fvu more flow — by canceling flow along (v, u).

I Gf = (V, Ef ) is the residual network with two sorts of edges, (u, v), with residual capacities:{

cuv − fuv, if (u, v) ∈ E and fuv < cuv;fvu, if (v, u) ∈ E and fvu > 0.

Gf = the network of unused capacities.

The “Simplex Strategy”

Start with zero flow.Repeat

Choose an appropriate path from s to tIncrease the flow along the path’s edges as much as possible

until there is no path that improves the flow

DPV Chapter 7, Part 1 Linear Programming 18 / 1

Page 19: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

From Simplex to a Direct Network Flow Algorithm, 2The “Simplex Strategy”

Start with zero flow.Repeat

Choose an appropriate path from s to tIncrease the flow along the path’s edges as much as possible

until there is no path that improves the flow

Gf = (V, Ef ) is the residual network with edges (u, v) with

residual capacities:

{cuv − fuv, if (u, v) ∈ E and fuv < cuv;fvu, if (v, u) ∈ E and fvu > 0.

The “Simplex Strategy” in terms of Gf

Gf ← G // Initially, capacity = unused flowRepeat forever

Use BFS to find a path in Gf along which we can increase flowif there is no such path then quitif there is such a path then revise f and Gf

This is known as the Edmonds-Karp Algorithm.DPV Chapter 7, Part 1 Linear Programming 19 / 1

Page 20: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

An Example

Gf ← G// Initially, capacity = unused flowRepeat forever

Use BFS to find a path in Gf along which we can increase flowif there is such a path

then revise f and Gf

else quit

Initially

After Step 1. . .

After Step 2DPV Chapter 7, Part 1 Linear Programming 20 / 1

Page 21: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Flows and Cuts, 1

DefinitionAn s, t-cut is a partition of V into two sets L and Rwith s ∈ L and t ∈ R.

capacity(L, R)= ∑{ cuv : (u, v) ∈ E, u ∈ L, v ∈ R }.

ClaimFor any flow f and any s, t-cut (L, R):

size(f ) ≤ capacity(L, R).

214 Algorithms

There is just one complication. What if we had initially chosen a different path, the one inFigure 7.5(e)? This gives only one unit of flow and yet seems to block all other paths. Simplexgets around this problem by also allowing paths to cancel existing flow. In this particularcase, it would subsequently choose the path of Figure 7.5(f). Edge (b, a) of this path isn’t inthe 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:

1. (u, v) is in the original network, and is not yet at full capacity.

2. The reverse edge (v, u) is in the original network, and there is some flow along it.

If the current flow is f , then in the first case, edge (u, v) can handle up to cuv ! fuv additionalunits of flow, and in the second case, upto fvu additional units (canceling all or part of theexisting flow on (v, u)). These flow-increasing opportunities can be captured in a residualnetwork Gf = (V,Ef ), which has exactly the two types of edges listed, with residual capacitiescf : !

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 inGf by using, say, a linear-time breadth-first search, and halting if there is no longer any suchpath along which flow can be increased.Figure 7.6 illustrates the algorithm on our oil example.

7.2.4 A certificate of optimalityNow 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

t3

3

4

10 1

2

1

5

1

2

5

L R

Any oil transmitted must pass from L to R. Therefore, no flow can possibly exceed the totalcapacity 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!The Max-Flow Min-Cut Theorem

The max flow in a network = min{ capacity(L, R) : (L, R) is an s, t-cut }.

Also, our algorithm finds the min-cut.

DPV Chapter 7, Part 1 Linear Programming 21 / 1

Page 22: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Flows and Cuts, 2

The Max-Flow Min-Cut TheoremThe max flow in a network =

min{ capacity(L, R) : (L, R) is an s, t-cut }.

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 215

More generally, an (s, t)-cut partitions the vertices into two disjoint groups L and R suchthat s is in L and t is in R. Its capacity is the total capacity of the edges from L to R, and asargued previously, is an upper bound on any flow:

Pick any flow f and any (s, t)-cut (L,R). Then size(f) ! capacity(L,R).

Some cuts are large and give loose upper bounds—cut ({s, b, c}, {a, d, e, t}) has a capacity of19. But there is also a cut of capacity 7, which is effectively a certificate of optimality of themaximum flow. This isn’t just a lucky property of our oil network; such a cut always exists.Max-flowmin-cut theorem The size of the maximum flow in a network equals the capacityof the smallest (s, t)-cut.Moreover, our algorithm automatically finds this cut as a by-product!Let’s see why this is true. Suppose f is the final flow when the algorithm terminates. We

know that node t is no longer reachable from s in the residual network Gf . Let L be the nodesthat are reachable from s in Gf , and let R = V " L be the rest of the nodes. Then (L,R) is acut in the graph G:

L R

ts

e!

e

We claim thatsize(f) = capacity(L,R).

To see this, observe that by the way L is defined, any edge going from L to R must be at fullcapacity (in the current flow f ), and any edge from R to L must have zero flow. (So, in thefigure, fe = ce and fe! = 0.) Therefore the net flow across (L,R) is exactly the capacity of thecut.

7.2.5 EfficiencyEach iteration of our maximum-flow algorithm is efficient, requiring O(|E|) time if a depth-first or breadth-first search is used to find an s " t path. But how many iterations are there?Suppose all edges in the original network have integer capacities ! C. Then an inductive

argument shows that on each iteration of the algorithm, the flow is always an integer andincreases by an integer amount. Therefore, since the maximum flow is at most C|E| (why?),it follows that the number of iterations is at most this much. But this is hardly a reassuringbound: what if C is in the millions?We examine this issue further in Exercise 7.31. It turns out that it is indeed possible to

construct bad examples in which the number of iterations is proportional to C, if s " t pathsare not carefully chosen. However, if paths are chosen in a sensible manner—in particular, by

Proof: LetI f = the algorithm’s final flowI L = nodes reachable (with positive capacity

edges) from s in Gf . Note: t /∈ L! (Why?)I R = V− L.

Claim: size(f ) = capacity(L, R).Proof of Claim:I The L to R edges must be at full capacity in Gf . (Why?)I The R to L edges must have 0 flow in f . (Why?)

Therefore, the claim follows.Thus, (L, R) must be a min-cut. (Why?)Therefore, the theorem follows.

DPV Chapter 7, Part 1 Linear Programming 22 / 1

Page 23: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Max Flow, Runtime

The “Simplex Strategy” in terms of Gf

Gf ← GRepeat forever

Use BFS to find a path in Gf along which we can increase flowif there is no such path then quitif there is such a path then revise f and Gf

I Each iteration takes O(|E|) time. (Why?)I With some work (see Exercise 7.31) one can show that there are at most O(|V| · |E|) many

iterations.I Therefore, the total run time is O(|V| · |E|2) time.

DPV Chapter 7, Part 1 Linear Programming 23 / 1

Page 24: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bipartite matching, 1

Definition(a) A graph G = (V, E) is bipartite when V can be

partitioned into two nonempty sets L and R and eachedge in E has one endpoint in L and the other in R.

(b) Given a bipartite graph G, a bipartite matching is a setof edges which give an 1-1 correspondence betweenL and R. (E.g., {(Alice,pie), (Bob,tart), (Carol,cake),(Dan,cookie)})

Alice

cake

pie

cookie

tartBob

Carol

Dan

We can turn the problem of finding a bipartite matchinginto a network flow problem by:I Adding a source and target verts s and t.I Adding edges from s to each u ∈ L.I Adding edges from each v ∈ R to t.I Setting the capacity of each edge 1.

Alice

cake

pie

cookie

tartBob

Carol

Dan

ts

DPV Chapter 7, Part 1 Linear Programming 24 / 1

Page 25: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bipartite matching, 1

Definition(a) A graph G = (V, E) is bipartite when V can be

partitioned into two nonempty sets L and R and eachedge in E has one endpoint in L and the other in R.

(b) Given a bipartite graph G, a bipartite matching is a setof edges which give an 1-1 correspondence betweenL and R. (E.g., {(Alice,pie), (Bob,tart), (Carol,cake),(Dan,cookie)})

Alice

cake

pie

cookie

tartBob

Carol

Dan

We can turn the problem of finding a bipartite matchinginto a network flow problem by:I Adding a source and target verts s and t.I Adding edges from s to each u ∈ L.I Adding edges from each v ∈ R to t.I Setting the capacity of each edge 1.

Alice

cake

pie

cookie

tartBob

Carol

Dan

ts

DPV Chapter 7, Part 1 Linear Programming 24 / 1

Page 26: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bipartite matching, 2bipartite matching→ network flowI Add a source and target verts s and t.I Add edges from s to each u ∈ L.I Add edges from each v ∈ R to t.I Set the capacity of each edge 1.

ProblemWe do not want fractional flows.(E.g., Dan wants a whole cookie!)

Saving FactIf all edge capacities are integers, then our algorithm finds anintegral optimal flow.

But!In general it is hard to find solutions to LP problems if youdemand all variables have integer values. (Set up for Chapter 8.)

Alice

cake

pie

cookie

tartBob

Carol

Dan

ts

Next Class:The Simplex Algorithm

DPV Chapter 7, Part 1 Linear Programming 25 / 1

Page 27: Linear Programming [1ex] DPV Chapter 7, Part 1 · 2019-03-20 · 1 + 6x 2 = c, whichhas a slope of #1/6 and is shown in Figure 7.1 for selected values of c. As c increases, this ÒproÞt

Bipartite matching, 2bipartite matching→ network flowI Add a source and target verts s and t.I Add edges from s to each u ∈ L.I Add edges from each v ∈ R to t.I Set the capacity of each edge 1.

ProblemWe do not want fractional flows.(E.g., Dan wants a whole cookie!)

Saving FactIf all edge capacities are integers, then our algorithm finds anintegral optimal flow.

But!In general it is hard to find solutions to LP problems if youdemand all variables have integer values. (Set up for Chapter 8.)

Alice

cake

pie

cookie

tartBob

Carol

Dan

ts

Next Class:The Simplex Algorithm

DPV Chapter 7, Part 1 Linear Programming 25 / 1