cosc 311: algorithms hw4: network flow solutions...cosc 311: algorithms hw4: network flow solutions...

12
COSC 311: A LGORITHMS HW4: N ETWORK F LOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes represent the amount of flow along an edge, and the “un- adorned” numbers represent the edge capacities): (a) What is the value of the current flow in the graph? Solution: The current flow in the graph is the sum of the flow leaving the source node s: v(f )= e out of s f (e). In this case, that value is 8 + 5 + 0 = 13. (b) Draw the residual graph for this flow. Use the residual graph to explain why the current flow is or is not a max flow. Solution: Here is the residual graph: In the residual graph, there is still a path from s to t. For example, P = s y x t is one such path. This tells us that the current flow is not a max flow: we could augment the current flow 1

Upload: others

Post on 25-Oct-2020

21 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

COSC 311: ALGORITHMS

HW4: NETWORK FLOW

Solutions

1 Warmup1) Finding max flows and min cuts.Here is a graph (the numbers in boxes represent the amount of flow along an edge, and the “un-adorned” numbers represent the edge capacities):

(a) What is the value of the current flow in the graph?

Solution: The current flow in the graph is the sum of the flow leaving the source node s: v(f) =∑e out of s f(e). In this case, that value is 8 + 5 + 0 = 13.

(b) Draw the residual graph for this flow. Use the residual graph to explain why the current flow isor is not a max flow.

Solution: Here is the residual graph:

In the residual graph, there is still a path from s to t. For example, P = s → y → x → t is onesuch path. This tells us that the current flow is not a max flow: we could augment the current flow

1

Page 2: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

by 10 (the lowest edge capacity along P ) to obtain a higher-value flow.

(c) What is the capacity of the min cut in this graph? Find a min cut with this capacity.

The min cut in this graph has capacity 25. One cut that achieves this value is A = {s, w, y},B = {x, z, t}.

2 Network Flow Theory2) Rational edge capacities.So far we have assumed that all of the edge capacities in our graph are positive integers. In thisproblem we will investigate ways in which we can relax this constraint.

(a) In class, we proved that the Ford-Fulkerson algorithm does in fact terminate. Where in thatargument did we use the fact that the edge capacities are integers? Why does the argument breakif our edge capacities are positive real numbers?

Our argument that Ford-Fulkerson terminates used three claims:

1. At every step, all residual capacities and flow values are integers.

2. After each call to augment(), the total value of the flow strictly increases.

3. The total value of the flow is at most C =∑

e out of s c(e).

We then concluded that Claims 1 and 2 together tell us that the value of the flow increases by atleast 1 on every iteration, and by Claim 3 there can be at most C iterations. If our edge capacitiesare real numbers instead of integers, it could be the case that the value of the flow increases on ev-ery iteration, but by a smaller and smaller amount each time. Thus the total value of the flow couldapproachC without ever actually reachingC, so the algorithm is no longer guaranteed to terminate.

(b) Suppose we allow our edge capacities to be positive rational numbers. Given a graph G =(V,E) with |V | = n vertices and |E| = m edges and positive rational edge capacities c(e), give anO(m) algorithm that transforms the rational-capacity max-flow problem into the standard integer-capacity max-flow problem. Explain why finding a max flow for the new problem (with integercapacities), means that you have also found a max flow for the original problem.

Let ` be the least common denominator of all the edge capacities. We can transform the rational-capacity max flow problem to an integer-capacity max flow problem by multiplying all of the edgecapacities by `. Once we have found `, it takes time O(m) to pass through all the edges and multi-ply all capacities by `.

To find `, we could pass through all of the edges once, and at each edge e update ` = LCD(`, c(e)).Using Euclid’s algorithm, for each edge this computation takes time proportional to the numberof digits in c(e), which could be very large since we have made no assumptions about how big

2

Page 3: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

the edge capacities might be. Another option is to, instead of using the LCD, simply let ` be theproduct of all the denominators of the edge capacities; it now takes time O(m) to compute `, but` could be very large. Neither solution is perfect; both end up depending on the magnitude of theedge capacities.

If we find a max flow with value v(f) in this new integer-capacity graph, we have found a flow withvalue v(f) = v(f)/` in the original graph, and this is a max flow in the original graph. To provethis, we will assume that v(f) is not a max flow in the original graph and derive a contradiction. Ifv(f) is not a max flow in the original graph, then there is some other flow f ′ that has higher valuev(f ′) > v(f). So we have:

v(f ′) > v(f)∑e out of s

f ′(e) >∑

e out of s

f(e)

` ·∑

e out of s

f ′(e) > ` ·∑

e out of s

f(e)∑e out of s

` · f ′(e) >∑

e out of s

` · f(e)∑e out of s

` · f ′(e) > v(f)

which is a contradiction because v(f) was a max flow in the scaled graph. Hence v(f) must be amax flow in the original graph.

3) Network flow with vertex capacities.In the standard Network Flow setting we assume that edges have capacities that limit the amountof flow along an edge, but we are allowed to send as much flow as we want through each vertex.Consider the following alternative version of the Maximum Flow problem:

Input:

• A directed, unweighted graph G = (V,E), where |V | = n and |E| = m

• For each node v ∈ V , a positive integer capacity c(v)

• A source node s ∈ V

• A target node t ∈ V

Output: An s-t flow of maximum value that satisfies the usual conservation constraints, as well asnew node-capacity constraints

∑(u,v) f(u, v) ≤ c(v) (that is, the amount of flow passing through

a node can be at most the capacity of the node).

3

Page 4: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

(a) Give an algorithm that solves this problem (i.e., that finds a maximum s-t flow in this node-capacitated graph). Your algorithm should transform the node-capacitated problem into a “stan-dard” max flow problem.

Solution: We will form a new edge-capacitated graph G as follows:

• For every node v in G, create two nodes v and v′ in G (these nodes have no capacities).

• For every node v in G, create an edge from (v′, v) in G with capacity c(v′, v) = c(v).

• For every edge (u, v) in G, create an edge from (v, u′) in G with capacity c(v, u′) =∞.

Then run Ford-Fulkerson on G, using s′ as the source and t as the target.

(b) Show how your transformation works on the example graph below (i.e., draw a picture of thenew graph produced by your transformation).

Solution: Our algorithm creates the following new graph G:

(c) What is the runtime required for your transformation (this should be polynomial in n and m)?

Solution: The transformation requires time O(n + m). We need O(n) time to create a pair ofnodes for each node in G and add an edge between them, and then O(m) time to add one edgeto G for each edge in G. If we assume that our original graph is connected (i.e., there is a pathfrom s to every other vertex), then we know thatm ≥ n−1, so we can say thatO(n+m) = O(m).

4

Page 5: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

(d) Explain why solving the “standard” max flow problem on your transformed graph yields a cor-rect solution to the node-capacitated problem.

We’ll show that there is a flow of value V in the original graph if and only if there is a flow of valueV in the transformed graph. This will tell us that the max flow in the transformed graph is equal tothe max flow in the original graph.

1) If there’s a flow of value V in the original graph, then there is a flow of value V in the trans-formed graph.Suppose our flow in the original graph sends flow xuv = f(u, v) along edge (u, v). Then in thetransformed graph, send an amount of flow xuv along edges (u, v′) and (v′, v). We need to checkthat this satisfies the capacity and conservation constraints in the transformed graph.

To show that the capacity constraints hold, first observe that each edge (u, v′) has capacity∞, soit is impossible to violate these capacity constraints. For each edge (v′, v), we need to show thatf(v′, v) ≤ c(v′, v). We have f(v′, v) = xuv = f(u, v) ≤ c(v) = c(v′, v). Hence the capacityconstraints hold.

To show that the conservation constraints hold, it will help to think about nodes v and v′ separately.We need to show that f in(v) = f out(v) for all nodes v, and f in(v′) = f out(v′) for all nodes v′. Forv′, we have:

f in(v′) =∑(u,v′)

f(u, v′)

=∑(u,v′)

xuv

= f(v′, v)

= f out(v′).

For v, we have:

f in(v) = f(v′, v)

=∑(u,v′)

xuv

=∑(u,v)

f(u, v)

= f in(v)

= f out(v)

=∑(v,w)

f(v, w)

=∑(v,w′)

f(v, w′)

= f out(v).

5

Page 6: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

So our capacity constraints and conservation constraints hold. The total value of the flow in thetransformed graph is f out(s′) =

∑(s′,u) f(s

′, u) =∑

(s,u) f(s, u) = f out(s) = V , which by defini-tion is the value of the flow in the original graph as desired.

2) If there’s a flow of value V in the transformed graph, then there is a flow of value V in theoriginal graph.

Suppose our flow in the transformed graph sends flow yuv′ along edge (u, v′). Then in the originalgraph, send an amount of flow yuv′ along edge (u, v). We need to check that this satisfies thecapacity and conservation constraints in the original graph.To show that the capacity constraints hold, observe that the flow into node v in the original graphis f in(v) =

∑(u,v) f(u, v) =

∑(u,v) yuv′ = f in(v′) = f out(v′) = f in(v) ≤ c(v′, v) = c(v). Hence

the capacity constraints are satisfied.

To show that the conservation constraints hold, we have f in(v) =∑

(u,v) f(u, v) =∑

(u,v) yuv′ =

f in(v′) = f out(v′) = f in(v) = f out(v) =∑

(v,w′) yvw′ =∑

(v,w) f(v, w) = f out(v). Hence theconservation constraints are satisfied.

The total value of the flow in the original graph is f out(s) =∑

(s,u) f(s, u) =∑

(s,u) ysu′ =

f out(s) = f in(s) = f out(s′) = V , which by definition is the value of the flow in the transformedgraph as desired.

3 Applications4) Blood transfusions.Suppose you work for a hospital, and your job is to determine whether the current supply of bloodwill be enough to meet the projected demand over the next week for blood transfusions.

Unfortunately, it’s not as simple as checking whether the number of liters currently available isgreater than the number of liters required: people have blood types that constrain the types ofblood they can receive. A person’s blood supply has certain antigens present, and a person cannotreceive blood that contains a particular antigen if their own blood does not have this antigen. Con-cretely, blood is divided into four types: A, B, AB, and O. Blood of type A has antigen A, bloodof type B has antigen B, blood of type AB has both antigens, and blood of type O has neither. Soa patient with type-A blood can receive only blood types A or O in a transfusion, a patient withtype-B blood can receive only blood types B or O, a patient with type-AB blood can receive anyof the four types, and a patient with type-O blood can only receive blood type O.

(a) Let sA, sB, sAB, and sO denote the supply of each of the different blood types (assume theseare all positive integers). Let dA, dB, dAB, and dO denote the demand of each of the different bloodtypes in the next week. Give an algorithm to determine if the current supply of blood is enough tomeet the demand. Your algorithm should model the blood-supply problem as a network flow prob-lem (i.e., represent the problem using a graph, and then run a max-flow algorithm on the resulting

6

Page 7: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

graph). In your algorithm description, it should be clear (i) what the vertices and edges representin the graph you create, and (ii) how, after running a max-flow algorithm on the graph you created,you can determine if the current blood supply is sufficient.

Solution: (i) We will form a graph G′ as follows:

• Create two nodes xi and yi for each blood type i = A,B,AB,O, where node xi is a “supply”node and node yi is a “demand” node.

• Create a source node s and a target node t.

• Create a directed edge from s to each node xi with capacity c(s, xi) = si, and create adirected edge from each node yi to t with capacity c(yi, t) = di.

• Create a directed edge from xi to yj with capacity∞ if patients with blood type j can receivedonations of type i.

(ii) Then run Ford-Fulkerson on the resulting graph to find v(f), the value of the max flow. Ifv(f) =

∑i di, then the available supply is sufficient to meet the demand. Furthermore, the flow

found by Ford-Fulkerson gives us a way of allocating blood to patients that meets the demand: ifthe flow along edge (xi, yj) is f(xi, yj), then use f(xi, yj) units of type-i blood for transfusionsto patients with type-j blood. Our capacity constraints on edges (s, xi) guarantee that we don’tuse more type-i blood than we have in the supply, and our capacity constraints on edges (yj, t)guarantee that we don’t allocate more blood to type-j patients than there is demand. Hence this isa valid allocation of blood to patients.

We can also show that there is a valid flow for any allocation of blood to patients that meets thedemand. Let’s suppose we have a valid allocation that uses ui,j units of type-i blood in transfusionsto patients with type-j blood. Then set the flow as follows:

f(s, xi) =∑j

ui,j

f(xi, yj) = ui,j

f(yj, t) =∑i

ui,j

The first line says that the total flow from s to xi is equal to the total number of units of type-iblood that are used. The second line says that the flow from the type-i supply node to the type-jdemand node is equal to the number of units of type-i blood that are used in transfusions to patientswith type-j blood. The third line says that the total flow from yj to t is equal to the total numberof units of blood used in transfusions to patients with type-j blood.

This flow satisfies the capacity constraints because in a valid allocation (1) we cannot use moreunits of type-i blood than we have, so f(s, xi) =

∑j ui,j ≤ si, and (2) we cannot allocate more

units of blood to type-j patients than there is demand, so f(yj, t) =∑

i ui,j ≤ dj . The flow alsosatisfies the conservation constraints because (1) at any node xi, we have f in(xi) = f(s, xi) =∑

j ui,j =∑

j f(xi, yj) = f out(xi), and (2) at any node yj , we have f in(yj) =∑

i f(xi, yj) =

7

Page 8: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

∑i ui,j = f(yj, t) = f out(yj).

(b) Consider the following example:

blood type supply demandO 50 45A 36 42B 11 8AB 8 3

Show how your algorithm works on this example. You should draw a picture of the graph youcreate and describe one possible run of Ford-Fulkerson on the graph.

Solution: Here is the graph we create:

Here is one possible run of Ford-Fulkerson:

1. Send 3 flow along path s, xAB, yAB, t.

2. Send 8 flow along path s, xB, yB, t.

3. Send 36 flow along path s, xA, yA, t.

4. Send 45 flow along path s, xO, yO, t.

5. Send 5 flow along path s, xO, yA, t.

At this point, here is the residual graph:We can see that there are no paths from s to t in the residual graph, so our run of Ford-Fulkersonis complete. The max flow that we found has value 97.

(c) In the example from part (b), is it possible to meet the full demand? Explain why or why not,using an argument based on a min cut.

8

Page 9: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

Solution: It is not possible to meet the full demand in this graph. The total demand is 98, andthe value of the max flow that we found is 97. We know that the value of the max flow isequal to the value of the min cut, and one cut that has value 97 is A = {s, xB, xAB, yB, yAB},B = {t, xO, xA, yO, yA}. The edges that cross this cut are (s, xO), (s, xA), (yB, t), and (yAB, t).

Intuitively, the reason we can’t meet the full demand is that if we look at the subset of blood typesO and A, we see that the total demand for these blood types is 45 + 42 = 87. However, the totalsupply that can be used in transfusions to either blood type is only 50+ 36 = 86, so we will not beable to meet the demand.

(d) In general, if it is not possible to meet the full demand, the output of your algorithm still ismeaningful. What does it tell you?

Solution: If it is not possible to meet the full demand, the output of our algorithm tells us how tomeet the maximum possible demand: use f(xi, yj) units of type-i blood for transfusions to patientswith type-j blood. In this case, the resulting flow tells us to use the following allocation:

• Use 45 units of type-O blood in transfusions to type-O patients.

• Use 5 units of type-O blood in transfusions to type-A patients.

• Use 36 units of type-A blood in transfusions to type-A patients.

• Use 8 units of type-B blood in transfusions to type-B patients.

• Use 3 units of type-AB blood in transfusions to type-AB patients.

While this allocation does not meet the full demand (we are short 1 unit of blood for type-Apatients), it does give us a way to meet as much of the demand as possible.

5) Friend cohesiveness.Graphs often are used to model friendship connections. For example, one could imagine a graph

9

Page 10: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

representing connections on Facebook, where there is a node for each user and an undirected edgebetween two people if they are friends on Facebook. These types of graphs are used to study thingslike the spread of information, the spread of disease, and even how to identify pairs of people whoare in a romantic relationship.

In this problem, we’re concerned with cohesion in friend groups—that is, we want to under-stand how “close-knit” a group is. Let’s assume that as described above, we are given a graphG = (V,E), where |V | = n and |E| = m, in which the nodes represent people and there is anundirected edge between two nodes if those people are friends. For a subset S of nodes, let e(S)denote the number of edges in S—that is, the number of edges e = (u, v) such that both u and vare in S. We define the cohesiveness of S to be e(S)/|S|, where |S| is the number of nodes in S.

(a) Give an algorithm that takes a rational number α and determines whether there exists a set Swith cohesiveness at least α. Your algorithm should involve solving a max-flow problem on somegraph. In your algorithm description, it should be clear (i) what the vertices and edges represent inthe graph on which you are running a max-flow algorithm, and (ii) how, after running a max-flowalgorithm on your graph, you can determine if there is a group with cohesiveness at least α.

Solution: (i) We will form a graph G′ as follows:

• For each node v in G, create a node v′ in G′.

• For each edge (u, v) in G, create a node xu,v in G′.

• Create a source node s and a target node t in G′.

• For each node xu,v in G′, create a directed edge (s, xu,v) with capacity c(s, xu,v) = 1.

• For each node xu,v in G′, create two directed edges (xu,v, u′) and (xu,v, v′) with capacity∞.

• For each node v′ in G′, create a directed edge (v′, t) with capacity c(v′, t) = α.

Using our result to problem (2), we will then further transform our graph by multiplying all edgeweights by the same constant factor β, where β is defined to be the smallest positive number suchthat αβ is an integer.

We then run Ford-Fulkerson on the graph G′ to find a max flow from s to t. There is a group withcohesiveness e(s)/|S| > α if and only if the max flow has value v(f) < βm.

To understand this, we will think about a min cut (A,B) in the graph. In this min cut, if nodexu,v is in A, then node u and node v must both be in A (why? Because edge c(xu,v, u) = ∞, andan infinite-capacity edge clearly cannot cross a min cut, because we could find a lower-cost cutsimply by letting A = {s}). Similarly, if node v′ is in B, then all nodes xu,v must also be in B.

Now think about the nodes that end up in set A. Let S be the set of “vertex” nodes v′ that end upin A. The number of “edge” nodes xu,v in A is exactly the number of edges in the original graphthat have both endpoints in S; there are e(S) such edges. The edges that cross our cut are (1) alledges (v′, t) for which v′ ∈ S, each of which has capacity αβ (there are |S| such edges), an (2)

10

Page 11: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

all edges (s, xu,v) for which u, v /∈ S, each of which has capacity β (there are (m − e(S)) suchedges). Hence the total capacity of our cut is c(A,B) = (m− e(S))β + |S|αβ. We can rearrangethis to get βm− c(A,B) = β(e(S)− |S|α), or m− 1

βc(A,B) = e(S)− |S|α. From this we can

see that e(S)−|S|α > 0 (i.e., e(S)/|S| > α) if and only ifm− 1βc(A,B) > 0, i.e., c(A,B) < βm.

So we have shown that if we have a max flow (equivalently, a min cut) with value v(f) < βm,then we can find a group of vertices in our original graph with cohesiveness greater than α.

On the other hand, if the max flow has value v(f) = βm, then we have saturated all of the edgesout of s. If we consider any subset of vertices S from our original graph, the total flow goinginto vertices v′ such that v ∈ S must be at least the total flow going into vertices xu,v such thatu, v ∈ S. This latter quantity is

∑(s,xu,v):u,v,∈S f(s, xu,v) =

∑(s,xu,v):u,v,∈S β = βe(S). So we

know that f in(S) ≥ βe(S). By conservation of flow, we have f out(S) ≥ βe(S). But we must alsohave f out(S) ≤ βα|S| by the capacity constraints. Putting this together, we have e(s) ≤ α|S|, ore(s)/|S| ≤ α. Hence the set S does not have cohesiveness greater than α.

Intuitively, it’s easier to understand why this works if we consider the graph before we multiply allthe edges by β. Imagine that we start off by sending 1 unit of flow into every “edge” node. We canonly get that 1 unit of flow from an “edge” node xu,v to t by sending it through either node u′ or v′.If we consider any subset S of nodes in the original graph, we have sent a total of e(S) flow intothe “edge” nodes belonging to this set, and we have a total capacity of α|S| to use to get the flowout of this set. So it is only possible to get all of the flow from s to t if e(S) ≤ α|S|. Otherwise theset S is too “dense”: there are too many edges within nodes in S for the flow we sent into S to beable to “escape” to t.

(b) Consider the following example of a friend graph:

Draw a picture of the “transformed” graph that your algorithm produces, on which you then run amax-flow algorithm (you do not need to show the actual run of Ford-Fulkerson).

Solution: Here is the graph that our algorithm produces:

11

Page 12: COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions...COSC 311: ALGORITHMS HW4: NETWORK FLOW Solutions 1 Warmup 1) Finding max flows and min cuts. Here is a graph (the numbers in boxes

(c) What is the runtime required for your transformation (this should by polynomial in n and m)?

Solution: We create one node inG′ for each node and each edge inG; this takes timeO(n+m). Wethen create up to three edges for each new node in G′, which again takes time O(n+m). We needto do the transformation from problem (3) to ensure that all capacities are integers; as discussedin problem (3) this can depend on the magnitude of the edge capacities, but if we suppose that wecan do it taking linear time in the number of nodes and edges in the new graph, we end up withO(n+m).

12