您的位置:首页 > 产品设计 > UI/UE

[Algorithms] Longest Increasing Subsequence

2015-06-15 11:46 344 查看
The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequence s, such that t satisfies two requirements:

Elements in t are sorted in ascending order;

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:

P[i] = max_{j = 0, ..., i - 1 and arr[j] < arr[i]} P[j] + 1;

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(), 1);
int maxlen = 0;
for (int i = 1; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
maxlen = max(maxlen, dp[i]);
}
}
}
return maxlen;
}

void longestIncreasingSubsequenceTest(void) {
int num[] = {10, 22, 9, 33, 21, 50, 41, 60, 80};
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
}

int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return 0;
}


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 == 0) {
reverse(seq.begin(), seq.end());
res.push_back(seq);
reverse(seq.begin(), seq.end());
return;
}
for (int i = end; i >= 0; i--) {
if (dp[i] == maxlen && (seq.empty() || nums[i] < seq.back())) {
seq.push_back(nums[i]);
findAllLCSHelper(nums, dp, seq, res, maxlen - 1, i - 1);
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() - 1);
return res;
}

/* Compute the length of LCS and print all of them. */
int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), 1);
int maxlen = 0;
for (int i = 1; i < (int)nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
maxlen = max(maxlen, dp[i]);
}
}
}
vector<vector<int> > lcss = findAllLCS(nums, dp, maxlen);
for (int i = 0; i < (int)lcss.size(); i++) {
for (int j = 0; j < (int)lcss[i].size(); j++)
printf("%d ", lcss[i][j]);
printf("\n");
}
return maxlen;
}

/* Test function. */
void longestIncreasingSubsequenceTest(void) {
int num[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
}

int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return 0;
}


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

0 2 6 9 11 15
0 4 6 9 11 15
0 2 6 9 13 15
0 4 6 9 13 15
6


The first four rows are the four LIS.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: