Question

In: Computer Science

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 in a knapsack of capacity W. To solve the problem show how to formulate it recursively, the recurrence, discuss the subproblem etc.

Solutions

Expert Solution

It is request to please don't leave without thumbs up I really need it so that I can provide quality answer to you hoping for good thank you!

I have answer this question according to the question in my language and provided the code below and adding comment so that you can understand well , hope you like and please don't dislike

If You have Any Query Regarding this please ask in comment section I will be there to solve all your query in comment section immediately hope you will like it

In 0-1 Knapsack problem, you are given a Knapsack or a bag and several items with their weight and profit. The bag has a maximum capacity of weight it can hold. Now your task is to maximise the profit by choosing several items. It is called 0-1 because of the decision values, either an item will be taken or it will be discarded.
The right approach to solve it is using DP (dynamic programming) , but you can also solve using recursive as in question it ask for recursive solution. Let the capacity of Knapsack be W, and there be N items, Wt[] be the array of weights :Wi be the weight of ith item; Val[] be the of cost of copies : val_i be the optimal gained by ith item.

//PLAESE DO THUMBS UP / UPVOTE FOR MY EFFORTS AND DON'T LEAVE WITHOUT UPVOTE THANKYOU
#include <bits/stdc++.h>//=+
using namespace std;

//this function will return the maximum capacity it can hold
// is it can  put in a knapSack_bag of capacity W
int knapSack_recursive(int W, int wt_copies[], int val_copies[], int n)
{

        // Base Case when item and size of knapsack is zero
        if (n == 0 || W == 0)
                return 0;
     //condtion while putting item in knapsack bag
        // If weight of the nth item is more
        // than knapSack_bag capacity W, then
        // this item cannot be included
        // in the optimal solution
        if (wt_copies[n-1] > W)
                return knapSack_recursive(W, wt_copies, val_copies, n - 1);

        // Return the maximum of two cases:
        // (1) nth item included
        // (2) not included
        else
                return max(
                        val_copies[n-1] + knapSack_recursive(W - wt_copies[n-1],
                                                                        wt_copies, val_copies, n - 1),
                        knapSack_recursive(W, wt_copies, val_copies, n - 1));
}

// Driver code
int main()
{
    int n ,W;
    cout<<"enter the no of items \n";
    cin>>n;

        int val_copies[1000], wt_copies[1000];
        cout<<"enter the copies\n ";
        for(int i=0;i<n;i++)
        cin>>val_copies[i];
        cout<<"enter the weight of diffrent copies\n";
    for(int i=0;i<n;i++)
        cin>>wt_copies[i];
    cout<<"enter the size of knapsack bag\n";
    cin>>W;
        cout <<"Optimal cost will be  : " << knapSack_recursive(W, wt_copies, val_copies, n);
        return 0;
}

Output :


Related Solutions

how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Consider the following items in the Knapsack Problem: Item                weight             value &nbsp
Consider the following items in the Knapsack Problem: Item                weight             value            Knapsack capacity W = 9. 1                      3                   $6 2 4 $12 3                      2                  $10 4                      5                  $20 Determine the maximum value in the knapsack if we allow repetitions; i.e., if there are an unlimited number of each item so that more than one such item can be chosen. Find the missing value in the following linear array. P(w) is the maximum profit obtainable for a knapsack of capacity w. w-->             0       1      2       3       4       5       6       7       8       9 P(w):    0 0 10 10     20     20     30    30 40 ?   P(9)...
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
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...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with only fractional inclusion (i.e. not entirely included or excluded) in the knapsack? (Let n denote the number of items for possible inclusion.) A) At least 0 items and at most n items B) At least 1 items and at most n items C) Exactly n items D) At least 0 items and at most n-1 items E) At least 1 items and at most...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
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)...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT