Question

In: Computer Science

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.
                   (a) No fraction allowed

Max Weight W= 9

item 1: profit $20, weight 2, prof/weight=$10

item 2: profit $30, weight 5, prof/weight=$6

item 3: profit $35, weight 7, prof/weight=$5

item 4: profit $12, weight 3, prof/weight=$4

item 5: profit $3, weight 1, prof/weight=$3

Solutions

Expert Solution

ANSWER :-

Language : C++

//0-1 Knapsack problem using Greedy approach
#include<iostream>        
using namespace std;
// function that returns maximum of two integers
int maximum(int a, int b)
{
        return (a > b) ? a : b;
}
         
// Returns the maximum value that can be put in a knapsack of capacity W
int GreedyknapSack(int mw, int weight[], int val[], int n)
{
        // Base Case
        if (n == 0 || mw == 0)
        return 0;
        // 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] > mw)
        return GreedyknapSack(mw, weight, val, n - 1);
        
        // Return the maximum of two cases: (1) nth item included (2) not included
        else
        return maximum(val[n - 1] + GreedyknapSack(mw - weight[n - 1], weight, val, n - 1),
        GreedyknapSack(mw, weight, val, n - 1));
}
 

int main()
{
        cout << "Enter number of items in Knapsack:";
        int n, mw;
        cin >> n;
        int val[n], weight[n];
        for (int i = 0; i < n; i++)
        {
                cout << "Enter profit & weight for "<< i << " item :";
                cin >> val[i];
                cin >> weight[i];
        }
 
        cout <<"Enter max weight: ";
        cin >> mw;
        cout <<"Knapsack value is: "<< GreedyknapSack(mw, weight, val, n);
        return 0;
}

Output :-


Related Solutions

Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items,...
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items, their values and weights. The maximum weight capacity of the knapsack is 113. What could be the maximum value of the knapsack and the most valuable set of items that fit in the knapsack? Item Weight Value 1 32 727 2 40 763 3 44 60 4 20 606 5 1 45 6 29 370 7 3 414 8 13 880 9 6 133...
create a genetic algorithm for knapsack problem in c++
create a genetic algorithm for knapsack problem in c++
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of items, let call them 1,2,3,4,...,n. There are exactly c_i copies of item i, and each such copy has value v_i and weight w_i. As before, the knapsack capacity is W, and the other constraint is that you can only take at most c_i copies of item i ( since no more are available). Show how to compute the optimal value that can be achieved...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1 14 20 2 6...
KNAPSACK Optimization Problem using Greedy Method Problem 1: Item Weight Value 1 14 20 2 6 16 3 10 8 4 5 10 5 4 12 Allowed weight = 24 Kg Problem 2: Item Weight Value 1 6 30 2 8 40 3 15 45 4 22 88 5 25 80 Allowed weight = 60 Kg Problem 3: Item Weight Value 1 6 30 2 8 40 3 15 45 4 22 88 5 25 80 Allowed weight = 60...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to compute the value of the best packing of the knapsack? For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to identify the items that are included in an optimal packing of the knapsack? For the Longest Common Subsequence algorithm, is it necessary to maintain the entire...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define what the function is computing.
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. **The arguments to the knapsack function are target weight and the array index where the remaining items start. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT