Longest Common Subsequence
By
Asif Shahriar
ID: 171-15-8617
#include<stdio.h>
int main()
{
int i=0,j=0,x[100],y[100],c[100][100],mx,ny,k;
printf("Enter the first(x) string lenght: ");
scanf("%d",&mx);
printf("Enter the first(x) string : ");
for(k=1;k<=mx;k++)
{
scanf("%d",&x[k]);
}
printf("Enter the second(y) string lenght: ");
scanf("%d",&ny);
printf("Enter the second(y) string : ");
for(k=1;k<=ny;k++)
{
scanf("%d",&y[k]);
}
for(i=1;i<=mx;i++)
{
c[i][0]=0;
c[0][i]=0;
}
#include<stdio.h>
int main()
{
int i=0,j=0,x[100],y[100],c[100][100],mx,ny,k;
printf("Enter the first(x) string lenght: ");
scanf("%d",&mx);
printf("Enter the first(x) string : ");
for(k=1;k<=mx;k++)
{
scanf("%d",&x[k]);
}
printf("Enter the second(y) string lenght: ");
scanf("%d",&ny);
printf("Enter the second(y) string : ");
for(k=1;k<=ny;k++)
{
scanf("%d",&y[k]);
}
for(i=1;i<=mx;i++)
{
c[i][0]=0;
c[0][i]=0;
}
#include<stdio.h>
int main()
{
int i=0,j=0,x[100],y[100],c[100][100],mx,ny,k;
printf("Enter the first(x) string lenght: ");
scanf("%d",&mx);
printf("Enter the first(x) string : ");
for(k=1;k<=mx;k++)
{
scanf("%d",&x[k]);
}
printf("Enter the second(y) string lenght: ");
scanf("%d",&ny);
printf("Enter the second(y) string : ");
for(k=1;k<=ny;k++)
{
scanf("%d",&y[k]);
}
for(i=1;i<=mx;i++)
{
c[i][0]=0;
c[0][i]=0;
}
#include<stdio.h>
int main()
{
int i=0,j=0,x[100],y[100],c[100][100],mx,ny,k;
printf("Enter the first(x) string lenght: ");
scanf("%d",&mx);
printf("Enter the first(x) string : ");
for(k=1;k<=mx;k++)
{
scanf("%d",&x[k]);
}
printf("Enter the second(y) string lenght: ");
scanf("%d",&ny);
printf("Enter the second(y) string : ");
for(k=1;k<=ny;k++)
{
scanf("%d",&y[k]);
}
for(i=1;i<=mx;i++)
{
c[i][0]=0;
c[0][i]=0;
}
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
i=1 j=1
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0
i=1 j=1
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0
i=1 j=2
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0 0
i=1 j=3
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0 0 1
i=1 j=4
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0 0 1 1
i=1 j=5
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0 0 1 1
1
i=2 j=1
for(i=1;i<=mx;i++)
{
for(j=1;j<=ny;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
}
else
{
c[i][j]=c[i][j-1];
}
}
}
0 0 0 1 1
1 1 1 1 2
1 1 2 2 2
1 1 2 2 3
printf("The lenght of LCS is %d",c[mx][ny]);
0 0 0 1 1
1 1 1 1 2
1 1 2 2 2
1 1 2 2 3
0/1 Knapsack
By
Asif Shahriar
ID: 171-15-8617
0 1 2 3 4 5
0 0 0 0 0 0
0 0
0
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 1
w = 1
wi = 2
bi = 3
w-wi = -1
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3
0
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 1
w = 1
wi = 2
bi = 3
w-wi = -1
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3
0
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 1
w = 2
wi = 2
bi = 3
w-wi = 0
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 1
w = 2,3,4,5
wi = 2
bi = 3
w-wi = 0
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 2
w = 1
wi = 3
bi = 4
w-wi = -2
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 2
w = 2
wi = 3
bi = 4
w-wi = -1
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3 4
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 2
w = 3
wi = 3
bi = 4
w-wi = 0
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3 4 4
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 2
w = 4
wi = 3
bi = 4
w-wi = 1
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3 4 4 7
0
0
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
i = 2
w = 5
wi = 3
bi = 4
w-wi = 2
if wi<=w and b1 + V[i-1, w-wi] > V[i-1,w]
V[i,w] = b1 + V[i-1,w-wi]
else
V[i,w] = V[ i-1,w]
0 1 2 3 4 5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3 4 4 7
0 0 3 4 5 7
0 0 3 4 5 7
0
1
2
3
4
weight
I
t
e
m
Item 1 2 3 4
Weight 2 3 4 5
Value 3 4 5 6
Max Value

Longest common sub sequence & 0/1 Knapsack