[Algorithms] Longest Increasing Subsequence

时间:2021-04-30 06:49:13

The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequence s, such that t satisfies two requirements:

  1. Elements in t are sorted in ascending order;
  2. t is as long as possible.

This problem can be solved using Dynamic Programming. We define the state P[i] to be the length of the longest increasing subsequence ends at i (with s[i] as its last element). Then the state equations are:

  1. P[i] = max_{j = 0, ..., i - 1 and arr[j] < arr[i]} P[j] + 1;
  2. If no such j exists, P[i] = 1.

Putting these into code using a table to store results for smaller problems and solve it in a bottom-up manner. We will have the following code.

 #include <iostream>
#include <string>
#include <vector> using namespace std; int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
return maxlen;
} void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}

This program only computes the length of the LIS. If you want to print all the possible LIS, you need to modify the above program. Specifically, you may want to use backtracking to obtain all the possible LIS. My code is as follows. Welcome for any comments. Thank you!

 #include <iostream>
#include <string>
#include <vector> using namespace std; /* Helper function to find all LCS. */
void findAllLCSHelper(vector<int>& nums, vector<int>& dp, vector<int>& seq, vector<vector<int> >& res, int maxlen, int end) {
if (maxlen == ) {
reverse(seq.begin(), seq.end());
res.push_back(seq);
reverse(seq.begin(), seq.end());
return;
}
for (int i = end; i >= ; i--) {
if (dp[i] == maxlen && (seq.empty() || nums[i] < seq.back())) {
seq.push_back(nums[i]);
findAllLCSHelper(nums, dp, seq, res, maxlen - , i - );
seq.pop_back();
}
}
} /* Function to find all LCS. */
vector<vector<int> > findAllLCS(vector<int>& nums, vector<int>& dp, int maxlen) {
vector<vector<int> > res;
vector<int> seq;
findAllLCSHelper(nums, dp, seq, res, maxlen, nums.size() - );
return res;
} /* Compute the length of LCS and print all of them. */
int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < (int)nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
vector<vector<int> > lcss = findAllLCS(nums, dp, maxlen);
for (int i = ; i < (int)lcss.size(); i++) {
for (int j = ; j < (int)lcss[i].size(); j++)
printf("%d ", lcss[i][j]);
printf("\n");
}
return maxlen;
} /* Test function. */
void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}

Running this program in Microsoft Visual Professional 2012 gives the following results.


The first four rows are the four LIS.