Longest Increasing Subsequence
Explore the different techniques used to solve the longest increasing subsequence problem efficiently.
We'll cover the following
Longest increasing subsequence problem
Another problem we considered in the previous chapter was computing the length of the longest increasing subsequence of a given array of numbers. We developed two different recursive backtracking algorithms for this problem. Both algorithms run in time in the worst case; both algorithms can be sped up significantly via dynamic programming.
First recurrence: Is this next?
Our first backtracking algorithm evaluated the function , which we defined as the length of the longest increasing subsequence of in which every element is larger than . We derived the following recurrence for this function:
To solve the original problem, we can add a sentinel value to the array and compute .
Each recursive subproblem is identified by two indices and so there are only distinct recursive subproblems to consider. We can memoize the results of these subproblems into a two-dimensional array Moreover, each subproblem can be solved in time, not counting recursive calls, so we should expect the final dynamic programming algorithm to run in time.
The order in which the memoized recursive algorithm fills this array is not immediately clear; all we can tell from the recurrence is that each entry is filled in after the entries and in the next column, as indicated on the left in the illustration below.
Fortunately, this partial information is enough to give us a valid evaluation order. If we fill the table one column at a time, from right to left, then whenever we reach an entry in the table, the entries it depends on are already available. This may not be the order that the recursive algorithm would use, but it works, so we’ll go with it. The right diagram in the illustration below illustrates this evaluation order, with a double arrow indicating the outer loop and single arrows indicating inner loops. In this case, the single arrows are bidirectional because the order that we use to fill each column doesn’t matter.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy