0% found this document useful (0 votes)
4 views

2023 Spring Final

The document is a final exam for the CS170 Algorithms course at U.C. Berkeley, detailing rules, guidelines, and various algorithmic problems related to topics such as DFS, Huffman encoding, max flow, zero-sum games, and NP-completeness. It includes specific questions requiring students to demonstrate their understanding of algorithms and problem-solving techniques. The exam is structured to assess knowledge through a series of problems that cover both theoretical and practical aspects of algorithms.

Uploaded by

sampurn.bhowmick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2023 Spring Final

The document is a final exam for the CS170 Algorithms course at U.C. Berkeley, detailing rules, guidelines, and various algorithmic problems related to topics such as DFS, Huffman encoding, max flow, zero-sum games, and NP-completeness. It includes specific questions requiring students to demonstrate their understanding of algorithms and problem-solving techniques. The exam is structured to assess knowledge through a series of problems that cover both theoretical and practical aspects of algorithms.

Uploaded by

sampurn.bhowmick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

U.C.

Berkeley — CS170 : Algorithms Final


Lecturers: Prasad Raghavendra and John Wright May 9, 2023

Final

Name: Targaryen

SID: 0123456789

Name and SID of student to your left: Lannister

Name and SID of student to your right: Stark

Exam Room:
Rules and Guidelines

• The exam will last 170 minutes.


• The exam has 170 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.
• 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.
• If multiple differing solutions are provided for a given question, only the first solution we read will
be graded.
• Good luck!
Final P. Raghavendra & J. Wright

1 SCCs (8 points)

Execute a DFS on the directed graph shown below starting at node A and breaking ties alphabetically.

A F J

B C D E

G H

1. Fill in the table of pre and post values.


Node pre post
A

2
Final P. Raghavendra & J. Wright

2. In the DFS execution from above, mark the following edges as as T for Tree, F for Forward, B for Back
and C for Cross.

Edge Type
A→C

F→J

H→J

E→H

3. List the strongly connected components of the above graph in some linearized order. (Note that the
number of SCCs is possibly smaller than the number of rows provided in the table below)
SCC no. Vertices in the component
1

10

4
SID: Final P. Raghavendra & J. Wright

2 Huffman Encoding (3 points)

Construct the Huffman code for the alphabet A, B, C, D, E, F, with the following frequencies.

Letter A B C D E F
Frequency 100 30 1 29 40 50

Draw the tree corresponding to the Huffman code.

5
Final P. Raghavendra & J. Wright

3 Max Flow via Linear Programming (12 points)

Consider the following directed capacitated graph.

3 6

S 3 T

5 3

1. Write the linear program corresponding to the maximum flow problem from S to T.

6
SID: Final P. Raghavendra & J. Wright

2. Write the dual to the above linear program. (The dual linear program corresponds to the minimum
cut problem.)

7
Final P. Raghavendra & J. Wright

4 Zero-Sum Games (6 points)

Consider a zero-sum game given by the following payoff matrix.

C1 C2 C3
R1 1 2 3
R2 2 3 1
R3 3 1 2

The row-player goes first, and the column player goes second. The row player is trying to maximize their
payoff, which is specified by the entries of the matrix.
The value of the game is the expected payoff of the row-player when both players use optimal mixed strate-
gies.
Mark the following statements as true or false (each subpart is independent of the others).

1. If the column player is restricted to the three pure strategies {(1, 0, 0), (0, 1, 0), (0, 0, 1)}, row player’s
expected payoff doesn’t change.
hTrue hFalse

2. If the row player is restricted to the three pure strategies {(1, 0, 0), (0, 1, 0), (0, 0, 1)}, column player’s
payoff doesn’t change.
hTrue hFalse

3. Suppose we modify the game to include an additional row (so the resulting matrix is a 4 × 3 payoff
matrix). Then the value of the game can possibly both increase or decrease depending on the entries
of the new row.
hTrue hFalse

Write a linear program to find the optimal mixed strategy for the row-player.

8
Final P. Raghavendra & J. Wright

5 Median (4 points)

Suppose we ran quickselect (the randomized divide and conquer algorithm for finding the k-th smallest
element of an array) on the following array to find it’s median.

3 8 4 12 1 9 4 6 5

1. Specify which elements should be picked as pivots at each step in order to maximize the runtime of
this algorithm. Write the numerical value of the elements, not their indices. You might not need to fill
all boxes provided.

2. Specify which elements should be picked as pivots at each step in order to minimize the runtime of
this algorithm. Write the numerical value of the elements, not their indices. You might not need to fill
all boxes provided.

10
SID: Final P. Raghavendra & J. Wright

6 MST weights via linear program (8 points)

Consider the following graph on 4 vertices A, B, C, D with edge weights {w AB , w BC , w BD , w AC , wCD , w AD }


on the edges.

What is the value of the optimum? B


1

A 2 D

3
C

We are given the weights w AB = 1, w BC = 2 and wCD = 3.


The other three weights w AC , w AD , w BD are unknown, but they are such that AB − BC − CD is a minimum
spanning tree of the graph. Write a linear program to determine the smallest possible value of 2w AC +
3w AD + 4w BD .

1. What are the constraints on the unknowns w AC , w AD , w BD ?

11
SID: Final P. Raghavendra & J. Wright

7 Short Answer (24 points)

1. Suppose T(n) = 2T(n/4) + n2 then T(n) = (write the tightest possible bound)

2. If T(n) = 2T(n/4) + n2 log8 n then T(n) = (write the tightest possible bound)

3. An n-vertex directed acyclic graph can have as many as ( ) directed edges, while

an n-vertex undirected graph with no cycles can have as many as edges.

4. Suppose A, B, C, D, E are five vertices that are part of a large directed graph. Here are the pre and post
values of these vertices in a DFS traversal. The traversal was carried out in an unknown order, not
necessarily lexicographical.
Node pre post
A 20 30
B 10 80
C 50 54
D 90 100
E 93 97
Then,
(a) There is necessarily no directed path from A to C in the graph.
hTrue hFalse

(b) The directed edge A → C is not part of the graph.

hTrue hFalse

(c) If the directed edge D → A exists, then it is a cross-edge the graph

hTrue hFalse

(d) D and E are necessarily part of the same SCC.

hTrue hFalse

(e) C and E are part of different SCCs.


hTrue hFalse

13
Final P. Raghavendra & J. Wright

5. Let ω = e2πi/16 be the 16th root of unity. Suppose the Fourier transform of a vector ( p0, p1, . . . , p15) is

(1, ω, ω2, . . . , ω15).


What is the value of ∑15 i
i =0 pi · 2 ?

The following points are some of the feasible solutions of a linear program.
(0, 0), (0, 10), (5, 8), (10, 10), (10, 0), (5, 2)

For each of these points indicate whether they are necessarily feasible, necessarily infeasible or neither.

(a) (0, 5)
hnecessarily feasible hnecessarily infeasible hneither

(b) (15, 18)


hnecessarily feasible hnecessarily infeasible hneither

(c) (5, 10)


hnecessarily feasible hnecessarily infeasible hneither

7. The dynamic programming based algorithm for edit distance between two strings x[1, . . . , m] and

y[1, . . . , n] takes time, because the algorithm uses .............

many subproblems computing each of which takes time.

8. Suppose we run the distinct elements algorithm on a stream s1 , . . . , sm . Assume that the algorithm
uses a hash-function h that is perfectly random.
If there is only one distinct element in the stream, what is the probability that the algorithm outputs
an estimate > 99?

9. Suppose we run the distinct elements algorithm on a stream s1 , . . . , sm . Assume that the algorithm
uses a hash-function h that is perfectly random.
If there are 100 distinct elements in the stream, what is the probability that the algorithm outputs an
estimate < 9?

10. Suppose we pick a hash function h : {0, 1. . . . , p − 1} → {0, 1, . . . , p − 1} from a pairwise independent
hash family. What is the probability that h(2) = (h(1) + 2)modp?

14
Final P. Raghavendra & J. Wright

8 Set Cover (6 points)

Let S1 , S2 , S3 denote the first 3 sets picked during an execution of the greedy algorithm for Set Cover prob-
lem.
Suppose the size of the universe is 24 and the sizes of the unions of sets are as follows

|S1 | = 12
|S1 ∪ S2 | = 15
|S1 ∪ S2 ∪ S3 | = 17

For each subpart, provide the tightest possible bound.

1. The size of optimal set cover is at least

2. The greedy algorithm will necessarily pick at least sets before termination.

3. The greedy algorithm will pick at most sets before termination.

16
SID: Final P. Raghavendra & J. Wright

9 NP-completness true/false (9 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. Rudrata Cycle reduces to Maximum Flow.

hT hF hP = NP hP ̸= NP

2. Every problem in NP reduces to the independent set problem.

hT hF hP = NP hP ̸= NP

3. Every problem in P reduces to the Maximum Flow problem.

hT hF hP = NP hP ̸= NP

4. 3-SAT can be reduced to independent set, but not vice-versa.

hT hF hP = NP hP ̸= NP

5. Knapsack problem can be solved in polynomial-time.

hT hF hP = NP hP ̸= NP

6. All NP-Hard problems can reduce to 3 SAT in polynomial time.

hT hF hP = NP hP ̸= NP

17
SID: Final P. Raghavendra & J. Wright

10 Non-MST Edge (10 points)

Devise a O(|V |)-time algorithm for the following problem:


Input: Graph G = (V, E) with distinct edge weights wij for each edge (i, j) ∈ E.
Solution: Find some edge (i, j) ∈ E such that (i, j) is NOT part of any MST.

1. Give a succinct and precise description of the algorithm:

2. Briefly justify the run-time of the algorithm.

19
Final P. Raghavendra & J. Wright

11 Discount Path (15 points)

Consider a directed graph G = (V, E) with positive weights c(i, j) on its edges.
Given a path P = v0 → v1 . . . → vk , the discounted cost of the path is the sum of lengths of all but the the
longest edge. Formally,

!
k −1
cost( P) = ∑ c ( v i , v i +1 ) − max c(vi , vi+1 )
i
i =0

(It is the sum of cost of all the edges - maximum cost edge)
Devise an algorithm to compute the smallest discounted cost path from a vertex s to vertex t. Give a succinct
and precise description of your algorithm. Proof of correctness and run-time analysis are not necessary. The
runtime of your algorithm should be O((|V | + | E|) log |V |).

20
Final P. Raghavendra & J. Wright

12 NP-Completeness Reductions (30 points)


You may assume that the following problems are NP-complete: Rudrata (Hamiltonian) Path, Rudrata
(Hamiltonian) Cycle, Vertex Cover, Independent Set, 3-Coloring, 3-SAT and Integer Programming.
For each of the following problems, fill in the details of the proof that they are NP-complete.

1. TriClique: Given a graph G = (V, E), a subset S ⊂ V of vertices is called a clique, if every pair of

vertices in S are connected by an edge, i.e., for every u, v ∈ S we have (u, v) ∈ E.


Here is the TriClique problem.
Input: A graph G = (V, E)
Solution: A partition of the vertices into three disjoint sets S1 ∪ S2 ∪ S3 = V such that, each of the sets
S1 , S2 , S3 is a clique.

Proof. It is clear that the TriClique 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:

22
SID: Final P. Raghavendra & J. Wright

2. Set Ordering:
Input: A family of subsets S1 , . . . , Sn of {1, . . . , m}.
Solution: An ordering of the sets so that consecutive sets in the ordering have intersection exactly
one. Formally, an ordering of the sets is given by a permutation π : [n] → [n], so that for each
i ∈ {1, . . . , n − 1}, |Sπ (i) ∩ Sπ (i+1) | = 1.

Proof. It is clear that the Set-Ordering 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:

23
SID: Final P. Raghavendra & J. Wright

13 All Pairs Shortest Paths (15 points)

In the lecture, we have seen the Floyd-Warshall algorithm for computing distances between every pair of
vertices in an input graph. The Floyd-Warshall algorithm is a dynamic programming based algorithm with
runtime O(n3 ) In this question, we will devise a simpler dynamic programming based algorithm for all
pairs shortest paths, with a slightly worse runtime.
Given an input graph G = (V, E) with positive edge weights wij for each edge (i, j). Consider the following
sub-problem:
D [i, j, t] = length of the shortest path from i to j with at most 2t hops
(Here ”hop” refers to the number of edges on the path).

1. Write the recurrence relation for D [i, j, t].


3. What is the runtime of your algorithm?

2. Write pseudocode for the algorithm to compute all-pair distances, including the initialization.

What is the runtime of your algorithm?


25
SID: Final P. Raghavendra & J. Wright

14 Triangle Removal (20 points)


In the triangle removal problem, the input is a graph G = (V, E). The goal is to remove the smallest number
of edges from E, so as to delete all triangles in the graph.
Formally, here is the definition of triangle-freeness.
Definition: A graph is triangle-free if there are NO three vertices i, j, k ∈ V such that (i, j), ( j, k) and (k, i ) are
all edges.
Here is the formal definition of triangle removal problem:
Input: An unweighted undirected graph G = (V, E)
Solution: Find the smallest set of edges E′ such that deleting E′ makes the graph become triangle-free
We will now develop an approximation algorithm for the problem. First, write a linear programming
relaxation for the triangle-removal problem.

1. What are the variables of the linear program? (Write out succinctly and precisely, the intended mean-
ing of the variables in your linear program)

2. What is the objective function?

3. What are the constraints of the linear program?

27
Final P. Raghavendra & J. Wright

4. Describe a rounding scheme to obtain a {0, 1}-solution from the optimal solution to the above linear
program.

5. Briefly justify why the rounding scheme produces a solution to the Triangle Removal problem.

6. What is the approximation ratio of your algorithm?

28

You might also like