Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does there exist a Top Down Dynamic Programming solution for Longest Increasing Subsequence?

I want to know how to find the LIS of an array using Top Down Dynamic Programming. Does there exist one such solution? Can you give me the pseudocode for finding the LIS of an array using Top Down Dynamic Programming? I am not able to find one on the internet. All of them use Bottom Up.

like image 387
Some Name Avatar asked Jun 01 '16 07:06

Some Name


People also ask

What is longest common subsequence (LCS) problem in dynamic programming?

Let us discuss Longest Common Subsequence (LCS) problem as one more example problem that can be solved using Dynamic Programming. LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous.

What is the longest common subsequence with maximum length?

Out of these common subsequences, subsequence CDE has a maximum length. Thus, it will be considered as the longest common subsequence for S1 and S2. Moving forward, we will look into a recursive solution for the longest common subsequence problem. Let’s say that we are given two sequences S1 and S2, having lengths m and n, respectively.

What is the longest chain/subsequence of increasing elements?

We are asked to form the longest chain/subsequence of increasing elements. The solution to the problem is the path that has most number of edges (2,5) -> (5,7) -> (7,101). This path gives the longest increasing subsequence as [2, 5, 7, 101]

Why is dynamic programming an optimal strategy to solve the LCS problem?

Due to that, the time taken by a dynamic programming approach to solve the LCS problem is equivalent to the time taken to fill the table, that is, O (m*n). This complexity is relatively low in comparison to the recursive paradigm. Hence, dynamic programming is considered as an optimal strategy to solve this space optimization problem.


1 Answers

Recursive approach to solve LIS length in java would be like below -

 public int LIS(int[] arr) {
        return LISLength(arr, Integer.MIN_VALUE, 0);
    }

    public int LISLength(int[] arr, int prev, int current) {
        if (current == arr.length) {
            return 0;
        }
        int include = 0;
        if (arr[current] > prev) {
            include = 1 + LISLength(arr, arr[current], current + 1);
        }
        int exclude = LISLength(arr, prev, current + 1);
        return Math.max(include, exclude);
    }

But it would work with O(2^n) time complexity so we need to use memoization technique to reduce the complexity with below approach -

public int LIS(int[] arr) {
        int memoTable[][] = new int[arr.length + 1][arr.length];
        for (int[] l : memoTable) {
            Arrays.fill(l, -1);
        }
        return LISLength(arr, -1, 0, memoTable);
    }
    public int LISLength(int[] arr, int prev, int current, int[][] memoTable) {
        if (current == arr.length) {
            return 0;
        }
        if (memoTable[prev + 1][current] >= 0) {
            return memoTable[prev + 1][current];
        }
        int include = 0;
        if (prev < 0 || arr[current] > arr[prev]) {
            include = 1 + LISLength(arr, current, current + 1, memoTable);
        }

        int exclude = LISLength(arr, prev, current + 1, memoTable);
        memoTable[prev + 1][current] = Math.max(include, exclude);
        return memoTable[prev + 1][current];
    }

So O(n^2) would be optimized time complexity with memoization technique.

like image 53
hims92sharma Avatar answered Sep 28 '22 02:09

hims92sharma