0% found this document useful (0 votes)
5 views10 pages

LPS 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

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>

using namespace std;


using namespace std::chrono;

int LCS(string X, string Y, int m, int n) {


if (m == 0 || n == 0) {
return 0;
}
if (X[m - 1] == Y[n - 1]) {
return 1 + LCS(X, Y, m - 1, n - 1);
} else {
return max(LCS(X, Y, m - 1, n), LCS(X, Y, m, n - 1));
}
}

int main() {
string X = "ABCBDAB";
string Y = "BDCABC";
int m = X.length();
int n = Y.length();

auto start = high_resolution_clock::now();


int lcs_length = LCS(X, Y, m, n);
auto stop = high_resolution_clock::now();

auto duration = duration_cast<microseconds>(stop - start);

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>

using namespace std;


using namespace std::chrono;

void printTable(vector<vector<int>>& table, int m, int n, const string&


name) {
cout << name << ":" << endl;
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
cout << table[i][j] << " ";
}
cout << endl;
}
}

void printLCS(string X, string Y, vector<vector<int>>& dp,


vector<vector<int>>& lcs_table, int m, int n) {
int i = m, j = n;
string lcs = "";

while (i > 0 && j > 0) {


if (X[i - 1] == Y[j - 1]) {
lcs = X[i - 1] + lcs;
i--;
j--;
} else if (lcs_table[i][j] == 1) {
i--;
} else {
j--;
}
}

cout << "Longest Common Subsequence: " << lcs << endl;
}

int LCS(string X, string Y) {


int m = X.length();
int n = Y.length();

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));


vector<vector<int>> lcs_table(m + 1, vector<int>(n + 1, 0));

for (int i = 1; i <= m; i++) {


for (int j = 1; j <= n; j++) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
lcs_table[i][j] = 1; // Indicates a match was found
} else {
if (dp[i - 1][j] >= dp[i][j - 1]) {
dp[i][j] = dp[i - 1][j];
lcs_table[i][j] = 2; // Indicates coming from top
} else {
dp[i][j] = dp[i][j - 1];
lcs_table[i][j] = 3; // Indicates coming from left
}
}
}
}

printTable(dp, m, n, "DP Table");


printTable(lcs_table, m, n, "LCS Table");

cout << "Length of LCS: " << dp[m][n] << endl;

printLCS(X, Y, dp, lcs_table, m, n);

return dp[m][n];
}

int main() {
string X = "ABCBDAB";
string Y = "BDCABC";

auto start = high_resolution_clock::now();


LCS(X, Y);
auto stop = high_resolution_clock::now();

auto duration = duration_cast<microseconds>(stop - start);


cout << "Time taken by function: " << duration.count() << "
microseconds" << endl;

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>

using namespace std;


using namespace std::chrono;

int LCSHelper(string &X, string &Y, int m, int n, vector<vector<int>>


&dp) {
if (m == 0 || n == 0) {
return 0;
}

if (dp[m][n] != -1) {
return dp[m][n];
}

if (X[m - 1] == Y[n - 1]) {


dp[m][n] = 1 + LCSHelper(X, Y, m - 1, n - 1, dp);
} else {
dp[m][n] = max(LCSHelper(X, Y, m - 1, n, dp), LCSHelper(X, Y,
m, n - 1, dp));
}

return dp[m][n];
}

void printMemoTable(vector<vector<int>> &dp, int m, int n) {


cout << "Memoization Table:" << endl;
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
if (dp[i][j] == -1)
cout << "X ";
else
cout << dp[i][j] << " ";
}
cout << endl;
}
}

int LCS(string X, string Y) {


int m = X.length();
int n = Y.length();

vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));

int lcs_length = LCSHelper(X, Y, m, n, dp);


printMemoTable(dp, m, n);
return lcs_length;
}

int main() {
string X = "ABCBDAB";
string Y = "BDCABC";

auto start = high_resolution_clock::now();


int lcs_length = LCS(X, Y);
auto stop = high_resolution_clock::now();

cout << "Length of LCS: " << lcs_length << endl;

auto duration = duration_cast<microseconds>(stop - start);


cout << "Time taken by function: " << duration.count() << "
microseconds" << endl;

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>

using namespace std;


using namespace std::chrono;

void printLCS(string X, string Y, vector<vector<int>> &dp, int m, int


n) {
string lcs = "";
int i = m, j = n;

while (i > 0 && j > 0) {


if (X[i - 1] == Y[j - 1]) {
lcs = X[i - 1] + lcs;
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}

cout << "Longest Common Subsequence: " << lcs << endl;
}

int LCS(string X, string Y) {


int m = X.length();
int n = Y.length();

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

for (int i = 1; i <= m; i++) {


for (int j = 1; j <= n; j++) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

printLCS(X, Y, dp, m, n);

return dp[m][n];
}

int main() {
string X = "ABCBDAB";
string Y = "BDCABC";

auto start = high_resolution_clock::now(); // Start time


int lcs_length = LCS(X, Y);
auto stop = high_resolution_clock::now(); // End time

cout << "Length of LCS: " << lcs_length << endl;

auto duration = duration_cast<microseconds>(stop - start);


cout << "Time taken by function: " << duration.count() << "
microseconds" << endl;

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>

using namespace std;


using namespace std::chrono;

void printLIS(vector<int> &X, vector<int> &dp, int n) {


int lis_length = *max_element(dp.begin(), dp.end());
vector<int> lis_sequence;
int current_length = lis_length;

for (int i = n - 1; i >= 0; i--) {


if (dp[i] == current_length) {
lis_sequence.push_back(X[i]);
current_length--;
}
}

reverse(lis_sequence.begin(), lis_sequence.end());

cout << "Longest Increasing Subsequence: ";


for (int num : lis_sequence) {
cout << num << " ";
}
cout << endl;
}

int LIS(vector<int> &X) {


int n = X.size();
vector<int> dp(n, 1);

for (int i = 1; i < n; i++) {


for (int j = 0; j < i; j++) {
if (X[i] > X[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
}
}
}

printLIS(X, dp, n);

return *max_element(dp.begin(), dp.end());


}

int main() {
vector<int> X = {10, 22, 9, 33, 21, 50, 41, 60, 80};

auto start = high_resolution_clock::now(); // Start time


int lis_length = LIS(X);
auto stop = high_resolution_clock::now(); // End time

cout << "Length of LIS: " << lis_length << endl;

auto duration = duration_cast<microseconds>(stop - start);


cout << "Time taken by function: " << duration.count() << "
microseconds" << endl;

return 0;
}
Output:

You might also like