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

CSE 373: Practice Midterm 2: I Followed The University's Honor Code

This document contains a practice midterm exam for a computer science course. The exam has 4 problems worth a total of 100 points. Problem 1 contains 8 true/false and multiple choice questions worth 10 points total. Problem 2 contains 3 subproblems about graphs and algorithms worth 20 points total. Problem 3 asks the student to modify the Bellman-Ford algorithm to run more efficiently on a certain graph and is worth 20 points. Problem 4 asks the student to write recurrences and an algorithm to count even and odd length paths in a directed acyclic graph, worth 25 points. The exam instructs students to show their work and not use outside materials.

Uploaded by

Peter Anderson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

CSE 373: Practice Midterm 2: I Followed The University's Honor Code

This document contains a practice midterm exam for a computer science course. The exam has 4 problems worth a total of 100 points. Problem 1 contains 8 true/false and multiple choice questions worth 10 points total. Problem 2 contains 3 subproblems about graphs and algorithms worth 20 points total. Problem 3 asks the student to modify the Bellman-Ford algorithm to run more efficiently on a certain graph and is worth 20 points. Problem 4 asks the student to write recurrences and an algorithm to count even and odd length paths in a directed acyclic graph, worth 25 points. The exam instructs students to show their work and not use outside materials.

Uploaded by

Peter Anderson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name:

I followed the University’s Honor Code.

Student ID:

CSE 373: Practice Midterm 2

Solve each of the following problems. There are 4 total problems and 100 total points. You may use
NO notes, books, phones etc.. If you need additional space, use the back of the exam pages (and note
that you’ve written there). If you need still more space, use the blank paper provided. If you think a
problem cannot be solved as stated, explain why. Regarding algorithms and proofs: If a question
asks for an algorithm, you need to provide a clear and unambiguous description; this should be in terms
of pseudocode, or clear, precise and unambiguous English sentences describing the computational steps. If
a question asks for you to show / prove something, you must provide a valid logical argument why the
statement is true or false (your argument should convince a skeptical reader). Finally, read the questions
and instructions carefully, credit will not be granted for “misinterpreting” the questions. Good luck! U
Very short problems Problem 1. (10 points)

i. True / False (circle the correct one). A dynamic programming algorithm enables computing the edit
distance between two strings X (of length n) and Y (of length m) in O(n log(m)) time (assume, n ≤ m).

ii. True / False (circle the correct one). For the same two strings as above, the edit distance can be
computed in O(n) space.

iii. Among the following, circle the tightest upper-bound for the runtime of the Bellman-Ford algorithm
on a graph with n vertices and m edges.
A. O(n log(m)) B. O(n3 ) C. O(nm) D. O(mn2 )

iv. True / False (circle the correct one). There are asymptotically faster algorithms for finding the shortest
s-t path in a DAG than in a weighted, undirected graph.

v. The Karatsuba multiplication algorithm follows a running-time recurrence of T (n) ≤ 3T ( n2 ) + O(n).


For an input of 2, n-bit numbers, this means the runtime is O( )

vi. For dynamic programming to be applicable to a problem, the problem must exhibit an optimal
property and have subproblems.

vii. The runtime of the dynamic program for solving the subset sum problem is O(nM ) for an instance
with n items and a total budget of M . Since the runtime depends on the numeric value of the budget,
this algorithm is said to have a runtime.

viii. True / False (circle the correct one). The asymptotic runtime for the divide-and-conquer algorithm to
count inversions is strictly greater than the asymptotic runtime of mergesort.

1
Short problems Problem 2. (20 points)

i. Sketch a small example of a weighted graph on which Dijkstra’s algorithm will fail to find the shortest
s-t path, but Bellman-Ford will not (specify the nodes s and t, and the weights of each edge).

2
ii. Consider extending the notion of computing the edit distance to 3 strings, X, Y and Z (of lengths `,
m and n, respectively). We can define the recurrence for computing the optimal edit distance as:


 cost(xi , yj , zk ) + OPT(i − 1, j − 1, k − 1)
cost(xi , −, −) + OPT(i − 1, j, k)





cost(xi , yj , −) + OPT(i − 1, j − 1, k)



OPT(i, j, k) = min cost(−, yj , zk ) + OPT(i, j − 1, k − 1)

cost(−, yj , −) + OPT(i, j − 1, k)







 cost(xi , −, zk ) + OPT(i − 1, j, k − 1)
cost(−, −, zk ) + OPT(i, j, k − 1)

Assuming it is implemented efficiently via dynamic programming, what would be the runtime for
evaluating OPT(`, m, n)? Briefly describe why.

iii. You are given a weighted directed graph G = (V, E) (not necessarily acyclic), a source node s, and
additionally, an integer k, which is the number of edges in the longest simple path in the graph. Briefly
describe (in a few clear English sentences) how you would modify the Bellman-Ford algorithm to run
in O(k |E|) time.

3
Problem 3. (20 points)

You are given a DAG G = (V, E) where each edge e = (u, v) has an associated weight w(u, v), as well as a
source vertex s and a target vertex t. Define the weight of a path p = v1 , v2 , . . . , vk , written as w(p) to be
w(v1 , v2 ) + w(v2 , v3 ) + · · · + w(vk−1 , vk ). Further, define the cost gap for a vertex v to be the weight of the
longest (i.e. heaviest) path from s to v minus the weight of the shortest (i.e. lightest) path from s to v. For
example, let pvmin be the shortest s-v path and let pvmax be the longest s-v path. The cost gap of v is given
by w(pvmax ) − w(pvmin ). Devise an O(V + E) algorithm to compute the cost gap for t and also to return the
pair of paths ptmin and ptmax that yield this cost gap.

4
Problem 4. (25 points)

You are given an unweighted, directed acyclic graph G = (V, E) and a source node s. For a given s-v path,
we say the path is odd if it contains an odd number of edges and even if it contains an even number of edges.
You wish to determine, for each vertex v ∈ V | v 6= s, the number of even and odd length paths. That is, for
each v you want to know the total number of even and odd length paths between s and v.

(a) First, write the relevant recurrences to compute the number of even and odd length paths to a vertex.
(hint: If a predecessor has k odd-length paths, what is the length of all of these paths after adding one more
edge?).

X
Neven (v) =
u∈Predecessors(v)

X
Nodd (v) =
u∈Predecessors(v)

(b) Now, write an algorithm that will efficiently compute Neven (·) and Nodd (·). Your algorithm should run
in O(|V | + |E|) time and space.

You might also like