Longest Increasing Subsequence:

We have discussed Overlapping Subproblems and Optimal Substructure properties respectively.

Let us discuss Longest Increasing Subsequence (LIS) problem as an example problem that can be solved using Dynamic Programming.
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. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}

Longest-Increasing-Subsequence

More Examples:

Input  : arr[] = {3, 10, 2, 1, 20}
Output : Length of LIS = 3
The longest increasing subsequence is 3, 10, 20

Input  : arr[] = {3, 2}
Output : Length of LIS = 1
The longest increasing subsequences are {3} and {2}

Input : arr[] = {50, 3, 10, 7, 40, 80}
Output : Length of LIS = 4
The longest increasing subsequence is {3, 7, 40, 80}
[ad type=”banner”]

Optimal Substructure:

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.
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.
To find the LIS for a given array, we need to return max(L(i)) where 0 < i < n.
Thus, we see the LIS problem satisfies the optimal substructure property as the main problem can be solved using solutions to subproblems.

Following is a simple recursive implementation of the LIS problem. It follows the recursive structure discussed above.

Python Programming

[pastacode lang=”python” manual=”%23%20A%20naive%20Python%20based%20recursive%20implementation%20of%20LIS%20problem%0A%20%0Aglobal%20max_lis_length%20%23%20stores%20the%20final%20LIS%0A%20%0A%23%20Recursive%20implementation%20for%20calculating%20the%20LIS%0Adef%20_lis(arr%2C%20n)%3A%0A%20%20%20%20%23%20Following%20declaration%20is%20needed%20to%20allow%20modification%0A%20%20%20%20%23%20of%20the%20global%20copy%20of%20max_lis_length%20in%20_lis()%0A%20%20%20%20global%20max_lis_length%0A%20%0A%20%20%20%20%23%20Base%20Case%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%0A%20%20%20%20current_lis_length%20%3D%201%0A%20%0A%20%20%20%20for%20i%20in%20xrange(0%2C%20n-1)%3A%0A%20%20%20%20%20%20%20%20%23%20Recursively%20calculate%20the%20length%20of%20the%20LIS%0A%20%20%20%20%20%20%20%20%23%20ending%20at%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20subproblem_lis_length%20%3D%20_lis(arr%2C%20i)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Check%20if%20appending%20arr%5Bn-1%5D%20to%20the%20LIS%0A%20%20%20%20%20%20%20%20%23%20ending%20at%20arr%5Bi%5D%20gives%20us%20an%20LIS%20ending%20at%0A%20%20%20%20%20%20%20%20%23%20arr%5Bn-1%5D%20which%20is%20longer%20than%20the%20previously%0A%20%20%20%20%20%20%20%20%23%20calculated%20LIS%20ending%20at%20arr%5Bn-1%5D%0A%20%20%20%20%20%20%20%20if%20arr%5Bi%5D%20%3C%20arr%5Bn-1%5D%20and%20%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20current_lis_length%20%3C%20(1%2Bsubproblem_lis_length)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20current_lis_length%20%3D%20(1%2Bsubproblem_lis_length)%0A%20%0A%20%20%20%20%23%20Check%20if%20currently%20calculated%20LIS%20ending%20at%0A%20%20%20%20%23%20arr%5Bn-1%5D%20is%20longer%20than%20the%20previously%20calculated%0A%20%20%20%20%23%20LIS%20and%20update%20max_lis_length%20accordingly%0A%20%20%20%20if%20(max_lis_length%20%3C%20current_lis_length)%3A%0A%20%20%20%20%20%20%20%20max_lis_length%20%3D%20current_lis_length%0A%20%0A%20%20%20%20return%20current_lis_length%0A%20%0A%23%20The%20wrapper%20function%20for%20_lis()%0Adef%20lis(arr%2C%20n)%3A%0A%20%0A%20%20%20%20%23%20Following%20declaration%20is%20needed%20to%20allow%20modification%0A%20%20%20%20%23%20of%20the%20global%20copy%20of%20max_lis_length%20in%20lis()%0A%20%20%20%20global%20max_lis_length%0A%20%0A%20%20%20%20max_lis_length%20%3D%201%20%23%20stores%20the%20final%20LIS%0A%20%0A%20%20%20%20%23%20max_lis_length%20is%20declared%20global%20at%20the%20top%0A%20%20%20%20%23%20so%20that%20it%20can%20maintain%20its%20value%0A%20%20%20%20%23%20between%20the%20recursive%20calls%20of%20_lis()%0A%20%20%20%20_lis(arr%20%2C%20n)%0A%20%0A%20%20%20%20return%20max_lis_length%0A%20%0A%23%20Driver%20program%20to%20test%20the%20functions%20above%0Adef%20main()%3A%0A%20%20%20%20arr%20%3D%20%5B10%2C%2022%2C%209%2C%2033%2C%2021%2C%2050%2C%2041%2C%2060%5D%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%20%20%20print%20%22Length%20of%20LIS%20is%22%2C%20lis(arr%2C%20n)%0A%20%0Aif%20__name__%3D%3D%22__main__%22%3A%0A%20%20%20%20main()” message=”Python” highlight=”” provider=”manual”/]

Output :

Length of LIS is 5

Overlapping Subproblems :

Considering the above implementation, following is recursion tree for an array of size 4. lis(n) gives us the length of LIS for arr[].

              lis(4)
        /        |     \
      lis(3)    lis(2)   lis(1)
     /   \        /
   lis(2) lis(1) lis(1)
   /
lis(1)

We can see that there are many subproblems which are solved again and again. So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memorization or Tabulation. Following is a tabluated implementation for the LIS problem.

[ad type=”banner”]

Python Programming

[pastacode lang=”python” manual=”%23%20Dynamic%20programming%20Python%20implementation%20of%20LIS%20problem%0A%20%0A%23%20lis%20returns%20length%20of%20the%20longest%20increasing%20subsequence%0A%23%20in%20arr%20of%20size%20n%0Adef%20lis(arr)%3A%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%20%20%20%23%20Declare%20the%20list%20(array)%20for%20LIS%20and%20initialize%20LIS%0A%20%20%20%20%23%20values%20for%20all%20indexes%0A%20%20%20%20lis%20%3D%20%5B1%5D*n%0A%20%0A%20%20%20%20%23%20Compute%20optimized%20LIS%20values%20in%20bottom%20up%20manner%0A%20%20%20%20for%20i%20in%20range%20(1%20%2C%20n)%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(0%20%2C%20i)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20arr%5Bi%5D%20%3E%20arr%5Bj%5D%20and%20lis%5Bi%5D%3C%20lis%5Bj%5D%20%2B%201%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lis%5Bi%5D%20%3D%20lis%5Bj%5D%2B1%0A%20%0A%20%20%20%20%23%20Initialize%20maximum%20to%200%20to%20get%20the%20maximum%20of%20all%0A%20%20%20%20%23%20LIS%0A%20%20%20%20maximum%20%3D%200%0A%20%0A%20%20%20%20%23%20Pick%20maximum%20of%20all%20LIS%20values%0A%20%20%20%20for%20i%20in%20range(n)%3A%0A%20%20%20%20%20%20%20%20maximum%20%3D%20max(maximum%20%2C%20lis%5Bi%5D)%0A%20%0A%20%20%20%20return%20maximum%0A%23%20end%20of%20lis%20function%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aarr%20%3D%20%5B10%2C%2022%2C%209%2C%2033%2C%2021%2C%2050%2C%2041%2C%2060%5D%0Aprint%20%22Length%20of%20lis%20is%22%2C%20lis(arr)” message=”Python” highlight=”” provider=”manual”/]

Output :

Length of lis is 5

Note that the time complexity of the above Dynamic Programming (DP) solution is O(n^2) and there is a O(nLogn) solution for the LIS problem.

[ad type=”banner”]