Question

In Python, write the following function: Knapsack Problem: Given a set of items, each with a...

In Python, write the following function:

Knapsack Problem:

Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is maximized.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

code:

def KnapSack(M, weight, value, num):

#The base case

    if num == 0 or M == 0 :

        return 0

    if (weight[n-1] > M):

        return KnapSack(M, weight, value, num-1)

    else:

        return max(value[n-1] + KnapSack(W-weight[n-1], weight, value, num-1),

                   KnapSack(M, weight, value, num-1))

Hit the thumbs up if you liked the answer. :)

Add a comment
Know the answer?
Add Answer to:
In Python, write the following function: Knapsack Problem: Given a set of items, each with a...
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
ADVERTISEMENT