Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello, I am trying to do the Kapsack problem using recursion in java, I think I have the recursive part correct because it works when
Hello, I am trying to do the Kapsack problem using recursion in java, I think I have the recursive part correct because it works when I hard code the array and target in main, but now I am trying to make the program more fluid for the directions being:
You may assume that the largest capacity (as well as any individual weight) is 100 and the largest number of weights is 25. You may also assume that the weights are sorted from largest to smallest. The basic idea is to send a capacity and an array of weights to a recursive method and to either insert the weight or not. In either case call the method again with a reduced capacity and a shorter array OR with the same capacity and a shorter array. There should be a base case(s) for easy capacity and/or easy array. IF you do it this way, you would probably return another array which would be the potential solution array which of course would only be printed it it is truly a solution.
I am having trouble in main and getting a null pointer exception for and am unsure of what bounds to use in some of my for statements.
Here is my code so far:
import java.util.ArrayList; import java.util.Scanner; public class KnapSackRecursive { //to fill the actual weights available to fill the bag public static int arrayItems[]; //use an array list to fill the bag with maximum target weight public static ArrayList64 Write a program that solves the knapsack problem for an arbitrary knapsack capacity and series of weights. Assume the weights are stored in an array. Hint: The arguments to the recursive knapsack() function are the target weight and the array index where the remaining items start. 64 Write a program that solves the knapsack problem for an arbitrary knapsack capacity and series of weights. Assume the weights are stored in an array. Hint: The arguments to the recursive knapsack() function are the target weight and the array index where the remaining items startvalues = new ArrayList (); public static void knapsack(int targetWeight, int i) { //base condition to stop the recursion if (i == (arrayItems.length)) { //If the target weight is 0 then displays the values if (targetWeight == 0) { System.out.println("The solution of recursive knapsack problem is by filling with weights: "); for (int j = 0; j values.size(); j++) { System.out.print(values.get(j) + "\t"); } System.out.println(); } return; } //if the i'th element is exist then the move the weights(arrayItems) //into values values.add(arrayItems[i]); knapsack(targetWeight - arrayItems[i], i + 1); values.remove(values.size() - 1); knapsack(targetWeight, i + 1); } //main method public static void main(String[] args) { Scanner input = new Scanner(System.in); String end = "start"; String userInput; while (end == "start"){ int[] numbers = new int[5]; //out.println("Please enter numbers"); if(input.hasNext()) { if (input.hasNextInt()) { for (int i = 0; i out.println("Weight of the items in the array are: "); for (int i = 0; i arrayItems.length; i++) System.out.print(arrayItems[i] + "\t"); System.out.println(" Target Weight: " + targetWeight); knapsack(targetWeight, 0); System.out.println(); } }else if(input.hasNext()) { userInput = input.next(); boolean x = false; while (x == false) { if (userInput.equals("stop")) { end = "stop"; System.out.println("END PROGRAM"); x = true; } else { System.out.println("Please enter a proper command: "); userInput = input.next(); } } } } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started