Lecture-13 - DP-Longest Increasing Subsequance - LIS
Lecture-13 - DP-Longest Increasing Subsequance - LIS
• For example,
{10, 22, 9, 33, 21, 50, 41, 60, 80}
9 2 5 3 7 11 8 10 13 6
2 5 7 8 10 13
2 3 7 8 10 13
So LIS = 2 3 7 8 10 13 OR 2 5 7 8 10 13
And Length of LIS = 6
9 7 11
5 3 11 13
are not increasing subsequences.
5
More Example
9 2 5 3 7 11 8 10 13 6
Index 0 1 2 3 4 5 6 7 8 9
Input 9 2 5 3 7 11 8 10 13 6
Length
Prev
A naive approach for LIS
Index 0 1 2 3 4 5 6 7 8 9
Input 9 2 5 3 7 11 8 10 13 6
Length 1 1 2 2 3 4 4 5 6 3
Prev -1 -1 1 1 2 4 4 6 7 2
Length of LIS = 6
The longest increasing subsequence 2, 5, 7, 8, 10, 13
Exercise
Index 0 1 2 3 4 5
Input 19 20 50 3 17 110
Psudocode or LIS
LIS( int arr[], int n )
• for (i = 0; i < n; i++ )
• length [i] = 1;