Question

Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to solve this problem. Answer the following questions. a) What is the optimal substructure of this problem? b) Show the pseudo code of your solution and its complexity in terms of asymptotic analysis c) Use two examples to demonstrate how your approach works. In cases where there are multiple longest sub-arrays, you just need to show one of them d) How can we use the table generated by the dynamic programming algorithm to tell whether there is more than one optimal solution?

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

Hello Student!

I am happy to help you!

If you look closely, the following question seems to be longest Increasing Subsequence(LIS).

a) Optimal Substructure : A problem can be described to be in optimal substructure property if optimal solution solution of the given problem can easily be attained by using optimal solution of the subproblem.

           lis(3)
         /     |     
      lis(2)  lis(1)   
       /           
   lis(1) 

Now, In this problem what will be the subproblems, which can give the optimal solution?

Given an array, if we are able to know that at particular point the longest increasing subarray is of length 'x' then we can utilise this concept and formulate further solutions. And how can we do it?

For ex : A1, A2, A3, A4, A5, A6, A7 are the elements of an array.

Now, at A4 it is given that the longest increasing subarray is size of 'x' where x is less than 4. Now at A5, if A5 is greater than A4 then we can say the new longest increasing subsequence length till x can be x+1 because A5 > A4.

So, that's how we can utilise the concept of optimal substrucre property.

b) Pseudo code :

// Function for LIS

int LIS (int arr[], int n)

int dp[n];

for i = 0 to n

dp[i] = 1 // because a single element can be described as increasing because it is neither greater than // anyone nor it is smaller than anyone

end

for i = 0 to n

for j = 0 ; j < i

if dp[i] < dp[j]+1 && arr[i] < arr[j]

dp[i] = dp[j]+1

end

int ans = INT_MIN;

for i = 0 to n

ans = max ( ans , dp[i]);

return ans;

Time complexity of the following algorithm will comes out to be - O(n2)

c) Test Case Example :

Input : 5,1,6,7,2,8,3,4

arr = 5 1 6 7 2 8 3 4

dp =1 1 1 1 1 1 1 1 // Initializing it by 1

Now, if we apply the condn for all i and j where dp[i] < dp[j]+1 && arr[i] < arr[j]

then, dp table comes out to be-

dp =1 1 2 3 2 4 3 4

Now, there exists two optimal answer both of them of size 4. The subsequence are - {1, 2, 3, 4} and {5, 6, 7, 8}

It will give the length 4 as the output for {1, 2, 3, 4} because it find max length where max > dp[i]

Another Example :

arr = 1 3 4 9 2 3 4 10 6 11

dp = 1 1 1 1 1 1 1 1 1 1

Now, if we apply the condn for all i and j where dp[i] < dp[j]+1 && arr[i] < arr[j]

then, dp table comes out to be-

dp = 1 2 3 4 2 3 4 5 5 6

In this case, the maximum length is 6. But, there could be two possibilites of subsequence. How?

{1, 3, 4, 9, 10, 11} and {1, 2, 3, 4, 10, 11}

Both are of size and both have size of 6. Now, our algorithm only finds the longest length of the subsequence it doesn't matters which sequence it is. So, it will print 6.

d) How can we use table to generate multiple solution

Both the cases given above had two optimal solution. But, both had different table structure. Now, how would we able to find whether there are multiple solutions to the problem or not.

If we observe closely.

Our dp table came out to be this -

dp =1 1 2 3 2 4 3 4

For case 1, now we know 4 (at position i) is the most optimal answer. We will find 3(at position j) in the left side of the array such that arr[i] > arr[j]. Similarly we will recursively computer for 3 to 2 and then 2 to 1. We will multiply the number of cases that exists.

For, first 4 we will go for the first encounterd 3 because 4 > 3 (these are values in array) and there is only one three which exists, so current solution will be (1x1..). Now for 2, we will look into the left side again, there exists only 1 case.. (1x1x1..) Now, for 1 there exists single case. So (1x1x1x1).

Now, for other value of 4. If we perform similary it comes out to be (1x1x1x1).

Adding both 1+1 = 2

Two different subsequences.

Thank you. Feel free to ask anything. Please upvote, if you like the answer. Thank you again.

Add a comment
Know the answer?
Add Answer to:
Suppose you are given an array of n integers ai, az, ..., an. You need to...
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
  • Suppose you are given an array of n integers a1, a2, ..., an. You need to...

    Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...

  • Let S be a sequence of n distinct integers stored in an array as array elements...

    Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...

  • Suppose you are given n ropes of different lengths, you need to connect these ropes into...

    Suppose you are given n ropes of different lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. You want to find the minimum cost way to do this. For example: Suppose you have three ropes with lengths 2, 5, and 8. If you chose first to connect the length 5 and 8 ropes, then connect the length 2 and 13 ropes, the total cost would be...

  • You are given an array A of integers in sorted order. However, you do not know...

    You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...

  • Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n)...

    Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n) has a starting time S[i] and a finish time F[i] such that 0 < S[i] < F[i]. Two activities ai and aj (1 ≤ i, j ≤ n) are compatible if intervals [S[i], F[i]) and [S[j], F[j]) do not overlap. We assume the activities have been sorted such that S [1] ≤ S [2] ≤ …≤ S[n]. (a) Design an O(n2) dynamic programming algorithm...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in in...

    You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in increasing order in O(n) time. Note: The number of integers in the array can be different for same value of n - for example the array with 2 integers 2468 and 12345 has 9 digits as well as the array with 5 integers...

  • (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different se...

    (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different search engines and presents an intersection of the two search results. Here is a simplified version of this problem: Given two sorted integer arrays of lengths m and n, return a new array with elements that are present in both input arrays. The input array may contain duplicates, but there should be no duplicates in the output array. For example, if...

  • QUESTION 3 (10 Marks) Suppose you are given an array A[0..n 1 of integers, each of...

    QUESTION 3 (10 Marks) Suppose you are given an array A[0..n 1 of integers, each of which may be positive, negative, or zero. A contiguous subarray A|i..j] which includes all the items starting at array index i and ending at array index j is called a positive interval if the sum of its entries is at least zero. For example let A-{-1, 3,-5, 2, 0,-4, 3,-6,-2). Ten A[3, 6) is a positive interval since its sum is 2+0+(-4)+3-1, but Al4,7isnot...

  • You will be given an array of N elements sorted small to large. For the X...

    You will be given an array of N elements sorted small to large. For the X and Y values ​​that satisfy the X ≤ Y condition, draw the flow diagram of the algorithm that finds the start and end addresses of the region with the numbers greater than X and less than Y with the divide and manage approach and with O (logN) complexity. Also code the algorithm in c language. (not c++) Example: A[0…8] array 3, 5, 7, 9,...

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
ADVERTISEMENT