Longest Increasing Subsequence using Longest Common Subsequence Algorithm

Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.

Examples:

Input: arr[] = {12, 34, 1, 5, 40, 80}
Output: 4
{12, 34, 40, 80} and {1, 5, 40, 80} are the
longest increasing subsequences.



Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80}
Output: 6

Prerequisite: LCS, LIS

Approach: The longest increasing subsequence of any sequence is the subsequence of the sorted sequence of itself. It can be solved using a Dynamic Programming approach. The approach is the same as the classical LCS problem but instead of the second sequence, given sequence is taken again in its sorted form.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the size of the
// longest increasing subsequence
int LISusingLCS(vector<int>& seq)
{
    int n = seq.size();
  
    // Create an 2D array of integer
    // for tabulation
    vector<vector<int> > L(n + 1, vector<int>(n + 1));
  
    // Take the second sequence as the sorted
    // sequence of the given sequence
    vector<int> sortedseq(seq);
  
    sort(sortedseq.begin(), sortedseq.end());
  
    // Classical Dynamic Programming algorithm
    // for Longest Common Subsequence
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
  
            else if (seq[i - 1] == sortedseq[j - 1])
                L[i][j] = L[i - 1][j - 1] + 1;
  
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
  
    // Return the ans
    return L[n][n];
}
  
// Driver code
int main()
{
  
    vector<int> sequence{ 12, 34, 1, 5, 40, 80 };
  
    cout << LISusingLCS(sequence) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java implementation of above approach
import java.util.*;
  
class GFG
{
      
// Function to return the size of the
// longest increasing subsequence
static int LISusingLCS(Vector<Integer> seq)
{
    int n = seq.size();
  
    // Create an 2D array of integer
    // for tabulation
    int L[][] = new int [n + 1][n + 1];
  
    // Take the second sequence as the sorted
    // sequence of the given sequence
    Vector<Integer> sortedseq = new Vector<Integer>(seq);
  
    Collections.sort(sortedseq);
  
    // Classical Dynamic Programming algorithm
    // for Longest Common Subsequence
    for (int i = 0; i <= n; i++) 
    {
        for (int j = 0; j <= n; j++) 
        {
            if (i == 0 || j == 0)
                L[i][j] = 0;
  
            else if (seq.get(i - 1) == sortedseq.get(j - 1))
                L[i][j] = L[i - 1][j - 1] + 1;
  
            else
                L[i][j] = Math.max(L[i - 1][j], 
                                   L[i][j - 1]);
        }
    }
  
    // Return the ans
    return L[n][n];
}
  
// Driver code
public static void main(String args[])
{
    Vector<Integer> sequence = new Vector<Integer>();
    sequence.add(12);
    sequence.add(34);
    sequence.add(1);
    sequence.add(5);
    sequence.add(40);
    sequence.add(80);
  
    System.out.println(LISusingLCS(sequence));
}
}
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the size of the
# longest increasing subsequence
def LISusingLCS(seq):
    n = len(seq)
  
    # Create an 2D array of integer
    # for tabulation
    L = [[0 for i in range(n + 1)] 
            for i in range(n + 1)]
      
    # Take the second sequence as the sorted
    # sequence of the given sequence
    sortedseq = sorted(seq)
  
    # Classical Dynamic Programming algorithm
    # for Longest Common Subsequence
    for i in range(n + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                L[i][j] = 0
  
            elif (seq[i - 1] == sortedseq[j - 1]):
                L[i][j] = L[i - 1][j - 1] + 1
  
            else:
                L[i][j] = max(L[i - 1][j], 
                              L[i][j - 1])
  
    # Return the ans
    return L[n][n]
  
# Driver code
sequence = [12, 34, 1, 5, 40, 80]
  
print(LISusingLCS(sequence))
  
# This code is contributed by mohit kumar

chevron_right


Output:

4

Time Complexity: O(n2) where n is the length of the sequence.



My Personal Notes arrow_drop_up

I am doing BTech at Dhirubhai Ambani Institute of Information and Communication Technology

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, andrew1234