Program For Finding Longest Common Subsequence: Dprogram 1
Program For Finding Longest Common Subsequence: Dprogram 1
Program For Finding Longest Common Subsequence: Dprogram 1
#include<stdio.h>
#include<curses.h>
#include<string.h>
#include<process.h>
void print_lcs(int b[10][10],char x[10],int i, int j);
int main()
{
clrscr();
int a,I,j,m,n,g,h,b[10][10];
char c[10][10];
char x[10],y[10];
printf(“\n Enter the length of sequence of x: “);
scanf(“%d”,&g);
printf(“\n Enter the length of sequence of y: “);
scanf(“%d”,&h);
printf(“\n Enter sequence x: “);
for(a=1;a<=g;a++)
scanf(“%d”,&x[a]);
printf(“\n Enter sequence y: “);
for(a=1;a<=h;a++)
scanf(“%d”,&y[a]);
for(i=1;i<=g;i++)
c[i][0]=0;
for(j=1;j<=h;j++)
c[0][j]=0;
for(i=1;i<=g;i++)
{
for(j=1;j<=h;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=0;
}
else
{
if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=1;
}
else if(b[i][j]=2)
print_lcs(b,x,I,j-1);
}
#include<iostream.h>
int m[20][20];
int num=1;
for(l=1;l<n;l++)
{
for(i=0,j=l;i<n-l;i++,j++)
{
min=(m[i][i]+m[i+1][j]+(p[i]*p[i+1]*p[j+1]));
flag=i;
for(k=i+1;k<j;k++)
{
temp=(m[i][k]+m[k+1][j]+
(p[i]*p[k+1]*p[j+1]));
if(min>temp)
{
min=temp;
flag=k;
}
}
mat_disp(0,n-1);
}
int main()
{
int n;
int p[20];
cout<<"\n Enter the number of matrices to be multplied :
";
cin>>n;
cout<<"\n Enter the order sequence of matrices : ";
for(int i=0;i<=n;i++)
cin>>p[i];
cout<<"\n Optimal Pranthesization is:";
mat_chain(n,p);
return 0;
}
#include <iostream.h>
int main()
{
int A[10];
int min(int *), minm;
int max(int *), maxm;
cout<<"Enter the values of the array\n";
for(int i=0;i<10;i++)
cin>>A[i];
minm=min(A);
maxm=max(A);
cout<<"\nMinimum = "<<minm<<endl;
cout<<"\nMaximum = "<<maxm<<endl;
system("PAUSE");
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char mstr[20];
char pstr[20];
int n,m;
int s,i,flag=0,j;
cout<<"\n\nEnter Main String : ";
cin>>mstr;
cout<<"\n\nEnter Pattern String : ";
cin>>pstr;
n=strlen(mstr);
m=strlen(pstr);
for(s=0;s<=(n-m);s++)
{
flag=0;
j=0;
while(!flag && (j<m))
{
if(pstr[j]!=mstr[j+s])
flag=1;
else
j++;
}
if(!flag)
cout<<"\nPattern Exists With
Shift :"<<s<<"\n";
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
double mstr;
int pstr;
int j;
cout<<"\nEnter Main Text : ";
cin>>mstr;
cout<<"\nEnter Pattern Text : ";
cin>>pstr;
int p=pstr % 13;
int q=0;
//To Know The No. Of Digits In Pattern Text
int n=pstr;
int dp=0;
while(n)
{
n=n/10;
dp++;
}
//This Logic Ends Here
//To Know The No. Of Digits In Main Text
int m=mstr;
int dm=0;
while(m)
{
m=m/10;
dm++;
}
//This Logic Ends Here
int s=0;
double marray[20],parray[20];
m=mstr;n=pstr;
int d,i;
for(i=dm;i>=0;i--)
for(s=1;s<=(dm-dp+1);s++)
{
i=dp+s-1;
j=0;
n=0;
while(i>=s)
{
n=n+(pow(10,j)*marray[i]);
i--;
j++;
}
q=int(n) % 13;
if(q==p) //checking the case of spurious hit
{
int flag=0;
int k=1;
while(!flag && (k<=dp))
{
if(parray[k]!=marray[k+s-1])
flag=1;
else
k++;
}
if(!flag)
cout<<"\nPattern Exists With Shift :"<<s-1<<"\n";
}
}
getch();
}
#include<stdio.h>
#include<curses.h>
#include<process.h>
void sort(float t[10],int n,float p[10],float w[10]);
int main()
{
clrscr();
int i,n,m,s,t;
float x[10],w[10],u,p[10],sum=0.0,tr[10];
printf(“\n Enter the size of knapsack bag: “);
scanf(“%d”,&m);
printf(“\n Enter the no. of items: “);
scanf(“%d”,&n);
printf(“\n Enter the weight of each items: “);
for(i=0;i<n;i++)
{
printf(“\nw[%d]: “,i);
scanf(“%d”,&w[i]);
}
printf(“\n Enter the profit of each items: “);
for(i=0;i<n;i++)
{
printf(“\np[%d]: “,i);
scanf(“%d”,&p[i]);
}
for(i=0;i<n;i++)
x[i]=0.0;
u=m;
for(s=0;s<n;s++)
{
tr[s]=p[s]/w[s];
}
sort(tr,n,p,w);
for(i=0;i<n;i++)
{
if(u>=w[i])
{
x[i]=1;
u=u-w[i];
}
}
w[0] = 18
w[1] = 15
w[2] = 10
p[0] = 25
p[1] = 24
p[2] = 15
Profit: 31.5
#include<stdio.h>
#include<curses.h>
#include<stdlib.h>
int min(int e,int f);
int main()
{
clrscr();
int n,m,b,c,d,i,j,a[10][10],pi[10][10];
printf(“\n Enter the no. of rows and column: “);
scanf(“%d %d”,&m,&n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf”%d”,&a[i][j]);
}
for(i=1;i<=m;i++)
{
printf(“\n”);
for(j=1;j<=n;j++)
{
if(i==j |\ a[i][j]>=100)
p[i][j]=0;
else
pi[i][i]=i;
printf(“ %d”,pi[i][j]);
}
}
for(b=1;b<=m;b++)
{
for(c=1;c<=m;c++)
{
for(d=1;d<=m;d++)
{
if(a[c][d]<=a[c][b]+a[b][d])
pi[c][d]=pi[b][d];
a[c][d]=min(a[c][d],a[c][b]+a[b][d]);
}
}
}
0 1 -3 2 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
0 3 4 5 1
4 0 4 2 1
4 3 0 2 1
4 3 4 0 1
4 3 4 5 0
#include<stdio.h>
#include<curses.h>
int main()
{
clrscr();
int n,s[15],f[15],m,i;
char A[15];
printf(“\n Enter the no. of activities: “);
scanf(“%d”,&n);
printf(“\n Enter the start values of activities: “);
for(i=1;i<=n;i++)
scanf(“%d”,&s[i]);
printf(“\n Enter the finishing values of activities:
“);
for(i=1;i<=n;i++)
scanf(“%d”,&f[i]);
for(i=1;i<=n;i++)
{
A[i]=96+i;
printf(“\n %s=(%d,%d)”,A[i],i,f[i]);
}
i=1;
printf(“ \n “);
printf(A[1]);
for(m=2;m<=n;m++)
{
if(s[m]>=f[i])
{
i=m;
printf(A[m]);
}
}
int getch();
}
a = (1,4)
b = (3,5)
c = (0,6)
d = (5,7)
e = (3,8)
#include<stdio.h>
#include<curses.h>
void relax(intd[10],int w[25][25],int p[10],int t);
int main()
{
clrscr();
int n,I,j,wt[25][25],d[10],pi[10],x;
printf(“\n Enter the no. of vertices (0<n<6): “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
printf(“\nweight[%d][%d]: “,I,j);
scanf(“%d”,&wt[i][j]);
}
}
}
printf(“\n Assume source node is 0th vertex “);
for(x=1;x<n;x++)
{
d[x]=100;
pi[x]=NULL;
}
d[0]=0;
pi[0]=NULL;
relax(d,wt,pi,n);
printf(“\n This is the path followed “);
int getch();
}
weight[0][1]: 5
weight[0][2]: 7
weight[1][0]: 0
weight[1][2]: -2
weight[2][0]: 0
weight[2][1]: 0
#include<iostream.h>
#include<conio.h>
int m3[2][2],m1[2][2],m2[2][2];
int p[6];
m3[0][0]=p[4]+p[3]-p[1]+p[5];
m3[0][1]=p[0]+p[1];
m3[1][0]=p[2]+p[3];
m3[1][1]=p[4]+p[0]-p[2]-p[6];
int main( )
{
clrscr( );
int i,j;
mat_multiply(m1,m2);
getch( );
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int k,a[5],i,j,temp;
for(i=0;i<5;i++)
{
cout<<"\nEnter value of array a["<<i<<"] : ";
cin>>a[i];
}
cout<<"\nArray after sorting is: ";
for(k=1;k<5;k++)
{
temp=a[k];
j=k-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
for(i=0;i<5;i++)
cout<<"\n value of array a["<<i<<"] : "<<a[i];
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int arr[100];
void merge(int f1, int l1,int f2,int l2)
{
int i=f1;
int j=f2;
int temp[100],k=0;
while(i<=l1 && j<=l2)
{
if(arr[i]<arr[j])
{
temp[k]=arr[i];
i++,k++;
}
else
{
temp[k]=arr[j];
j++,k++;
}
}
while(i<=l1)
{
temp[k]=arr[i];
k++,i++;
}
while(j<=l2)
{
temp[k]=arr[j];
k++,j++;
}
for(i=f1,k=0;i<=l2;i++,k++)
arr[i]=temp[k];
}
void mergesort(int f,int l)
{
int m;
if(l>f)
{
m=(f+l)/2;
#include <iostream>
#define n 10
int main()
{
int A[n]={45,92,37,33,95,78,26,48,82,63};
void quicksort(int [n], int, int);
quicksort(A,0,n-1);
cout<<"The sorted array is";
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
system("PAUSE");
return 0;
}
#include <iostream>
#define n 10
int main()
{
int A[n]={45,92,37,33,95,78,26,48,82,63};
void quicksort(int [n], int, int);
quicksort(A,0,n-1);
cout<<"The sorted array is";
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
system("PAUSE");
return 0;
}
#include<iostream.h>
#include<string.h>
if(b[i][j]=='a')
{
print_lcs(b,x,i-1,j-1);
cout<<x[i];
}
else if(b[i][j]=='b')
{
print_lcs(b,x,i-1,j);
}
else
print_lcs(b,x,i,j-1);
return 0;
}
int main()
{
char x[20],y[20],c[20][20];
int m,n,i,j;
char b[20][20];
for(i=0;i<n;i++)
{
cin>>y[i];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][-1]=0;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[-1][j]=0;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='a';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='b';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='c';
}
}
cout<<"\nLCS is...";
print_lcs(b,x,i,j);
return 0;
}
LCS is: b c b a
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],n,i,j,p,d,temp,med1,med2,q;
clrscr();
printf("enter the no. of elements:\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("the elements after sorting are :\n");
for(i=0;i<n;i++)
printf("%d,",a[i]);
d=n%2;
if(d==0)
{
p=n/2;
q=((n/2)-1);
med1=a[p];
med2=a[q];
}
else
{
p=(n-1)/2;
med1=a[p];
}
getch();
}
# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
void Table(int , int matrix [size][size], struct Vertex
vert[size]);
struct Edge *Insert_Vertex (int , struct Edge *);
void DFS ( int , int *dist, struct Vertex vert [size]);
void Input(int, int a [size][size]);
void Output(int, int a [size][size]);
/* Initializing entries */
/* Output function */
void Output(int number, int a [size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for (i = 0; i < number; i++)
{
for (j = 0; j < number; j ++)
{
printf(" %d", a [i][j]);
}
printf("\n");
}
}
/* Function main */
void main()
{
int i;
int number, index, dist;
int a [size][size];
struct Vertex vert [size];
struct Edge *List;
printf("\n Input the number of vertices in the
graph: ");
scanf("%d", &number);
Input(number, a);
Output(number, a);
Table(number, a, vert);
printf("\n Input the starting vertex 0- %d:",
number-1);
scanf("%d", &index);
dist = 0;
DFS (index, &dist, vert);
3
2
3
4
5
4
Adjacency matrix
1 2 1
3 2 3
4 5 4
A 1 A B C
B 0 A B C
C 2 A B C
# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
struct Q
{
int info;
struct Q *next;
};
/* Initializing entries */
/* Function main */
void main()
{
int i, number, index;
int mat [size][size];
struct Vertex vert [size];
struct Edge *List;
printf("\n Input the number of vertices in the
graph: ");
scanf("%d", &number);
Input(number, mat);
Output(number, mat);
Table(number, mat, vert);
printf("\n Input the starting vertex 0-
%d :",number-1);
scanf("%d", &index);
BFS (index, vert);
1
2
1
4
5
4
Adjacency matrix
2 3 2
1 2 1
4 5 4
A 1 A B C
B 1 A B C
C 0 A B C
#include<stdio.h>
#include<curses.h>
void relax(int d[10],int w[25][25],int p[10],int t);
int main()
{
clrscr();
int n,i,j,wt[25][25],d[10],pi[10],x;
printf(“\n Enter the no. of vertices (0<n<6): “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
printf(“\nweight[%d][%d]: “,i,j);
scanf(“%d”,&wt[i][j]);
}
}
}
printf(“\n Assume source node is 0th vertex “);
for(x=1;x<n;x++)
{
d[x]=100;
pi[x]=NULL;
}
d[0]=0;
pi[0]=NULL;
relax(d,wt,pi,n);
printf(“\n This is the path followed “);
int getch();
}
weight[0][1]: 5
weight[0][2]: 7
weight[1][0]: 0
weight[1][2]: -2
weight[2][0]: 0
weight[2][1]: 0
# include<stdio.h>
# define size 20
# define P 1
# define T 0
# define infinity 9999
struct Label
{
int predecessor;
int length;
int flag;
};
k = s;
do
{
/* Check all the paths from kth vertex and find
their distance from k vertex */
for ( i = 1; i <= n; i++)
{
if ((a[k][i] > 0) && (state[i].flag == T))
{
if ((state[k].length + a[k][i]) <
state[i].length)
{
state[i].predecessor = k;
state[i].length = state[k].length +
a[k][i];
}
}
}
min = infinity;
k = 0;
for ( i =1; i <= n; i++)
{
if ((state[i].flag == T) && (state[i].length
< min))
{
min = state[i].length;
k = i;
}
}
if ( k==0)
return (0);
state[k].flag = P;
} while(k != t);
visit_path[count] = k;
count ++;
k = state[k].predecessor;
} while(k!= 0);
count --;
for ( i = 1; i <= count ; i++)
path[i] = visit_path[count-i+1];
/* Input function */
void Input(int n, int a[size][size])
{
int i, j;
printf("\n Input adjacency matrix \n");
for(i =0; i < n; i++)
{
for(j =0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
printf("\n");
}
}
/* Output function */
void Output(int n, int a[size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for(i =0; i < n; i++)
{
/* Function main */
void main()
{
int a[size][size];
int path [size];
int org, dest, dist;
int count, i, n;
Input(n,a);
Output(n,a);
3
2
3
4
5
4
Adjacency matrix
1 2 1
3 2 3
4 5 4
Input destination: 3
#include<stdio.h>
#include<curses.h>
#include<stdlib.h>
int min(int e,int f);
int main()
{
clrscr();
int n,m,b,c,d,i,j,a[10][10],pi[10][10];
printf(“\n Enter the no. of rows and column: “);
scanf(“%d %d”,&m,&n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf”%d”,&a[i][j]);
}
for(i=1;i<=m;i++)
{
printf(“\n”);
for(j=1;j<=n;j++)
{
if(i==j |\ a[i][j]>=100)
p[i][j]=0;
else
pi[i][i]=i;
printf(“ %d”,pi[i][j]);
}
}
for(b=1;b<=m;b++)
{
for(c=1;c<=m;c++)
{
0 1 -3 2 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
0 3 4 5 1
4 0 4 2 1
4 3 0 2 1
4 3 4 0 1
4 3 4 5 0
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char mstr[20];
char pstr[20];
int n,m;
int s,i,flag=0,j;
cout<<"\n\nEnter Main String : ";
cin>>mstr;
cout<<"\n\nEnter Pattern String : ";
cin>>pstr;
n=strlen(mstr);
m=strlen(pstr);
for(s=0;s<=(n-m);s++)
{
flag=0;
j=0;
while(!flag && (j<m))
{
if(pstr[j]!=mstr[j+s])
flag=1;
else
j++;
}
if(!flag)
cout<<"\nPattern Exists With
Shift :"<<s<<"\n";
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
double mstr;
int pstr;
int j;
cout<<"\nEnter Main Text : ";
cin>>mstr;
cout<<"\nEnter Pattern Text : ";
cin>>pstr;
int p=pstr % 13;
int q=0;
//To Know The No. Of Digits In Pattern Text
int n=pstr;
int dp=0;
while(n)
{
n=n/10;
dp++;
}
//This Logic Ends Here
//To Know The No. Of Digits In Main Text
int m=mstr;
int dm=0;
while(m)
{
m=m/10;
dm++;
}
//This Logic Ends Here
int s=0;
double marray[20],parray[20];
m=mstr;n=pstr;
int d,i;
for(s=1;s<=(dm-dp+1);s++)
{
i=dp+s-1;
j=0;
n=0;
while(i>=s)
{
n=n+(pow(10,j)*marray[i]);
i--;
j++;
}
q=int(n) % 13;
if(q==p) //checking the case of spurious hit
{
int flag=0;
int k=1;
while(!flag && (k<=dp))
{
if(parray[k]!=marray[k+s-1])
flag=1;
else
k++;
}
if(!flag)
cout<<"\nPattern Exists With Shift :"<<s-1<<"\n";
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int *x;
int n;
void NQueens(int *,int,int);
clrscr();
cout<<"\n\tHow many Queens you want to place\n";
cin>>n;
x=(int *)malloc(n+1);
for(int i=0;i<n;i++)
x[i]=0;
cout<<"\n\tThe solution Matrix is :- \n\t";
NQueens(x,1,n);
getch();
}
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define INFINITY 10000
void main()
{
clrscr();
int n,i,j,k,l,p,mincost,min;
cout<<"\n Enter no. of nodes: ";
cin>>n;
int **cost=new int*[n];
int *closest=new int[n];
int *mstree[2];
for(i=0;i<n;i++)
*(cost+i)=new int[n];
for(i=0;i<2;i++)
*(mstree+i)=new int[n-1];
cout<<"\n Assuming there are no self loops and the
graph is undirected";
cout<<"\n Enter cost matrix(0 for no path)........\n";
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{ if(i==j)
cost[i][j]=INFINITY;
else
{
cout<<"\n Weight of edge between Node "<<i+1<<"
&Node "<<j+1<<" : ";
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=INFINITY;
cost[j][i]=cost[i][j];
mstree[0][0]=k;
mstree[0][1]=l;
// Initialize closest
for(i=0;i<n;i++)
if(cost[i][l]<cost[i][k])
closest[i]=l;
else
closest[i]=k;
closest[k]=closest[l]=-1;
for(i=1;i<(n-1);i++)
{
//find remaining n-2 edges of spanning tree
p=0;j=0;
while(closest[p]==-1)p++;
min=cost[p][closest[p]];
j=p;
for(;p<n;p++)
if(closest[p]!=-1 && cost[p][closest[p]]<min)
{
j=p;
min=cost[p][closest[p]];
}
mstree[i][0]=j;
mstree[i][1]=closest[j];
mincost+=cost[j][closest[j]];
closest[j]=-1;
delete cost;
delete closest;
getch();
}