Final Exam Solutions
Final Exam Solutions
Final Exam Solutions
(b) T F For all positive f (n), g(n) and h(n), if f (n) = O(g(n)) and f (n) = (h(n)),
then g(n) + h(n) = (f (n)).
Solution: True. This follows from f (n) = O(g(n)) g(n) = (f (n)).
Name
(c) T F Under the simple uniform hashing assumption, the probability that three specific
data elements (say 1, 2 and 3) hash to the same slot (i.e., h(1) = h(2) = h(3)) is
1/m3 , where m is a number of buckets.
Solution: False. The above formula only describes the probability of collision
in a fixed bucket (say bucket number 1). The correct answer is 1/m2 .
(d) T F Given an array of n integers, each belonging to {1, 0, 1}, we can sort the array
in O(n) time in the worst case.
Solution: True. Use counting sort, e.g., after adding 1 to all numbers.
Name
(f) T F
R ADIX S ORT does not work correctly (i.e., does not produce the correct output)
if we sort each individual digit using I NSERTION S ORT instead of C OUNTING
S ORT.
Solution: False. I NSERTION S ORT (as presented in class) is a stable sort, so
R ADIX S ORT remains correct. The change can worsen running time, though.
3 points for correct answer and explanation
1 point for incorrect answer, but mentioning that radix sort needs to use a stable
sort
Name
(g) T F Given a directed graph G, consider forming a graph G0 as follows. Each vertex
u0 G0 represents a strongly connected component (SCC) of G. There is an
edge (u0 , v 0 ) in G0 if there is an edge in G from the SCC corresponding to u0 to
the SCC corresponding to v 0 .
Then G0 is a directed acyclic graph.
Solution: True. If there were any cycles in the graph of strongly connected
components, then all the components on the cycle would actually be one strongly
connected component.
3 points for correct answer
Name
An optimal solution to a knapsack problem will always contain the object i with
the greatest value-to-cost ratio vi /ci .
Solution: False. Greedy choice doesnt work for the knapsack problem. For
example, if the maximum cost is 2, and there are two items, the first with cost
1 and value 2, and the second with cost 2 and value 3, the optimal solution is to
take just the second item.
3 points for correct answer
Name
f2 = log nn
f3 = nn
1/2
(b) Solve the following recurrences by giving tight -notation bounds. You do not need
to justify your answers, but any justification that you provide will help when assigning
partial credit.
i. T (n) = 4T (n/2) + n2 log n
ii. T (n) = 8T (n/2) + n log n
6006
), checking that the regiii. Case 3 of the Master Method to obtainT (n) = (n
6006
n
6006
ularity condition af (n/2) = 6006 2
cn
for some c < 1. Condition
6006
holds for any c > 6006/2
.
Name
(c) Give a recurrence T (n) = for the running time of each of the following algorithms,
along with the asymptotic solution to that recurrence:
i. Insertion sort
ii. Merge sort
iii. 2D peak finding (the fastest algorithm weve seen)
Solution:
i. T (n) = T (n 1) + O(n) = O(n2 )
ii. T (n) = 2T (n/2) + (n) = (n lg n)
iii. T (n) = T (n/2) + (n) = (n)
(d) Could a binary search tree be built using o(n lg n) comparisons in the comparison
model? Explain why or why not.
Solution: No, or else we could sort in o(n lg n) time by building a BST in o(n lg n)
time and then doing an in-order tree walk in O(n) time.
Name
(e) Given n integers in the range 0 . . . k, describe how to preprocess these integers into a
data structure that can answer the following query in O(1) time: given two integers a
and b, how many integers fall within the range a . . . b?
Solution: same preprocessing as for counting sort, then numbers in range is C 0 (b)
C 0 (a).
(f) Describe how any comparison-based sorting algorithm can be made stable, without
affecting the running time by more than a constant factor.
Solution: Tag elements with their original positions in the array, only increase by a
factor of 2 at most
Name
(g) In dynamic programming, we derive a recurrence relation for the solution to one subproblem in terms of solutions to other subproblems. To turn this relation into a bottomup dynamic programming algorithm, we need an order to fill in the solution cells in a
table, such that all needed subproblems are solved before solving a subproblem. For
each of the following relations, give such a valid traversal order, or if no traversal order
is possible for the given relation, briefly justify why.
i. A(i, j) = F (A(i, j 1), A(i 1, j 1), A(i 1, j + 1))
ii. A(i, j) = F (A(min{i, j} 1, min{i, j} 1), A(max{i, j} 1, max{i, j} 1))
iii. A(i, j) = F (A(i 2, j 2), A(i + 2, j + 2))
Solution:
i. Solve A(i, j) for(i from 0 to n: for(j from 0 to n))
ii. Solve A(k, k) for(k from 0 to n) then solve rest in any order
iii. Impossible: cyclic.
Name
10
T
C
A
G
0
1
2
3
4
A
1
2
3
5
6
T
2
4
5
6
7
C
3
5
7
8
9
3 points for correctly filled grid, 2 points for correct alignment of sequences
(b) Draw a max-heap on the following set of integers: {2, 3, 5, 7, 11, 13, 17}. (You do not
need to use the Build-Heap algorithm.)
Solution: multiple possible solutions
5 points for a correct answer - (almost) everyone got this correct
Name
11
(c) Compute 3 6006 using two iterations of Newtons method, i.e., fill out the following
table. Your entry for x1 should be fully simplified. Your entry for x2 can be left
unsimplified.
i
0
xi
1
1
2
Solution:
i
0
1
2
xi
1
2002.6
2/3x1 + 2002
x2
1
Name
12
S EARCH(A, i, j, x)
first A[i]
middle A[(i + j)/2]
last A[j]
if x {first, middle, last}:
then
return the corresponding index
else :
if (x < first and x > middle) or (x > first and x > middle): [xxx Isnt it equivalent to just x > mi
then
return S EARCH(A, (i + j)/2, j, x)
else :
return S EARCH(A, i, (i + j)/2, x)
Solution II: Let k 0 be the number of elements A was rotated by. We show how to identify the
value of k, after which one can simply perform binary search in the left part A[1 . . . k] and the
right part A[k + 1 . . . n]. To this end, observe that all elements in the left part are not smaller
than A[1], while all elements in the right part are smaller than A[1]. By binary search we find the
smallest j > 1 such that A[j] is smaller than A[1] (or set j = 1 if no such j exists). We then report
k = j 1.
Name
13
Name
14
Name
15
Name
16
1. Impose a square grid onto the plane where each cell is a 2 2 square.
2. Hash each point into a bucket corresponding to the cell it belongs to.
3. If there is a bucket with 9 points in it, return YES.
4. Otherwise, for each point p, calculate distance to all points in the cell containing p as well as
the neighboring cells. Return YES if the number of points within distance 2 is 8. Clearly
the algorithm will find a clumped chicken if one exists, either in step 3 or in step 4. By using
a hash table of size O(n), we can make the above algorithm run in expected linear time.
Name
17
(a) Give an efficient algorithm to find the largest area of a hangable rectangle for the initial
order A[1], A[2], . . . , A[n] of columns.
Solution: The best algorithms we know run in O(n2 ) time.
The simplest O(n2 ) algorithm is the following. The biggest rectangle is bounded on
top by some column. Guess that column i. So the height of the rectangle is A[i]. Now
walk left until reaching a column of height < A[i], and similarly walk to the right.
Count the number k of traversed columns, and multiply by A[i].
Another O(n2 ) algorithm is the following. Define m[i, j] to be the minimum of the
interval A[i], . . . , A[j]. Then m[i, j] = min{m[i, j 1], A[j]}, so by memoization, we
can compute all m[i, j]s in O(n2 ) time. Now the solution is max{m[i, j] (j i + 1) :
i j}, which takes O(n2 ) time to compute given the m[i, j]s.
The easy brute-force algorithm already runs in O(n3 ) time (and was worth a base
value of 45 out of 8 points for this part). Just use the computation above, but without
memoizing the m[i, j]s, so each takes O(n) to compute, and we use O(n2 ) of them.
An O(nh) algorithm, where h = maxi A[i], was worth a base value of 6 out of 8
points. We define one subproblem per (x, y) coordinate: R(x, y) is the maximum
possible area of a rectangle whose upper-right corner is at (x, y). There are O(nh)
Name
such subproblems. To solve the subproblem, we can use the O(1)-time recurrence
(
R(x 1, y) + y if A[x] y
R(x, y) =
0
otherwise.
(b) Devise an efficient algorithm to permute the columns into an order that maximizes the
area of a hangable rectangle.
Solution: The intended algorithm is to sort the columns in decreasing order, e.g.,
using merge sort in O(n lg n) time. This works because, if the correct height of a
rectangle is k, then at best it can involve all columns with height k, and these are
consecutive in the sorted order. In fact, increasing order works just as well, as does a
strange order (suggested by several students) of putting the maximum in the middle,
then repeatedly placing the next smaller column alternately between the left and right
sides of the construction so far.
We can compute the hangable rectangle in O(n) additional time, though this was not
necessary to receive full credit. For each prefix B[1 i] of the sorted array, wed like
to compute (min B[1 i]) i, and take the maximum over all i. But min B[1 i] = B[i],
so this actually takes constant time per choice of i, for a total cost of O(n) time.
2 out of 7 points were removed for lack of justification of sorting. 1 out of 7 points
was removed for using counting (or radix) sort, which is not necessarily efficient given
the setup of the problem.
18
Name
19
Name
20
1
|S|
sS
A segment variance data structure supports the following operations on an array A[1 . . . n]:
Assignment: given an index i and a value a, set A[i] = a.
Segment variance: given a pair of indices i and j, compute V ({A[i], A[i + 1], . . . , A[j]}).
Initially all entries in A are set to 0.
Design a data structure that supports both operations in O(log n) time.
P
2 = 1/|S| P s2 S2 . Both terms can be maintained
Solution: We have 1/|S| sS (s S)
sS
using an augmented data structure that, in each internal node, keeps (i) the sum of all leaves and
(ii) the sum of squares of all leaves.