Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Fundamental Algorithms

CSCI-GA.1170-001/Summer 2021

Midterm Exam

Problem 1. (30 points) Prove or disprove the following conjectures:


p
(a) (5 points) − log p1 n = Ω(log3 n2 ).
2
Pn Pn n
 a

(b) (5 points) Functions f (n) = i j (i· j) and g(n) = n· 4 , where b denotes the binomial
coefficient, grow at the same rate.
(c) (5 points) The following function prints "Hello" Θ(n2 lg n) times:

FUN(n)
1 if n > 1
2 FUN(bn/2c)
3 for i = 1 to n
4 for j = 1 to n
5 PRINT("Hello")
6 FUN(bn/2c)

(d) (5 points) A particular version of MERGESORT breaks the array into three equal parts,
recursively sorts the first two parts, merges them, recursively sorts the last part, and merges
it with the already merged parts one and two. This MERGESORT has the same worst-case
running time as the standard version.
(e) (5 points) There exist such f (n) and g(n) that f (n) = O(g(n)) but 2 f (n) 6= O(2 g(n) ).
(f) (5 points) There exists an algorithm that takes a binary heap with n arbitrary elements
and prints its elements in sorted order in Θ(n) time.

Problem 2. (22 points) We can use the modulo operation to break a number into its digits.
(a) (8 points) Give a procedure GET-DIGITS(n) that implements this method and returns the
digits of a positive integer n.
(b) (14 points) Prove GET-DIGITS correct using a loop invariant.
Clearly state your invariant and make it as formal as possible. A proof that reads like "D
contains the last i digits of n; on each iteration we add one more; therefore it is correct" is
explaining rather than proving the algorithm. Aim for an invariant that is a mathematical
expression.
Remember that if you are not using both your invariant and your loop condition at termi-
nation, your proof is likely off track.

The exam continues on the next page.

1
Problem 3. (28 points) Answer the following questions about QUICKSORT:
(a) (4 points) Consider the following way of picking pivots:
• Define skew of element x as the absolute value of the difference between the number
of elements ≤ x and the number of elements > x.
• Iterate over the array, computing skew for each element (naively, by iterating over all
other elements and counting the number of smaller and greater ones).
• Pick an element with the smallest skew as the pivot.
What is the worst-case running time of QUICKSORT selecting pivots that way, assuming all
input elements are distinct?
(b) (4 points) What is the worst-case running time of QUICKSORT selecting pivots as specified
in (a), but assuming the input elements are arbitrary and may include duplicates?
(c) (4 points) Consider the following way of picking pivots:
• Put all elements into a binary min-heap.
• Extract the minimum bn/2c times.
• Pick the last extracted element as the pivot.
What is the worst-case running time of QUICKSORT selecting pivots that way, assuming all
input elements are distinct?
(d) (4 points) (This item starts a new subsection; (a), (b), and (c) are no longer relevant.)
Consider the following version of QUICKSORT with only one recursive call:

QUICKSORT(A, p, r)
1 while p < r
2 q = PARTITION(A, p, r)
3 QUICKSORT(A, p, q − 1)
4 p =q+1

Prove that this version is equivalent to the standard one.


(e) (4 points) What is the worst-case recursion depth of this version?
(f) (8 points) Give a modification of the code that achieves the worst-case recursion depth
of Θ(lg n) without degrading the asymptotic running time. (Note that we are not trying to
save work or make the algorithm run faster – only to limit the recursion depth.)

The exam continues on the next page.

2
Problem 4. (20 points) Answer the following questions about probability in algorithms:
(a) (6 points) Consider inserting n keys into a hash table of size m, assuming simple uniform
hashing. What is the probability that the only collision occurs on the last insertion?
(b) (6 points) n distinct integers 1, 2, . . . , n are shuffled and passed to the procedure NO -
MEMORY one by one. NO -MEMORY tries to guess each integer by naming an integer from
the range [1, n] uniformly at random. (As the name suggests, NO -MEMORY does not take
into account what integers it has already seen.)
What is the expected number of times NO -MEMORY guesses correctly?
(c) (8 points) Same as the previous problem, but we use the procedure HAS-MEMORY, which
remembers the previously seen integers. It then guesses by naming an integer uniformly
at random from those not yet seen.
What is the expected number of times HAS-MEMORY guesses correctly?

This is the end of the exam.

You might also like