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

Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming...
Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming based algorithm and (ii) Branch and Bound Search based algorithm Go through the related text and implement each of these algorithms using the efficient data structure. Show the results of different steps of these algorithms for an instance of the 0/1 Knapsack problem with number of items, n=4. The capacity of the knapsack, weights and profits of the items may be generated randomly with...
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)...
C Program: How do I write a Greedy function for 0-1 knapsack, to find total value...
C Program: How do I write a Greedy function for 0-1 knapsack, to find total value only( replace struct Knapsack) # include #include #include struct Knapsack {    int value;    int weight; };    // returns maximum of two integers    int max(int a, int b) { return (a > b)? a : b; }    // Returns the maximum value that can be put in a knapsack of capacity W    struct Knapsack knapSackExhaustive(int W, int wt[], int...
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...
java code for scheduling problem (start time,end time , profite) using greedy algorithm
java code for scheduling problem (start time,end time , profite) using greedy algorithm
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT