Question

In: Computer Science

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, 7 and 5 pounds. Please show without using import library classes

0/1 knapsack problem

The knapsack problem in its simplest form involves trying to fit items of different weights into a knapsack so that the knapsack ends up with a specified total weight

Solutions

Expert Solution

class tryknap{

    static int max(int a, int b) { return (a > b) ? a : b; }

    static int knapSack(int W, int wt[], int val[], int n)

    {

        if (n == 0 || W == 0)

            return 0;

   

        if (wt[n - 1] > W)

            return knapSack(W, wt, val, n - 1);

      

        else

            return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),

                       knapSack(W, wt, val, n - 1));

    }

    public static void main(String args[])

    {

        int val[] = new int[] { 11,8,7,6,5 };

        int wt[] = new int[] { 8, 7, 5 };

        int W = 20;

        int n = val.length;

        System.out.println(knapSack(W, wt, val, n));

    }

}


Related Solutions

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
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
How do we solve the problem of the federal budget? Do you think you can do...
How do we solve the problem of the federal budget? Do you think you can do better than our lawmakers in Washington? Why is it so complicated? Here's your chance to take a crack at it. Use the budget simulator below to try to balance the federal budget, or at least bring it under control. Now, before you even get started, you must seriously think about a couple of things: you can't just cut programs you don't like, without thinking...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and contrast the different methods.
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
In your opinion, how do you think we could solve the problem of scarcity, moreover, how...
In your opinion, how do you think we could solve the problem of scarcity, moreover, how do you think we could cut down on the usage of so limited resources and still be able to produce peoples wants and desires? Do you think that is possible at all?
How do we solve teh problem of exercise addiction?
How do we solve teh problem of exercise addiction?
*Solve the question with arbitrary number* Thank you! 1. a. Each milliliter of an otic solution...
*Solve the question with arbitrary number* Thank you! 1. a. Each milliliter of an otic solution (Auralgan) contains the following ingredients. Calculate the amount of benzocaine required to manufacture (a given #) 12 mL containers of this product. (Answer to the nearest tenth of a gram – one decimal point) Antipyrine                (a given amount) mg Benzocaine              (a given amount) mg Dehydrated glycerin   to make (a given volume) mL b. Given the following prescription for a child (a given #) pounds....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT