Professional Documents
Culture Documents
Midterm 2
Name:
SID:
• The problems may not necessarily follow the order of increasing difficulty.
• Any algorithm covered in the lecture can be used as a blackbox unless stated otherwise.
• Please either latex your answers, or provide a neatly handwritten version of the solutions
where every subpart is on a new page of your submission and the number is clearly labelled.
• Good luck!
Midterm 2 A. Chiesa & J. Nelson
Discussion Section
Which of these do you consider to be your primary discussion section(s)? Feel free to choose multi-
ple, or to select the last option if you do not attend a section.
2
SID: Midterm 2 A. Chiesa & J. Nelson
2 3
2
B C
3 2
(b) (2 points) Consider a linear program with a minimization objective. If there are infinitely many
integral feasible solutions, where all variables take integral values, then the objective value can
be made arbitrarily small.
hTrue hFalse
Solution: False: Consider min x subject to x ≥ 0. The LP has infinitely many integral feasible
solutions, that is, {0, 1, 2, · · · }, but the optimal solution is just x = 0.
(c) (2 points) Let A be a m × n matrix, b be a m-dimensional vector, and c be a n-dimensional
vector. If the linear program
has a feasible solution with objective value M, then every feasible point of its dual has an
objective value at most M.
hTrue hFalse
Solution: True: this is the definition of weak duality.
(d) (2 points) If a graph has a unique lightest edge, then that edge must be part of every MST of the
graph.
hTrue hFalse
Solution: True: Suppose the unique lightest edge is e = (u, v). Consider the cut {u} and the
rest of the graph. Since e is the unique lightest edge among the cut edges in this cut, the cut
property guarantees it must be chosen by any MST.
3
Midterm 2 A. Chiesa & J. Nelson
(e) (2 points) Let F1 and F2 be feasible s-t flows in a directed graph G, satisfying the flow conser-
vation and capacity constraints. Then the flow F defined as F (e) := 0.6F1 (e) + 0.4F2 (e) for all
e ∈ E is also a feasible s-t flow in G.
hTrue hFalse
Solution: True: the set of feasible flows is a convex set. Any convex combination of feasible
flows is feasible.
(f) (2 points) We say an edge is saturated by a flow if the amount of flow on the edge equals its
edge capacity. In a directed graph, let e be an edge saturated by some maximum s-t flow of
value F. Let c be its capacity. If we reverse e, then in the new graph any maximum s-t flow
must have value at most F − c.
hTrue hFalse
Solution: False. Consider the counterexample below. Maximum flow of 10 units results from
sending 5 units along SABT and 5 units along SAT. Edge AB is used to capacity. After reversing
AB, we can achieve the same amount of flow by sending 5 units along SAT and 5 units along
SBT.
10 5
S 5 T
10 5
(g) (2 points) Consider maximum s-t flow in a directed graph. Let T be the number of incoming
edges of t. If we add a constant C > 0 to all edge capacities, then the value of maximum s-t
flow will increase by C · T.
hTrue hFalse
Solution: False: There might be vertices connected to t that s can’t reach. For example, consider
t has many incoming edges like (u1 , t), (u2 , t) and so on, but ui doesn’t have any incoming edge.
4
SID: Midterm 2 A. Chiesa & J. Nelson
(a) (2 points) Assume S only uses union-by-rank, without path compression. Give the tightest
possible upper bound on the average time complexity of running 1000 additional Find opera-
tions. Give your answer in big-O notation as a function of n.
(b) (2 points) Assume S uses union-by-rank with path compression. Give the tightest possible
upper bound on the average time complexity of running 1000 additional Find operations. Give
your answer in big-O notation as a function of n.
Solution: O(log n). Just the first find takes O(log n), this is enough to bring the average up to
O(log n).
(c) (2 points) Assume S uses union-by-rank with path compression. Then the time complexity of
each individual Find is at most O(log∗ n). Mark if this is true or false, and justify.
hTrue hFalse
Solution: False. As in b), the first find is at worst O(log n) individually. It is true that the time
complexity of m > n Finds is O(m log∗ n), but this is only amortized, and not for individual
Finds.
5
Midterm 2 A. Chiesa & J. Nelson
Notice how this function is not fully specified due to a lack of base cases.
(a) (3 points) Describe a minimal set of base cases that would make this a well-defined recurrence.
We call (i, j) a base case if it is not in [0, m] × [0, n]. Set the value of S for each base case to be 0.
Make sure to only include the base cases that are necessary.
(b) (2 points) For all possible algorithms, give the tightest asymptotic bound on the space com-
6
SID: Midterm 2 A. Chiesa & J. Nelson
Solution:
i, ii, iv should be selected.
i) n subproblems, which take at most n work to compute, so O(n2 ).
However as students pointed out you can actually do this is constant time, as
S ( k ) + S ( k )2
is a monotonically increasing function so when k ¿= 2 it is always 30. Thus the answer can be
computed in O(1) which is O(n2 ). Since this wasn’t intended, we gave everyone points for this
part.
ii) n2 subproblems, which take constant time to compute, so O(n2 ).
iii) n3 subproblems, which takes constant time to compute, O(n3 ).
iv) n2 subproblems, which takes constant time to compute, O(n2 ).
7
Midterm 2 A. Chiesa & J. Nelson
3 3
1
A D 8
2 k 4
7
C F
a) (2 points) For what range of values of g are you guaranteed to include edge BE in the MST?
Solution: Construct a cut where one set is E and the other set is (A, B, C, D, F). In order to
guarantee g is in the MST, it must be the lightest edge. g < 8.
b) (2 points) For what range of values of k are you guaranteed to include edge CD in the MST?
Solution: There is a cycle using edges AD, AC, and CD. The heaviest edge in the cycle will not
be used and so in order to make CD not the heaviest edge k < 2.
c) (2 points) For what range of values of k are you guaranteed to NOT include edge CD in the
MST?
Solution: There is a cycle using edges AD, AC, and CD. The heaviest edge in the cycle will not
be used and so in order to make CD the heaviest edge, k > 2.
d) (3 points) Suppose an adversary can set g and k arbitrarily. What is the maximum cost of a
MST that the adversary can force? Solution: MST will only choose BE and CD if it is better
than or equal to cuts of the other edges, so the maximum cost is 2 + 1 + 3 + 4 + 8 = 18.
e) (3 points) Draw seven edges on the following graph with six vertices, and then mark six edges
with 1 and one edge with x such that an adversary can choose x such that the cost of the MST
is arbitrarily large.
B E
A D
C F
8
SID: Midterm 2 A. Chiesa & J. Nelson
B E
1 1
A 1 x 1 D
1 1
C F
Solution:
9
Midterm 2 A. Chiesa & J. Nelson
max 2x1 + x2
x1 + x2 ≥ 2
x1 ≤ 3
kx1 + x2 ≤ 3k + 5
x1 , x2 ≥ 0
Solution: Note that (3, 5) is always feasible, regardless of the value of k. The constraint with k
(3rd condition) is always tight at (3, 5). Thus adjusting k simply modifies the slope of the line
that runs through (3, 5). The slope of this line is −k (plot the 3rd condition as x2 = 3k + 5 − kx1 ).
The slope of the objective function is -2. If the slope of condition 3 is steeper than -2 (i.e.
smaller than -2), then our objective function would keep pushing to the right, surpassing the
point (3, 5). Thus, for (3, 5) to be optimal the slope of the constraint must be greater than -2:
−k ≥ −2 → k ≤ 2.
(b) (3 points) Construct the dual of the linear program in part a. Leave k in your answer.
Solution: The primal is:
max 2x1 + x2
− x1 − x2 ≤ −2
x1 ≤3
kx1 + x2 ≤ 3k + 5
x1 , x2 ≥0
10
SID: Midterm 2 A. Chiesa & J. Nelson
(c) (2 points) Convert the linear program in part a to be of the form where your objective function
is a minimization function, and your constraints are all in the form variable ≥ equation (e.g.
x1 ≥ 5). Leave k in your answer.
Note that this question is not asking you to find the dual
Solution:
min −2x1 − x2
x1 + x2 ≥ 2
− x1 ≥ −3
−kx1 − x2 ≥ −3k − 5
x1 , x2 ≥ 0
11
Midterm 2 A. Chiesa & J. Nelson
You can put an arbitrary number of candles on top of the same candle. The cost to putting candle i
on top of candle j is C (i, j). We assume that C (i, j) ≥ 0 and C (i, j) = C ( j, i )) for i 6= j. No candle can
be reused or put on top of itself. You cannot use the candles {1, . . . , k} anymore, as they have already
been fixed to the floor.
Grading Remark: We will only look at your part b) and c) if you got part (a) correct; Any
mistake in algorithm design would render an automatic 0 in the other two parts. Any re-
grade request won’t help.
Solution: Create a complete weighted undirected graph on n vertices, where the edge weights
are distances. Call the graph G. Add a dummy node q to G which has k edges, one to each of
the candles 1, . . . , k. Let the new grpah be G 0 . Each of these edges has weight −1, for example.
We then compute the MST of G 0 , then remove q to get the trees.
(b) (5 points) Prove the correctness of your algorithm.
Solution: If you take the cut {q, x1 , . . . , xn+k }\{ xn+i } versus { xn+i }, the edge (q, xn+i ) is the
unique cheapest edge crossing this cut and thus must be in any MST of G 0 . Thus any MST of
G 0 takes all these −1-weighted edges incident on q, which means the cluster heads will be in
distinct trees (the clusters).
For optimality, note that if there is a cheaper way to build the k trees { T1 , · · · , Tk } in G (with
each rooted at node i ∈ {1, · · · , k }), then it corresponds to a cheaper spanning tree of G 0 .
(c) (3 points) Give a runtime analysis of your algorithm in the previous part.
Solution: Since the graph has at most n+ vertices and Θ(n2 ) edges, the runtime of either
Kruskal or Prim is O(n2 log n). Forget about heap, naive Prim would be able to give us O(n2 ).
12
SID: Midterm 2 A. Chiesa & J. Nelson
This question asks you to provide an algorithm to compute the length of the longest fancy subse-
quence of a given length-n array A. The sequence need not be contiguous. You may assume arith-
metic operations (addition, division and multiplication) and comparisons (<, >, ==) can be done in
constant time.
(a) (6 points) For i < j, let f (i, j) denote the length of the longest fancy subsequence of A[i..n]
whose first two elements are A[i ] and A[ j]. Write a recurrence relation for f (i, j) and specify
the base case(s).
(b) (3 points) Give a polynomial-time algorithm to compute the length of the longest fancy subse-
quence of A. You may write pseudocode.
(c) (2 points) Give a runtime analysis of your algorithm.
Solution:
(a) The recurrence is given by
(c) O(n3 )
13
Midterm 2 A. Chiesa & J. Nelson
We call a signing σ balanced if for every cycle C in G, the product of signs of edges in the cycle is
equal to 1, i.e.,
∏ σ(e) = 1.
e∈C
(a) (2 points) A partial signing is given for the below graph. Sign the remaining edges so that the
resulting signing is balanced.
B E
−1
+1
−1 C D −1
+1
A F
BE
CD
CF
AC
AF
Solution:
BE -1
CD +1
CF +1
AC -1
AF -1
You are allowed to perform the following operation known as a switching — choose a vertex v
and toggle the sign of all edges incident to v, i.e., update the sign on every edge e incident to v
from σ (e) to −σ (e).
(b) (4 points) Prove that any signing of G constructed via a sequence of switching operations must
be balanced. Recall that initially all the edge signs are +1.
Solution: For vertex v, let γv = −1 if a switching operation is performed on v and +1 other-
wise. The sign on edge uv is then γu γv . Let v1 v2 . . . vk be any cycle in the graph. The product
of signs of edges in the cycle is (γv1 γv2 )(γv2 γv3 ) . . . (γvk γv1 ) = ∏ik=1 γv2k = 1.
(c) (4 points) Let T be a spanning tree of G, and σT be an arbitrary signing of edges in T. Prove
that there is a unique balanced signing σ of G such that σ (e) = σT (e) for all e ∈ T.
Hint: first try to show that there is at least one such balanced signing (part (b) might be useful for this),
and next show that there is at most one balanced signing that agrees with σT .
14
SID: Midterm 2 A. Chiesa & J. Nelson
Solution: To prove the desired statement, we (i) prove that there exists a balanced extension of
σT , (ii) prove that there is at most one balanced extension of σT . To prove (i), observe that σT
can be induced by a sequence of switching operations obtained via the following algorithm.
Fix a root r, and perform a switching operation on vertex v if and only if the product
of edges along the path from r to v is equal to −1.
15
Midterm 2 A. Chiesa & J. Nelson
100 100
B
100 100
S T
200 200
C
200 200
No such edge
Path Units
Solution:
16
SID: Midterm 2 A. Chiesa & J. Nelson
100 100
B
100 100
S T
200 200
C
200 200
1
A clearly drawn or specified edge, with direction and capacity, was necessary in order to receive
credit. We gave partial credit if you increased the largest possible number of iterations but did
not find the maximum, as long as all parts were correct with respect to the edge added.
b) (6 points) Add one directed edge and its capacity to the graph below that maximizes the largest
possible number of iterations of Edmonds-Karp. If no edge can be added to strictly increase
the maximum number of iterations, leave the graph blank and fill in the ”No such edge” box.
17
Midterm 2 A. Chiesa & J. Nelson
100 100
B
100 100
S T
200 200
C
200 200
No such edge
Path Units
Solution:
100 100
B
100 100
c
S T
200 200
C
200 200
18
SID: Midterm 2 A. Chiesa & J. Nelson
A clearly drawn or specified edge, with direction and capacity, was necessary in order to receive
credit. We gave partial credit if you increased the largest possible number of iterations but did
not find the maximum, as long as all parts were correct with respect to the edge added.
i) Number of iterations: 5
Path Units
SAT 100
SBT 100
ii)
SCT 200
SDT 200
ST c
19