Algorithms and Data Structures Sample 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Submit Assignment For Help

Go To Code Directly
Massachusetts Institute of Technology Handout 7
6.854J/18.415J: Advanced Algorithms
[email protected] Wednesday, September 28, 2005
David Karger

Problem Set 4

Due: Friday, October 7 2005.

Notice that one problem is marked noncollaborative. As you might expect, this prob­
lem should be done without any collaboration.

NONCOLLABORATIVE Problem 1. Which of the following statements about flows


are true and which are false? Justify your answer with a (short) proof or counterexample.

(a) If all directed edges in a network have distinct capacities, then there is a unique

maximum flow.

(b) If we replace each directed edge in a network with two directed edges in opposite

directions with the same capacity and connecting the same vertices, then the

value of the maximum flow remains unchanged.

(c) If we add the same positive number λ to the capacity of every directed edge, then

the minimum cut (but not its value) remains unchanged.

Problem 2. In this problem we develop a scaling algorithm for shortest paths that achieves
running time O(m log C) with no data structure more complicated than an array. Recall
that when we discussed shortest paths with positive integer edge lengths, we showed how
Dial’s algorithm uses a single array of buckets to find shortest paths in O(m + nC) time for
maximum edge length C.
Consider a solution to a shortest path problem with edge lengths lvw from v to w in which
the (shortest path) distance of vertex w from the source s is dv . Define the reduced edge
d d
lengths lvw by the rule lvw = lvw + dv − dw .

(a) Argue that in fact the running time of Dial’s algorithm is O(m + D), where D

is the maximum distance, and that the algorithm still works if some edges have

length 0.

(b) Show that the reduced edge lengths defined above are all nonnegative integers.
(c) Show that the shortest paths under the reduced length function are the same as

those under the original length function. What is the length of shortest paths

under ld ?

(d) Devise a scaling algorithm for shortest paths. Use the reduced edge lengths and

Dial’s bucketing algorithm to keep the task of shifting in a new bit simple.

https://www.programminghomeworkhelper.com/
2 Handout 7: Problem Set 4

(e) Is base­2 scaling the best possible, or can you achieve a (slightly) better running

time by scaling with a different base?

Problem 3. At lunchtime it is crucial for people to get to the food trucks as quickly as
possible. We will show how to use maximum flow to solve this problem. Consider the follow­
ing model. The building is represented by a graph G = (V, E), where each room, landing,
or other location is represented by a vertex and each corridor or stairway is represented by
an edge. Each corridor has an associated capacity c, meaning that at most c people can
pass through the corridor at once. Traversing a corridor from one end to the other takes one
timestep. (Traversing a room takes zero time.)

(a) Suppose all people are initially in a single room s, and that the building has

a single exit t. Show how to use maximum flow to find a fastest way to get

everyone out of the building. Hint: create an auxiliary graph G� that has vertices

to represent each room at each time step.

(b) Show that the same technique can be used when people are initially in multiple

locations and there are multiple exits.

(c) Generalize the approach to where different corridors have different (integral)

transit times.

OPTIONAL (d) Suppose transit times are not integral. Is there still a way to solve

the problem?

OPTIONAL (e) The above algorithm is polynomial in the number of people. Can

you improve it to be polynomial (in the graph size) regardless of the number of

people?

Problem 4. The minimum flow problem is a close relative of the max flow problem with
nonnegative lower bounds on edge flows (if an edge (i, j) has a lower bound lij ≥ 0, then
fij must satisfy lij ≤ fij ≤ uij ). In the minimum flow problem we wish to send a minimum
amount of flow from the source s to the destination t while satisfying given lower and upper
bounds on edge flows fij .

(a) Show how to solve the minimum flow problem by using two applications of any

maximum flow algorithm that applies to problems with zero lower bounds on

edge flows (e.g., the algorithms described in the lecture). Hint: first construct

any feasible flow and then convert it into a minimum flow.

(b) Prove the following min­flow­max­cut theorem. Let the lower bound on the cut
capacity of an s­t cut (S, T = V \ S) be defined as (i,j)∈S×T lij − (i,j)∈T ×S uij .
� �

Show that the minimum value of all feasible flows from node s to node t equals
to the maximum lower bound on cut capacity of all s­t cuts.
Handout 7: Problem Set 4 3

(c) A group of students wants to minimize their lecture attendance by sending only

one of the group to each of n lectures to take notes. Lecture i begins at time ai

and ends at time bi . It requires rij time to commute from lecture i to lecture j.

Develop a flow­based algorithm for identifying the minimum number of students

needed to cover all the lectures. Hint: reduce to minimum flow.

OPTIONAL Problem 5. Augmenting paths are guaranteed to terminate in finite time


with a maximum flow only if the edge weights are rational.

(a) Give a graph on which some augmenting path algorithm fails to terminate it

finite time.

(b) (Harder) Give a graph on which some augmenting path algorithm fails to termi­

nate it finite time, and whose limiting flow is not maximum.

Massachusetts Institute of Technology Handout 10


6.854J/18.415J: Advanced Algorithms Wednesday, October 5, 2005
David Karger

Problem Set 4 Solutions

Problem 1. (a) False. Consider graph with

V = {s, 1, 2, 3, t},

and

E = {(s, 1), (1, 2), (1, 3), (2, t), (3, t)}.

The capacities are u(s, 1) = 1, u(1, 2) = 5, u(1, 3) = 6, u(2, t) = 7, u(3, t) = 8.


There are at least 2 possible maximal flows: 1) f (s, 1) = f (1, 2) = f (2, t) = 1
and 0 in the rest; 2) f (s, 1) = f (1, 3) = f (3, t) = 1 and 0 in the rest.
(b) False. Consider graph with V = {s, t} and E = {(t, s)}, the edge having a
capacity of 1. Then, the initial graph has a max flow of 0, while the modified
graph has a max flow of 1.
(c) False. Consider graph with

V = {s, 1, 2, 3, t},

and

E = {(s, 1), (1, 2), (1, 3), (2, t), (3, t)}.

The capacities are u(s, 1) = 3, u(1, 2) = 1, u(1, 3) = 1, u(2, t) = 1, u(3, t) = 1.


For this graph a min cut is S = {s, 1}. However, if we add a value of λ = 100 to
the capacity of each edge, then the min cut becomes S = {s}

Problem 2. Let G be the graph under consideration.

(a) We assume that the array creation takes constant time. There are n insert, m
decrease-key and n delete-min operations. The insert and decrease-key operations
take O(1) time. Delete-min takes O(1 + d) time, where d is the number of empty
buckets skipped during the delete-min operation. The bucket number of the last
element deleted is D. Thus the total cost of delete-min is O(m + D).
(b) Given the shortest path P from s to v of length dv , the path P + vw is known
to have length dv + lvw . Therefore

dw ≤ dv + lvw (1)

and the reduced edge length lvw is non-negative.


2 Handout 10: Problem Set 4 Solutions

(c) For any path P = (sv2 , v2 v3 , . . . , vk−1 vk ), the total reduced edge length is
d
lsv2
+ . . . + lvdk−1 vk = (lsv2 + ds − dv2 ) + . . . + (lvk−1 vk + dvk−1 − dvk )
= ds + (lsv2 + . . . + lvk−1 vk ) − dvk

Therefore all paths to a vertex v have reduced length as the length minus (con-
stant) dvk . So the shortest path to v is the same and has length dv − dv = 0.
(d) The scaling algorithm works as follows. We initially start with edge lengths 0,

and distance function d1 (v) = 0 for all v. In step k, we shift a bit of the length in

each edge, and compute distances dk+1 0


with reduced edge lengths based on dk .

k+1 k k+1
We use distance function d = d + d0
for the reduced costs in the next step.
After log C steps the exact distance will be computed. We will now prove the
correctness of this algorithm and analyze its running time.
Consider graph G = (V, E) constructed from the original graph G = (V, E) with

edge length lvw = lvw /2. In a scaling step, the distances in G are used to
compute reduced edge length in G. Notice that part (b) and (c) of this problem
work for any distance function satisfying (1). So if we define the distance function
as distance in G , we still have the same shortest paths in G and G . This proves
the correctness of the algorithm.
The length of shortest paths is 0 in the reduced graph G . This means that in the
original graph G the length of the shortest path is at most n. So Dial’s algorithm
takes O(m + n) = O(m) time. The total time complexity for log C steps is
therefore O(m log C).
(e) If a base b representation is used, there are logb C scaling steps. The maximum

distance D in each shortest path computation is bounded tightly by n(b − 1).

Thus the time complexity of our scaling algorithm is O((m + n(b − 1)) · logb C).

If we set b = 2 + m/n we achieve O(m log2+m/n C) running time.

Problem 3. (a) We give an algorithm to decide if all people can be moved out in
t steps. Now, we can increment t to find the shortest time in which all the people
can move out.
The algorithm is as follows: given G, construct Gt as follows. For each v ∈ V ,
make t copies of v: v1 . . . vt . Construct an edge from vi to vi+1 at time t with
infinite capacity (people can just stay in rooms at a time step). Construct an
edge from vi to wi+1 with capacity C if there exists an edge from v to w with
capacity C in G.
To test if all the people can get from the source to the sink in t timesteps, we
check if the max flow in Gt is equal to the number of people initially at the source.
If so, we can move all the people across this graph in t timesteps.
Note that the size of the graph is polynomially large, so the algorithm runs in
polynomial time.
Handout 10: Problem Set 4 Solutions 3

(b) We can use the same overall idea: construct a graph Gt , and compute its max

flow. If its max flow is equal to the total number of people we are trying to move,

then t time units suffice to move all the people across the graph.

The construction of Gt is the same, except for the following. We create a sink
s and source t. Let S be the start vertices, and let T be the sink vertices. We
create a link from s to each x1 , for each x ∈ S with capacity equal to the number
of people starting at x. Similarly, we create a link from each xt (for each x ∈ T )
to t with infinite capacities.

(c) Again, the overall idea is the same. But when we construct Gt now, we create

edges between the layers in a different way: construct the edge linking vi to wi+δ

with capacity C if there is an edge between v and w with transit time δ.

Problem 4. 1. As suggested by the hint, we first construct some feasible flow on the
graph G, and then construct a minimum flow using the feasible flow.
We find a feasible as follows. Note that just finding a max flow from s to t is not suffi-
cient as that does not guarantee that the flow flows along the correct edges (to satisfy
the minimums). Instead, we modify the original graph G to get a graph G as follows.
For each edge (i, j) in G, we change the capacity uij = uij − lij . Then we drop the
minimum capacities on all edges. Thus, in essence, we are forcing a flow of lij across
each of the edges, but we have a surplus and deficit of lij at i and j respectively. What
ways could we get rid of this deficit? Well, we could find a path from a surplus to a
deficit node in G . Or, we could find a path from a surplus node j to the sink t and
the source s to the surplus node i. We want to solve this problem using max flows. So,
we add to G a new source s and a new sink t . We add edges (i, t ) and (s , j) with
capacities uit = usj = lij 1 . Finally, we add an edge (t, s) (between the original source
and sink) with unbounded capacity uts = ∞. If we start with the graph:

lij ; uij
s i j t

We would end with the graph

1
We may add more than one edge (i, t� ) for example, or � we can add a single � edge with the sums of the
relevant capacities. More formally, we could say that u� it� = j lij and us� � j = i lij .
4 Handout 10: Problem Set 4 Solutions

s'

lij

uij - lij
s i j t

lij

t'

Then all we need to do is find a max flow from s to t . We saturate all the edges (s , j)
if and only if for every node j with a surplus, there are path(s) to t and/or some other
deficit node(s) with enough capacity. Thus, we just check, are all these edges saturated?
If no, there is no feasible flow on the graph G . If yes, then there is a feasible flow,
AND we know what it is. All we need to do is drop all the extra edges we added,
look at the flow f  we found on G , and add the original edge minimums. For example,
if we find flow of fij across an edge (i, j), then the flow in the original graph is just
f = fij + lij . Another way of thinking about this is that going from s to t in the graph
G is equivalent to going from s to t in the original graph, because we move the flow
from (s , j) and (i, t ) to the edge (i, j).
Next, we convert the feasbile flow into a minimum flow. Let our feasible flow on any
edge from i to j be fij (in gross flow formulation). We construct a “reverse residual
graph” by taking every edge from i to j in the original graph G, and replacing it with
two edges in G : one edge from i to j with capacity uij − fij , and one edge from j to
i with capacity fij − lij . We might get two edges by this construction going from i to
j, so we combine the edges by adding the capacities. So the reverse residual graph has
an edge from i to j with capacity uij − fij + fji − lji .
Then we compute a normal maximum flow gij from t to s in G , and add the result
back into the flow in G to get a flow f  . When we add the flow back, we decompose
g into gij = gij + hij , where 0 ≤ gij ≤ uij − fij and 0 ≤ hij ≤ fji − lji. We add the
flow back by adding g  in the forward direction and subtracting out hji in the backward
direction. So the new flow from i to j in our original graph G is

fij = fij + gij − hji .


The inequality relationships we have are
lij ≤ fij ≤ uij
0 ≤ gij ≤ uij − fij
0 ≤ hij ≤ fji − lji
Handout 10: Problem Set 4 Solutions 5

Adding fij to both sides of gij ≤ uij and using the fact that hji ≥ 0 tells us that
fij ≤ uij . Adding fij to the inequality −hji ≥ lij − fij and using the fact that gij ≥ 0
gives us fij ≥ lij . Therefore, when we add our flow back, we still get a feasible flow.
This flow f  we compute must be a minimum flow. Otherwise, we could reduce the
flow further by finding some path from t to s in G. This corresponds to finding some
augmenting path in G from t to s, suggesting that the flow we computed in wasn’t a
maximum flow for G . This is a contradiction, so f  must be a minimum flow.
2. �
Claim 0.1 Let � the lower bound on the cut capacity of an s-t cut S be defined as L(S) =
(i,j)∈S×T lij − (i,j)∈T ×S uij . Then the minimum value of all feasible flows from node
s to t equals the maxS L(S).

Proof. We show that for any flow, f >= maxS L(S) and that min flow f
>
maxS L(S), which implies that minimum value of all feasible flows from s to t equals
maxS L(S).
Suppose we have a flow f . Then we want to show that f ≥ max L(S). Instead,
we show that f ≥ L(S) for any S. Consider any cut S. Since the flow is fea-
sible, we satisfy all the minimum capacities. So, the flow from S to T is at least

(i,j)∈S×T lij . Since the flow is feasible,

the most that can be flowing from T to S is
bounded by the maximum capacities (i,j)∈T ×S uij . Thus, the net flow across the cut
� �
is ≥ (i,j)∈S×T lij − (i,j)∈T ×S uij . Therefore f ≥ L(S).
Suppose that we have a min flow f . Assume �for the sake of� contradiction that
f > max L(S). Then for all cuts f > L(S). f > (i,j)∈S×T lij − (i,j)∈T ×S uij implies
that either we are sending more flow than the minimum from S to T , or we are not
sending the maximum amount back from T to S. Consider the first case. Then our
modified residual graph Gf from part (a) has extra capacity available from T to S on
the residual (reverse) edges. In the second case, we also have capacity available from T
to S. Since there is capacity available across ALL CUTS going from T to S, there is
an augmenting path in Gf from t to s,2 and we don’t have a min flow.

3. For lecture i, we create two vertices in our graph: Ai to represent the start of the lec-
ture and Bi to represent the end. We draw and edge (Ai , Bi ) for every lecture i, with
a minimum flow l(Ai , Bi ) = 1 and a max flow f (Ai, Bi ) = ∞. We create a source s
and a sink t. We create edges (s, Ai ) from the source to the start of every class, with
unconstrained capacities l(s, Ai ) = 0 and f (s, Ai) = ∞. Similarly, we add edges (Bi , t)
from the end of every lecture to the sink, with unconstrained capacities l(Bi , t) = 0
and f (Bi , t) = ∞. Finally, we introduce edges from the end of lecture i to the start of
lecture j if it is possible to commute from lecture i to lecture j. That is, (Bi , Aj ) exists
if bi + rij ≤ aj . Again, these edges have unconstrained capacity. The graph looks like:

2
The max-flow min-cut theorem tells us that |f | = u(S) for any S implies that there is an augmenting
path in the residual graph.
6 Handout 10: Problem Set 4 Solutions

Now we just solve for the min flow. We claim that the min flow is the minimum number
of students necessary to attend all the lectures. Basically, each unit of flow represents
a student. We argue correctness by showing that our graph has the same constraints
as the students. In particular, a student can only attend two lectures i and j if the
commute time allows it. Similarly, we only have an edge in the graph allowing flow
between lectures i and j if the commute times allow it. A student can choose any
lecture to be the first one he goes to (modeled by edges from s to all lecture starts),
and he can choose any lecture to be his last (modeled by edges from lecture ends to
t). Finally, the only other constraint we have is that at least one student must be in
attendance at each lecture. By creating two vertices for each lecture and having an edge
with min capacity of 1 between lecture start and end, we guarantee at least one unit
of flow across the lecture—that is, we guarantee that at least one student sits through
the entire lecture.

You might also like