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)...
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...
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...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
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,...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
Java programming Trace the algorithm for minimum spanning tree (eager, lazy Prim algorithm)
Java programming Trace the algorithm for minimum spanning tree (eager, lazy Prim algorithm)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT