Ok, so, for those of you that are unfamiliar with the 0-1 Knapsack problem, I have to write an to take the weights and corresponding values of 10 items, and find which items to put in a knapsack that holds 85 pounds to have the maximum value in the knapsack.
I keep getting compiler errors, and quite honestly, I'm really bad at debugging.
Code:
[CODE=Java]
// The "Knapsack" class.
public class Knapsack
{
public static void main (String[] args)
{ int n;
int nmax = 10;
int wmax = 85;
int w;
int addVal;
int itemArray = new int [2][nmax];
int solArray = new int [nmax][wmax];
itemArray = {{10, 63, 84, 81, 33, 11, 67, 10, 65, 81}, { 9, 19, 5, 31, 48, 17, 46, 80, 38, 55}};
//This section of code fills the first row of the Solution Array
while(w <= wmax){
if (itemArray [0][0] < w){
solArray[0][w] = itemArray[0][1];
}
else solArray[0][w] = 0;
}
for (n = 1; n <= nmax-1; n++){
for(w = 1; w <= wmax-1; w++){
//checks to see if the item being looked at weighs more than the subproblem can carry
if (itemArray[0][n] > w){
solArray[n][w] = solArray[n-1][w];
}
//adds together the values of the previous subproblem and the current iteration of n
//and checks if it is of greater value than the previous row
addVal = solArray[n-1][w-itemArray[0][n]] + itemArray[1][n];
if (addVal > solArray[n-1][w]){
solArray[n][w] = addVal;
}
//if only the current item can be fit in the knapsack, checks if the item is more valuable than the value in the row above
else if (w >= itemArray[0][n] && solArray[n-1][w] < itemArray[1][n])
{solArray[n][w] = itemArray[1][n];}
else solArray[n][w] = solArray[n-1][w];
}
}
n = nmax-1;
w = wmax-1;
int includeArray = new int[i];
int i = 0;
//populates a 1-dimensional array with the numbers of the items filling the knapsack in the optimal solution
while (n <= 0){
if ((solArray[n][w] != solArray[n-1][w]) && (solArray [n-1][w] != 0)){
includeArray[i] = n;
w = w - itemArray[0][n];
i++;
}
n--;
}
includeArray.le ngth = i;
//print out solution array
for (n = 0; n <= nmax; n++){
for (w = 0; w <=wmax; w++){
System.out.prin tln(solArray[n][w]);
}
}
} // main method
} // Knapsack class[/CODE]
P.S. NOOOOO, the forum messes with all my nice indentation!
I keep getting compiler errors, and quite honestly, I'm really bad at debugging.
Code:
[CODE=Java]
// The "Knapsack" class.
public class Knapsack
{
public static void main (String[] args)
{ int n;
int nmax = 10;
int wmax = 85;
int w;
int addVal;
int itemArray = new int [2][nmax];
int solArray = new int [nmax][wmax];
itemArray = {{10, 63, 84, 81, 33, 11, 67, 10, 65, 81}, { 9, 19, 5, 31, 48, 17, 46, 80, 38, 55}};
//This section of code fills the first row of the Solution Array
while(w <= wmax){
if (itemArray [0][0] < w){
solArray[0][w] = itemArray[0][1];
}
else solArray[0][w] = 0;
}
for (n = 1; n <= nmax-1; n++){
for(w = 1; w <= wmax-1; w++){
//checks to see if the item being looked at weighs more than the subproblem can carry
if (itemArray[0][n] > w){
solArray[n][w] = solArray[n-1][w];
}
//adds together the values of the previous subproblem and the current iteration of n
//and checks if it is of greater value than the previous row
addVal = solArray[n-1][w-itemArray[0][n]] + itemArray[1][n];
if (addVal > solArray[n-1][w]){
solArray[n][w] = addVal;
}
//if only the current item can be fit in the knapsack, checks if the item is more valuable than the value in the row above
else if (w >= itemArray[0][n] && solArray[n-1][w] < itemArray[1][n])
{solArray[n][w] = itemArray[1][n];}
else solArray[n][w] = solArray[n-1][w];
}
}
n = nmax-1;
w = wmax-1;
int includeArray = new int[i];
int i = 0;
//populates a 1-dimensional array with the numbers of the items filling the knapsack in the optimal solution
while (n <= 0){
if ((solArray[n][w] != solArray[n-1][w]) && (solArray [n-1][w] != 0)){
includeArray[i] = n;
w = w - itemArray[0][n];
i++;
}
n--;
}
includeArray.le ngth = i;
//print out solution array
for (n = 0; n <= nmax; n++){
for (w = 0; w <=wmax; w++){
System.out.prin tln(solArray[n][w]);
}
}
} // main method
} // Knapsack class[/CODE]
P.S. NOOOOO, the forum messes with all my nice indentation!
Comment