ASSIGNMENT – 6
Vishw Vekariya
U21CS104
1.) CODE:
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
//bottom-up DP
int LPSBU(string a, string b) {
vector<vector<int>> dp(a.size()+ 1, vector<int>(b.size()+ 1,0));
for(int i=0; i<a.size(); i++)
{
for(int j=0; j<b.size(); j++)
{
if(a[i] == b[j])
{
dp[i+1][j+1] = dp[i][j]+1;
}
else
{
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
}
}
}
return dp[a.size()][b.size()];
}
//iterative approach
int LPSI(string a, string b)
{
vector<int> cur(b.length()+ 1, 0);
vector<int> next(b.length()+ 1, 0);
for (int i = a.length() -1; i >= 0; i--)
{
for (int j = b.length() -1; j >= 0; j--)
{
int ans =0;
if(a[i] == b[j])
{
ans = 1+ next[j+1];
}
else
{
ans = max(next[j], cur[j+1]);
}
cur[j] = ans;
}
next = cur;
}
return next[0];
}
//top-down DP
int LPSTD(string &a, string &b, int i, int j, vector<vector<int>>&dp)
{
//base case
if(i == a.length())
{
return 0;
}
if(j == b.length())
{
return 0;
}
if (dp[i][j] != -1)
{
return dp[i][j];
}
int ans =0;
if (a[i] == b[j])
{
ans = 1+ LPSTD(a, b, i+1, j+1, dp);
}
else
{
ans = max(LPSTD(a, b, i, j+1, dp), LPSTD(a, b, i+1, j, dp));
}
return dp[i][j] = ans;
}
//recursive approach
int LPSR(string &a, string &b, int i, int j)
{
//base case
if(i == a.length())
{
return 0;
}
if(j == b.length())
{
return 0;
}
int ans =0;
if (a[i] == b[j])
{
ans = 1+ LPSR(a, b, i+1, j+1);
}
else
{
ans = max(LPSR(a, b, i, j+1), LPSR(a, b, i+1, j));
}
return ans;
}
int main(int argc, char const *argv[])
{
string a;
a = "bbbabb";
string b = a;
reverse(b.begin(),b.end());
vector<vector<int>> dp(a.length(), vector<int> (b.length(), -1));
cout<<"The length of longest palindrome subsequence is :
"<<LPSR(a,b,0,0)<<endl;
cout<<"The length of longest palindrome subsequence is : "<<LPSTD(a, b, 0,
0, dp)<<endl;
cout<<"The length of longest palindrome subsequence is : "<<LPSBU(a,
b)<<endl;
cout<<"The length of longest palindrome subsequence is :
"<<LPSI(a,b)<<endl;
return 0;
}
OUTPUT:
2.)
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int editRecursive(string str1, string str2, int m, int n)
{
if (m == 0)
return n;
if (n == 0)
return m;
if (str1[m - 1] == str2[n - 1])
return editRecursive(str1, str2, m - 1, n - 1);
else
{
int mini = min(editRecursive(str1, str2, m, n - 1),
editRecursive(str1, str2, m - 1, n));
mini = min(mini, editRecursive(str1, str2, m - 1, n - 1));
return 1 + mini;
}
}
int edittopDown(string str1, string str2, int n, int m, vector<vector<int> >
&dp)
{
if (n == 0)
return m;
if (m == 0)
return n;
if (dp[n][m] != -1)
return dp[n][m];
if (str1[n - 1] == str2[m - 1])
return dp[n][m] = edittopDown(str1, str2, n - 1, m - 1, dp);
else
{
int mini = min(edittopDown(str1, str2, n, m - 1, dp),
edittopDown(str1, str2, n - 1, m, dp));
mini = min(mini, edittopDown(str1, str2, n - 1, m - 1, dp));
return dp[n][m] = (1 + mini);
}
}
int editBottomUp(string str1, string str2, int n, int m, vector<vector<int> >
&dp)
{
for (int i = 0; i <= n; i++)
dp[i][0] = i;
for (int i = 0; i <= m; i++)
dp[0][i] = i;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (str1[i - 1] == str2[j - 1])
{
dp[i][j] = dp[i - 1][j - 1];
}
else
{
dp[i][j] = 1 + min(dp[i][j - 1], min(dp[i - 1][j - 1], dp[i -
1][j]));
}
}
}
return dp[n][m];
}
int editIterative(string str1, string str2, int n, int m)
{
vector<int> prev(m + 1, 0), curr(m + 1, 0);
for (int j = 0; j <= m; j++)
{
prev[j] = j;
}
for (int i = 1; i <= n; i++)
{
curr[0] = i;
for (int j = 1; j <= m; j++)
{
if (str1[i - 1] == str2[j - 1])
{
curr[j] = prev[j - 1];
}
else
{
int mn = min(1 + prev[j], 1 + curr[j - 1]);
curr[j] = min(mn, 1 + prev[j - 1]);
}
}
prev = curr;
}
return prev[m];
}
int main()
{
string a = "tis";
string b = "nisarg";
int n = a.length();
int m = b.length();
vector<vector<int> > dp(n + 1, vector<int>(m + 1, -1));
cout << editRecursive(a,b,n,m)<<endl;
cout << edittopDown(a,b,n,m,dp) << endl;
cout << editBottomUp(a,b,n,m,dp) << endl;
cout << editIterative(a,b,n,m) << endl;
return 0;
}
OUTPUT: