Longest ascending subsequence algorithm (n^2 and Nlogn) (LIS)

Source: Internet
Author: User

Problem Description:

A number of sequence bi, when B1 < B2 < ... < BS, we call this sequence ascending. For a given sequence (A1, A2, ..., an), we can get some ascending sub-sequences (AI1, AI2, ..., AiK), here 1 <= i1 < I2 < ... < IK <= N. For example, for sequences (1, 7, 3, 5, 9, 4, 8), there are some ascending sub-sequences of it, such as (1, 7), (3, 4, 8) and so on. The longest length of these subsequence sequences is 4, such as subsequence (1, 3, 5, 8).

Your task is to find the length of the longest ascending sub-sequence for a given sequence.

Problem Solving Ideas:

1.n^2

How do we break this problem down into sub-problems? After analysis, found "AK (K=1, 2, 3 ... N) The length of the longest ascending subsequence of the end point "is a good sub-problem-here is the rightmost number of a ascending subsequence, called the" end point "of that subsequence. Although this sub-problem and the original problem form is not exactly the same, but as long as the N sub-problem is solved, then the solution of the N sub-problem, the biggest one is the solution of the whole problem.

The sub-problem described above is only related to one variable, which is the position of the number. So the position K of the number in the sequence is "state", and the "value" corresponding to the state k is the length of the longest ascending subsequence with AK as the "end point". The state of the problem is a total of n. When the state is defined, the transfer equation is not difficult to think about. Suppose MaxLen (k) represents the length of the longest ascending subsequence with AK as the "end point", then:

MaxLen (1) = 1

MaxLen (k) = Max {MaxLen (i): 1<i < K and AI < AK and k≠1} + 1

This state transfer equation means that the value of MaxLen (k) is the left side of AK, the "end point" value is less than AK, and the length of the largest ascending sub-sequence is increased by 1. Because any "end point" to the left of AK is less than the sub-sequence of AK, a longer ascending sequence can be formed after the AK is added.

The actual implementation, you can not write a recursive function, because from MaxLen (1) can be calculated out of MaxLen (2), with MaxLen (1) and MaxLen (2) can calculate the MaxLen (3) ...

2.nlogn

Typical variants of the longest ascending subsequence (LIS), the n^2 of the familiar is timed out. The LIS problem can be optimized for NLOGN algorithms.
Definition D[k]: The last element of the ascending subsequence of the length k, if there are multiple ascending sub-sequences with a length of K, the smallest of the last elements is recorded.
Note that the elements in D are monotonically increasing, and this property is used below.
First len = 1,d[1] = a[1], then to A[i]: if A[i]>d[len], then len++,d[len] = A[i];
Otherwise, we will find a J in d[1] to d[len-1], according to the definition of D, we need to update the last element of the ascending sub-sequence of length J (to make it the smallest), i.e. d[j] = A[i];
The final answer is Len.
Using the monotonicity of D, when looking for J, we can find two points, thus the time complexity is NLOGN.
Nlogn algorithm for longest ascending subsequence sequence

In the Sichuan OJ encountered a problem can not be used n^2 too, all kinds of entanglements, the last acquisition of NLOGN algorithm

The longest increment subsequence, longest increasing subsequence below we précis-writers for LIS. The sort +lcs algorithm and the DP algorithm are ignored, both of which are too easy to understand.

Suppose there is a sequence d[1..9] = 2 1 5 3 6 4 8 9 7, it can be seen that the LIS length is 5. n try to figure it out in one step at a pace. We define a sequence B and then let i = 1 to 9 examine the sequence one at a. In addition, we use a variable len to record the maximum number of times now.

First, put d[1] in order B, make b[1] = 2, that is, when there are only 11 digits 2, the smallest end of the LIS with a length of 1 is 2. Then Len=1

Then, put d[2] in an orderly place in B, so that b[1] = 1, that is, the minimum length of the LIS is 1,d[1]=2 is useless, it is easy to understand it. Then Len=1

Next, d[3] = 5,d[3]>b[1], so make b[1+1]=b[2]=d[3]=5, that is, the minimum end of the LIS with a length of 2 is 5, it is easy to understand. This time b[1..2] = 1, 5,len=2

Again, d[4] = 3, it just add to 1, 5, placed in the position of 1 is obviously inappropriate, because 1 is less than 3, the minimum length of the LIS is 1, so it is easy to infer that the length of the LIS min 1 is 2, so you can eliminate 3, this time b[1..2] = 5, 3,len = 1

Continue, d[5] = 6, it is behind 3, because b[2] = 3, and 6 is behind 3, so it is easy to infer b[3] = 6, then b[1..3] = 1, 3, 6, or is it easy to understand? Len = 3, OH.

6th, D[6] = 4, you see it between 3 and 6, so we can replace 6, get b[3] = 4. B[1..3] = 1, 3, 4, Len continues to be equal to 3

7th one, d[7] = 8, it's big, bigger than 4, uh. So b[4] = 8. Len becomes 4.

8th, D[8] = 9, get b[5] = 9, uh. Len continues to grow, to 5.

The last one, d[9] = 7, which is between b[3] = 4 and b[4] = 8, so we know that the latest b[4] =7,b[1..5] = 1, 3, 4, 7, 9,len = 5.

So we know the length of the LIS is 5.

!!!!! Attention. This 1,3,4,7,9 is not the LIS, it just stores the corresponding length to the minimum end of the LIS. With this at the end, we can insert data one at a-one place. Although the last d[9] = 7 update is not meaningful for this set of data, but if there are two numbers 8 and 9, then you can update 8 to d[5], 9 update to d[6], the length of the LIS is 6.

Then you should find one thing: inserting data in B is ordered and is replaced without moving--that is, we can use a binary search to optimize the insertion time of each number to O (logn) ~~~~~ The time complexity of the algorithm is reduced to O (NLOGN)!

Longest ascending subsequence algorithm (n^2 and Nlogn) (LIS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.