0% found this document useful (0 votes)
121 views7 pages

MIT6 046JS15 Pset7sols

The document provides solutions to problems from a problem set on algorithms and analysis of algorithms. It includes solutions to maximum flow problems in dynamic networks where the capacity of an edge changes, finding disjoint paths in a road network for multiple trucking companies, and assigning lunch orders to customers from a food truck to minimize voucher costs. The solutions describe algorithms, prove their correctness, and analyze their time complexities.

Uploaded by

ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views7 pages

MIT6 046JS15 Pset7sols

The document provides solutions to problems from a problem set on algorithms and analysis of algorithms. It includes solutions to maximum flow problems in dynamic networks where the capacity of an edge changes, finding disjoint paths in a road network for multiple trucking companies, and assigning lunch orders to customers from a food truck to minimize voucher costs. The solutions describe algorithms, prove their correctness, and analyze their time complexities.

Uploaded by

ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Design and Analysis of Algorithms

Massachusetts Institute of Technology


Profs. Erik Demaine, Srini Devadas, and Nancy Lynch

March 0, 2015
6.046J/18.410J
Problem Set 7 Solutions

Problem Set 7 Solutions


This problem set is due at 11:59pm on Thursday, April 9, 2015.

Exercise 7-1. Read CLRS, Sections 26.1-26.3.


Exercise 7-2. Exercise 26.1-2.
Exercise 7-3. Exercise 26.1-4.
Exercise 7-4. Exercise 26.2-4.
Exercise 7-5. Exercise 26.2.6.

Problem 7-1. Maximum Flow in a Dynamic Network [20 points]

In this problem, you will design an algorithm that takes the following inputs:

A ow network F = (G, c), where G = (V, E) is a graph with source vertex s and target
vertex t, and c is a capacity function mapping each directed edge of G to a nonnegative
integer;
A maximum ow f for F ; and
A triple (u, v, r), where u and v are vertices of G and r is a nonnegative integer = c(u, v).
The algorithm should produce a maximum ow for ow network F / = (G, c/ ), where c/ is identical
to c except that c/ (u, v) = r. The algorithm should run in time O(k (V +E)), where |c(u, v)r| =
k. The algorithm should behave differently depending on whether r > c(u, v) or r < c(u, v).
(a) [4 points] Start by proving the following basic, general results about ow networks:
1. Increasing the capacity of a single edge (u, v) by 1 can result in an increase of at
most 1 in the max ow.
2. Increasing the capacity of a single edge (u, v) by a positive integer k can result in
an increase of at most k in the max ow.
3. Decreasing the capacity of a single edge (u, v) by 1 can result in a decrease of at
most 1 in the max ow.
4. Decreasing the capacity of a single edge (u, v) by a positive integer k can result
in a decrease of at most k in the max ow.

Problem Set 7 Solutions


Solution:
1. If (u, v) is in every min cut, then increasing the capacity of (u, v) by 1 increases
the min cut value by 1. If (u, v) is not in every min cut, then increasing the ca
pacity of (u, v) by 1 leaves the min cut value unchanged. Either way, the capacity
increases by at most 1. The claim follows from the max-ow-min-cut theorem.
2. Increasing by k is the same as increasing in steps of 1. By part 1, each such step
increases the max ow by at most 1. So the total increase is at most k.
3. If (u, v) is in some min cut, then decreasing the capacity of (u, v) decreases the
min cut value by 1. If (u, v) is not in any min cut, then decreasing the capacity of
(u, v) by 1 leaves the min cut value unchanged. Either way, the capacity decreases
by at most 1. The claim follows from the max-ow-min-cut theorem.
4. Decreasing by k is the same as decreasing in steps of 1. By part 4, each such step
decreases the max ow by at most 1. So the total decrease is at most k.
(b) [8 points] Suppose that r > c(u, v). Describe your algorithm for this case in detail,
prove that it works correctly, and analyze its time complexity (in terms of V , E, and
k).
Solution: Then the max ow might increase; by Part (a) 2, it increases by at most k.

Algorithm: Regard the existing ow f as a ow in the new ow network F / = (G, c/ ).

Start with the residual network of F / for ow f .

Repeat k times:

1. Look for an augmenting path in the residual network.


2. If you nd one, add it to the existing ow, else return.
Analysis: Using DFS to search for an augmenting path, each pass through the loop
takes time O(V + E), so the total time is O(k (V + E)).
Proof: As noted, increasing the capacity of a single edge by k can result in an increase
of at most k in the value of the max ow. Each time we nd an augmenting path, it
increases the ow by at least 1. So within at most k tries, we reach the max ow.
(c) [8 points] Suppose that r < c(u, v). Describe your algorithm for this case in detail,
prove that it works correctly, and analyze its time complexity.
Solution: Then the max ow might decrease; by Part (a) 4, it decreases by at most k.
Algorithm: The algorithm consists of two phases, rst removing some ow to t the
reduced capacity of the new ow network F / = (G, c/ ), and then restoring ow to
achieve the new max ow.
In Phase 1, if f (u, v) r then we do not reduce any ows. Otherwise, we reduce
the ow on (u, v) one unit at a time, until it reaches r. For each unit, we proceed as
follows.

Problem Set 7 Solutions

First reduce the ow on (u, v) by 1. Then, using DFS, search backwards from vertex
u, following incoming edges with positive (incoming) weight, looking for a simple
reverse path to s, t, or v. To see why such a path exists, proceed step by step from
u, each time following some incoming edge with positive (incoming) weight. Such
an edge must exist unless we have reached one of the three listed vertices, because
we started with a ow satisfying the ow conservation property. Subtract 1 from the
ows along each of the edges in this path.
If the path we found ended in v, we have already restored the ow conservation condi
tion throughout the network. Otherwise, vertex v still does not satisfy ow conserva
tion. So in this case, we use DFS again to search forwards from v, following outgoing
edges with positive (outgoing) weight, looking for a simple path to t or s. Subtract 1
from the ows along each of the edges in this path.
At this point, we have restored the ow conservation condition while reducing the
ow on (u, v) by 1. We repeat this a total of f (u, v) r times, reducing the ow on
(u, v) to r and restoring the ow conservation condition.
The result of this reduction is now a valid ow for the new network F / = (G, c/ );
however, it needs not to be maximum.

In Phase 2, we augment it to restore maximality. We proceed as in Part (b), trying k

times to augment the ow from s to t.

Analysis: In Phase 1, each reduction of capacity by 1 takes time O(V +E), using DFS
for the two searches. Thus, Phase 1 takes time O(k (V + E)). Phase 2 is like Part
(b), and so takes time O(k (V + E)).
Proof: As we noted, after Phase 1, we have a valid ow. Moreover, in each reduction,
we have reduced the value of the ow by at most 1. Therefore, in all, we have reduced
the value of the ow by at most k.
The value of the max ow can be anywhere between the value of the ow after Phase
1 and the value of the original max ow. The difference between these two values is
at most k, so k iterations are enough to restore the max.
Problem 7-2. Disjoint Roads [15 points]
A number k of trucking companies, c1 , . . . , ck , want to use a common road system, which is mod
eled as a directed graph, for delivering goods from source locations to a common target location.
Each trucking company ci has its own source location, modeled as a vertex si in the graph, and the
common target location is another vertex t. (All these k + 1 vertices are distinct.)
The trucking companies want to share the road system for delivering their goods, but they want
to avoid getting in each others way while driving. Thus, they want to nd k edge-disjoint paths
in the graph, one connecting each source si to the target t. We assume that there is no problem if
trucks of different companies pass through a common vertex.
Design an algorithm for the companies to use to determine k such paths, if possible, and otherwise
return impossible.

Problem Set 7 Solutions

Solution: Model this as a max ow problem. Add a special source vertex s, with edges to all
the individual sources si , each with capacity 1. Also associate capacity 1 with every other edge, as
shown in the following gure:

Figure 1: Graph model for problem 7-2.


Proof: Suppose we have any integral ow f on this graph; thus, the ow on every edge is either
0 or 1. From f , we can obtain a collection of disjoint paths to t from those individual sources
si such that f (s, si ) = 1. We can do this sequentially, one i at a time. Namely, consider any i
such that f (s, si ) = 1. Since the ow into si is 1, the ow out of si is also 1, so identify an edge
(si , u) such that f (si , u) = 1. Continue by identifying an edge out of u with f (u, v) = 1, and so
on, always choosing unused edges. Eventually, this must reach t, so we have discovered a (not
necessarily simple) path from si to t. Now dene a reduced ow by changing all the ows along
the discovered path, plus f (s, si ), to 0. Repeat to identify another path, and continue until we have
exhausted all the ow from s to sources si and identied paths from all si .
Conversely, suppose we have a set of disjoint paths from some subset of the individual sources to
si . Then we can dene a ow on the network by dening f (u, v) = 1 for any edge (u, v) that
appears in any of the paths, and f (u, v) = 0 for other edges.
Thus, we have an immediate correspondence between ows in the network and sets of disjoint
paths from subsets of the individual sources. A max ow through this network yields the greatest
number of paths, because the denition of a max ow says that it maximizes the total ow out of
s (which here corresponds to the number of individual sources that have paths to t).
Algorithm: So, to solve the problem, all we need to do is run a max ow algorithm on the network
derived from the truck problem, and interpret the result as a set of paths. If the value of the ow is
strictly less than k, we return impossible.
Analysis: The max ow |f | < k. Using the Ford-Fulkerson algorithm, the time complexity is
O(E|f |) = O(Ek).

Problem Set 7 Solutions

Problem 7-3. Food Truck Orders [15 points]


The Miso Good food truck produces a large variety of different lunch menu items. Unfortunately,
they can only produce their foods in limited quantities, so they often run out of popular items,
making customers sad.
To minimize sadness, Miso Good is implementing a sophisticated lunch-ordering system. Cus
tomers text in their acceptable choices before lunch time. Then they can use an algorithm to
preassign lunches to customers. Customers who do not get one of their choices should receive a
$10 voucher. Miso Good would like to minimize the number of vouchers they give out.
Give an efcient algorithm for Miso Good to assign lunches to customers. In general, suppose that,
on a given day, Miso Good has produced m types of food items b1 , . . . , bm , and the quantity of each
type of food item bj is exactly qj . Suppose that n customers a1 , . . . , an text in their preferences,
where each customer ai submits a set Ai of one or more acceptable lunch choices. The algorithm
should assign each customer either one of his/her choices or a $10 voucher. It should minimize the
number of vouchers.
(Hint: Model this as a max ow problem.)
Solution: Model this as a max ow problem. Dene a ow network as follows.

Figure 2: Graph model for problem 7-3, in which a1 prefers b1 , a2 prefers b1 and bm ,... an prefers
b2 and bm .
Include special vertices s and t as usual. Have a rst layer of n vertices corresponding to the
customers, and a second layer of m vertices corresponding to the foods. Include an edge from
s to each customer, with capacity 1. Include an edge from customer ai to food item bj exactly if
j Ai ; this will also have capacity 1. Include an edge from food item bj to t with capacity qj .

Problem Set 7 Solutions

Proof: A ow f on this network yields an assignment of food items to customers that satises the
customer and food quantity constraints. Specically, for each customer ai , if some edge f (ai , bj )
has ow 1, then assign food item bj to customer ai . Note that each customer ai can get at most one
food item, because ai has incoming ow at most 1, so its outgoing ows must also total at most
1. Since all ows are integral, only one outgoing edge can have positive ow. Also note that each
food item bj cannot be assigned to more than qj customers, because bj has outgoing ow at most
qj , so its incoming ows must also total at most qj . Thus, the food assignment arising from ow f
satises all the customer and food constraints.
Conversely, any food assignment satisfying all these constraints corresponds directly to a ow
through the network: Assign ow 1 to edge (ai , bj ) exactly if customer a1 gets assigned food item
bj . Assign ows to the edges from s and to t to achieve ow conservation.
Moreover, a max ow through this network satises the maximum number of customers, because
the denition of a max ow says that it maximizes the total ow out of s (which corresponds to
the number of customers satised).
Algorithm: So, all we need to do is run a max ow algorithm on this network, produce an integral
max ow f , and interpret it as a food assignment. The maximum number of satised customers
yields the minimum number of vouchers.
Analysis: The number of edges is E = O(mn). The max ow |f | n. Using the Ford-Fulkerson
algorithm, the time complexity is O(E|f |) = O(mn2 ).

MIT OpenCourseWare
http://ocw.mit.edu

6.046J / 18.410J Design and Analysis of Algorithms


Spring 2015

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like