Longest increasing subsequence (LIS) is a common algorithm problem that can be solved using dynamic programming. This problem refers to finding the largest number of sub-sequences in ascending order in a numerical sequence. For example, there is a digital sequence:
S = {8, 4, 1, 7, 6, 2, 0, 5, 3}
Its Lis Is (1, 2, 3) and (1, 2, 5 ). In addition to this definition, there is also a definition called longest increasing run (I don't know how to translate it), which refers to finding the subsequence of the adjacent maximum number in ascending order. For example, in the above sequence () and ).
Obviously it is very easy to find a longest increasing run, but it is not easy to find a lis.
To apply dynamic programming, we need to establish a recursion here to calculate the length of the longest sequence. There are two algorithms with different time complexity:
For I = 1 to total-1
For J = I + 1 to total
If height [J]> height [I] Then
If length [I] + 1> length [J] Then
Length [J] = length [I] + 1
Predecessor [J] = I
The time complexity of this algorithm is O (n2 ). Here is an example to demonstrate this algorithm. Take a sequence as follows: Height = {9, 5, 2, 8, 7, 3, 1, 6, 4}
Length = {1, 1, 1, 1, 1, 1, 1, 1}
Predecessor = {nil, nil}
Use this algorithm to obtain the following results:
Height = {9, 5, 2, 8, 7, 3, 1, 6, 4}
Length = {1, 1, 1, 2, 2, 2, 1, 3}
Predecessor = {nil, 2, 2, 3, nil, 6, 6}
There is also an O (n log K) algorithm, where k is the actual length of LIS. The algorithm uses an ascending sequence a to store the entire Lis. The initial test status of A is an array composed of one negative infinity and n-1 positive infinity. What we need to do is to traverse the entire sequence and put each number in a to see if it belongs to the LIS. If so, insert it into it. Whether or not it belongs refers to looking forward from the first non-infinite number of A. If this position is found, that is, the number before is smaller than the number to be inserted, and the last number is greater than that. The insert method replaces the following number. This kind of search uses binary search, whose time complexity is O (log K ). Therefore, the time complexity of the entire algorithm is O (n log K ). The following is an example:
0 1 2 3 4 5 6 7 8
A-7,10, 9, 2, 3, 8, 8, 1
A-I, I
A-I-7, I, I (1)
A-I-7,10, I, I (2)
A-I-7, 9, I, I (3)
A-I-7, 2, I, I (4)
A-I-7, 2, 3, I, I (5)
A-I-7, 2, 3, 8, I (6)
A-I-7, 2, 3, 8, I (7)
A-I-7, 1, 3, 8, I (8)
Reference: http://www.comp.nus.edu.sg /~ Steven HA/programming/prog_dynamicprogramming.html
Http://www2.toki.or.id/book/AlgDesignManual/BOOK/BOOK2/NODE47.HTM