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

how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
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 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.
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.
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...
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...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
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...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT