All Pair
All Pair
#include<stdio.h>
int main()
{
int a[20][20],i,j,k,n;
printf("Enter the no. of nodes:\n");
scanf("%d",&n);
printf("Enter the cost between paths:\n");
printf("If no path exists enter value=100\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Node %d to %d is:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Adjncent node values is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
printf("Shortest path is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
Dijikstra
#include<stdio.h>
int a[10][10];
void path(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
a[i][j]=0;
else
{
printf("enter the cost of %d to %d path:\n",i,j);
printf("if no path exists enter a value>999\n");
scanf("%d",&a[i][j]);
}
}
}
}
int minv(int a[10][10],int s)
{
int v,i=9999,pos;
for(i=2;i<=s;i++)
if(v>a[1][i])
{
v=a[1][i];
pos=1;
}
return pos;
}
void fsp(int n)
{
int i ,s=n;
while(s>1)
{
int sma=minv(a,s);
s=s-1;
for(i=1;i<=n;i++)
if(i!=sma)
{
if(a[1][i]>a[i][sma]+a[sma][1])
a[1][i]=a[1][sma]+a[sma][1];
}
}
}
void disp(int n)
{
int i;
for(i=1;i<=n;i++)
printf("\n The path from 1 to %d is %d.",i,a[1][i]);
}
int main()
{
int n;
printf("Enter the no. of nodes:\n");
scanf("%d",&n);
path(n);
fsp(n);
disp(n);
}
Fractional knapsack
#include<stdio.h>
int main()
{
int n,w[50],v[50],p[50],item[50],W,i,j,temp,amount,solution[50],max_value=0;
printf("Enter the number of items ::");
scanf("%d",&n);
printf("Enter the weights and value of each items ::\n");
printf("Item\tWeights\tValue\n");
printf("-----------------------\n");
for(i=0;i<n;i++)
{
item[i]=i+1;
printf("I[%d]\t",item[i]);
scanf("%d",&w[i]);
scanf("%d",&v[i]);
}
printf("Enter the capacity of knapsack ::\n");
scanf("%d",&W);
for(i=0;i<n;i++)
{
p[i]=(v[i]/w[i]);
}
printf("\n");
printf("Item\tWeights\tValue\tratio(pi)\n");
printf("--------------------------------\n");
for(i=0;i<n;i++)
{
printf("I[%d]\t",item[i]);
printf("%d\t",w[i]);
printf("%d\t",v[i]);
printf("%d\n",p[i]);
}
printf("\n");
printf("Arrange the value of pi in decreasing order ::\n");
/*using sort to sort pi in decreasing order*/
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(p[j]<p[j+1])
{
temp=v[j+1];
v[j+1]=v[j];
v[j]=temp;
temp=w[j+1];
w[j+1]=w[j];
w[j]=temp;
temp=item[j+1];
item[j+1]=item[j];
item[j]=temp;
temp=p[j+1];
p[j+1]=p[j];
p[j]=temp;
}
}
}
printf("\n");
printf("Item\tWeights\tValue\tratio(pi)\n");
printf("--------------------------------\n");
for(i=0;i<n;i++)
{
printf("I[%d]\t",item[i]);
printf("%d\t",w[i]);
printf("%d\t",v[i]);
printf("%d\n",p[i]);
}
printf("\n");
printf("The solution of Knapsack problems using Greedy Algorithm is::\n");
i=0;
while(W>0)
{
if(w[i]<W)
{
amount=w[i];
max_value=max_value+v[i];
}
else
{
amount=W;
max_value=max_value+((v[i]/w[i])*W);
}
solution[i]=amount;
printf("%d\t",solution[i]);
W=W-amount;
i=i+1;
}
printf("\n");
printf("The maximum value is ::%d\n",max_value);
return 0;
}
Binary knapsack
#include<stdio.h>
int sum=0;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void knapsack(int m,int n,int w[],int p[])
{
int v[100][200],x[10],i,j;
for(i=0;i<=m;i++)
v[0][i]=0;
for(i=1;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(j>=w[i])
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
else
v[i][j]=v[i-1][j];
}
}
for(i=1;i<=n;i++)
x[i]=0;
i=n;
j=m;
while(i>0 && j>0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
}
i--;
}
printf("\nTHE OPTIMAL SET OF WEIGHTS IS:\n");
for(i=1;i<=n;i++)
{
if(x[i]==1)
{
printf("X%d=1\t",i);
sum=sum+p[i];
}
else
printf("X%d=0\t",i);
}
printf("\nTotal profit = %d",sum);
}
int main()
{
int w[10],p[10],i,m,n;
printf("\t0/1 KNAPSACK PROBLEM\n\n");
printf("ENTER THE NUMBER OF ITEMS: ");
scanf("%d",&n);
printf("ENTER THE WEIGHTS OF THE ITEMS:\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("ENTER THE PROFITS OF THE ITEMS:\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("ENTER THE CAPACITY OF KNAPSACK: ");
scanf("%d",&m);
knapsack(m,n,w,p);