Question

help please!! due today!!

help please!! due today!!

media%2Ffef%2Ffef8efdf-e123-41df-91d0-f4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We are considering that we have to take full item (not its some part) : (0/1 knapsack )

Greedy approach to solve this is :

Consider all subsets of the items and calculate the total weight and value of all subsets. We will consider only those subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.

Algorithm :

Input : W is max weight possible

item[] : It is an structure array . It has two parts :

item.v = it's value ; item.w = it's weight

1. Get the max no. of subsets possible

max_no_of_subsets = pow (2 , no_of_items)

2. Assign the result to zero

result = 0

3. Loop for counter from 0 to max_no_of_subsets

a) Assign the temporary variable to use them

present_weight = 0;

present_value = 0

(b) Loop for i = 0 to no_of_items

i) if ith bit is set in counter

present_weight = present_weight + item[i].w;

  present_value = present_value + item[i].v;

(c) If present_weight is less than max_weight_possible, then update result i.e.

if (( present_weight <= W ) && (result < current_result))

result = current_result

4. return the value of result // It is the answer

-------------------------------------------------------------------

Solution to your problem :

We have 5 items . So max no. of subsets = 32;

Answer for your problem is : 21

------------------------

Code :

---------------

#include <iostream>

using namespace std;

struct item

{

int v;

int w;

  

};

int main() {

// your code goes here

item data[5];

int tot_weight = 600;

  

// Enter the data

for(int i=0; i<5; i++)

cin>>data[i].v;

  

for(int i=0; i<5; i++)

cin>>data[i].w;

  

int res = 0;

  

for(int i=0; i<32; i++) // Max no. of subset = 32

{

int temp_weight = 0; int temp_val = 0;

for(int j =0; j<5; j++)

{

if(i & (1<<j)) // If ith bit is set

{

temp_weight += data[j].w;

temp_val += data[j].v;

}

}

if((temp_weight < tot_weight) && (temp_val > res)) // update the result

res = temp_val;

}

cout<<res;

return 0;

}

-----------------------------------Screenshot -------------------------

(45:0) 45:0 Open File Custom Input Custom Input 55483 120 150 200 150 140 Status Successfully executed Date 2018-05-02 14:47:

If any doubt ... please ask in comment section i wiil clear it

Add a comment
Know the answer?
Add Answer to:
help please!! due today!!
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT