[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.
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!
Running this program in Microsoft Visual Professional 2012 gives the following results.
The first four rows are the four LIS.
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.
相关文章推荐
- string -- value
- jsp 中 request.getHeader() 相关详细
- On-Demand Resources Guide中文版(按需加载资源--下)
- On-Demand Resources Guide中文版(按需加载资源--上)
- MIUI显示悬浮窗
- jqueyr proxy()
- DuiLib : 修改内嵌网页的安全选项
- sequioadb源码分析2
- VisualSVN错误 Cannot query proxy blanket解决办法
- 【黑马程序员】------java基础----String类、StringBuffer(StringBuilder)、基本数据类型包装类
- Qt Quick - 开启下一代交互动效原型设计
- jquer使用 $.parseJSON 使用方法
- MySQL 中 key, primary key ,unique key,index的区别
- GUILayout自动布局
- uCGUI 学习笔记
- UCGUI移植初级解析
- STM32学习——uCGUI移植
- NTP Server disallowed request (authentication?) 问题解决
- UIImage 保存为图片
- --@ui-router--登录页通过路由跳转到内页的demo