2022 Spring Final
2022 Spring Final
Final
Name:
SID:
Exam Room:
Rules and Guidelines
• Answer all questions. Read them carefully first. Not all parts of a problem are weighted equally.
• Write your student ID number in the indicated area on each page.
• Be precise and concise. Write in the solution box provided. You may use the blank page on the back
for scratch work, but it will not be graded. Box numerical final answers.
• The problems may not necessarily follow the order of increasing difficulty. Avoid getting stuck on a
problem.
• Any algorithm covered in lecture can be used as a blackbox. Algorithms from homework need to be
accompanied by a proof or justification as specified in the problem.
• Throughout this exam (both in the questions and in your answers), we will use ωn to denote the first
nth root of unity, i.e., ωn = e2πi/n .
• You may assume that comparison of integers or real numbers, and addition, subtraction, multiplica-
tion and division of integers or real or complex numbers, require O(1) time.
• Good luck!
Final P. Raghavendra & J. Wright
Discussion Section
Which section(s) do you attend? Feel free to choose multiple, or to select the last option if you do not attend
a section. Please color the checkbox completely. Do not just tick or cross the boxes.
□ Video walkthroughs
□ Don’t attend Section
2
SID: Final P. Raghavendra & J. Wright
Suppose we run depth-first search on the following graph, breaking ties alphabetically.
C B A F
D G H
C D G H
3
Final P. Raghavendra & J. Wright
2. For each of these edges, indicate whether they are tree edges, forward edges, back edges or cross
edges.
Solution: TBTTBTTCCCTT
3 6
0
C 2 B A 11 F
1
4 5 7 9
D 8 G 10 H
4
SID: Final P. Raghavendra & J. Wright
if (n < 1) return
for i = 1 to n {
for j = 1 to n/2 {
for k = 1 to n/4 {
print BLAH
}
}
}
what(n/2)
what(n/2)
what(n/2)
what(n/2)
}
Let F (n) denote the number of ”BLAH”s printed by a call to what(n). Write a recurrence relation for F (n).
F (n) =
4T (n/2) + O(n3 )
Write the tightest upper bound for F (n) (a bound that is asymptotically tight, up to constant factors).
T ( n ) = O ( n3 )
5
Final P. Raghavendra & J. Wright
Recall that Djikstra’s algorithm for shortest paths maintains a priority queue. Let H denote the priority
queue that is maintained by Djikstra’s algorithm starting at vertex S in the graph shown below.
4
S G
2 8
5
7 C D 1
4 7
2
A E
Initially, every vertex other than S is inserted in the queue with a key value of ∞. Then, Djikstra’s algorithm
performs a sequence of deleteMin( H ) and decreaseKey( H, v) operations on the queue H, where v denotes
some vertex.
List all the decreaseKey( H, ) operations performed during the execution of Djikstra on the following graph
starting at node S. (Note that there may be more spaces provided below than the number of decreaseKey
operations executed, leave the additional spaces blank)
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
decreaseKey H,
Solution: ACGADE
6
SID: Final P. Raghavendra & J. Wright
Suppose we execute DFS on a directed graph G and compute pre and post values for each node. Let
hA hB hC
hA hB hC
hA hB hC hD
hA hB hC
Solution:
7
Final P. Raghavendra & J. Wright
The tree shown below is a minimum spanning tree in a graph H. The remaining edges of the graph H are
NOT shown in the picture.
B G
2 8
5
C D
4 7
1
A E F
1. There is an edge AG in the graph (not shown in the picture). The smallest possible value for the
weight of edge AG is
2. The cost of the MST shown above is 2 + 5 + 8 + 4 + 7 + 1 = 27. A new edge AE with weight 5 was
added to the graph. The cost of the minimum spanning tree in the new graph is
3. In this class, we studied two algorithms for MST: Kruskal’s and Prim’s algorithm.
We executed one of these two algorithms (Kruskal’s/Prim’s) on the graph H and it produced the tree
shown above. In first three iterations, the algorithm added edges CD, BC, AC to the tree. The next
4. In this class, we studied two algorithms for MST: Kruskal’s and Prim’s algorithm.
We executed one of these two algorithms (Kruskal’s/Prim’s) on the graph H and it produced the tree
shown above. In first three iterations, the algorithm added edges EF, BC, AC to the tree. The next
Solution:
1. 8. Note cycle ACDG. Note also edge weight are not distinct and no assumption made on tiebreaking.
Suppose there are n vertices with distances dij between vertices i and j. Assume that the distances dij satisfy
the triangle inequality.
8
SID: Final P. Raghavendra & J. Wright
1. If the cost of the minimum travelling salesman tour is C, then the cost of the minimum spanning tree
is at most
2. If there is a spanning tree T (not necessarily the minimum spanning tree) of cost W, then the cost of
Solution:
1. C. DPV p. 294
2. 2W. Idem. (”[T]his tour has a length at most twice the MST cost”)
hT hF hP = NP hP ̸= NP
hT hF hP = NP hP ̸= NP
4. If there is a polynomial time algorithm for one problem in NP, there is one for all of them.
hT hF hP = NP hP ̸= NP
Solution:
9
Final P. Raghavendra & J. Wright
10
SID: Final P. Raghavendra & J. Wright
Given two strings x [1, . . . , n] and y[1, . . . , m], recall that the edit distance is the smallest number of keystrokes
needed to edit the string x into string y. Here each insertion and deletion of a character takes one key stroke.
We devised a dynamic programming algorithm for the problem wherein the subproblems are
ED [i, j] = minimum number of keystrokes needed to edit the prefix x [1, . . . , i ] into the prefix y[1, . . . , j].
Solution:
ED [i − 1, j] + 3
ED [i, j] = min ED [i, j − 1] + 2
ED [i − 1, j − 1] + 4 · 1[ x [i ] ̸= y[ j]]
Solution: ED [i, 0] = 3i ∀i
ED [0, j] = 2j ∀ j
11
Final P. Raghavendra & J. Wright
1. A directed acyclic graph (DAG) on n vertices can have at most number of edges
Solution: Cn2 , maybe in some CS70 note.
3. Let G be a graph on n nodes. The dynamic programming based algorithm for All-Pairs-Shortest-Paths
Moreover, the recurrence relation expresses the value of each subproblem in terms of
many other subproblems. Solution: n3 , 3. DPV pp. 187-188. d[i ][ j][k] = min(d[i ][k][k − 1] + [k][ j][k −
1], d[i ][ j][k − 1]).
4. Suppose a hash function h : {1, . . . , n} → {1, . . . , 2n} is drawn from a universal hash family. Then
Pr[h(2) = h(1)] = .
and
5. If ω is a primitive 16th root of unity then ω 8 is a kth root of unity for k = . (Write
the smallest possible value of k.) Solution: 2.
6. Suppose we execute the reservoir sampling algorithm on a stream s1 , . . . , sm . What is the probability
that the reservoir contains s2 at the end of 10th iteration (i.e., after seeing s1 , . . . , s10 )?
1
Solution: 10 What is the probability that every element of the stream was in the
reservoir at some point during the execution of the algorithm?
1
Solution: m!
7. For each of the following quantum states, write down the probabilities that we observe 0 and 1 when
we perform a measurement on them.
12
SID: Final P. Raghavendra & J. Wright
Solution:
q 2 12 = 1 q
and 02 = 0.
1 1 2 2 2
3 = 3 and (− 3 ) = 3 . See https://inst.eecs.berkeley.edu/ cs191/fa10/notes/chap1&2.pdf.
8. Recall the quantum operation Rotate(·, ·) that we saw in class. Let |−⟩ = √1 · |0⟩ − √1 · |1⟩.
2 2
Supposing that we perform the operation Rotate(π/4, |−⟩) and then measure the resulting state, the
" √1
− √1
#
π cos π4 − sin π4
Rotate( , ·) = = 12 2
4 sin π4 cos π4 √ √1
2 2
√1
" #
and |−⟩ = 2
√1
2
√1 − √1
" #
2 2 1
|−⟩ =
√1 √1 0
2 2
13
Final P. Raghavendra & J. Wright
10 True/False (6 points)
1. Let G = (V, E) be a graph with distinct positive edge weights {we | e ∈ E}. Construct a set of edges
Elight as follows: for each vertex v ∈ V, include the lightest edge incident to v in Elight . Then the edges
in Elight form a Minimum Spanning Tree of G.
hTrue hFalse
Solution: False. Result might not even be a tree.
3. There are connected, undirected graphs G such that decreasing the capacity of any single edge in G
does not change the value of the maximum flow.
hTrue hFalse
Solution: False. Decrease those on the min-cut.
4. There are connected, undirected graphs G such that increasing the capacity of any single edge in G
does not change the value of the maximum flow.
hTrue hFalse
Solution: True, A-3-B-3.
5. There are directed graphs G such that decreasing the capacity of any single edge in G does not change
the value of the maximum flow.
hTrue hFalse
Solution: True, A-3->B-3->C, consider flow C->A.
14
SID: Final P. Raghavendra & J. Wright
Suppose we have an undirected connected graph, and we would like to find a strongly connected orientia-
tion of its edges: that is, an assignment of directions to its edges such that the entire graph becomes strongly
connected (i.e. every node is reachable from every other node via a directed path). For the undirected con-
nected graph below, the next figure is a strongly connected orientation, and the last is an orientation that is
not strongly connected.
A B
A B A B
(b) A strongly connected orientation (c) An orientation which is not strongly connected
Solution: The easiest graph has two vertices 1 and 2 and an edge (1, 2) between them.
15
Final P. Raghavendra & J. Wright
2. Call an edge lonely if it is NOT part of any cycle. If an undirected graph G has a strongly connected
3. Suppose e is a lonely edge in a graph G. In a DFS search tree of G, what kind of edge will e be?
Solution: tree
4. Let G be a connected undirected graph with no lonely edges. We executed a DFS on G and compute
the pre[v] and post[v] values for every node v. How would you find a strongly connected orientation
of the graph G from its pre and post values?
Specifically, fill in the blanks below.
The following procedure will produce a strongly connected orientation if there exists one:
For each edge (u, v) in the graph G,
(a) Direct the edge u → v if pre[u], pre[v], post[u], post[v] satisfy the condition
Solution: **Note, this part had a bug, so all students were given full credit for this part.
(b) Else direct the edge v → u.
5. Argue that the above algorithm produces a strongly connected orientation if there exists one.
Solution:
16
SID: Final P. Raghavendra & J. Wright
**Note, due to the bug in the above part, all students were given full credit for this part.
If G has a strongly connected orientation, then by part (2) above it has zero lonely edges. We will
show that this algorithm produces a strongly connected orientation whenever there are zero lonely
edges.
Consider any tree edge (u, v) in the DFS tree with u the parent of v. We will show that v is reachable
from u in the strongly connected orientation and vice versa. This will imply that G is strongly con-
nected given this orientation, because each vertex can reach the root of the DFS tree by moving up the
tree edges, and likewise the root can reach each vertex by moving down the tree edges.
Now, v is clearly reachable from u in the strongly connected orientation because (u, v) is a tree edge
and is therefore directed as u → v. To show that u is reachable from v, consider a cycle of distinct
vertices containing the edge (u, v):
v0 = u, v1 = v, v2 , v3 , . . . , vk , v0 ,
which must exist because (u, v) is not lonely. Let (vi , vi+1 ) be the first edge in this path in which vi+1
is not a descendent of u. Then vi is either equal to v or a descendent of v and is therefore reachable
from v in the orientation. In addition, vi+1 must be a non-parent ancestor of vi . (That it is an ancestor
follows from no cross edges in an undirected graph.) Hence (vi , vi+1 ) is a back edge, and is oriented
vi → vi+1 and is therefore also reachable from v. Finally, vi+1 must either be equal to u or an ancestor
of u, and so there must be a path from vi+1 to u following the tree edges.
17
Final P. Raghavendra & J. Wright
The Frugal Inc. company runs four electric car charging stations A, B, C, D connected by the network of
roads as shown below.
C D
Frugal Inc. would like to compute the optimal schedule for keeping these charging stations open. Here are
the details.
3. At any time in the day, for every road, at least one of the charging stations at its end points must be
open.
The charging stations are allowed to be open for fractional (non-integer) number of hours.
12.1 A failed attempt
Frugal Inc. tried to write a linear program to determine the minimum total cost per day of running these
charging stations while satisfying all the constraints.
Frugal Inc. started with the following choice of variables:
• ”No charging station can be open for more than 18 hours a day”
x A , x B , xC , x D ≤ 18
• At any time in the day, for every road, at least one of the charging stations at its end points must be open.
18
SID: Final P. Raghavendra & J. Wright
– Road AB: x A + x B ≥ 24
– Road BC: x B + xC ≥ 24
– Road CA: xC + x A ≥ 24
– Road CD: xC + x D ≥ 24
Unfortunately, this linear program failed. Frugal Inc. solved the linear program to get a feasible solution
x A = x B = xC = x D = 12. Argue that it is impossible to create a valid schedule with x A = x B = xC = x D =
12.
Solution: There are probably several good ways to argue this. Here’s one. First, at every time of the
day, at least two charging stations must be open. This is because there is no one charging station which is
connected to every road. Next, if x A = x B = xC = x D = 12 then at all times, exactly two charging stations
must be open, as otherwise if at some time there were three or more charging stations open, we would have
x A + x B + xC + x D > 48, a contradiction. But consider any time in which charging station C is not open
(such a time must exist because xC = 12 < 24). Then all three of A, B, and D must be on to cover C’s
neighboring roads, which is a contradiction.
12.2 Correct Algorithm
Write a linear program that correctly computes the minimum total cost of running the 4 charging stations
per day. (Hint: An entirely different choice of variables is needed. Observe that there are 7 permitted
open/close states for the stations.)
1. List all the variables of your linear program, and describe what each of them is intended to mean.
19
Final P. Raghavendra & J. Wright
Solution: There are 7 permitted open/close states for the stations: ABCD on, ABC on, ABD on, BCD
on, ACD on, AC on, and BC on. The linear program will have 7 variables, one for each permitted
state: x ABCD , x ABC , x ABD , x BCD , x ACD , x AC , and x BC . We will interpret x ABCD as follows:
20
SID: Final P. Raghavendra & J. Wright
Solution: There are 7 permitted open/close states for the stations: ABCD on, ABC on, ABD on, BCD on,
ACD on, AC on, and BC on. The linear program will have 7 variables, one for each permitted state: x ABCD ,
x ABC , x ABD , x BCD , x ACD , x AC , and x BC . We will interpret x ABCD as follows:
• ”No charging station can be open for more than 18 hours a day”
The first equation is for charging station A, the next is for charging station B, and so on. You could
also much more easily write the first equation as
∑ xS ≤ 18,
states S with A on
21
Final P. Raghavendra & J. Wright
No constraints need to be added to ensure that at any time in the day, for every road, at least one of
the charging stations at its end points must be open. This is because the variables correspond only to
configurations which already satisfy this.
The objective function being minimized is:
22
SID: Final P. Raghavendra & J. Wright
1 n
This graph consists of an (n − 1)-cycle involving the vertices 1 through n − 1, which contains the edges
(1, 2), (2, 3), . . . , (n − 2, n − 1), and (n − 1, 1). (The dashed lines in the picture represent vertices and edges
from this cycle which are not drawn.) In addition, the graph has another vertex, vertex n, and an edge
between vertices 1 and n.
1. What is minimum cut in this graph? What is the size of the minimum cut?
Solution: The minimum cut in this graph consists of the sets S = {1, . . . , n − 1} and S = {n}. It cuts
only the edge (1, n) and therefore has size 1.
2. Suppose we run Karger’s algorithm on this graph. What is the probability that it outputs the mini-
mum cut? (Note: this probability may or may not be equal to the lower bound on the success proba-
bility of Karger’s algorithm that we proved in class.) Explain your reasoning.
23
Final P. Raghavendra & J. Wright
Solution: The probability that Karger’s algorithm outputs the minimum cut is 1/n. On this graph,
at every step of Karger’s algorithm, each remaining edge is equally likely to be contracted and re-
moved from the graph, so the probability that any particular edge remains uncontracted at the end
is 1/n, since this graph has n edges. Karger’s algorithm outputs the minimum cut only if (1, n) is
uncontracted at the end, and this therefore happens with probability 1/n.
24
SID: Final P. Raghavendra & J. Wright
A sequence of integers a1 , . . . , aℓ is k-modal if it starts off increasing and switches between increasing and
decreasing at most k times. For example, a 0-modal sequence is just an increasing sequence; a 1-modal
sequence is one that increases until it reaches a maximum value, and then it decreases.
Formally, a sequence is k-modal if there exists ”switch points” i1 , . . . , ik such that a1 < a2 < . . . < ai1 and
ai1 > ai1 +1 > . . . > ai2 , and so on. For example, 1, 2, 3, 5, 8, 13, 11, 7, 5, 3, 2 is 1-modal.
In class, we saw an O(n2 )-time dynamic programming algorithm for computing the longest increasing
subsequence of a sequence of numbers a = ( a1 , . . . , an ). In this problem, devise a dynamic programming
based algorithm for computing the length of the longest k-modal subsequence given k and an input sequence
a1 , . . . , a n .
Solution: I think the best way to do this is to define the following subproblems, one for each 1 ≤ i ≤ n
and 0 ≤ m ≤ k:
25
Final P. Raghavendra & J. Wright
Solution: For each 1 ≤ i ≤ n and 0 ≤ m ≤ k, we have the following recurrence relation. Suppose that
m is even. Then
26
SID: Final P. Raghavendra & J. Wright
1. Matching
Input: Graph G = (V, E) and a positive integer k
Solution: A set of k edges e1 , . . . , ek , no pair of which share a vertex.
2. Independent Set
Input: Graph G ′ = (V ′ , E′ ) and a positive integer k
Solution: A set of k vertices S ⊂ V ′ , no pair of which are connected by an edge.
Among the above two problems, one of them reduces to the other but not vice-versa. Fill in the blanks
below (with answers in the context of above two problems).
Solution: The problem Independent Set is believed to not reduce in polynomial time to Bipartite
Matching because Independent Set is a NP-Complete problem while Bipartite Matching is a problem
in P. Since Independent Set is a harder problem than Bipartite Matching, proving that Independent
Set reduces to Bipartite Matching would show that P = NP, which is currently not believed.
27
Final P. Raghavendra & J. Wright
28
SID: Final P. Raghavendra & J. Wright
Show that the following problems are NP-complete by providing a polynomial-time reduction. You may
assume that the following problems are NP-complete: Rudrata (Hamiltonian) Path, Rudrata (Hamiltonian)
Cycle, Vertex Cover, Independent Set, 3-SAT and Integer Programming.
1. Hitting Set
Input: Positive integers n, m, k and a family of subsets S1 , . . . , Sn of {1, . . . , m}.
Solution: A subset H ⊂ {1, . . . .m} of size k that overlaps every one of the sets S1 , . . . , Sn , i.e., for each
Si , we have Si ∩ H ̸= ∅.
Proof. It is clear that the Hitting Set problem is in NP. Now we will use a polynomial time reduction to show
that the problem is indeed NP-complete.
as follows.
Solution:
29
Final P. Raghavendra & J. Wright
First blank: Vertex Cover. Second blank: Hitting Set. We want to reduce from VC to HS so we construct an
instance of HS to solve a given instance of VC.
Let G = (V, E) be the input graph to Vertex Cover. For each edge (u, v) ∈ E, add a subset Si = {u, v} to the
Hitting Set instance. Finding a vertex cover of size k is the same as finding a hitting set H ⊂ V of size k on
inputs S1 , . . . , S| E| (Represent vertices as integers in the Hitting Set instance).
Proof: A vertex cover S ⊂ V has the property that for all (u, v) ∈ E, at least one of u, v is in S. Therefore
S intersects with each of Si and is a hitting set. Now suppose we have a hitting set H. For each (u, v) ∈ E,
{u, v} ∩ H ̸= ∅. Therefore H covers all edges and is a vertex cover.
Alternative solution (Reduction from 3SAT): see
https://cs.stackexchange.com/questions/80774/prove-that-hitting-set-is-np-complete
30
SID: Final P. Raghavendra & J. Wright
Given an array of numbers a1 , . . . , an (NOT sorted), a good pivot is a number b such that between 25% to
75% of the numbers in the array are less than b.
n 3n
Formally if we define Sb = {i | ai < b} then a good pivot satisfies 4 ≤ | Sb | ≤ 4 .
1. Devise a randomized algorithm that will output a number b such that b is a good pivot with proba-
bility at least 1 − 10−6 . With probability 10−6 , the algorithm may output a number that is not a good
pivot.
Give a succinct and precise description of your algorithm. (For full credit, the runtime of your algo-
rithm must be smaller than log2 n)
Solution: Main idea: Pick a constant number t of samples from the set and take their median. In order
for this to fail, at least half of the samples must be in top quartile, or half must be in the bottom quartile.
We can bound the probability of this with a Chernoff bound. There are correct variations on this solution
which sample O(log n) values and have easier to prove bounds.
This is a constant time algorithm.
31
Final P. Raghavendra & J. Wright
Analysis: A probabilistic analysis wasn’t required for full points on this problem, but we provide one here
for the sake of clarity.
The probability of failure is the probability that either half of the sampled elements are from the lower
quartile or half are from the upper quartile. These situations are symmetric, so we will consider the lower
quartile and double our error bound later. Let B be the number of sampled elements in the lower quartile
of the data. It is distributed as Bin(t, 14 ). Then we have
1
Pr[lower-quartile failure] ≤ Pr[ B ≥ t]
2
1 1 1
≤ Pr B− ≥
t 4 4
2 !
1
≤ 2 exp −2 t Chernoff bound
4
= 2 exp(−t/8)
Doubling this to account for the fact that failures could also happen in the upper quartile, we get that
the total probability of failure is at most 4 exp(−t/8). We can make this less than 10−6 by setting t >
− 6
8 ln 4 · 10 ≈ 121.6.
Note: Mean-based solutions generally don’t work because we know nothing about the distribution of array
elements, so there can be lots of “bad” elements arbitrarily close to the outer limits of what a good pivot
can be.
32
SID: Final P. Raghavendra & J. Wright
Solution:
33
Final P. Raghavendra & J. Wright
2. The cut property still holds. If the lightest edge in a cut is not in a MMT, replace it with that and you
get a better MMT. Therefore any MST is a MMT.
34
SID: Final P. Raghavendra & J. Wright
35
Final P. Raghavendra & J. Wright
36