Provide an example instance of the fractional knapsack probl

Provide an example instance of the fractional knapsack problem where a greedy strategy baesd on repeatedly choosing as much of the highest-benefit item as possible results in a suboptimal solution.

Solution

The example of instance of the fractional knapsack problem is:

In order To consider all subsets of items, we can have two cases for every item:

1)In the first case, the item is included in the optimal subset,

2)In the second case it not included in the optimal set.


As a result we can fine the maximum value which can be obtained from n items is max of following two values.


1) The Maximum value can be obtained by n-1 items and W weight by excluding nth item.
2) Moreover the value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item

*This ais an of an a Naive recursive implementation of 0-1 Knapsack problem */

#include<stdio.h>

// This is a utility function that returns maximum of two integers

int maximumint a, int b) { return (a > b)? a : b; }

// This function is used to returns the maximum value that can be put in a knapsack of capacity W

int knapSackanswer(int W, int weightt[], int val[], int n)

{

   // First we have the base Case

   if (n == 0 || W == 0)

       return 0;

   //Now we will check if weight of the nth item is more than Knapsack capacity W, then this item cannot be included in the optimal solution

   if (weight[n-1] > W)

       return knapSackanswer(W, weight, val, n-1);

   //Now we will return the maximum of two cases:

   // 1)      In the first case nth item included

   // 2)In the second case it is not included

   else return maximum( val[n-1] + knapSackanswer(W-weight[n-1], wt, val, n-1),

                    knapSackanswer(W, weight, val, n-1)

                  );

}

// Driver program to test above function

int main()

{

    int value[] = 560, 100, 200};

    int weight[] = {10, 20, 30};

    int WI = 80;

    int n = sizeof(value)/sizeof(value[0]);

    printf(\"%d\", knapSackanswer(WI, weight, value, n));

    return 0;

}

Provide an example instance of the fractional knapsack problem where a greedy strategy baesd on repeatedly choosing as much of the highest-benefit item as possi
Provide an example instance of the fractional knapsack problem where a greedy strategy baesd on repeatedly choosing as much of the highest-benefit item as possi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site