You are on page 1of 5

Algorithms and Data Structures 2014

Exercises and Solutions Week 14


1 Linear programming
1. Consider the following linear program.

maximize 5x + 3y
5x − 2y ≥ 0
x+y ≤ 7
x ≤ 5
x ≥ 0
y ≥ 0

Plot the feasible region and identify the optimal solution.


Solution Just for checking: x = 5 and y = 2.
2. A cargo plane can carry a maximum weight of 100 tons and a maximum volume of 60 cubic
meters. There are three materials to be transported, and the cargo company may choose to
carry any amount of each, upto the maximum available limits given below.
• Material 1 has density 2 tons/cubic meter, maximum available amount 40 cubic meters,
and revenue e1.000 per cubic meter.
• Material 2 has density 1 tons/cubic meter, maximum available amount 30 cubic meters,
and revenue e1.200 per cubic meter.
• Material 3 has density 3 tons/cubic meter, maximum available amount 20 cubic meters,
and revenue e12.000 per cubic meter.
Write a linear program that optimizes revenue within the constraints.
Solution We introduce variables v1 , v2 , and v3 representing the volumes of the respec-
tive materials. The requested linear program is then given by

maximize 1000v1 + 1200v2 + 12000v3


2v1 + v2 + 3v3 ≤ 100
v1 + v2 + v3 ≤ 60
v1 ≤ 40
v2 ≤ 30
v3 ≤ 20
v1 , v2 , v3 ≥ 0.

3. You are given the following points (xi , yi ) in the plane:

(1, 3), (2, 5), (3, 7), (5, 11), (7, 14), (8, 15), (10, 19).

Suppose you want to determine a line ax + by = c that approximately passes through these
points (it might not contain all the points). Write a linear program to find a line that mini-
mizes the maximum absolute error, being

max |axi + byi − c|


0<i≤7
Solution We introduce a variable z to replace the formula max0<i≤7 |axi + byi − c|. It
remains to constrain z such that z = max0<i≤7 |axi + byi − c|. Since we are minimizing z,
it is sufficient to have z ≥ max0<i≤7 |axi + byi − c|, which is equivalent to the requirements
z ≥ |axi + byi − c| for 0 < i ≤ 7. To get rid of the absolute values, we split each of those
requirements into z ≥ axi + byi − c and z ≥ −axi − byi + c. Note that because z is greater
than certain absolute values, we can add the restriction z ≥ 0 without affecting the solution.
In standard form, the linear program is given by

maximize −z
−z + xi a + yi b − c ≤ 0 ∀0<i≤7
z + xi a + yi b − c ≤ 0 ∀0<i≤7
a, b, c, z ≥ 0.

4. The following figure shows a graph with four nodes on the left representing boys and four
nodes on the right representing girls. There is an edge between a boy and girl if they like
each other (for instance, Al likes all the girls).

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. Is it possible to choose couples so that
everyone has exactly one partner? In graph-theoretic jargon, is there a perfect matching?
Write a linear program that solves the matching problem.
Solution We turn the graph into a directed graph, choosing to direct the edges towards
the girls. Furthermore, we add nodes s and t to the graph with for every boy an edge from s
to that boy and for every girl an edge from that girl to t. For all nodes i and j, we have
a constant cij that is 0 if there is no edge and 1 if there is an edge from i to j. Finally, we
introduce variables fij for all nodes i and j. The problem has turned into a maximum flow
problem (after solving it as such, we simply check if the size of the flow equals the number
of boys), where s is the source node, t the sink node, c the capacities and f the flow. Thus,
the problem can be modelled by a linear program as explained during the lecture.
5. In the last lecture, we saw the single-source-single-target shortest path problem being for-
mulated as a linear maximization problem. Alternatively, one can derive the following dual
minimization variant: X
minimize xuv w(u, v)
(u,v)∈E

Here, xuv = 1 if the edge (u, v) is part of the shortest path, and xuv = 0, otherwise. Write
down the corresponding constraints.
Solution The constraints are
X X X X
xsv − xvs = xvt − xtv = 1
v v v v
and, for all w ∈
/ {s, t}, X X
xvw − xwv = 0.
v v
Here s is the source node and t is the target node. To make this a linear problem, we need
to replace the assertion xuv ∈ {0, 1} for all u and v by xuv ≥ 0. Note that there may now
be solutions with non-integer values for some xuv . This means that the “flow” is divided
over multiple paths between certain nodes, but because of the minimization it can only be
divided if these paths have the same length. Thus, any path along which all xuv are nonzero
will be a shortest path.
6. Convert the following linear program into standard form:
minimize 2x1 + 7x2 + x3
x1 − x3 = 7
3x1 + x2 ≥ 24
x2 ≥ 0
x3 ≤ 0

Solution We first switch from minimization to maximization by multiplying the objec-


tive function by −1. Now observe that x1 has no nonnegativity constraint, so we replace
it by x01 − x001 . The same goes for x3 , but we can exploit the x3 ≤ 0 constraint to simply
replace −x3 by x03 .
maximize −2x01 + 2x001 − 7x2 + x03
x01 − x001 + x03 = 7
3x01 − 3x001 + x2 ≥ 24
x01 , x001 , x2 , x03 ≥ 0
Now we replace the equality constraint.
maximize −2x01 + 2x001 − 7x2 + x03
x01 − x001 + x03 ≥ 7
x01 − x001 + x03 ≤ 7
3x01 − 3x001 + x2 ≥ 24
x01 , x001 , x2 , x03 ≥ 0
Finally, we fix the constraints with opposite signs.
maximize −2x01 + 2x001 − 7x2 + x03
−x01 + x001 − x03 ≤ −7
x01 − x001 + x03 ≤ 7
−3x01 + 3x001 − x2 ≤ −24
x01 , x001 , x2 , x03 ≥ 0

7. Convert the following linear program into slack form:


maximize 2x1 − 6x3
x1 + x2 − x3 ≤ 7
3x1 − x2 ≥ 8
−x1 + 2x2 + 2x3 ≥ 0
x1 , x2 , x3 ≥ 0
What are the basic and nonbasic variables?
Solution First we convert to standard form.

maximize 2x1 − 6x3


x1 + x2 − x3 ≤ 7
−3x1 + x2 ≤ −8
x1 − 2x2 − 2x3 ≤ 0
x1 , x2 , x3 ≥ 0

Now we introduce slack variables x4 , x5 , and x6 and display the program in slack form.

z = 2x1 − 6x3
x4 = 7 − x1 − x2 + x3
x5 = −8 + 3x1 − x2
x6 = −x1 + 2x2 + 2x3

Those are the basic variables. The nonbasic variables are x1 , x2 , and x3 .
8. Solve the following linear program using SIMPLEX

maximize 18x1 + 12.5x2


x1 + x2 ≤ 20
x1 ≤ 12
x2 ≤ 16
x1 , x2 ≥ 0

Solution We first convert to slack form.

z = 18x1 + 12.5x2
x3 = 20 − x1 − x2
x4 = 12 − x1
x5 = 16 − x2

The variable x1 has a positive coefficient in the objective function. With respect to x1 , the
second constraint is the most restrictive. Thus, we substitute x1 using x1 = 12 − x4 .

z = 216 + 12.5x2 − 18x4


x3 = 8 − x2 + x4
x1 = 12 − x4
x5 = 16 − x2

The variable x2 has a positive coefficient in the new objective function, and now the first
constraint is the most restrictive. We replace it by x2 = 8 − x3 + x4 and substitute x2
everywhere else.

z = 316 − 12.5x3 − 5.5x4


x2 = 8 − x3 + x4
x1 = 12 − x4
x5 = 8 + x3 − x4

Now all coefficients are negative, so the basic solution, with x1 = 12 and x2 = 8, is optimal.
9. For the following network, with edge capacities as shown, find the maximum flow from S
to T , along with a matching cut.

4 5
A D G

6 1
2 2 12
10
1
S B E T
20

2 6
10 4
5
C F

A cut partitions the vertices of a graph into two disjoint subsets, say L and R. The capacity
of a cut is the total capacity of the edges from L to R. A cut is said to match a flow if the size
of the flow is equal to the capacity of the cut.
Solution Sending a flow of 4 over the path (S, A, D, G, T ), the residual graph is as
follows.
1
4
A D G
2 4
1
2 2 4 8
4
10
S B E T
1 20

2 6
10 4
5
C F

A possible choice of the rest of the paths is shown below, along with the additional flow
each path admits and the final residual graph that results from all this.

1
4
A D 4 G

6 1
2 2 9 3
Path Flow 5
(S, A, B, E, G, T ) 2 1 5
(S, B, E, G, T ) 1 S B 15 E T
(S, C, F, T ) 4 5
(S, C, B, E, G, T ) 2 4
2 6
4
6 1
C F
4
Together, the size of the flow is 13. A matching cut can be found by taking for L the nodes
that in the final residual graph can be reached from S, i.e., L = {S, C, F }.

You might also like