Beruflich Dokumente
Kultur Dokumente
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();
knapsack(n,W);
backtracking();
getch();
}
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");
}
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 main()
{
int size,i,value;
int chain_matrix[100];
int index[20][20];
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);
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 main()
{
int n;
clrscr();
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;
}
}
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);
#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 main()
{
int i,j,l=1;
clrscr();
LCS(str1,str2);
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();
}
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(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");
}
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);