LPS 3
LPS 3
LPS 3
Eniya Sre AG
22BPS1002
2)Given two sequences X =< x1, x2, ..., xm > and Y =< y1, y2, ..., yn >, design a brute-force recursive
algorithm to compute the maximum-length common subsequence of X and Y . Analyse your
algorithm with all the required components .
#include <iostream>
#include <string>
#include <algorithm>
#include <chrono>
int main() {
string X = "ABCBDAB";
string Y = "BDCABC";
int m = X.length();
int n = Y.length();
cout << "Length of LCS is: " << lcs_length << endl;
cout << "Time taken by function: " << duration.count() << "
microseconds" << endl;
return 0;
}
Output:
3) Given two sequences X =< x1, x2, ..., xm > and Y =< y1, y2, ..., yn >, design a bottom-up dynamic
programming pseudocode to compute the maximum-length common subsequence of X and Y . Your
code should print the two tables, the maximum-length of the common subsequence and the
common subsequence. Analyse your pseudocode with all the required components .
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <chrono>
cout << "Longest Common Subsequence: " << lcs << endl;
}
return dp[m][n];
}
int main() {
string X = "ABCBDAB";
string Y = "BDCABC";
return 0;
}
Output:
4) Given two sequences X =< x1, x2, ..., xm > and Y =< y1, y2, ..., yn >, design a top-down memoized
dynamic programming pseudocode to compute the maximum-length common subsequence of X and
Y . Your pseudocode should print a table (used for the memoization process) and the
maximumlength of the common subsequence.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <chrono>
if (dp[m][n] != -1) {
return dp[m][n];
}
return dp[m][n];
}
int main() {
string X = "ABCBDAB";
string Y = "BDCABC";
return 0;
}
Output:
5)Modify the bottom-up dynamic programming algorithm to compute the longest common
subsequence of the two sequences X and Y such that the modified algorithm uses only one table
(i.e., the c-table ) and out put the maximum-length common subsequence along with the
subsequence
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <chrono>
cout << "Longest Common Subsequence: " << lcs << endl;
}
return dp[m][n];
}
int main() {
string X = "ABCBDAB";
string Y = "BDCABC";
return 0;
}
6) Given a sequence of n numbers, X =< x1, x2, ..., xn >, write a pseudocode and an appropriate
algorithm to compute the longest monotnically increasing subsequence of X. Given X =< 10, 22, 9,
33, 21, 50, 41, 60, 80 >, the longest monotonically increasing subsequence of X is 6 and the longest
subsequence is < 10, 22, 33, 50, 60, 80 >.
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
reverse(lis_sequence.begin(), lis_sequence.end());
int main() {
vector<int> X = {10, 22, 9, 33, 21, 50, 41, 60, 80};
return 0;
}
Output: