0% found this document useful (0 votes)
11 views7 pages

Experiment No.2

Uploaded by

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

Experiment No.2

Uploaded by

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

EXPERIMENT No.

AIM: Program to implement LCS problem in C/Java/Python.

THEORY:

The longest common subsequence problem is finding the longest sequence which exists in both
the given strings.

Subsequence

Let us consider a sequence S = <s1, s2, s3, s4, …,sn>.

A sequence Z = <z1, z2, z3, z4, …,zm> over S is called a subsequence of S, if and only if it can be
derived from S deletion of some elements.

Common Subsequence

Suppose, X and Y are two sequences over a finite set of elements. We can say that Z is a common
subsequence of X and Y, if Z is a subsequence of both X and Y.

Longest Common Subsequence

If a set of sequences are given, the longest common subsequence problem is to find a common
subsequence of all the sequences that is of maximal length.

ALGO:

function LCS Length(X[1..m], Y[1..n])

C = array(0..m, 0..n)

for i := 0..m

C[i,0] = 0

for j := 0..n

C[0,j] = 0

for i := 1..m

for j := 1..n

if X[i] = Y[j]
C[i,j] := C[i-1,j-1] + 1

else

C[i,j] := max(C[i,j-1], C[i-1,j])

return C[m,n]

ANALYSIS:

1) The time complexity of above algorithm is O (mn)

Code:
#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i,j,m,n,k;

int b[100][100],c[100][100];

char x[100],y[100];

char t[100],p[100];

clrscr();

printf("\n Enter the first string:");

gets(x);

printf("\n Enter the second string:");

gets(y);

printf("1 is vertical,0 is Horizontal,-1 is diagonal:");

if(strlen(x)>strlen(y))
{

m=strlen(x);

n=strlen(y);

else

m=strlen(y);

n=strlen(x);

for(i=0;i<=m;i++)

c[i][0]=0;

for(j=0;j<=n;j++)

c[0][j]=0;

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

for(j=1;j<=n;j++)

if(x[i-1]==y[j-1])

c[i][j]=c[i-1][j-1]+1;

b[i][j]=-1;
}

else if(c[i-1][j]>=c[i][j-1])

c[i][j]=c[i-1][j];

b[i][j]=1;

else

c[i][j]=c[i][j-1];

b[i][j]=0;

printf("\n The cost is:\n");

for(i=0;i<=m;i++)

for(j=0;j<=n;j++)

printf("\t%d",c[i][j]);

printf("\n");

printf("\n The direction is:\n");

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

{
for(j=1;j<=n;j++)

printf("\t %d",b[i][j]);

printf("\n");

i=m;

j=n;

k=0;

while(i>=0 || j>=0)

if(b[i][j]==1)

i--;

else if(b[i][j]==0)

j--;

else

t[k]=y[j-1];

k++;

i--;

j--;

strrev(t);

printf("\n The path is:%s",t);


printf("\n The length is %d",strlen(t));

getch();

/*Use below Input and execute your code and attach output screenshot below*/

Enter the first string: SAVANNAH

Enter the second string: HAVANA

1 is vertical, 0 is Horizontal,-1 is diagonal

*/

Output:-

You might also like