简体   繁体   中英

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. 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.

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.

If we need to construct the LIS, we will need to store more information and modify the algorithm that is presented. Peter has mentioned storing parent[i] and changing S[i] to store indexes instead of values in the later part of this article.

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] . Using his example: 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.

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) by looking at S , we know there is an LIS of length 5 ending at index 8 with value 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 }

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 }

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 }

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 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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