Download as pdf or txt
Download as pdf or txt
You are on page 1of 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:
P 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
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 Theory


2) Rational edge capacities.
So far we have assumed that all of the edge capacities in our graph are positive integers. In this
problem 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 that
argument did we use the fact that the edge capacities are integers? Why does the argument break
if 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.
P
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 at
least 1 on every iteration, and by Claim 3 there can be at most C iterations. If our edge capacities
are 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 could
approach C without ever actually reaching C, 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 an
O(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 integer
capacities), 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 edge
capacities 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 number
of digits in c(e), which could be very large since we have made no assumptions about how big

2
the edge capacities might be. Another option is to, instead of using the LCD, simply let ` be the
product 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 the
edge capacities.

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

v(f 0 ) > v(fˆ)


X X
f 0 (e) > fˆ(e)
e out of s e out of s
X X
`· 0
f (e) > ` · fˆ(e)
e out of s e out of s
X X
0
` · f (e) > ` · fˆ(e)
e out of s e out of s
X
` · f 0 (e) > v(f )
e out of s

which is a contradiction because v(f ) was a max flow in the scaled graph. Hence v(fˆ) must be a
max 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 amount
of 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 P value that satisfies the usual conservation constraints, as well as
new 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
(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 Ĝ as follows:


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

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

• For every edge (u, v) in G, create an edge from (v̂, û0 ) in Ĝ with capacity c(v̂, û0 ) = ∞.
Then run Ford-Fulkerson on Ĝ, using ŝ0 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 the
new graph produced by your transformation).

Solution: Our algorithm creates the following new graph Ĝ:

(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 of
nodes for each node in G and add an edge between them, and then O(m) time to add one edge
to Ĝ for each edge in G. If we assume that our original graph is connected (i.e., there is a path
from s to every other vertex), then we know that m ≥ n−1, so we can say that O(n+m) = O(m).

4
(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 value
V in the transformed graph. This will tell us that the max flow in the transformed graph is equal to
the 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 the
transformed graph, send an amount of flow xuv along edges (û, v̂ 0 ) and (v̂ 0 , v̂). We need to check
that this satisfies the capacity and conservation constraints in the transformed graph.

To show that the capacity constraints hold, first observe that each edge (û, v̂ 0 ) has capacity ∞, so
it is impossible to violate these capacity constraints. For each edge (v̂ 0 , v̂), we need to show that
f (v̂ 0 , v̂) ≤ c(v̂ 0 , v̂). We have f (v̂ 0 , v̂) = xuv = f (u, v) ≤ c(v) = c(v̂ 0 , v̂). Hence the capacity
constraints hold.

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

= f (v̂ 0 , v̂)
= f out (v̂ 0 ).

For v̂, we have:

f in (v̂) = f (v̂ 0 , v̂)


X
= xuv
(û,v̂ 0 )
X
= f (u, v)
(u,v)

= f in (v)
= f out (v)
X
= f (v, w)
(v,w)
X
= f (v̂, ŵ0 )
(v̂,ŵ0 )

= f out (v̂).

5
So our capacity constraints and P conservation constraints hold. The total value of the flow in the
transformed graph is f out (ŝ0 ) = (ŝ0 ,û) f (ŝ0 , û) = (s,u) f (s, u) = f out (s) = V , which by defini-
P
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 the
original graph.

Suppose our flow in the transformed graph sends flow yûv̂0 along edge (û, v̂ 0 ). Then in the original
graph, send an amount of flow yûv̂0 along edge (u, v). We need to check that this satisfies the
capacity and conservation constraints in the original graph.
To show thatPthe capacity constraints hold, observe that the flow into node v in the original graph
is f in (v) = (u,v) f (u, v) = (u,v) yûv̂0 = f in (v̂ 0 ) = f out (v̂ 0 ) = f in (v̂) ≤ c(v̂ 0 , v̂) = c(v). Hence
P
the capacity constraints are satisfied.

To show that the conservation constraints hold, we have f in (v) = (u,v) f (u, v) = (u,v) yûv̂0 =
P P

f in (v̂ 0 ) = f out (v̂ 0 ) = f in (v̂) = f out (v̂) = (v̂,ŵ0 ) yv̂ŵ0 = (v,w) f (v, w) = f out (v). Hence the
P P
conservation constraints are satisfied.

The total value of the flow in the original graph is f out (s) =
P P
(s,u) f (s, u) = (s,u) yŝû0 =
out in out 0
f (ŝ) = f (ŝ) = f (ŝ ) = V , which by definition is the value of the flow in the transformed
graph as desired.

3 Applications
4) Blood transfusions.
Suppose you work for a hospital, and your job is to determine whether the current supply of blood
will 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 is
greater than the number of liters required: people have blood types that constrain the types of
blood they can receive. A person’s blood supply has certain antigens present, and a person cannot
receive 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, blood
of type B has antigen B, blood of type AB has both antigens, and blood of type O has neither. So
a patient with type-A blood can receive only blood types A or O in a transfusion, a patient with
type-B blood can receive only blood types B or O, a patient with type-AB blood can receive any
of 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 these
are all positive integers). Let dA , dB , dAB , and dO denote the demand of each of the different blood
types in the next week. Give an algorithm to determine if the current supply of blood is enough to
meet 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
graph). In your algorithm description, it should be clear (i) what the vertices and edges represent
in 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 G0 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 a
directed 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 receive
donations of type i.

(ii) ThenPrun Ford-Fulkerson on the resulting graph to find v(f ), the value of the max flow. If
v(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: if
the flow along edge (xi , yj ) is f (xi , yj ), then use f (xi , yj ) units of type-i blood for transfusions
to patients with type-j blood. Our capacity constraints on edges (s, xi ) guarantee that we don’t
use 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 is
a 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 the
demand. Let’s suppose we have a valid allocation that uses ui,j units of type-i blood in transfusions
to patients with type-j blood. Then set the flow as follows:
X
f (s, xi ) = ui,j
j

f (xi , yj ) = ui,j
X
f (yj , t) = ui,j
i

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

This flow satisfies the capacity constraints becausePin a valid allocation (1) we cannot use more
units of type-i blood than we have, so f (s, xi ) = j ui,j ≤ si , and P (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 also
conservation constraints because (1) at any node xi , we have f in (xi )P= f (s, xi ) =
satisfies theP
out
(xi ), and (2) at any node yj , we have f in (yj ) = i f (xi , yj ) =
P
j ui,j = j f (xi , yj ) = f

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

(b) Consider the following example:

blood type supply demand


O 50 45
A 36 42
B 11 8
AB 8 3
Show how your algorithm works on this example. You should draw a picture of the graph you
create 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-Fulkerson
is 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
Solution: It is not possible to meet the full demand in this graph. The total demand is 98, and
the value of the max flow that we found is 97. We know that the value of the max flow is
equal 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 types
O and A, we see that the total demand for these blood types is 45 + 42 = 87. However, the total
supply that can be used in transfusions to either blood type is only 50 + 36 = 86, so we will not be
able to meet the demand.

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

Solution: If it is not possible to meet the full demand, the output of our algorithm tells us how to
meet the maximum possible demand: use f (xi , yj ) units of type-i blood for transfusions to patients
with 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-A
patients), 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
representing connections on Facebook, where there is a node for each user and an undirected edge
between two people if they are friends on Facebook. These types of graphs are used to study things
like the spread of information, the spread of disease, and even how to identify pairs of people who
are 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 graph
G = (V, E), where |V | = n and |E| = m, in which the nodes represent people and there is an
undirected 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 v
are 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 S
with cohesiveness at least α. Your algorithm should involve solving a max-flow problem on some
graph. In your algorithm description, it should be clear (i) what the vertices and edges represent in
the graph on which you are running a max-flow algorithm, and (ii) how, after running a max-flow
algorithm on your graph, you can determine if there is a group with cohesiveness at least α.

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


• For each node v in G, create a node v 0 in G0 .

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

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

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

• For each node xu,v in G0 , create two directed edges (xu,v , u0 ) and (xu,v , v 0 ) with capacity ∞.

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


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

We then run Ford-Fulkerson on the graph G0 to find a max flow from s to t. There is a group with
cohesiveness 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 node
xu,v is in A, then node u and node v must both be in A (why? Because edge c(xu,v , u) = ∞, and
an infinite-capacity edge clearly cannot cross a min cut, because we could find a lower-cost cut
simply by letting A = {s}). Similarly, if node v 0 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 0 that end up
in A. The number of “edge” nodes xu,v in A is exactly the number of edges in the original graph
that have both endpoints in S; there are e(S) such edges. The edges that cross our cut are (1) all
edges (v 0 , t) for which v 0 ∈ S, each of which has capacity αβ (there are |S| such edges), an (2)

10
all edges (s, xu,v ) for which u, v ∈
/ S, each of which has capacity β (there are (m − e(S)) such
edges). Hence the total capacity of our cut is c(A, B) = (m − e(S))β + |S|αβ. We can rearrange
this 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 if m − β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 edges
out of s. If we consider any subset of vertices S from our original graph, the total flow going
into vertices v 0 such that v ∈ S must
P be at least the total flowPgoing into vertices xu,v such that
u, 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 also
have f out (S) ≤ βα|S| by the capacity constraints. Putting this together, we have e(s) ≤ α|S|, or
e(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 all
the edges by β. Imagine that we start off by sending 1 unit of flow into every “edge” node. We can
only get that 1 unit of flow from an “edge” node xu,v to t by sending it through either node u0 or v 0 .
If we consider any subset S of nodes in the original graph, we have sent a total of e(S) flow into
the “edge” nodes belonging to this set, and we have a total capacity of α|S| to use to get the flow
out of this set. So it is only possible to get all of the flow from s to t if e(S) ≤ α|S|. Otherwise the
set S is too “dense”: there are too many edges within nodes in S for the flow we sent into S to be
able 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 a
max-flow algorithm (you do not need to show the actual run of Ford-Fulkerson).

Solution: Here is the graph that our algorithm produces:

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

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

12

You might also like