0% found this document useful (0 votes)
10 views5 pages

Daa Lab 8

The document discusses two algorithms - finding the longest common subsequence of two strings and matrix chain multiplication. It contains code to implement both algorithms and print outputs like the DP arrays, solution lengths/strings, cost matrix, split matrix and optimal parenthesis formation.

Uploaded by

Kamlesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Daa Lab 8

The document discusses two algorithms - finding the longest common subsequence of two strings and matrix chain multiplication. It contains code to implement both algorithms and print outputs like the DP arrays, solution lengths/strings, cost matrix, split matrix and optimal parenthesis formation.

Uploaded by

Kamlesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DAA

LAB-08
TASK – 01
Largest Common Subsequence
Input

#include<iostream>
#include<vector>
#include<string>

int main(){
std::cout<<"Name : Kamlesh Kumar\nRoll No : 22103081\n";
std::string s1="BDCABA";
std::string s2="ABCBDAB";
int n1=s1.length(),n2=s2.length();

std::vector<std::vector<int>> dp(n1+1, std::vector<int>(n2+1, 0));

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


for(int j=1; j<=n2; j++){
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = std::max(dp[i-1][j], dp[i][j-1]);
}
}

std::cout<< "DP Array:" << std::endl;


for(int i=0; i<=n1; i++){
for(int j=0; j<=n2; j++){
std::cout << dp[i][j] << " ";
}
std::cout << std::endl;
}

std::cout<<"Longest Common Subsequence Length: " << dp[n1][n2] << std::endl;

std::string lcs = "";


int i = n1, j = n2;
while (i > 0 && j > 0) {
if (s1[i-1] == s2[j-1]) {
lcs = s1[i-1] + lcs;
i--;
j--;
}
else if (dp[i-1][j] >= dp[i][j-1])
i--;
else
j--;
}

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


}
Output
TASK - 02
Matrix Chain Multiplication
Input

#include<iostream>
#include<vector>

void paranthesis (std::vector<std::vector<int>>& Split, int i, int j){


if(i == j) {
std::cout << " A" << i+1<<" ";
return;
}
std::cout << "(";
paranthesis(Split, i, Split[i][j]);
paranthesis(Split, Split[i][j]+1, j);
std::cout << ")";
}

int main(){
std::cout<<"Name : Kamlesh Kumar\nRoll No : 22103081\n";

std::vector D={6,2,3,5,7};
int n=D.size();

std::vector<std::vector<int>> Cost(n-1, std::vector<int>(n-1, 0));


std::vector<std::vector<int>> Split(n-1, std::vector<int>(n-1, 0));

int i=0, j=1;


while(i<n-1 || i+j<n-1){
if(i >= n-1 || i+j>=n-1){
i=0;
j++;
if(j >= n-i) break;
continue;
}

Cost[i][i+j] = INT_MAX;
for(int k=i; k<i+j; k++){
int newCost= Cost[i][k] + Cost[k+1][i+j] + D[i]*D[k+1]*D[i+j+1] ;
if(newCost < Cost[i][i+j]){
Cost[i][i+j] = newCost;
Split[i][i+j] = k;
}
}
i++;

std::cout << "\nCost Matrix:\n";


for (int i = 0; i < Cost.size(); i++)
{
for (int j = 0; j < Cost[0].size(); j++)
{
std::cout << Cost[i][j] << " ";
}
std::cout << "\n";
}

std::cout << "\nSplit Matrix:\n";


for (int i = 0; i < Split.size(); i++)
{
for (int j = 0; j < Split[0].size(); j++)
{
std::cout << Split[i][j] << " ";
}
std::cout << "\n";
}

std::cout << "\nResult:\n";


paranthesis (Split, 0, Split[0].size()-1);

Output

You might also like