73.6k views
3 votes
A) Given a sequence of n real numbers A(1) ... A(n), give a dynamic-programming solution that determines a subsequence (not necessarily contiguous) of maximum length in which the values in the subsequence form a strictly increasing sequence.

b) What is the time complexity of the algorithm? You must justify your answer..
c) Apply your algorithm to the following sequence of 6 numbers: A={3, -2, 1, 4, 7, 1}.

User AlloVince
by
8.5k points

1 Answer

4 votes

Final answer:

The dynamic programming solution involves creating an array to track the longest increasing subsequence ending at each element, updating it through comparison with all previous elements, and has a time complexity of O(n^2). Applying this to the sequence A={3, -2, 1, 4, 7, 1}, the longest increasing subsequence is four elements long.

Step-by-step explanation:

The question involves designing a dynamic-programming solution for finding the longest increasing subsequence in a given sequence of real numbers. The dynamic programming approach involves determining the maximum length of an increasing subsequence ending at each element in the sequence and using this information to build up the overall solution.

Dynamic Programming Solution

  1. Create an array dp[] of length n, where n is the number of elements in sequence A. Initialize each element of dp[] to 1. The dp[i] will store the length of the longest increasing subsequence ending at the ith element.
  2. For each element A[i], compare it with all previous elements A[j] where j < i. If A[i] > A[j], update dp[i] to max(dp[i], dp[j] + 1).
  3. The length of the longest increasing subsequence will be the maximum value in dp[].

Time Complexity

The time complexity of this algorithm is O(n^2) because there are two nested loops: one to go through each element of the array and another to compare each element with the previous ones.

Applying the Algorithm to A={3, -2, 1, 4, 7, 1}

  1. Initial dp array: [1, 1, 1, 1, 1, 1]
  2. After processing each element, dp array becomes: [1, 1, 2, 3, 4, 2]
  3. The longest increasing subsequence has a length of 4, corresponding to the subsequence [-2, 1, 4, 7].

User Diego Carrera
by
7.4k points