Question

The Knapsack Problem in Python Not using the exhaustive search method or the Dynamic Programming ...

The Knapsack Problem in Python
Not using the exhaustive search method or the Dynamic Programming Method, find another method that accomplishes the task of the knapsack problem in python.

PLEASE DON'T USE THE EXHAUSTIVE SEARCH METHOD OR THE DYNAMIC PROGRAMMING METHOD, I DON'T NEED THOSE. THANK YOU.

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

Answer:

# A recursive implementation of 0-1 Knapsack Problem
# Returns the maximum value that can be put in a knapsack of capacity W
def knapSack(C, weight, val, n):
    # Base Case
    if n == 0 or C == 0:
        return 0

    # If weight of the nth item is more than Knapsack of capacity C,
    # then this item cannot be included in the solution
    if (weight[n - 1] > C):
        return knapSack(C, weight, val, n - 1)

    # return the maximum of two cases:
    # (1) nth item included
    # (2) not included
    else:
        return max(val[n - 1] + knapSack(C - weight[n - 1], weight, val, n - 1),
                   knapSack(C, weight, val, n - 1))




# To test above function
val = [60, 100, 120]
weight = [10, 20, 30]
C = 50
n = len(val)
print(knapSack(C, weight, val, n))

Output:

C:\Userslasus\PycharmProjects\SamplelvenviScri 220 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
The Knapsack Problem in Python Not using the exhaustive search method or the Dynamic Programming ...
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
  • a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should...

    a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should read inputs from a file called “data.txt”, and the output will be written to screen, indicating the optimal subset(s). b) For the bottom-up dynamic programming algorithm, prove that its time efficiency is in Θ(nW), its space efficiency is in Θ(nW) and the time needed to find the composition of an optimal subset from a filled dynamic programming table is in O(n). Consider the following...

  • Algorithm and computing system(Python) What is dynamic programming? Why does it usually work faster? Using the...

    Algorithm and computing system(Python) What is dynamic programming? Why does it usually work faster? Using the dynamic programming solution for the knapsack problem, compute a solution to this knapsack problem: Weight          value 2                     16 3                     19 4                     23 5                     28 total number of items = 4 capacity of the knapsack = 7 Suppose that the similarity between an object O and 6 other objects in the database A,B,C,D,E and F are as follows: sim(A,O) = 0.1 sim(B,O) = 0.3 sim(C,O)...

  • 1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points)...

    1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5

  • 3. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (20 points)...

    3. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (20 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? Item 1 weighs 2 pounds and is worth $9.00 Item 2 weighs 3 pounds and is worth $12.00 Item 3 weighs 5 pounds and is worth $14.00 Item 4...

  • Solve 01 Knapsack problem using 1) Backtracking 2) Breath first search with branch and bound 3)...

    Solve 01 Knapsack problem using 1) Backtracking 2) Breath first search with branch and bound 3) Best fit search with branch bound. Find out maxprofit and solution vector X=(x1,x2,x3,x4,x5). You need to show how you solve it using pruned state space tree. plw $20* $30» $35.» $12* $3. pi/wi- 10» 60 5» 4° 30 2» 5* 2» 30 4° 30 W=12(Knapsack capacity)-

  • really need help. All information that i have is posted, In java Dynamic programming allows you...

    really need help. All information that i have is posted, In java Dynamic programming allows you to break a large problem into multiple subproblems. Each of these subproblems must be solved, and the solution must be stored as a memory-based solution. Solve the following binary search algorithm using dynamic programming (Adapted from Esquivalience, 2018): Graph To solve this problem, complete the following tasks: Create a binary search tree using a data structure. Insert the node values as described in the...

  • The purpose of this assignment is to reinforce dynamic programming algorithms. Specifically, the assignment is to...

    The purpose of this assignment is to reinforce dynamic programming algorithms. Specifically, the assignment is to do problem 15-8 on page 409 of the text. In addition, implement the algorithm that you construct. You may write your program in any modern language. I suggest Python because the output can be put in a visual representation Part 1 reinforces the recurrence relation of the Fibonacci sequence with a comparison to the dynamic programming algorithm. (goals 1, 3, 4) Using the same...

  • An Introduction to Programming, Using Python. By David Schneider Chapter 6.2, Problem 10 Using Python, write...

    An Introduction to Programming, Using Python. By David Schneider Chapter 6.2, Problem 10 Using Python, write lines of code to carry out the stated task. Assume that the random module has been imported. Perfect Square Display a perfect square integer between 1 and 10,000 (inclusive) selected at random.

  • Problem 1. (Calculating Edit Distance Using Dynamic Programming) A direct implementation of the above recursive scheme...

    Problem 1. (Calculating Edit Distance Using Dynamic Programming) A direct implementation of the above recursive scheme will work, but it is spectacularly inefficient. If both input strings have N characters, then the number of recursive calls will exceed 2N. To overcome this performance bug, we use dynamic programming. Dynamic programming is a powerful algorithmic paradigm, first introduced by Bellman in the context of operations research, and then applied to the alignment of biological sequences by Needleman and Wunsch. Dynamic programming...

  • Rod-cutting problem Design a dynamic programming algorithm for the following problem. Find the maximum total sale...

    Rod-cutting problem Design a dynamic programming algorithm for the following problem. Find the maximum total sale price that can be obtained by cutting a rod of n units long into integer-length pieces if the sale price of a piece i units long is pi for i = 1, 2, . . . , n. What are the time and space efficiencies of your algorithm? Code or pseudocode is not needed. Just need theoretical explanation with dynamic programming with recurrence relation...

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