You are on page 1of 3

19103307 Vaibhav Sharma B9

Dynamic Programming
Week 13
SOLUTIONS

1. Let’s consider two sequences, X and Y of length m and n, respectively, that both end in the
same element.
To find their SCS, shorten each sequence by removing the last element, find the SCS of the
shortened sequences, and that SCS append the removed element. So, we can say that.

CS(X[1…m], Y[1…n]) = SCS(X[1…m-1], Y[1…n-1]) + X[m] if X[m] = Y[n]

Now suppose that the two sequences do not end in the same element.
In this case SCS will be shorter of the SCS(X[1…m-1], Y[1…n]) + X[m] and SCS(X[1…m],
Y[1…n-1]) + Y[n].

Code:
#include <iostream>
#include <string>
using namespace std;

// Function to find the length of the shortest common supersequence of


// sequences `X[0…m-1]` and `Y[0…n-1]`
int SCSLength(string X, string Y, int m, int n)
{
// if the end of either sequence is reached, return
// the length of another sequence
if (m == 0 || n == 0) {
return n + m;
}

// if the last character of `X` and `Y` matches


if (X[m - 1] == Y[n - 1]) {
return SCSLength(X, Y, m - 1, n - 1) + 1;
}
else {
// otherwise, if the last character of `X` and `Y` don't match
return min(SCSLength(X, Y, m, n - 1) + 1,
SCSLength(X, Y, m - 1, n) + 1);
}
}

int main()
{
string X = "ABCBDAB", Y = "BDCABA";
int m = X.length(), n = Y.length();

cout << "The length of the shortest common supersequence is "


<< SCSLength(X, Y, m, n);

return 0;
}

Output:

The length of the shortest common supersequence is 9


The worst-case time complexity of the above solution is O(2(m+n)) and occupies space in the call
stack where

2. You are given an exam with questions numbered 1, 2, 3, . . . , n. Each question i is worth pi
points. You must answer the questions in order, but you may choose to skip some questions. The
reason you might choose to do this is that even though you can solve any individual question i
and obtain the pi points, some questions are so frustrating that after solving them you will be
unable to solve any of the following fi questions. Suppose that you are given the pi and fi values
for all the questions as input. Devise the most efficient algorithm you can for choosing set of
questions to answer that maximizes your total points, and compute its asymptotic worstcase
running time as a function of n.

Solution:
Let S(i) be the maximum total score that can be obtained from questions i through n. Any such
score is obtained from a set of questions that either includes i or not; in the first case, the best
score is pi +S(i+fi + 1), and in the second case, the best score is S(i + 1). The following loop
calculates the best possible total score, given a large array S with all entries initialized to 0:
for i = n downto 1: S[i] = max(p[i] + s[i+f[i]+1], s[i+1]) return S[1] The running time is easily
seen to be O(n) (possibly

3.a. We are given an integer array A[0..n) and we have to find the segment A[i..j) such that the
sum of the integers A[i] + A[i + 1] + · · · + A[j − 1] is maximal. We simplify this slightly
by only requesting the maximal sum itself, but not the bounds of the segment. The problem is
not trivial because we can negative integers in the array. Write a Dynamic Programming
solution for this problem.
b. What is the asymptotic complexity of max_seg_sum as a function of the n, the length of the
array A?

Ans. a
.
b. O(n3) because there are three nested loops of order n, where the innermost loop body takes
constant time.
Alternatively, we can say that the code checks every segment. There are n left endpoints,
on the average n/2 right endpoints for each, and, on the average, n/2 elements in each segment
to sum. So we obtain O(n3) in total.

4. Explain what would happen if a dynamic programming algorithm is designed to solve a


problem that does not have overlapping sub-problems.

Ans. Then it will be just a waste of memory, because the answers of sub-problems will never be
used again. And the running time will be the same as using D&C algorithm.

You might also like