Sie sind auf Seite 1von 4

Fall 2010 - 91.

503 - Algorithms
Computer Science Department
University of Massachusetts Lowell
Lowell, MA 01854
Oct. 5, 2010.

Name:
1.

2.

Total:

3.

4.

5.

6.

/50

Exam Time: 1h & 15m. Each problem is worth 10 points. 50 pts total. Choose any 5 problems.
If needed, use the back of each page.
1. Dynamic Programming.
(2 pts) Define the Longest Common Subsequence problem
(3 pts) State the theorem that details the Optimal Substructure property of an LCS
(5 pts) Using the theorem, sketch and apply a recursive Dynamic Programming algorithm
to compute a LCS of X = (1, 0, 0, 1, 0, 1) and Y = (0, 1, 0, 1, 1, 0, 1).
Solution:
1. p.391: What do you mean by subsequence? What do you mean by common subsequence? What is the Longest Common Subsequence Problem? Providing circular definitions
(e.g., a Common Subsequence is a sequence that is common to two (or more) other sequences)
is not adequate: it just reshuffles the same words.
2. p.392, Theorem 15.1
3. p.393, Formula 15.9 - derived from Theorem 15.1 - applied to the two given sequences
provides the justification for the Dynamic Programming algorithm on p.394. Note that, by
simple inspection, an LCS is {1, 0, 1, 0, 1}, so any computation that led to a 4-sequence had
to be incorrect.
2. Greedy Algorithms. Suppose that, instead of always selecting the first activity to finish,
we select the last activity to start that is compatible with all previously selected activities.
(4 pts) Describe how this approach is a greedy algorithm and
(6 pts) Prove that it yields an optimal solution.
Solution:
1

1. A greedy algorithm always makes the best available choice. In this case the best
available choice is given by the order of the activities: the best choice among the available
activities (= those compatible with all previous choices) is, by definition, that of an activity
with the latest start. At each choice point, all activities overlapping with the already chosen
ones are discarded, and the latest starting one among those remaining is chosen.
2. Theorem 16.1, specifically changed to the current best criterion. A direct variant
could be: Assume there exits a maximal solution A which does not contain the last starting
activity, say b. Let ai be the last starting activity in the solution A. Then ai must start no
later than b (b is a latest starting activity), which means that b is compatible with all other
activities of A (why?). (A {ai }) {b} provides a set of activities with the same (maximal)
cardinality, but containing b. You can repeat the recursion until the last activity added by
the algorithm.
3. Amortized Analysis. Suppose we have a potential function such that (Di ) (D0 )
for all i, but (D0 ) 6= 0. Show that there exists a potential function 0 such that 0 (D0 ) =
0, 0 (Di ) 0 for all i 1, and the amortized cost using 0 is the same as the amortized cost
using .
Solution: 0 (Di ) = (Di ) (D0 ). Check that this choice has the desired properties (what are those properties?). Some proposed variants added constants, without quite
specifying them, creating some confusion, mostly notational.
4. Sets.
(3 pts) Define rank and union by rank.
(7 pts) Give a sequence of m Make-Set, Union and Find-Set operations, n of which
are Make-set operations, that takes (m lg n) time when we use union by rank only. Explain
your reasoning.
Solution:
1. p.569, supplemented by the pseudo-code on p. 571. You must not forget the way in
which rank increases (as given in LIN K).
2. We need to know how large m needs to be, and we must guarantee that, at the
end of the M AKE SET s and U N ION s, the tree constructed has a root r such that
r.rank blg nc.
Let M AKE SET be applied to x1 , x2 , . . . , xn to obtain the sets {x1 }, {x2 }, . . . , {xn }.
The application of U N ION must be a little careful: the sequence of applications
U N ION ({x1 }, {x2 }), U N ION ({x2 }, {x3 }), . . . U N ION ({xn1 }, {xn }) results in a tree
whose root has rank 1, and hence all F IN D SET s applied to the elements of this set will
have cost O(1). Why?
We apply in this order: U N ION ({x1 }, {x2 }), U N ION ({x3 }, {x4 }), . . . U N ION ({xn1 }, {xn })
(if n is even - otherwise the last tree has root with rank 0.
We repeat
U N ION ({x1 }, {x3 }), U N ION ({x5 }, {x7 }), . . . U N ION ({xn3 }, {xn1 }), etc...
The total number of U N ION s can be as high as n 1, leading to a tree whose root has rank at
least blg nc. The cost of the n M AKE SET s is O(n), the cost of the n 1 U N ION s is
O(n lg n) (why?). What can the cost of the m 2n + 1 F IN D SET s be? Since the tree
root has rank (lg n), the choice of a leaf farthest from the root for m 2n + 1 times will
2

have cost ((m 2n + 1) lg n). Choose m 3n. Then m 2n + 1 m/3 and we end up
with cost (m lg n).
5. Graphs, Flows and Cuts.
(2 pts) Define flow network.
(2 pts) Define flow.
(2 pts) Define the value of a flow.
(4 pts) Prove the Integrality Theorem: You are given a flow network (G, V ), whose
capacity function c takes only integral values. Prove that the maximum flow f produced by
the Ford-Fulkerson method has the property that |f | is an integer. Moreover, for all vertices
u and v, the value of f (u, v) is an integer.
Hint: use induction on the number of iterations (of what?).
Solution:
1, 2, 3: Sec. 26.1, pp. 709-710. For those who have the second edition, my notes contained
all the necessary definitions.
4. We are given a flow network (G, V ), with distinguished vertices s and t. Let c be the
capacity function on edges.
Base Case: the initial value of the flow is f (u, v) = 0 for all (u, v) R2 . 0 is an integer,
|f | = 0 (finite sum of zeros). The desired properties for the flow f hold. Note that G = Gf ,
in terms of capacities : the original network is also the residual network for a null flow.
From Base Case to first step: Use any heuristic you want to construct a simple path from
s to t in Gf . If one exists, edges on it must have positive integer capacity. The minimum
of a set of positive integers is a positive integer. Let c0 be the minimum (integer) capacity
along this path. The augmenting path assigns the values f 0 (u, v) = c0 for edges on this path
and values f 0 (u, v) = 0 for all other edges. f 0 is a flow on Gf = (V, E). We add f and f 0 :
f f 0 and obtain a flow with integer values, since we are always adding integers to integers.
We now define the residual capacity cf f 0 according to formula (26.2), p. 716, and the new
residual network Gf f 0 . Note that this new network has all integer capacity values and all
integer flow values, by construction. If no simple path exists, you are done (why?).
From any step to the next: let Gf be the residual network, with integer capacities, for
some flow f with integer values. Apply any heuristic to find a simple path from s to t. If
no such path is found, we are done, since the flow we have is maximal and has the required
integer properties. If such a path exists, the same exact arguments as in the previous part
show that the augmenting flow has only integer values, the sum flow has integer values and
that the new residual network has integer capacities.
6. Graphs, Flows and Cuts.
(3 pts) Describe the Edmonds-Karp algorithm (textbook version).
(7 pts) Show the execution of the algorithm on the flow network below (the numbers on
the edges are capacities). Make sure to describe your execution so that a reader can follow
the details of the application of the algorithm.

Solution:
1. The Edmonds-Karp heuristic is applied in the Ford-Fulkerson framework (p. 715):
given a flow network G = (V, E), with a flow f on it initialized to 0, and the residual network
Gf , look for an augmenting path p in Gf which has the property of being a shortest (simple)
path from s to t. Augment the flow f along p, construct the new residual network and repeat
until no more augmentations are possible. Return the last flow computed.
2. A shortest (e.g., by BFS) path is given by s v1 v3 t, with capacities 15, 12
and 25. min(15, 12, 25) = 12. We have f 0 (s, v1 ) = 12, f 0 (v1 , v3 ) = 12, f 0 (v3 , t) = 12, and all
other f 0 (u, v) = 0. We now have a residual capacity of 3 on (s, v1 ), of 0 on (v1 , v3 ), an of 3 on
(v3 , t), with appropriate residual capacities on the reverse edges, as given by (26.2). A next
shortest simple path on the (updated) residual network is s v2 v4 t, with capacity
5. We then choose f 00 (s, v2 ) = 5, f 00 (v2 , v4 ) = 5, f 00 (v4 , t) = 5, and all other f 00 (u, v) = 0. The
(again updated) residual network will have capacities 8 on (s, v2 ), of 9 on (v2 , v4 ), and of 0
on (v4 , t). The next shortest simple path is s v2 v4 v3 t, with path capacity of
7 (the smallest residual capacity on the edges). The value of the sum flow is 12 + 5 + 7 =
24. Notice that constructing the new residual network results in a network that has no paths
connecting s and t.

Das könnte Ihnen auch gefallen