Question

Given an array A[1..n] representing a sequence of n integers, a subsequence is a subset of...

Given an array A[1..n] representing a sequence of n integers, a subsequence is a subset of elements of A, in the same order as they appear in A. A subsequence is monotonic if it is a sequence of strictly increasing numbers. Define LMS(i) to be the length of a longest monotonically increasing subsequence of A[1..i] that must have A[i] as its last element. Write a recurrence for LMS(i) and convert into a dynamic program that calculates LMS(i) for i=1..n.

This should be in pseudocode.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us first discuss, whehter this problem is of dynamic programming or not. And to verify that we need to have optimal substructure
and overlapping subproblems in our quesiton. In given problem one can easily notice the optimal substructure.
Let us say length[0...n-1] be the array and length[i] indicates the length of LIS ending at that index. Then you may have noticed that
we can easily calculate this array in recursive fashion as indicated below -
length[i]=1+max(length[j])     {where 0<j<i and A[j]<A[i], A[0...n-1] is the input array}
length[i]=1                    {when no such j exist}

So solving it recusrsively is quite easy task.
And while solving this sharp eyed reader has easily pointed out the overlapping subproblems. For every i we will be using the answers of every
j smaller then that i. So what we will do is nothing but just store the anser for every j in the array and then we can use it later
whenever it is required. Now, let us see the pseducode below.

A[0...n-1] is the input array
length[0...n-1] is the dp array that stores the length of LIS ending at each index.

//declare the length array and initialize the first index as 1
length[0]=1

//calculating the length array
for i = 1 to n
//initialize each length[i] as 1.
length[i]=1
for j = 0 to i
//checking whether current A[j] is worth considering or not for given i
if A[i]>A[j] and length[i]<length[j]+1
length[i]=length[j]+1

and finally this length array has LIS ending at each index.
This solution is definitely O(n^2). We can very well solve it in O(n*logn) time but that method we can not utilize here because we want to
find LIS ending at each array element and that method will give us LIS for entire array.

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUIMBS UP

Add a comment
Know the answer?
Add Answer to:
Given an array A[1..n] representing a sequence of n integers, a subsequence is a subset of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT