|
| 1 | +/* |
| 2 | +The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. |
| 3 | +example - length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80} |
| 4 | +*/ |
| 5 | + |
| 6 | +//RECURSIVE APPROACH |
| 7 | +/* |
| 8 | +Two possibilities, explore both- |
| 9 | +1. Include current element in LIS if current element is greater than the previous element, then recur for remaining elements. |
| 10 | +2. Exclude current element from LIS if current element is smaller or greater than previous element, then recur for remaining elements. |
| 11 | +
|
| 12 | +Basically, condition to be checked ==> A[current] > A[prev] |
| 13 | + if(true) INCLUDE A[current] in LIS and recur |
| 14 | + EXCLUDE A[current] in LIS and recur |
| 15 | +the condition must be satisfied to INCLUDE the element however the same is not true for EXCLUDE |
| 16 | +We EXCLUDE each element irrespective of whether it is greater than A[prev] in order to explore all possibilties. |
| 17 | +*/ |
| 18 | + |
| 19 | +public int LISlength(int A[], int current, int n, int prev){ |
| 20 | + // base case : nothing more to explore |
| 21 | + if(i == n) return 0; |
| 22 | + |
| 23 | + //By calling the function with the same value of prev as in the original call, we are excluding the current element |
| 24 | + int exc = LISlength(A, current+1, n, prev); |
| 25 | + |
| 26 | + int inc = 0; |
| 27 | + |
| 28 | + //Here ........A[prev], A[current] form an increasing subsequence, we call the function again by including A[current] |
| 29 | + //A[current] is included becuase we have changed the value of prev to A[current] |
| 30 | + if(A[current] > A[prev]){ |
| 31 | + inc = LISlength(A, current+1, n, A[current]) + 1; |
| 32 | + } |
| 33 | + |
| 34 | + return Math.max(exc,inc); |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +/* |
| 40 | +DYNAMIC PROGRAMMING APPROACH - BOTTOM UP |
| 41 | +Let arr[0..n-1] be the input array and L(i) be the length of the LIS ending at index i such that arr[i] is the last element of the LIS. |
| 42 | +Then, L(i) can be recursively written as: |
| 43 | +L(i) = 1 + max( L(j) ) where 0 < j < i and arr[j] < arr[i]; or |
| 44 | +L(i) = 1, if no such j exists. |
| 45 | +To find the LIS for a given array, we need to return max(L(i)) where 0 < i < n. |
| 46 | +
|
| 47 | +How does this work? |
| 48 | +1. In the above logic we assume each element of the array is at the end of an increasing subsequence. |
| 49 | +2. To find the length of this increasing subsequence we consider all elements before the current element and also the |
| 50 | + respective lengths of increasing subsequences associated with each of these previous elements. |
| 51 | + Let the current element be A[i] and all elements before it be A[0...j] |
| 52 | + 2.1. If (A[j] < A[i]) |
| 53 | + A[j] is a potential candidate of an increasing subsequence ending at A[i] |
| 54 | + 2.2. However the above condition does not gurantee an increasing subsequence of maximal length. |
| 55 | + To get an increasing subseq of maximal length ending at A[i], |
| 56 | + we also need to consider the maximum length of increasing subseq formed ending at A[j] (0<j<i) |
| 57 | + 2.3. A[j] < A[i] && L[j] > L[i] |
| 58 | + Include A[j] only if it maintains the increasing subseq and offers more elements than the current maximum. |
| 59 | +
|
| 60 | +*/ |
| 61 | + |
| 62 | + |
| 63 | +public int LCslength(int A[]){ |
| 64 | + int L[] = new int[A.length]; |
| 65 | + int maxLen = 1; |
| 66 | + |
| 67 | + //length of increasing subseq ending at A[0] is 1 |
| 68 | + L[0] = 1; |
| 69 | + |
| 70 | + for(int i=1 ; i<A.length ; i++){ |
| 71 | + for(int j=0 ; j<i ; j++){ |
| 72 | + if(A[j] < A[i] && L[j] > L[i]){ |
| 73 | + L[i] = L[j]; |
| 74 | + } |
| 75 | + } |
| 76 | + L[i]++; //to include the last element in the subseq |
| 77 | + if(maxLen < L[i]) maxLen = L[i]; |
| 78 | + } |
| 79 | + |
| 80 | + return maxLen; |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | +Time Complexity - O(n^2) |
| 85 | +Space Complexity - O(n) |
| 86 | +*/ |
0 commit comments