You are on page 1of 5

Winter 2015

Feb 26, 2015

ADA: Midsem

Max: 100 points


120 minutes

For each of the following, choose the correct answer.


1. (10 points) Given three algorithms:
Algorithm A: Partitions a problem of size n into 3 problems of size
n/2 and combines them in time O(n).
Algorithm B: Partitions a problem of size n into 8 problems of size
n/2 and combines them in time O(n/ log n).
Algorithm C: Partitions a problem of size n into 5 problems of size
n/5 and combines them in time O(n log n).
The asymptotic running times of the algorithms in increasing order are:
(a) A, B, C
(b) A, C, B
(c) B, A, C
(d) B, C, A
(e) C, A, B
(f) C, B, A
(g) All have the same asymptotic running time.
Solution: Correct answer is option (b) A, C, B .
For Algorithm A complexity is O(n1.5 ), for Algorithm B complexity is
O(n3 ) and for Algorithm C complexity is O(n2 log n)
2. (10 points) A friend of yours has written a super-fast algorithm that,
given an array of n numbers, finds a pseudo-median, i.e., a number x in
n
the array such x is larger than at least 20
of the numbers, and smaller
n
than at least 20 numbers. We want to use this pseudo-median as the pivot
in our implementation of quick-sort. What is the worst-case running time
of your quick-sort algorithm?
(a) O(n)
(b) O(n11/10 )
(c) O(n log n)
(d) O(n2 )
(e) O(n10/11 log n)
(f) None of the above.

Solution: Correct answer is option (c) O(n log n).


For given algorithm pivot divides some fraction of elements on one side
and remaining on others i.e. in ratio of 1:19.
T(n) = T(n/20) + T(19n/20) + O(n) + O(n).
T(n) = O(n log n).
3. (10 points) Given a set of n students and m projects, we want to assign a
project to each student. Each student only prefers a subset of the projects,
and we are given for each student, the subset of projects she prefers.
Consider the following algorithm that assigns the maximum number of
students to projects such that each student is assigned a single project,
and each project is assigned to at most one student: As long as there is
an unassigned project for a student who prefers this project, we assign this
student to this project and repeat on the remaining students and projects.
Which of the following is true.
(a) The algorithm maximizes the number of students assigned to projects.
(b) The algorithm may produce an allocation where fewer than the maximum number of possible students are assigned.
(c) The algorithm is optimal. However, if we consider the weighted setting where a student assigns a weight to a project (corresponding to
how much she likes the project), and we now want to maximize the
weight of this
P assignment: i.e., if M are the pairs chosen, we want to
maximize {s,p}M w({s, p}) then the algorithm fails to produce an
optimal solution.
(d) If we modify the greedy algorithm where at each step we pick the
student-project pair of maximum weight, the algorithm produces an
optimal solution.
Solution: T
he correct option is only (b). Take an example of a path of 3 edges. With
weights say 2, 3, 2 for edges e1 , e2 , e3 . The greedy algorithm in option (d)
picks edge e2 , but the optimal solution is to pick e1 and e3 . The same
graph also serves as a counter-example for option (c). The same also serves
as a counter-example for option (a) with no weights on the edges. The
algorithm may pick the edge e2 but the optimal solution may pick e1 , e3 .
4. (10 points) Given an array A of n numbers, we want to find a subsequence of A of longest length that is monotonically non-decreasing. Let
M [i] denote the longest such subsequence that that has A[i] as the last
element. Then, the recurrence for the maximum possible length is given
by:
Solution:
Option a is the answer.
Lets try to eliminate the options. Option b cannot be the answer because

of A[k]>A[n], since we are looking for monotonically increasing subsequence.


Option c do not check for monotonically increasing condition of A[k]<A[n]
And option d have wrong indexing of M[n] instead of M[k].

(a)


1
n=1
1 + max1k<n,A[k]<A[n] M [k] otherwise

1
n=1
1 + max1k<n,A[k]>A[n] M [k] otherwise

M [n] =
(b)
M [n] =
(c)


M [n] =

1
n=1
1 + max1k<n M [k] otherwise

(d)

M [n] =

1
n=1
1 + max1k<n,A[k]<A[n] M [n] otherwise

(e) None of the above.


Solution:
Option a is the answer.
Lets try to eliminate the options. Option b cannot be the answer because
of A[k]>A[n], since we are looking for monotonically increasing subsequence.
Option c do not check for monotonically increasing condition of A[k]<A[n]
And option d have wrong indexing of M[n] instead of M[k].
5. (10 points) Fill each entry in the table below with Y or N.
f
log(n!)
nk
2n
2n+1
n1/3

g
log(nn )
cn
2n/2
2n
nsinn

O
Y
Y
N
Y
N

o
N
N
N
N
N

Y
N
Y
Y
N

N
N
Y
N
N

Y
N
N
Y
N

Note: For each of the following questions, if you do not know how to
answer the question write the following line:
I do not know how to answer this question. for 10% credit.

6. (10 points)
The following is a sequence of matrix dimensions: 4 5, 5 2, 2 6, 6 3.
Write the entries in the dynamic programming table to compute their
product using the fewest number of multiplications.

7. (20 points) For any tree T on n nodes, show that there is a node v such
that T \ v partitions into two disjoint sets, each with at most 2n/3 nodes.
Give a greedy algorithm to find such a node (in fact the algorithm will
lead to a proof that such a node always exists).
Solution:
The algorithm is as follows. We start at an arbitrary vertex, say v. Let
Tv1 , . . . , Tvk be the sub-trees in T \v. If each Tvi has at most 2n/3 vertices,
then v is the desired separator. Why? Create a set A packing sets Tv1 , Tv2
and so on until the set A has at least n/3 nodes. The remaining elements
constitute the set B, and neither set has size more than 2n/3.
The only reason v fails to be a separator is if one of the sub-trees, say Tv1
contains > 2n/3 nodes. Let u be the node in Tv1 adjacent to v. Then,

pick u as the candidate separator. This decreases the size of the largest
set, and therefore this algorithm is guaranteed to find such a separator.
8. (20 points) Given a string S of length n with alphabet , design an
algorithm that computes a palindromic subsequence of longest length. A
palindromic subsequence is a subsequence that reads the same left-to-right
or right-to-left. For example: If the string is: abcda your output is aba, or
aca, or ada, all of length 3.
(a) Define the sub-problems and state the number of sub-problems.
For each i, j {1, . . . , n}, i < j our sub-problem is the longest palindromic sequence in the string A[i . . . , j].
(b) Prove sub-problem optimality: The proof follows the standard cutand-paste argument. We claim OP T |[i, . . . , j] = OP T [i, . . . , j]. Suppose the length of the longest palindromic sequence in A[i . . . j] is
larger than that induced by an optimal solution, namely OP T |[i, . . . , j].
Then, replacing OP T |[i, . . . , j] by OP T [i, . . . , j] is a palindromic sequence, and therefore results in a palindromic sequence of longer
length, a contradiction.
(c) The recurrence relation satisfied by OP T is: Let OP T (i, j) denote
the length of the longest palindromic sequence in A[i, . . . , j].
(
2 + OP T (i + 1, j 1) + a[j], if a[i] = a[j]
OP T (i, j) = max
max{OP T (i + 1, j), OP T (i, j 1)}, otherwise
(1)
(d) Give a dynamic programming algorithm:
Create an n n matrix M , where M [i, j] stores the longest palindromic sequence in the sub-string A[i, . . . , j]. We build it bottom-up,
where M [i, i] = 1 for each i = 1, . . . , n, Compute

2 + M [i 1, j 1], A[i] = A[j],
M [i, j] = max
max{M [i 1, j], M [i, j 1]}, A[i] 6= A[j]
(e) Running time: The algorithm runs in O(n2 ) time. There are n2 subproblems, and for each it takes O(1) time to compute the solution.

You might also like