Question

In a Knapsack problem, given n items {I1, I2, · · · , In} with weight {w1, w2, · · · , wn} and value {v1,v2, ···, vn}, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity W .

i-1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using . an array wi

For example, if a solution only includes items 12, Is and 15, the binary array representation will be [0, 1,1,0,1,, ,0 and th

i-1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using . an array with size n: X = [x1,x2, . . . ,xn] linked list L that stores the indices of items that are included in a solution.
For example, if a solution only includes items 12, Is and 15, the binary array representation will be [0, 1,1,0,1,, ,0 and the linked list representation will be head-235. (a) 4 marks] You decide to use a Monte Carlo-style simulation approach to investigate possible solutions to the Knapsack problem. In this approach, you randomly generate m feasible solu- tions {Xi, X2, . . . , Xm.) with objective values {Vị, ½, . . . , V,n} , where Xi can be represented using either an array or list as described above. Write a function COMPUTEMEAN(argument list) to calculate the mean value of each variable rj (j-1, 2, . . . ,n) in the given m samples: zj-「ini Xi Li]. Note: the format of the argument list will depend on the representation you have selected. Full marks will be given if your algorithm runs in O(1 Li) time, where |Lil denotes the size of the linked list representation of Xi (b) 2 marksl Briefly describe how a greedy algorithm (heuristic) could be developed for solving the Knapsack problem using the COMPUTEMEAN) function. You do not have to write an algorithm in pseudo code to answer part (b) of this ques tion. We are expecting that you write a short paragraph or a short list of bullet points describing the important steps of the algorithm. . You should also comment on whether such a greedy approach would be effective.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ans)

Here is the code of 0,1 Knapsack problem written in C with output.

/////////////////////////////////////////////////////Code////////////////////////////////////////////////

#include<stdio.h>
#include<conio.h>

void knapsack(int n, int m, int w[], int p[]);
void findknap(int n, int m, int w[], int p[]);
int v[10][10];
void main()
{
int n,i,w[15],p[15],m;
clrscr();
printf("Enter The Number Of Items");
scanf("%d",&n);
printf("enter knapsack wt");
scanf("%d",&m);

printf("Enter The VALUE AND WEIGHT OF ITEM");
for(i=1;i<=n;i++)
{
scanf("%d%d",&p[i],&w[i]);
}


knapsack(n, m, w, p);
findknap(n, m, w, p);
getch();

}

void knapsack(int n, int m, int w[], int p[])
{
int i, j;

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

for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(j>=w[i])
   {
   if(v[i-1][j] > v[i-1][j-w[i]]+p[i])
   v[i][j]=v[i-1][j];
   else
   v[i][j]=v[i-1][j-w[i]]+p[i];
   }
else
   v[i][j]= v[i-1][j];
}
}

for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
printf("\t%d",v[i][j]);
}
printf("\n");
}

}


void findknap(int n, int m, int w[], int p[])
{
int x[10], i,j, prof=0;

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("\nSelected Item : ");
for(i=1;i<=n;i++)
printf("\t%d",x[i]);

for(i=1;i<=n;i++)
{
prof= prof+(p[i]*x[i]);
}
printf("\nprofit=%d", prof);

}

and

/////////////////////////////////////////////////////Code of Greedy algorithm fractional knapsack problem////////////////////////////////////////////////////

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void input(float w[],float v[],int n)
{
   int i;
   randomize();
   for(i=0;i<n;i++)
   {
       w[i]=rand()%100;
       v[i]=rand()%100;
   }
   cout<<endl;
}
void display(float w[],float v[],float r[],int n)
{
   int i;
   cout<<"\nWeight\tVolume\tRatio\n";
   for(i=0;i<n;i++)
   {
       cout<<w[i]<<"\t"<<v[i]<<"\t"<<r[i]<<endl;
   }
}
void cal(float w[],float v[],float r[],int n,float c)
{
   int i,j;
   float x[10],k;
   for(i=0;i<n;i++)
   {
       x[i]=0.0;
   }
   //processing by bubble sort
   for(j=0;j<n;j++)
   {
       for(i=0;i<n-1;i++)
       {
           if(r[i]<r[i+1])
           {
               k=r[i]; //swapping of ratio
               r[i]=r[i+1];
               r[i+1]=k;
               k=w[i];
               w[i]=w[i+1]; //swapping of weight
               w[i+1]=k;
               k=v[i];
               v[i]=v[i+1]; //swapping of volume
               v[i+1]=k;
           }
       }
   }
   for(i=0;i<n;i++)
   {
       if(w[i]>c)
       {
           break;
       }
       else
       {
           x[i]=1.0;
           c=c-w[i];
       }
   }
   if(i<=n)
   {
       x[i]=c/w[i];
   }
   float sum=0.0;
   for(i=0;i<n;i++)
   {
       x[i]=x[i]*v[i];
       sum=sum+x[i];
   }
   cout<<"\nProfit is : "<<sum;

}
void main()
{
   clrscr();
   int i,j,n;//c=capacity
   float x[10],c,k,w[10],v[10],r[10]; //r=ratio,w=weight,v=volume
   cout<<"Enter the maximum value \n";
   cin>>n;
   input(w,v,n);
   cout<<"\nEnter the capacity :\n";
   cin>>c;
   for(i=0;i<n;i++)
   {
       r[i]=v[i]/w[i];
   }
   display(w,v,r,n);
   cal(w,v,r,n,c);

getch();
}
////////////////////////////////////////////OUTPUT//////////////////////////////////////////////

Enter the maximum value 4 nter the capacity 50 Weight Uolume Ratio 17 0.058824 73 28 28 50 0.684932 34 67 1.214286 2.392857 P

///////////////////////////////////////////////////////////

Thank you very much :-)

Keep asking queries

do comment below and please hit like button

Thank you once again :-)
////////////////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
In a Knapsack problem, given n items {I1, I2, · · · , In} with weight {w1, w2, · · · , wn} and value {v1,v2, ···, vn}, the goal is to select a combination of items such that the total value V is maxim...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT