I got this question today and I'm nowhere near the solution, Given a sequence of real numbers (X1, X2, ..,Xn). write an algorithm as efficient there is, that finds the number of strictly increasing sub-sequences for every index j, that end with Xj.

My solution should include a recurrence formula that solves this problem in O(n^2) and a correctness proof, I was only able to solve it using a nested for loop and I'm not sure if there's an O(n^2) recursion solution.

List a[1…n] <- [1…1]
 For j= 1 to n
    For i= 1 to j-1
       If xi<xj then
          a[i]= a[j]+a[i]; 
  • 1
    $\begingroup$ Welcome to Computer Science Stack Exchange. This site is intended as cooperative. Since you seem to have worked on i, could you tell us what you tried, what you do not understand, what is blocking you. It is important for you to try on your own, and for us to answer the specific issues that may be a problem. Don't you feel that the second sentence is a bit too imperative for a request? $\endgroup$ – babou Jul 2 '15 at 21:45
  • $\begingroup$ your absolutely right! edited my post and added my solution. $\endgroup$ – TCP_Explorer Jul 3 '15 at 6:15
  • 1
    $\begingroup$ So your question is only whether it is $O(n^2)$. Well, this double loop is a pretty standard case for it, and you should find lots of examples.with precislely the same kind:: for J=1 to n for i= 1 to j-1 making a total of $(n-1)n/2$ steps (the sum of the first $n-1$ integers). Check your notes or examples. --- Regarding your solution, do you have at least an explanation for it hat could be the start of a proof? $\endgroup$ – babou Jul 3 '15 at 6:49
  • $\begingroup$ No, I'm not asking whether it is O(n^2), I'm trying to solve this problem using a dynamic programming style (recursion/memoization). $\endgroup$ – TCP_Explorer Jul 3 '15 at 8:36
  • 2
    $\begingroup$ Well, we try our best to understand what your specific problem is. But you have to state clearly what the question is, and have that in the question, not i a comment. So I guess you want a recursive version of what you wrote. --- BW I think you mixed up i and j in the last line of code. $\endgroup$ – babou Jul 3 '15 at 9:08

The recursive equivalent of what you wrote is:

function S(j) is
  r <- 1
  For i <- 1 to j-1
    if xi<xj then
      r <- r+S(i)
  return r

It is very similar to what you wrote, except that a[1] is replaced by S(i), i.e., a recursive call to the function, since it is the function that is supposed to compute the number of sequences that you store in a[i]. The array a is used to remember previous results, and the new result you are computing is obtained using the previous results.

This array is no longer needed, as it is replaced by the memoization of the dynamic programming interpretation of the recursive function. If I understood correctly what you are asking.

Now, to do the proof, you should explain why computing in this way is correct.

This works with the same costs as your algorithm, provided the recursion is interpreted with dynamic programming memomization, and you should be able to prove it easily.

Of course standard recursive execution of the function, without memoization of results, is a lot more costly.

| cite | improve this answer | |
  • $\begingroup$ Thanks @babou, I'm trying to think on how the recurrence formula looks for this as there is no stop condition in the recursion. $\endgroup$ – TCP_Explorer Jul 3 '15 at 10:47
  • $\begingroup$ @TCP_Explorer Actually, there is a stop condition, but it is not very visible. You want the recursion to stop when j==1. So you should check what actually happens when j==1, you will see there is no recursive call. But you are right: it is always wise to check for the stopping condition, as well as for something that decreases to some lower bound at each recursive call. $\endgroup$ – babou Jul 3 '15 at 11:07
  • $\begingroup$ @TCP_Explorer Did you find how it stops? --- BTW you know you can upvote and possibly accept answers when they are interesting or useful to you (see cs.stackexchange.com/tour), or downvote when an answer is wrong. It is the local way of showing appreciation for the work of others. $\endgroup$ – babou Jul 4 '15 at 8:13
  • $\begingroup$ Thanks! I did understand how it works, except it doesn't use memoization and I'm not sure about the recurrence formula. Thanks! $\endgroup$ – TCP_Explorer Jul 5 '15 at 12:19
  • $\begingroup$ Notice you do not use another array to store the values you recursively get, so that part is missing for the "dynamic planning" part $\endgroup$ – TCP_Explorer Jul 5 '15 at 12:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.