2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the answer returned in the exercise b is the length of the longest rising trend starting from P[1], so think "backward" beginning from P[1]
Your friends have been studying the closing prices of tech stocks, looking for interesting patterns They've defined something called a rising trend, as follows They have the closing price for a given stock recorded for n days in succession; let these prices be denoted P[1], P[2],..,PIn].Arising trend in these prices is a subsequence of the prices P[i1], P[i2],... ,P[ik], for days i12<... .i11, and ·Pfijjj+1 ] for each j = 1,2, k-1. Thus a rising trend is a subsequence of the days-beginning on the first day and not necessarily contiguous-so that the price strictly increases over the days in this subsequence. They are interested in finding the longest rising trend in a given sequence of prices Example. Suppose n = 7, and the sequence of prices is 10,1,2,11,3,4,12 Then the longest rising trend is given by the prices on days 1, 4, and 7. Note that days 2, 3, 5, and 6 consist of increasing prices; but because this subsequence does not begin on day 1, it does not fit the definition of a rising trend (a) Show that the following algorithm does not correctly return the length of the longest rising trend, by giving an instance on which it fails to return the correct answer. Define1 For j-2 to n If Pl> Pl] then Set i-j Add 1 to L Endif Endf or In your example, give the actual length of the longest rising trend, and say what the algorithm above returns (b) Give an efficient algorithm that takes a sequence of prices P[1], P[2],PIn] and returns the length of the longest rising trend