Sub-sequence with the longest increase (no decrease) (detailed, conversion)

Source: Internet
Author: User

Lis (longest increasing subsequence) has the longest ascending (not falling) subsequence. There are two kinds of algorithm complexity: O (N * logn) and O (N ^ 2 ). In the above algorithm, if you use the simple sequence to search in d1 .. dlen search, because a total of O (n) elements need to be calculated, the complexity of each calculation is O (n), the time complexity of the entire algorithm is O (n ^ 2 ), there is no progress compared with the original algorithm. However, due to the characteristics of D (2), binary search can be used to efficiently complete the search in D, and the time complexity of the entire algorithm is reduced to O (nlogn ), it has been significantly improved. Note that after the algorithm is completed, D does not record the longest ascending subsequence that matches the question! The algorithm can also be extended to the entire topic series.
There are two algorithms: O (N * logn) and O (N ^ 2)
O (N ^ 2) algorithm analysis is as follows:
(A [1]... A [n] stores all input values)
1. For a [n], because it is the last number, when searching from a [n], there is only a child sequence with a length of 1 and no descent;
2. If you start searching from a [n-1], there are two possibilities:
(1) If a [n-1] <A [n], there is a child sequence a [n-1], a [n] with a length of 2.
(2) If a [n-1]> A [n], there is a child sequence a [n-1] or a [n] with a length of 1.
3. Generally, if the subsequence starts from a [T], the maximum length of the subsequence is not decreased at this time, which should be obtained using the following method:
In a [t + 1], a [T + 2],... in a [n], find a child sequence that is larger than a [T] and is the longest and does not drop as its successor.
4. Define an array for algorithm needs:
D: array [1. N, 1. 3] of integer;
D [T, 1] indicates a [T]
D [T, 2] indicates the maximum length of the subsequence from the I position to n.
D [T, 3] indicates the next position of the subsequence starting from position I.
O (N * logn) algorithm with the longest subsequence not dropped
First, review the classical O (N ^ 2) dynamic planning algorithm, and set a [T] to represent the number of T in the sequence, f [T] indicates the length of the longest ascending subsequence ending with T from 1 to T. It is set to f [T] = 0 (T = 1, 2 ,..., len ()). Then there is a dynamic planning equation: F [T] = max {1, F [J] + 1} (j = 1, 2 ,..., t-1, and a [J] <A [T]).
Now, we carefully consider the situation when calculating f [T. Assume that two elements a [X] and a [y] Meet
(1) x <Y <t (2) A [x] <A [y] <A [T] (3) f [x] = f [y]
Select f [X] and select f [y] to obtain the same f [T] value. Then, in the position of the longest ascending subsequence, should I select a [x] or a [y?
Obviously, selecting a [x] is better than selecting a [Y. Because of the condition (2), in a [x + 1]... in the section A [T-1], if a [Z], a [x] <A [Z] <A [Y, A longer ascending subsequence is obtained.
Then, based on the condition (3), we will get an inspiration: classification based on the value of F. For each value K of F [], we only need to retain the minimum values of all a [T] that satisfy f [T] = K. Set d [k] to record this value, that is, d [k] = min {A [T]} (F [T] = K ).
Note the two features of d:
(1) The value of d [k] does not increase monotonically throughout the calculation process.
(2) The values of d [] are ordered, that is, d [1] <D [2] <D [3] <... <D [N].
Using d [], we can obtain another method to calculate the longest ascending subsequence length. Set the maximum length of the obtained ascending subsequence to Len. First, judge a [T] And d [Len]. If a [T]> d [Len], a [T] is connected to d [Len] and a longer ascending subsequence is obtained. Len = Len + 1, d [Len] = A [T]; otherwise, in D [1] .. in D [Len], find the largest J, satisfying d [J] <A [T]. If K = J + 1, then d [J] <A [T] <= d [K], after a [T] is connected to d [J], a longer ascending subsequence is obtained, and D [k] = A [T] is updated. Finally, Len is the length of the requested longest ascending subsequence.
In the above algorithm, if you use the simple sequence to search for the data in the d [1] .. d [Len] search, because a total of O (n) elements need to be calculated, the complexity of each calculation is O (n ), the time complexity of the entire algorithm is O (n ^ 2), which is not improved compared with the original algorithm. However, due to the features of d [] (2), we can use binary lookup to efficiently complete the search in D, the time complexity of the entire algorithm is reduced to O (nlogn), which is greatly improved. Note that after the algorithm is completed, d [] does not record the longest ascending subsequence that matches the question!
This algorithm can also be extended to the entire eldest generation sequence. The difficulty of the entire algorithm lies in the design of binary search, which requires careful attention.

 

Introduction 2:

The longest rising subsequence Lis algorithm implements the longest rising subsequence. It is a common question type in various informatics competitions and is often used as an example to introduce dynamic planning algorithms, next, I will summarize the issues that have occurred on poj and introduce two common algorithms (N ^ 2) and (nlogn) for solving the LIS problem ).

Problem description: a sequence of A1, A2, A3, A4, A5, A6, a7 .... an, calculate a subsequence of it (set to S1, S2 ,... SN), so that this sub-sequence meets this nature, S1 <S2 <S3 <... <Sn and the length of this subsequence is the longest. Output the longest length. (In order to simplify this type of problem, we regard the longest descent sub-sequence and the longest non-ascending sub-sequence as the same problem. In fact, we will find that after careful consideration, this is actually just a problem in the definition of the symbol, and does not affect the essence of the problem)

For example, there is a sequence: 1 7 3 5 9 4 8, and its longest ascending subsequence is 1 3 4 8 with a length of 4.

Algorithm 1 (n ^ 2): We traverse the entire sequence in sequence, and find the longest ascending subsequence from the first number to the current number each time until the last number is traversed, then, the largest in the DP array is the longest ascending subsequence of the entire sequence. We use DP [I] to store the length of the longest ascending sub-sequence of sequence 1-I, so DP [I] = max (DP [J]) + 1, (J, [1, i-1]); apparently DP [1] = 1, we can traverse the following elements from I = 2.

The following is a template:

1 // longest ascending subsequence (N ^ 2) template 2 // entry parameter: 1. array name 2. array length (starting from position 1) 3 Template <class T> 4 int Lis (t a [], int N) 5 {6 int I, J; 7 int ans = 1; 8 int m = 0; 9 int * dp = new int [n + 1]; 10 DP [1] = 1; 11 for (I = 2; I <= N; I ++) 12 {13 m = 0; 14 for (j = 1; j <I; j ++) 15 {16 if (DP [J]> M & A [J] <A [I]) 17 m = DP [J]; 18} 19 DP [I] = m + 1; 20 if (DP [I]> ans) 21 ans = DP [I]; 22} 23 return ans; 24}
View code

 

Algorithm 2 (nlogn): maintains a one-dimensional array C, and the array is dynamically expanded. The initial size is 1, c [I] indicates that the length of the longest ascending subsequence is the smallest number at the end of all the substrings of I. Based on this number, we can know that, as long as the number is larger than C [I], C [I] can be used to form an ascending subsequence with an I + 1 length. Of course, we want to find a number in the C array as far as possible, so that we can get the longest length of the rising substring, and use the binary search when searching, so that the time complexity is reduced.

The template is as follows:

1 // The longest ascending subsequence nlogn template 2 // entry parameter: array name + array length, unlimited type, the struct type can be implemented through the overload Operator 3 // The array subscript starts from 1. 4/** // begin_template_by_abilitytao_acm ///////// ///// // 5 template <class T> 6 int bsearch (t C [], int N, t a) 7 {8 int L = 1, R = N; 9 While (L <= r) 10 {11 int mid = (L + r)/2; 12 if (a> C [Mid] & A <= C [Mid + 1]) return mid + 1; // >&<=for:> = & <13 else if (a <C [Mid]) r = mid-1; 14 else l = Mid + 1; 15} 16} 17 template <class T> 18 int Lis (t a [], int N) 19 {20 int I, j, size = 1; 21 T * c = new T [n + 1]; 22 int * dp = new int [n + 1]; 23 C [1] = A [1]; DP [1] = 1; 24 for (I = 2; I <= N; ++ I) 25 {26 if (a [I] <= C [1]) j = 1; // <= Replace: <27 else if (a [I]> C [size]) 28 J = ++ size; //> Replace:> = 29 else30 J = bsearch (C, size, a [I]); 31 C [J] = A [I]; DP [I] = J; 32} 33 return size; 34}
View code

 

Sub-sequence with the longest increase (no decrease) (detailed, conversion)

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.