Python program for Longest Increasing Subsequence
Last Updated :
28 Jun, 2023
Given an array arr[] of size N, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.
Examples:
Input: arr[] = {3, 10, 2, 1, 20}
Output: 3
Explanation: The longest increasing subsequence is 3, 10, 20
Input: arr[] = {3, 2}
Output:1
Explanation: The longest increasing subsequences are {3} and {2}
Input: arr[] = {50, 3, 10, 7, 40, 80}
Output: 4
Explanation: The longest increasing subsequence is {3, 7, 40, 80}
Longest Increasing Sequence using Recursion:
The problem can be solved based on the following idea:
Let L(i) be the length of the LIS ending at index i such that arr[i] is the last element of the LIS. Then, L(i) can be recursively written as:
- L(i) = 1 + max(L(j) ) where 0 < j < i and arr[j] < arr[i]; or
- L(i) = 1, if no such j exists.
Formally, the length of LIS ending at index i, is 1 greater than the maximum of lengths of all LIS ending at some index j such that arr[j] < arr[i] where j < i.
We can see that the above recurrence relation follows the optimal substructure property.
Below is the implementation of the recursive approach:
Python3
global maximum
def _lis(arr, n):
global maximum
if n = = 1 :
return 1
maxEndingHere = 1
for i in range ( 1 , n):
res = _lis(arr, i)
if arr[i - 1 ] < arr[n - 1 ] and res + 1 > maxEndingHere:
maxEndingHere = res + 1
maximum = max (maximum, maxEndingHere)
return maxEndingHere
def lis(arr):
global maximum
n = len (arr)
maximum = 1
_lis(arr, n)
return maximum
if __name__ = = '__main__' :
arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ]
n = len (arr)
print ( "Length of lis is" , lis(arr))
|
Output
Length of lis is 5
Complexity Analysis:
- Time Complexity: O(2n) The time complexity of this recursive approach is exponential as there is a case of overlapping subproblems as explained in the recursive tree diagram above.
- Auxiliary Space: O(1). No external space is used for storing values apart from the internal stack space.
Longest Increasing Subsequence using Memoization:
If noticed carefully, we can see that the above recursive solution also follows the overlapping subproblems property i.e., same substructure solved again and again in different recursion call paths. We can avoid this using the memoization approach.
We can see that each state can be uniquely identified using two parameters:
- Current index (denotes the last index of the LIS) and
- Previous index (denotes the ending index of the previous LIS behind which the arr[i] is being concatenated).
Below is the implementation of the above approach.
Python3
import sys
def f(idx, prev_idx, n, a, dp):
if (idx = = n):
return 0
if (dp[idx][prev_idx + 1 ] ! = - 1 ):
return dp[idx][prev_idx + 1 ]
notTake = 0 + f(idx + 1 , prev_idx, n, a, dp)
take = - sys.maxsize - 1
if (prev_idx = = - 1 or a[idx] > a[prev_idx]):
take = 1 + f(idx + 1 , idx, n, a, dp)
dp[idx][prev_idx + 1 ] = max (take, notTake)
return dp[idx][prev_idx + 1 ]
def longestSubsequence(n, a):
dp = [[ - 1 for i in range (n + 1 )] for j in range (n + 1 )]
return f( 0 , - 1 , n, a, dp)
if __name__ = = '__main__' :
a = [ 3 , 10 , 2 , 1 , 20 ]
n = len (a)
print ( "Length of lis is" , longestSubsequence(n, a))
|
Output
Length of lis is 3
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Because of the optimal substructure and overlapping subproblem property, we can also utilise Dynamic programming to solve the problem. Instead of memoization, we can use the nested loop to implement the recursive relation.
The outer loop will run from i = 1 to N and the inner loop will run from j = 0 to i and use the recurrence relation to solve the problem.
Below is the implementation of the above approach:
Python3
def lis(arr):
n = len (arr)
lis = [ 1 ] * n
for i in range ( 1 , n):
for j in range ( 0 , i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1 :
lis[i] = lis[j] + 1
maximum = 0
for i in range (n):
maximum = max (maximum, lis[i])
return maximum
if __name__ = = '__main__' :
arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ]
print ( "Length of lis is" , lis(arr))
|
Output
Length of lis is 5
Time Complexity: O(N2) As a nested loop is used.
Auxiliary Space: O(N) Use of any array to store LIS values at each index.
Please Refer Longest Increasing Subsequence for detailed article.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...