简体   繁体   English

最长的递增子序列-无法了解LIS的实际创建

[英]Longest Increasing Subsequence - Not able to understand the actual LIS creation

Can someone please explain the last of the algo given by Peter in the article How to determine the longest increasing subsequence using dynamic programming? 谁能解释一下彼得在文章中给出的最后算法: 如何使用动态编程确定最长的递增子序列? I am not able to get the construction of actual LIS, how the parent array will be constructed. 我无法获得实际的LIS的构造,即如何构造父阵列。 Please explain with the same example which Peter has taken. 请用彼得所举的相同例子来解释。 Thanks in advance. 提前致谢。

The longest increasing subsequence in Peter's example is actually "2 3 4 5 8", resulting in length 5. 在Peter的示例中,最长的递增子序列实际上是“ 2 3 4 5 8”,长度为5。

It is not possible to obtain the LIS by looking at the array S at the end of the algorithm and S is not the longest increasing subsequence. 在算法末尾无法通过查看数组S来获得LIS,并且S 并不是最长的增长子序列。

If we need to construct the LIS, we will need to store more information and modify the algorithm that is presented. 如果需要构建LIS,则需要存储更多信息并修改所提供的算法。 Peter has mentioned storing parent[i] and changing S[i] to store indexes instead of values in the later part of this article. Peter在本文的后面部分提到了存储parent[i]和更改S[i]以存储索引而不是值。

Let me modify his proposed algorithm and define Si to store the index of the numbers in the array S and parent[i] to store the index of the previous number in the LIS that ends at array[i] . 让我修改他提出的算法,并定义Si来将数字的索引存储在数组S并定义parent[i]来将前一个数字的索引存储在LIS中(以array[i]结束array[i] Using his example: array[] = {2, 6, 3, 4, 1, 2, 9, 5, 8} . 以他的示例为例: array[] = {2, 6, 3, 4, 1, 2, 9, 5, 8} Do note that -1 is used in the parent array when they form LIS of maximum length 1. 请注意,当父数组形成最大长度为1的LIS时,将在父数组中使用-1

0. S = {} - Initialize S to the empty set
   Si = {}
   parent = {}
1. S = {2} - New largest LIS
   Si = {0}
   parent = {-1}
2. S = {2, 6} - New largest LIS
   Si = {**0**, 1}
   parent = {-1, **0**}
3. S = {2, 3} - Changed 6 to 3
   Si = {**0**, 2}
   parent = {-1, 0, **0**}
4. S = {2, 3, 4} - New largest LIS
   Si = {0, **2**, 3}
   parent = {-1, 0, 0, **2**}
5. S = {1, 3, 4} - Changed 2 to 1
   Si = {4, 2, 3}
   parent = {-1, 0, 0, 2, -1}
6. S = {1, 2, 4} - Changed 3 to 2
   Si = {**4**, 5, 3}
   parent = {-1, 0, 0, 2, -1, **4**}
7. S = {1, 2, 4, 9} - New largest LIS
   Si = {4, 5, **3**, 6}
   parent = {-1, 0, 0, 2, -1, 4, **3**}
8. S = {1, 2, 4, 5} - Changed 9 to 5
   Si = {4, 5, **3**, 7}
   parent = {-1, 0, 0, 2, -1, 4, 3, **3**}
9. S = {1, 2, 4, 5, 8} - New largest LIS
   Si = {4, 5, 3, **7**, 8}
   parent = {-1, 0, 0, 2, -1, 4, 3, 3, 7}

At the end, we get the parent array {-1, 0, 0, 2, -1, 4, 3, 3, 7} 最后,我们得到父数组{-1, 0, 0, 2, -1, 4, 3, 3, 7}

1) by looking at S , we know there is an LIS of length 5 ending at index 8 with value 8. LIS = { ?, ?, ?, ?, 8 } 1)通过查看S ,我们知道存在一个长度为5的LIS,它的结尾是索引8,值为LIS = { ?, ?, ?, ?, 8 }

2) we now look at the parent of index 8, parent[8] is 7 and the preceding member of the LIS will be in index 7. This is the number 5. LIS = { ?, ?, ?, 5, 8 } 2)现在,我们看一下索引8的父级, parent[8]是7,LIS的前一个成员将在索引7中。这是数字LIS = { ?, ?, ?, 5, 8 }

3) we now look at the parent of index 7 (value 5), parent[7] is 3 and the previous member of the LIS will be in index 3. This is the number 4. LIS = { ?, ?, 4, 5, 8 } 3)现在我们看一下索引7的父级(值5), parent[7]是3,LIS的前一个成员将在索引3中。这是数字LIS = { ?, ?, 4, 5, 8 }

4) we now look at the parent of index 3 (value 4), parent[3] is 2 and the previous member of the LIS will be in index 2. This is the number 3. LIS = { ?, 3, 4, 5, 8 } 4)现在我们看一下索引3的父级(值4), parent[3]是2,LIS的前一个成员将在索引2中。这是数字LIS = { ?, 3, 4, 5, 8 }

5) we now look at the parent of index 2 (value 3), parent[2] is 0 and the previous member of the LIS will be in index 0. This is the number 2. LIS = { 2, 3, 4, 5, 8 } 5)现在我们看一下索引2的父级(值3), parent[2]为0,LIS的前一个成员将在索引0中。这是数字LIS = { 2, 3, 4, 5, 8 }

6) The actual LIS would be 2 3 4 5 8 . 6)实际LIS为2 3 4 5 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM