Sie sind auf Seite 1von 15

PROGRAM

5
Implementation of a knapsack problem using dynamic programming.
#include< stdio.h >
#include< conio.h >
#define MAX 20

void
knapsack(int,int); int
max(int,int);
void backtracking();
int weight[MAX],value[MAX],W,n,*x;
int v[MAX][MAX];

void main()
{
int i,j;
clrscr();

printf("\n Enter number of Object you want:");


scanf("%d",&n);

printf("\n Enter weight and values in ascending order of vales");


for(i=1;i<=n;i++)
{
printf("\n Enter Weight and Value for Object %d:",i);
scanf("%d %d",&weight[i],&value[i]);
}

printf("\n Enter knapsack Capacity:");


scanf("%d",&W);

knapsack(n,W);
backtracking();

getch();
}

void knapsack(int n,int W)


{
int i,j;

for(i=0;i<=W;i++)
v[0][i]=0;

for(i=0;i<=n;i++)
v[i][0]=0;

for(i=1;i<=n;i++)
{
for(j=1;j<=W;j++)
{
if((j-weight[i])< 0)
v[i][j]=v[i-1][j];
else

v[i][j]=max(v[i-1][j],v[i-1][j-weight[i]]+value[i]);

}
}
printf("\n \t ");

for(i=0;i<=W;i++)
printf("%2d ",i);

printf("\n\n");

for(i=0;i<=n;i++)
{
printf("\n w%d=%2d v%d=%2d |",i,weight[i],i,value[i]);

for(j=0;j<= W;j++)
printf("%2d ",v[i][j]);
}
printf("\n\n");
printf("\n Maximum value carry by knapsack is:%2d",v[n][W]);
printf("\n\n");
}

int max(int a,int b)


{
return (a>b)?a:b;
}

void backtracking()
{
int j1,I;
j1=W;
printf("\nIncluded Object \t weight \t value\n\n");

for(i=n;i>=0;i--)
{
if(v[i][j1]!=v[i-1][j1]&&(v[i][j1]==v[i-1][j1-weight[i]]+value[i]))
{
printf("\n%2d \t\t\t %2d \t\t
%2d",i,weight[i],value[i]); j1=j1-weight[i];
}
}
}

PROGRAM
6
Implementation of chain matrix multiplication using dynamic
programming.
#include<stdio.h>

int chain(int index[20][20],int chain_matrix[],int i,int


j); void print_order(int index[20][20],int i,int j);

int main()
{
int size,i,value;
int chain_matrix[100];
int index[20][20];

printf("Enter the number of matrices:


"); scanf("%d",&size);

printf("\nEnter the dimensions of %d matrices...\n",size);


printf("\n");

for(i=0;i<size+1;i++)
{
printf("Enter the dimensions of the matrix: ");
scanf("%d",&chain_matrix[i]);
}
value=chain(index,chain_matrix,1,size);

printf("\nNumber of multiplication: %d\n\n",value);


printf("\nThe Optimal order is:\t");
print_order(index,1,size);
getch();
return 0;
}

int chain(int index[20][20],int chain_matrix[],int i,int j)


{
if(i==j)
return 0;
int
k,count;
int min=1000000;

for (k=i;k<j;k++)
{
count=chain(index,chain_matrix,i,k)
+chain(index,chain_matrix,k+1,j)+c hain_matrix[i1]*chain_matrix[k]*chain_matrix[j];
if(count<min)
{
min=count; index[i]
[j]=k;
}
}

return min;
}
void print_order(int index[20][20],int i,int j)
{
if(i==j) printf("M
%d",i); else
{
printf("(");
print_order(index,i,index[i][j]);
print_order(index,index[i][j]+1,j);
printf(")");
}

}
PROGRAM
7
Implementation of making a change problem using dynamic
programming.
#include< stdio.h>
#include< conio.h>

int C[]={1,5,10,25,100};

void make_change(int n);


int bestsol(int,int);

void main()
{
int n;
clrscr();

printf("\n Enter amount:");


scanf("%d",&n);
make_change(n);

getch();
}

void make_change(int n)
{

int S[100],s=0,x,ind=0,i;
printf("\nAvailable coins:\n");

for(i=0;i<= 4;i++)
printf("%5d",C[i]);

while(s!=n)
{
x=bestsol(s,n);

if(x==-1)
{"\n"; }

else
{

S[ind++]=x;
s=s+x;

}
}

printf("\nMaking change for %4d",n);

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


{
printf("\n%5d",S[i]);
}
}

int bestsol(int s,int n)


{
int i;
for(i=4;i>-1;i--)
{
if((s+C[i]) <=
n) return C[i] ;
}
return -1;
}

PROGRAM
8
Implementation of a knapsack problem using greedy algorithm.
#include<stdio.h>
#include<conio.h>
void knapsack(int n,float weight[],float value[],float w)
{
float x[10],t=0;
int i,j,u;

u=w;
for(i=0;i< n;i++)
x[i]=0.0;

for(i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{

x[i]=1.0;
t=t+value[i];
u=u-

weight[i];

if(i<
n)

x[i]=u/weight[i];
t=t+
(x[i]*value[i]);

printf("\n\n");
printf("weight |
");

for(i=0;i<n;i++)
{
printf("%1.2f\t",weight[i]);
}

printf("\nvalue | ");
for(i=0;i<n;i++)
{
printf("%1.2f\t",value[i]);
}

printf("\n x | ");
for(i=0;i<n;i++)
{
printf("%1.2f\t",x[i]);
}

printf("\n\n");
printf("\n Maximum value that knapsack carry is:
%.2f", t); printf("\n\n");
}

void
main()
{
float weight[10],value[10],w;
int n, i ,j;
float r[10],temp;
clrscr();
printf ("\n Enter number of objects you want:");
scanf ("%d", &n);

printf ("\nEnter the weights and values of each object:");


for (i=0;i<n;i++)
{
printf ("\nEnter weight and value for object%d:",i+1);
scanf("%f %f", &weight[i], &value[i]);
}
printf ("\nEnter the capacity of knapsack:");
scanf ("%f",&w);
for(i=0;i<n;i++)
{
r[i]=value[i]/weight[i];
}
for(i=0; i< n; i++)
{
for(j=i+1;j<n;j++)
{
if(r[i]<r[j])
{
temp=r[j];
r[j]=r[i];
r[i]=temp;
temp=weight[j
];
weight[j]=weight[i
]; weight[i]=temp;
temp=value[j];
value[j]=value[i];
value[i]=temp;
}
}
}
knapsack(n,weight,value,w);
getch();
}
Implement LCS problem.

#include<stdio.h>

PROGRAM 12

#include<conio.h>
#include<string.h>
#define MAX 50

int c[MAX][MAX];
char b[MAX][MAX];
char str1[MAX],str2[MAX];
char
x[MAX],y[MAX],ans[MAX]; int
m,n,ind;

void LCS(char x[MAX],char y[MAX]);


void display(char[MAX][MAX],char[MAX],int,int);

void main()
{
int i,j,l=1;
clrscr();

printf("\n Enter string1:");


scanf("%s",&str1);

printf("\n Enter string2:");


scanf("%s",&str2);

LCS(str1,str2);

printf("\n Longest Common Subsequence:");


printf("\n\n");

for(i=n;i>0;i--)
{
if(c[m][i]==c[m][n])
{
l=0;
display(b,x,m,i);
printf("\n solution %d: %s",l++,strrev(ans));
}
else
{
break;
}
}
printf("\n\n")
; getch();
}

void LCS(char x1[MAX],char y1[MAX])


{
int i,j;

m=strlen(x1
);
n=strlen(y1)
;

for(i=1;i<=m;i++)
{
x[i]=x1[i-1];
}

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

y[j]=y1[j-1];
}

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]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='\\';
}
else if(c[i-1][j] >=c[i][j-1])
{
c[i][j]=c[i-1]
[j]; b[i][j]='|';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='-';
}
}
}
printf("\n

");

for(i=1;i<= n;i++)
printf(" %3c",y[i]);
printf("\n\n");

for(i=0;i<= m;i++)
{
printf("\n%1c |",x[i]);
for(j=0;j<= n;j++)
{
printf(" %d %c",c[i][j],b[i][j]);
}
}
printf("\n\n");
}

void display(char b[MAX][MAX],char x[MAX],int i,int j)


{
if(i==0 || j==0)
{"\n; }

else if(b[i][j]=='\\')
{
ans[ind++]=x[i];
display(b,x,i-1,j1);
}

else if(b[i][j]=='|')
display(b,x,i-1,j);

els
e

display(b,x,i,j-1);

Das könnte Ihnen auch gefallen