Midterm Exam 1
CompSci 330: Design and Analysis of Algorithms, Spring 2023
Tuesday, March 2, 2023
This exam has six pages (including the cover page).
INSTRUCTIONS
1. No external help (books, notes, laptops, tablets, phones, etc.) or collaboration is allowed.
2. Answers should be brief, clear, and to the point. You will not get credit for incomprehensible answers,
even if they are correct.
3. You can refer to an algorithm described in the class without describing it.
4. Every given target running time is a worst-case running time. If the target running time is not
mentioned in a problem, it is intentional and we want you to determine the target running time.
5. Please remember that these exams will be scanned, so avoid writing near the staple or the very edge
of the paper. We recommend writing your NetID on all pages, but it is not required.
6. This exam is one-sided, so if you need extra space, use the back of the same page the question is on,
and indicate the continuation on the front side.
7. Please be sure to hand in your exam in the correct pile, based on last name.
Writing Expectations. If you are asked to provide an algorithm, you should clearly and unambiguously
define every step of the procedure as a combination of precise sentences in plain English and pseudocode. If
you are asked to explain your algorithm, its runtime complexity, or argue for its correctness, your written
answers should be clear, concise, and should show your work; do not skip details but do not write paragraphs
where a sentence suffices.
All the best!
Name:
NetID:
1
Problem 1 [20 points]. Let X = (x1 , . . . , xn ) be a sequence of n real numbers sorted in increasing order,
and let D 6= 0 be another real number. Describe a O(n)-time algorithm without the use of hash table to
determine whether there are two elements xi , xj ∈ X such that xi − xj = D. Justify the correctness of your
algorithm and its running time.
2
Problem 2 [25 points]. Let C be a set of n cities, and let F = {F1 , . . . , Fm } be a set of m flights between
the cities in C. Each flight is denoted by a tuple Fi = (si , di , ai , wi ), where si is the source city of Fi , di is
the destination city of Fi , ai is the airline that runs the flight Fi , and wi > 0 is the fare of Fi .
Given the starting city Cs ∈ C and the destination city Cd ∈ C, a flight plan from Cs to Cd is a sequence of
flights of the form h(C1 , C2 , a1 , w1 ), (C2 , C3 , a2 , w2 ), . . . , (Ck , Ck+1 , ak , wk )i for some k ≥ 1 where C1 , ..., Ck+1
are cities of C, C1 = Cs , and Ck+1 = Cd .
We wish to compute a flight plan from Cs to Cd under different criteria. The goal is to describe a fast
algorithm for each criterion. Justify your answer in each case and describe the corresponding time complexity.
(a) [10 points] The cost of a flight plan is the sum of the flight fares in the plan. How would you compute
the cheapest flight plan from Cs to Cd ?
3
(b) [15 points] How would you compute a flight plan from Cs to Cd that minimizes the number of changes
in airlines along the plan?
For example, the number of airline changes is 0 for h(Cs , C1 , A1 , 100), (C1 , Cd , A1 , 100)i, and 2 for
h(Cs , C1 , A1 , 100), (C1 , C2 , A2 , 50), (C2 , Cd , A1 , 100)i.
4
Problem 3 [25 points]. Suppose m friends want to eat dinner together and there are n possible restaurants
to choose from. Each friend k reports an array Ak of length nk ≤ n where each element
Pkis a restaurant, and
Ak is sorted in decreasing order of preferred restaurants for the friend k. Set N = i=1 ni . Describe an
O(n + N )-time algorithm that either computes an array B of length n sorted in a way that is consistent with
the preference of all of the friends, or reports that none exists. That is, for all 1 ≤ k ≤ m and 1 ≤ j < nk ,
Ak [j] appears before Ak [j + 1] in B, or reports that no such ordering exists.
For example, suppose there are three restaurants and three friends, and their preferences are A1 = [a, c], A2 =
[c, b] and A3 = [a, b]. Then (a, c, b) is a valid ordering of the three restaurants.
Justify the correctness of your algorithm and its running time.
5
Problem 4 [30 points]. A shuffle of two strings A and B is a new string formed by all of the characters
from A and B, with those characters appearing in the same relative order as their original string. For
example, if A = abc and B = ijk, then abcijk, ijkabc, and aibjck are valid shuffles. However, cijkab would
not be a valid shuffle.
Given string A of length m, string B of length n, and string C of length m + n, describe an iterative
O(nm)-time algorithm that returns true if C is a shuffle of A and B, or false otherwise.
State your recurrence, which includes the base case for the problem, briefly explain your recurrence in English
and then describe your iterative algorithm. You need not give any further proof of either correctness of your
algorithm or running time.
(Hint: Modify the algorithm for finding the longest common subsequence.)
6