4. What is LIS?
LIS abbreviated as “Longest Increasing Subsequence”.
Longest Increasing Subsequence is a subsequence where one item is
greater than its previous item.
It consists of three parts:
Longest - stands for its own meaning.
Increasing - means that it must be an increasing something, for example
[1, 2, 3, 7, 20] is an increasing sequence but [1, 4, 2, 5] is definitely not
an increasing sequence because we have 2 after 4.
Formally, a sequence [a1, a2 ... an] is increasing if a1 < a2 < a3 < … < an.
2/25/2019 4
5. Subsequence - means a new sequence consisting of some numbers from the
original sequence but the numbers maintain the relative ordering.
For example:
If the sequence is [2,5,3,6,7] then [2, 5, 6,7], [2, 3, 6,7] and [5, 6, 7] etc are
valid subsequences but [2, 3, 5] is not valid. Cause in the original sequence 3 is
listed after 5.
2/25/2019 5
6. O(nlogk) Approach:
Now I want to describe O(nlogk) approach with An ARRAY.
1) Initially i make a new array, let the name be M[], initially all the
values of the array are infinite, only the 0th element contains
negative infinite. The size of M[] will be total elements in the
sequence + 1.
2) Now I iterate from left and I pick the numbers from Sequence one by
one and insert them into M[]. When inserting a number, I find the
position where all the numbers in left are strictly smaller than the
number.
3) If I insert the numbers this array in M[] will always be in ascending
order.
However, the example will show the procedure fully…
Let’s start…….. 2/25/2019 6
7. Let, the sequence be
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
There are 10 elements, so, I will contain 11 elements with index from 0 to 10.
So, as described, the elements of M[] will be initially,
M [-i, i, i, i, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [n, n, n, n, n, n, n, n, n, n]
Here i denotes ‘infinite’ and n denotes ‘not calculated yet’.
Now let’s insert the numbers from sequence into M[].
1) At first we have 8, so the position for 8 in M[] is 1. Since the left item is –i which is
smaller than 8. So, I will assign M[1] = 8. Since 8 is inserted in 1st place, so the
LIS[] value of 8 will be 1. After the first number we will get
M [-i, 8, i, i, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, n, n, n, n, n, n, n, n, n]
2/25/2019 7
8. 2) Now I have 1, the position for 1 in M[] is 1. So, we will assign M[1] = 1 and since 1 is inserted in
1st place, the LIS[] value of 1 will be 1. Observe that M[1] was 8, but after this iteration 8 will be
replaced by 1.
M [-i, 1, i, i, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, n, n, n, n, n, n, n, n]
3) The next number is 9. The position for 9 in M[] is definitely the 2nd position. So, we will assign
M[2] = 9, and since 9 is inserted in the second position, so the LIS[] value of 9 will be 2.
M [-i, 1, 9, i, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, n, n, n, n, n, n, n]
4) Now we have 8, and the position of this 8 in M[] is the second position. So, we put 8 in the
second position of M[] (thus replacing 9). And the LIS[] value of 8 will be 2.
M [-i, 1, 8, i, i, i, i, i, i, i, i]
Index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, n, n, n, n, n, n]
2/25/2019 8
9. 5) The next item is 3, and the position in M[] is still the second position. So, I place 3 in the second
position of M[]. And the LIS[] value of 3 is 2.
M [-i, 1, 3, i, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, n, n, n, n, n]
6) Now we have 4, and the position in M[] is 3. So, the LIS[] value of 4 is 3.
M [-i, 1, 3, 4, i, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, 3, n, n, n, n]
7) Next item is 6, it will be inserted in the 4th position of M[]. So, the LIS[] value of 6 will be 4.
M [-i, 1, 3, 4, 6, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, 3, 4, n, n, n]
8) Next item is 1, it will be inserted in the 1st position of M[]. So, the L[] value of 1 will be 1.
M [-i, 1, 3, 4, 6, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, 3, 4, 1, n, n]
2/25/2019 9
10. 9)Next item is 5, it will be inserted in the 4th position of M[]. So, the LIS[] value of 5 will be 4.
M [-i, 1, 3, 4, 5, i, i, i, i, i, i]
index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, 3, 4, 1, 4, n]
10)Last item is 2, it will be inserted in the 2 nd position of M[]. So, the LIS[] value of 2 will be 2.
M [-i, 1, 2, 4, 5, i, i, i, i, i, i]
Index 0 1 2 3 4 5 6 7 8 9 10
Sequence [8, 1, 9, 8, 3, 4, 6, 1, 5, 2]
LIS [1, 1, 2, 2, 2, 3, 4, 1, 4, 2]
Now, see that completely process the values in M[] are in ascending order. And since we place a
number in such a place where all the numbers in left are strictly less than the number so, the LIS[]
value will be correct for all the numbers.
One more thing, after all the iterations are over, don’t think that M[] will also contain a sequence.
Because in the example above, see that 1, 2, 4, 5 is not the correct sequence. To get a sequence,
since we already have the LIS[] values, we can use the reconstruction method. Now since the values in
M[] will always be in ascending order, so, to find a position of a number we can definitely use binary
search. So, if the final LIS length is k and there are n numbers then 10 . To insert a number using
binary search the complexity will be O(logk). Thus the overall complexity will be O(nlogk), since we
are inserting n items.
2/25/2019 10