Professional Documents
Culture Documents
ADA: Midsem
(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
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.