Dynamic Programming 4 Longest Increasing Subsequence LCS DP

Dynamic Programming (4) Longest Increasing Subsequence (LCS)

DP alg. for LIS • L P 1 2 3 4 5 6 7 8 9 2 4 10 9 7 5 6 8 8

DP alg. for LIS • L P 1 2 3 4 5 6 7 8 9 2 4 10 9 7 5 6 8 6 1 2 3 3 4 5 4 1 2 2 6 7 6

Running time ? •

A more efficient algorithm ? • Back to our previous solution, where we wanted to know the smallest integer that ends a longest increasing subsequence • I[j] = the smallest integer that ends an increasing subsequence of length j

Smallest integers ending subsequence of each length • Back to our previous solution, where we wanted to know the smallest integer that ends a longest increasing subsequence • I[j] = the smallest integer that ends an increasing subsequence of length j I 1 2 3 4 5 6 7 8 9 2 4 3 5 1 7 6 9 8

Lets try to compute this list for every prefix • Back to our previous solution, where we wanted to know the smallest integer that ends a longest increasing subsequence • I[j] = the smallest integer that ends an increasing subsequence of length j I 1 2 3 4 5 6 7 8 9 2 4 3 5 1 7 6 9 8 [2] [2, 4] [2, 3, 5] [1, 3, 5, 7] [1, 3, 5, 6, 9] [1, 3, 5, 6, 8]

How do we maintain the list I ? • Back to our previous solution, where we wanted to know the smallest integer that ends a longest increasing subsequence • I[j] = the smallest integer that ends an increasing subsequence of length j I 1 2 3 4 5 6 7 8 9 2 4 3 5 1 7 6 9 8 [2] [2, 4] [2, 3, 5] [1, 3, 5, 7] [1, 3, 5, 6, 9] [1, 3, 5, 6, 8]

Smallest integers ending subsequence of each length • Back to our previous solution, where we wanted to know the smallest integer that ends a longest increasing subsequence • I[j] = the smallest integer that ends an increasing subsequence of length j I 1 2 3 4 5 6 7 8 9 2 4 10 9 7 5 6 8 3 [2] [2, 4, 10] [2, 4, 9] [2, 4, 7] [2, 4, 5, 6] [2, 4, 5, 6, 8] [2, 3, 5, 6, 8]

Efficient Implementation •

How do we find the list itself ? Try to find the algorithm by yourself …
- Slides: 11