Dynamic Programming: Assignment
Dynamic Programming: Assignment
Dynamic Programming: Assignment
Assignment -
Question 1)
Longest Common Subsequence
Code-
//Longest Common Subsequence
//lcs of 'AABCABB' and 'BCABABB'
//assignment 2
#include <bits/stdc++.h>
using namespace std;
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
L[i][j]=0;
}
}
cout<<"DP Table - "<<endl;
for(int j=0;j<=m;j++)
{
for(int h=0;h<=n;h++)
{
cout<<L[j][h]<<" ";
}
cout<<endl;
}
}
int main(){
cout<<"Longest Common Subsequence"<<endl;
char X[]="AABCABB";
char Y[]="BCABABB";
cout<<"String X: "<<X<<endl;
cout<<"String Y: "<<Y<<endl;
int m = strlen(X);
int n = strlen(Y);
lcs(X,Y,m,n);
}
Output:
Longest Common Subsequence
String X: AABCABB
String Y: BCABABB
DP Table -
Iteration - 1
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
Iteration - 2
00000000
00011111
00000000
00000000
00000000
00000000
00000000
00000000
Iteration - 3
00000000
00011111
00011222
00000000
00000000
00000000
00000000
00000000
Iteration - 4
00000000
00011111
00011222
01112233
00000000
00000000
00000000
00000000
Iteration - 5
00000000
00011111
00011222
01112233
01222233
00000000
00000000
00000000
Iteration - 6
00000000
00011111
00011222
01112233
01222233
01233333
00000000
00000000
Iteration - 7
00000000
00011111
00011222
01112233
01222233
01233333
01234444
00000000
Iteration - 8
00000000
00011111
00011222
01112233
01222233
01233333
01234444
01234455
Length of LCS is: 5
LCS of given sequence AABCABB is: BCABB
Question 2)
Longest Palindromic Subsequence
Code:
//Longest Palindromic Subsequence
//lps of 'AABCABB'
//assignment 3
#include <bits/stdc++.h>
return (a>b)?a:b;
int i , cl , j;
for(i=0;i<m+1;i++){
for(j=0;j<m+1;j++){
L[i][j] = 0;
for(i=0;i<=m;i++){
}
//Building dp table from string of length 2 to n
int t=0;
for(cl=2;cl<=m;cl++){
for(i=0;i<=m-cl;i++){
//Optimal substructure
j=i+cl-1;
if(seq[i]==seq[j]&&cl==2){
L[i][j] = 2;
else if(seq[i]==seq[j])
L[i][j] = 2 + L[i+1][j-1];
else
L[i][j] = max(L[i][j-1],L[i+1][j]);
t++;
for(j=0;j<m;j++)
{
for(int h=0;h<m;h++)
cout<<L[j][h]<<" ";
cout<<endl;
// to print lps. lcs of a string and its reverse is lps of that string
int L[m+1][m+1];
int i,j;
for(i=0;i<=m;i++){
for(j=0;j<=m;j++){
if(i==0 || j==0){
L[i][j] = 0;
else if(seq[i-1]==rev[j-1]){
L[i][j] = 1 + L[i-1][j-1];
else{
L[i][j] = max(L[i][j-1],L[i-1][j]);
i=j=m;
char lps[index+1];
lps[index] = '\0';
if(seq[i-1] == rev[j-1]){
lps[index-1] = seq[i-1];
index--;
i--;
j--;
else if(L[i-1][j]>L[i][j-1]){
i--;
else {
j--;
int main(){
char seq[]="AABCABB";
cout<<"String : "<<seq<<endl;
char rev[]="BBACBAA";
int m = strlen(seq);
lps(seq,m);
lcs(seq,rev,m);
return 0;
Output:
Longest Palindromic Subsequence
String : AABCABB
DP Table -
Iteration - 1
1200000
0110000
0011000
0001100
0000110
0000012
0000001
Iteration - 2
1220000
0111000
0011100
0001110
0000112
0000012
0000001
Iteration - 3
1222000
0111300
0011130
0001112
0000112
0000012
0000001
Iteration - 4
1222300
0111330
0011133
0001112
0000112
0000012
0000001
Iteration - 5
1222330
0111333
0011133
0001112
0000112
0000012
0000001
Iteration - 6
1222333
0111333
0011133
0001112
0000112
0000012
0000001
Length of LPS is 3
Question 3)
Matrix Chain Multiplication(MCM)
Code:
//Matrix Chain Multiplication
//assignment 5
#include <bits/stdc++.h>
using namespace std;
#define n 5
if(i == j){
return;
cout<<"(";
print(*((S+i*m)+j)+1 , j , m , S , name );
cout<<")";
int i,d,j,k,min,q;
for(i=1;i<n-d;i++){
j=i+d;
min = MAX;
for(k=i;k<=j-1;k++){
//Optimal Substructure
min = q;
S[i][j] = k;
m[i][j] = min;
for(j=1;j<n;j++)
for(int h=1;h<n;h++)
cout<<m[j][h]<<" ";
}
cout<<endl;
int main(){
int p[]={5,7,9,3,5};
cout<<"Array - ";
for(int i=0;i<sizeof(p)/sizeof(int);i++){
cout<<p[i]<<" ";
cout<<endl;
mcm(p,m,S);
}
Output:
Array - 5 7 9 3 5
Iteration - 1
0 315 0 0
0 0 189 0
0 0 0 135
0000
Iteration - 2
0 315 294 0
0 0 189 294
0 0 0 135
0000
Iteration - 3
0 0 189 294
0 0 0 135
0000
//Assignment 12
#include <bits/stdc++.h>
int main(){
ll n;
cin>>n;
cout<<"Enter array"<<endl;
ll arr[n];
//store maximum sum in max_so_far.calculate sum for each element and store in
max_ending _here.Then compare.
for(ll i=0;i<n;i++){
//Optimal Substructure
max_ending_here += arr[i];
max_so_far = max_ending_here;
start = s;
end = i;
max_ending_here = 0;
s = s+1;
cout<<endl;
cout<<endl;
Output:
Enter size of the array
15
Enter array
Iteration : 1
Iteration : 2
50
Iteration : 3
50 45
Iteration : 4
50 45
Iteration : 5
50 45
Iteration : 6
50 45
Iteration : 7
50 45
Iteration : 8
50 45
Iteration : 9
50 45
Iteration : 10
50 45
Iteration : 11
Iteration : 12
Iteration : 13
Iteration : 14
45 -21 -33 0 9 -56 9 80 76 -88 56 78
Iteration : 15
Question 5)
Element appearing maximum number of times
Code:
//Element which appears maximum number of times.
//Assignment 14
#include <bits/stdc++.h>
int main(){
ll n;
cin>>n;
//Optimal Substructure
count [arr[i]] += 1;
PMax = count[arr[i]];
Pnumber = arr[i];
else{
negative_count[abs(arr[i])] += 1;
Nnumber = arr[i];
else{
return 0;
Output:
Enter size of array
14
41521598653247
The number appearing maximum number of times is 5 and it appears 3 times.
__________________________________________________________________
__________