0

I have this code for Longest Increasing Subsequence. Right now it returns length of the longest increasing subsequence, but I don't know how to make it return this exact subsequence. For ex. in this case it should return [3,6,7,8,9]. Any ideas? I would appreciate not using very complicated syntax :D

a = [3, 6, 7, 2, 1, 8, 9, 5]
n = len(a)
q = [0] * n
for k in range(n):
    max = 0
    for j in range(k):
        if a[k] > a[j]:
            if q[j] > max:
                max = q[j]
    q[k] = max + 1
return(max(q))

outer loop iterates after all elements from a and inner loop checks if element k from table is greater than item from index 0 to k-1 Thanks to that q table for this example looks like this [1,2,3,1,1,4,5,2] We can see that first element makes subsequence of length 1, second one makes subsequence of length 2 (with first element), third element makes subsequence of length 3 (with first and second elements), fourth element makes subsequence of length 1 because non of previous elements is smaller than it, and so on. So basically at each iteration it gets the length of the longest increasing subsequence ending at index k

Shorter version of the same program:

for i in range(n):
        for j in range(i):
            if a[i] > a[j]:
                q[i] = max(q[i], 1 + q[j])
lolos
  • 37
  • 1
  • 5

1 Answers1

1

It would have been great if you had described what your code was doing but as I understand at each iteration it gets the length of the longest increasing subsequence ending at index k.

To trace back the actual indices in the array, just add another array previous[].

Initialize previous = [-1] * n.

And then

if q[j] > max: 
   max = q[j]
   previous[k] = j
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • This is not right, adding this results [-1, 0, 1, -1, -1, 2, 5, 0] in the outcome, not [3,6,7,8,9]. – lolos May 12 '20 at 16:57
  • 1
    @lolos This is list of **indexes**, you need to rewind it in order 6-5-2-1-0 and reverse – MBo May 12 '20 at 17:52
  • @MBo It would be true if instead of last 0 was 6, so something is not right. It always fails for last element in subsequence – lolos May 12 '20 at 18:15
  • Or should I do something else, to get 6-5-2-1-0 order? – lolos May 12 '20 at 19:22
  • How from this I can find those elements? – lolos May 12 '20 at 20:27
  • @lolos From an index `i` you can construct the longest sequence ending at `i` by walking backwards. So in the array for `i = 7` you see a `0` and at `0` you see `-1` so the longest rising sequence ending at position `7` has indexes `7, 0, -1` or in other words is `5, 3`. Now just try each `i` until you find one of the right length... – btilly May 12 '20 at 23:50
  • We know that there is an answer of length 5, and it didn't end at index `7`. So try index `6`. We get the following list of indexes following the cookie trail. `6, 5, 2, 1, 0, -1`. That turns into the list `9, 8, 7, 6, 3` which has the right length so is the answer. Unfortunately we got it constructed in reverse, so you need to reverse it to get your final answer of `3, 6, 7, 8, 9`. – btilly May 12 '20 at 23:52
  • @lolos You have to look for index of max value in q (instead of inbuilt `max`) - here this index is 6, then walk from `previous[6]` to lower indexes of sequence. – MBo May 13 '20 at 06:09