0% found this document useful (0 votes)
14 views36 pages

2022 Spring Final

The document outlines the final exam for CS170 at U.C. Berkeley, detailing rules, guidelines, and sections attended by students. It includes various algorithm-related questions, such as depth-first search, runtime analysis, Dijkstra's algorithm, and minimum spanning trees. The exam consists of multiple choice and problem-solving sections, with a total of 183 points available.

Uploaded by

sampurn.bhowmick
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)
14 views36 pages

2022 Spring Final

The document outlines the final exam for CS170 at U.C. Berkeley, detailing rules, guidelines, and sections attended by students. It includes various algorithm-related questions, such as depth-first search, runtime analysis, Dijkstra's algorithm, and minimum spanning trees. The exam consists of multiple choice and problem-solving sections, with a total of 183 points available.

Uploaded by

sampurn.bhowmick
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/ 36

U.C.

Berkeley — CS170 : Algorithms Final


Lecturers: Prasad Raghavendra and John Wright May 11, 2022

Final

Name:

SID:

Name and SID of student to your left:

Name and SID of student to your right:

Exam Room:
Rules and Guidelines

• The exam will last 170 minutes.


• The exam has 183 points in total.

• 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.

□ Kevin Zhu, Wednesday 12 - 1 pm, Wheeler 200

□ Andrew Che, Wednesday 1-2 pm, Dwinelle 79


□ Kevin Li and Param (Exam Prep), Wednesday 1-2 pm, Wheeler 224
□ Adnaan Sachidanandan, Wednesday 2-3 pm, Wheeler 108
□ Wilson Wu, Wednesday 2-3 pm, Hearst Memorial Gym 242

□ Cindy Zhang, Wednesday 3-4 pm, Cory 289


□ Tyler Hou (Leetcode), Wednesday 4-5 pm, Etcheverry 3109
□ Elicia Ye, Thursday 11-12 pm, Wheeler 130

□ Cindy Zhang, Thursday 12-1pm, Remote


□ Reena Yuan, Thursday 1-2pm, Etcheverry 3113
□ Tynan Sigg, Thursday 4-5 pm, Soda 310
□ Adnaan Sachidanandan (LOST), Thursday 5-7, Cory 258

□ Video walkthroughs
□ Don’t attend Section

2
SID: Final P. Raghavendra & J. Wright

1 Search tree (8 points)

Suppose we run depth-first search on the following graph, breaking ties alphabetically.

C B A F

D G H

1. Draw the DFS search tree.

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.

A→B hTree/Foward Edge hBack Edge hCross Edge

B→A hTree/Foward Edge hBack Edge hCross Edge

B→C hTree/Foward Edge hBack Edge hCross Edge

B→D hTree/Foward Edge hBack Edge hCross Edge

D→A hTree/Foward Edge hBack Edge hCross Edge

C→E hTree/Foward Edge hBack Edge hCross Edge

A→E hTree/Foward Edge hBack Edge hCross Edge

F→A hTree/Foward Edge hBack Edge hCross Edge

G→D hTree/Foward Edge hBack Edge hCross Edge

H→G hTree/Foward Edge hBack Edge hCross Edge

A→G hTree/Foward Edge hBack Edge hCross Edge

A→H hTree/Foward Edge hBack Edge hCross Edge

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

2 Runtime Analysis (5 points)

Consider the following snippet of code.


Function what(n) {

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

3 Djikstra’s algorithm (5 points)

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

4 Strongly Connected Components (8 points)

Suppose we execute DFS on a directed graph G and compute pre and post values for each node. Let

u ← vertex with smallest pre value


v ← vertex with largest post value

1. The vertex with the smallest pre value is


(A) necessarily part of a source SCC.
(B) necessarily part of a sink SCC.
(C) none of the above.

hA hB hC

2. The vertex with the largest post value is


(A) necessarily part of a source SCC.
(B) necessarily part of a sink SCC.
(C) none of the above.

hA hB hC

3. If the graph G is strongly connected then


(A) there is necessarily an edge from u → v.
(B) there is necessarily an edge from v → u.
(C) vertex u is the same as vertex v.
(D) none of the above.

hA hB hC hD

4. The vertex with the largest pre value


(A) is necessarily in a sink SCC.
(B) is necessarily not in a source SCC.
(C) none of the above.

hA hB hC

Solution:

1. C. Start the DFS anywhere that anywhere has smallest pre.


2. A. DPV p. 103 Property 2.
3. C. DFS can and will visit every other vertices before getting back to u.
4. C. See Q1.1 of this exam for counterexample, F has largest pre but it’s in a source and not sink SCC.

7
Final P. Raghavendra & J. Wright

5 Minimum Spanning Tree (8 points)

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

edge added by the algorithm is

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

edge added by the algorithm is

Solution:

1. 8. Note cycle ACDG. Note also edge weight are not distinct and no assumption made on tiebreaking.

2. 25. In cycle ACDE, kick DE (weight 7, heaviest) and add AE.


3. This is Prim’s from D. We have DG and DE remaining connecting {ABCD} and {EFG}, DE smallest.
4. This is Kruskal’s. Next to add would be CD, smallest weight after EF BC AC.

6 TSP tour (4 points)

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

the minimum travelling salesman tour is at most

Solution:

1. C. DPV p. 294

2. 2W. Idem. (”[T]his tour has a length at most twice the MST cost”)

7 NP-completness true/false (15 points)

For each of the following questions, there are four options:


(1) True (T); (2) False (F); (3) True if and only if P = NP; (4) True if and only if P ̸= NP.
Circle one for each question.
Note: By “reduction” in this exam it is always meant “polynomial-time reduction with one call to the
problem being reduced to.”

1. There is a polynomial-time reduction from Integer Programming to Circuit-SAT.

hT hF hP = NP hP ̸= NP

2. There is a polynomial-time reduction from Circuit-SAT to Integer Programming.

hT hF hP = NP hP ̸= NP

3. There is a polynomial-time reduction from Minimum-Spanning Tree to Integer Programming.


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

5. The Longest Increasing Subsequence problem is NP-complete.


hT hF hP = NP hP ̸= NP

Solution:

9
Final P. Raghavendra & J. Wright

1. T. DPV pp. 274-275

2. T. DPV p. 262 Fig. 8.7


3. T. MST is P so just solve inside the cook reduction
4. P = N P . Note here P ⊆ N P , so this only happens when P = N P = N P -Complete.

5. P = N P . LIS is P , for it to be N P -Complete it’s gonna be P = N P = N P -Complete.

10
SID: Final P. Raghavendra & J. Wright

8 Edit Distance (10 points)

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].

Suppose we had a special key-board in which

• each insertion takes 2 keystrokes,


• each deletion takes 3 keystrokes, and
• each substitution takes 4 keystrokes.

1. Write down the modified recurrence relation for ED [i.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]]

2. Write down the modified base cases for ED [i, j].

Solution: ED [i, 0] = 3i ∀i
ED [0, j] = 2j ∀ j

11
Final P. Raghavendra & J. Wright

9 Fill in the Blanks (24 points)

1. A directed acyclic graph (DAG) on n vertices can have at most number of edges
Solution: Cn2 , maybe in some CS70 note.

2. A strongly connected directed graph on n vertices must have at least number of


edges. Solution: n, just a cycle.

3. Let G be a graph on n nodes. The dynamic programming based algorithm for All-Pairs-Shortest-Paths

(also known as the Floyd-Warshall algorithm) on G has many subproblems.

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

Pr[h(2) > h(1)] = .


1 2n−1 1 1
Solution: 2n from DPV, 4n = 2 × (1 − 2n )

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

State Pr[ outcome is 0] Pr[outcome is 1]


|0⟩
q q
1 2
3 · |0⟩ − 3 · |1⟩

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

probability of outcome 0 is Solution: 1. OG Style:

 " √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

And the rest is more than obvious which I will omit.

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.

2. A linear program cannot have exactly 2 distinct optimal solutions.


hTrue hFalse
Solution: True. A convex optimisation problem may have 0, 1, or ∞ optimal solutions.

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.

6. Quantum computers are believed to be able to efficiently solve NP-complete problems.


hTrue hFalse
Solution: False, there are some problems in BQP but not in NP; e.g. Grover’s algorithm covered in
lecture. Special topic of this semester.

14
SID: Final P. Raghavendra & J. Wright

11 One-Way Streets (15 points)

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) An undirected graph


C C

A B A B

(b) A strongly connected orientation (c) An orientation which is not strongly connected

1. Give an example of a connected undirected graph where this cannot be done.

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

orientation, then it can have at most many lonely edges. Solution: 0

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

12 Power Stations (15 points)

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.

1. No charging station can be open for more than 18 hours in a day.


2. Each charging station costs a certain amount per hour to keep open. The costs are given in the follow-
ing table.

Charging Station Cost per hour


A 1
B 2
C 3
D 4

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:

x A = number of hours station A is open


x B = number of hours station B is open
xC = number of hours station C is open
x D = number of hours station D is open

The constraints were:

• ”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:

x ABCD = number of hours the state is in configuration with ABCD on,

and likewise for the other 6 permitted states.


Fun fact (no extra credit): To be more efficient, one could notice that AC is always better than ABC,
ACD, and ABCD. In addition, BC is better than BCD. So, we can just have AC, BC and ABD variables.
(There was also a question about having a binary variable per station per hour. I think this is just
as wrong as the initial failed attempt, since for each hour i you could still have the variables set
x A,i = x B,i = xC,i = x D,i = 1/2, which gives the failed linear program output from Section 12.1
above. So I would think this would be a 0 point answer, though I’m happy if someone disagrees here
:).)

20
SID: Final P. Raghavendra & J. Wright

2. What are the constraints of the LP?

3. What is the objective function being minimized?

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:

x ABCD = number of hours the state is in configuration with ABCD on,

and likewise for the other 6 permitted states.


The constraints are:

• ”No charging station can be open for more than 18 hours a day”

x ABCD + x ABC + x ABD + x ACD + x AC ≤ 18,


x ABCD + x ABC + x ABD + x BCD + x BC ≤ 18,
x ABCD + x ABC + x BCD + x ACD + x AC + x BC ≤ 18,
x ABCD + x ABD + x ACD ≤ 18.

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

and likewise for the other 3 charging stations.

21
Final P. Raghavendra & J. Wright

• All states occur for a nonnegative amount of time.

x ABCD , x ABC , x ABD , x ACD , x BCD , x AC , x BC ≥ 0.

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:

10 · x ABCD + 6 · x ABC + 7 · x ABD + 8 · x ACD + 9 · x BCD + 4 · x AC + 5 · x BC .

22
SID: Final P. Raghavendra & J. Wright

13 Karger’s algorithm (5 points)

Consider the following undirected graph G = (V, E) on n vertices.

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

14 Longest k-modal subsequence (15 points)

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 .

1. What are the subproblems? (precise and succinct definition needed)

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:

L[i, m] = length of the longest m-modal subsequence ending at letter ai .

2. What are the recurrence relations?

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

L[i, m] = 1 + max{ L[ j, m − 1] | j < i, a j > ai } ∪ { L[ j, m] | j < i, a j < ai }.

On the other hand, if m is odd,

L[i, m] = 1 + max{ L[ j, m − 1] | j < i, a j < ai } ∪ { L[ j, m] | j < i, a j > ai }.

26
SID: Final P. Raghavendra & J. Wright

15 Reductions (10 points)

Consider the following two problems:

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).

1. The problem is believed to not reduce in polynomial time to


because

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.

2. On the other hand, reduces in polynomial time to


Here is the reduction.

Reduction: Given an instance Φ of the problem we will construct an instance Ψ

of the problem as follows.

27
Final P. Raghavendra & J. Wright

The proof that this is a valid reduction is omitted here


Solution: On the other hand, Matching reduces in polynomial time to Independent Set.
Reduction: Given an instance Φ of the problem Matching we will construct an instance Ψ of the problem
Independent Set as follows.
For each edge e in G of Φ, create an vertex v′ in G ′ of Ψ. For each pair of edges (e1 , e2 ) that share vertices, add
′ between the corresponding vertices v′ , v′ in G ′ of Ψ.
an edge e1,2 1 2
Note: Some students attempted to run the max flow algorithm to solve independent set. Unfortunately, this is
not a reduction, since we do not need a solution to the problem we are reducing to.
Some students did not clarify that we create edges in the new graph based on which vertices are shared. These
answers lost a point.

28
SID: Final P. Raghavendra & J. Wright

16 NP-Completeness Reductions (15 points)

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.

Given an instance Φ of the problem we will construct an instance Ψ of the problem

as follows.

The proof that this is a valid reduction is 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

17 Good Pivot (10 points)

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 .

A good pivot b does not necessarily have to be an element of the array.

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)

2. What is the asymptotic run-time of your algorithm?

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

18 Minimum Median Tree (5 points)

Design an efficient algorithm for the following problem:


Input: A graph G = (V, E) and weights on the edges {we | e ∈ E}.
Solution: A spanning tree T that minimizes the medianCost( T ), where medianCost( T ) is defined as
medianCost( T ) = median of the weights of the edges in the tree T.
The runtime of your algorithm must be polynomial in the number of vertices n = |V |.

1. Give a succinct and precise description of your algorithm.

2. Prove the correctness of your algorithm.

Solution:

33
Final P. Raghavendra & J. Wright

1. Run Kruskal’s or Prim’s.

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

Blank scratch page.

35
Final P. Raghavendra & J. Wright

Blank scratch page.

36

You might also like