Thursday 16 March 2017

Solution to Fractional Knapsack

Fractional Knapsack
for Understanding this program please refer my previous post: Fractional Knapsack Problem (Meaning &Explaination given)

Source code in C:
Topic:Fractional Knapsack
#include<stdio.h>
#include<stdlib.h>
struct knapsack //creating structure for Knapsack
{
int item;//to store element numbers
int weight;//to store weight of the item
int profit;//to store profit associated with the item
    double ratio;// to store profit/weight ratio
};
void main()
{
        int i, n;
        printf("enter no of elements");
        scanf("%d",&n);
        struct knapsack k[n];double x[n];
for( i=0;i<n;i++)
{
        printf("enter weight and value of an item");
        scanf("%d%d",&k[i].weight,&k[i].profit);
       k[i].ratio=k[i].profit/k[i].weight;
       k[i].item=i;
}
struct knapsack temp;
int j;
for(i=0;i<n;i++)//sorting items in descending order of profit/weight ratio
{
for( j=0;j<n-1-i;j++)
{
if(k[j].ratio<k[j+1].ratio)
{
temp=k[j];
k[j]=k[j+1];
k[j+1]=temp;
}


}
}
int capacity;double total_profit=0.0;
int map;
printf("enter the capacity");
scanf("%d",&capacity);
for( i=0;i<n;i++)
{
if(capacity>=k[i].weight)//if true
{

capacity-=k[i].weight;// decreasing the capacity of the bag
total_profit+=k[i].profit;//incresing the profit
int q=k[i].item;//adding item completly
x[q]=1;
}
else if(capacity<k[i].weight && capacity!=0)

{

      double fractprofit=capacity*k[i].ratio;//partial profit of the item added

total_profit=total_profit+fractprofit;

int y=k[i].item;

double fractweight=(double)capacity/(double)k[i].weight;

x[y]=fractweight;

break;


}
else

   break;

}
printf("total profit is: %f\n",total_profit);
for( i=0;i<n;i++)
{
printf("%f\t",x[i]); //indicates that the i th item is added completly(ie 1) or                                                                          //partially(ie.ratio)


}
}

/* output:
enter no of elements4                                                                                                                                        
enter weight and value of an item50 100                                                                                                                      
enter weight and value of an item20 100                                                                                                                      
enter weight and value of an item30 90                                                                                                                        
enter weight and value of an item10 80                                                                                                                        
enter the capacity70                                                                                                                                          
total profit is: 290.000000                                                                                                                                  
0.200000        1.000000        1.000000        1.000000                                                                                              
*/                                                 

No comments:

Post a Comment