我有一个Knapsack问题的天真解决方案的代码,我想获得所选项目的索引列表,目前它返回所选项目的值的总和。 任何帮助将不胜感激。 JAVA代码:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/* A Naive recursive implementation of 0-1 Knapsack problem */
class Knapsack
{
// A utility function that returns maximum of two integers
static int max(int a, int b) {
return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(float W, float wt[], int val[], int n)
{
// Base Case
if (n == 0 || W == 0)
return 0;
// If weight of the nth item is more than Knapsack capacity W, then
// this item cannot be included in the optimal solution
if (wt[n-1] > W)
{
return knapSack(W, wt, val, n-1);
}
// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else {
return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1)
);
}
}
// Driver program to test above function
public static void main(String args[])
{
int val[] = new int[]{29,74,16,55,52,75,74,35,78};
float wt[] = new float[]{85.31f,14.55f,3.98f,26.24f,63.69f,76.25f,60.02f,93.18f,89.95f};
float W = 75f;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}
目前的结果:148 预期结果:2,7
答案 0 :(得分:1)
这是你如何做到的(虽然它使用了一些额外的内存) -
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/* A Naive recursive implementation of 0-1 Knapsack problem */
class Knapsack
{
// A utility function that returns maximum of two integers
static int max(int a, int b) {
return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(float W, float wt[], int val[], int n,int visited[])
{
// Base Case
if (n == 0 || W == 0)
return 0;
// If weight of the nth item is more than Knapsack capacity W, then
// this item cannot be included in the optimal solution
if (wt[n-1] > W)
{
return knapSack(W, wt, val, n-1,visited);
}
// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else {
int v1[]=new int[visited.length];
System.arraycopy(visited, 0, v1, 0, v1.length);
int v2[]=new int[visited.length];
System.arraycopy(visited, 0, v2, 0, v2.length);
v1[n-1]=1;
int ans1 = val[n-1] + knapSack(W-wt[n-1], wt, val, n-1,v1);
int ans2 = knapSack(W, wt, val, n-1,v2);
if(ans1>ans2){
System.arraycopy(v1, 0, visited, 0, v1.length);
return ans1;
}
else{
System.arraycopy(v2, 0, visited, 0, v2.length);
return ans2;
}
}
}
// Driver program to test above function
public static void main(String args[])
{
int val[] = new int[]{29,74,16,55,52,75,74,35,78};
float wt[] = new float[]{85.31f,14.55f,3.98f,26.24f,63.69f,76.25f,60.02f,93.18f,89.95f};
float W = 75f;
int n = val.length;
int visited[] = new int[n];
System.out.println(knapSack(W, wt, val, n, visited));
for(int i=0;i<n;i++)
if(visited[i]==1)
System.out.println(i+1);
}
}
我所做的是我创建了一个访问过的数组,如果使用的是当前元素而不是标记当前元素1的访问,否则它保持为零。最后,我遍历了这个数组并打印了访问过的每个元素
答案 1 :(得分:0)
尽管已接受的解决方案可以满足需要,但我认为它存在一些缺陷。基本上,我们可以使用一个技巧,使其更具时效性,并跟踪所选项目。
背包问题可以通过动态编程来解决,这意味着,我们需要缓存中间结果并使用它们来进行更少的计算。请注意,接受的代码不存储中间结果,这意味着某些组合不止一次计算。这里有很好的描述:geeksforgeeks。 回到你的问题(正如关于geekforgeeks的文章没有说明如何恢复所选项目):
我们可以使用缓存数组来恢复所选项目: 我们从最后一个元素开始,因为我们的缓存是2d数组,这是
var r = K.length -1
car c = K(0).length -1
var curr = K(r)(c)
现在,虽然我们没有到达第一行(我们不检查第一行,因为它是辅助的),我们将当前值与前一行(同一列)的值进行比较。如果它们相同,这意味着,我们没有使用相应的项目,所以我们只去(r - 1)行。如果它们不同 - 使用了该项,我们会更新行和列。
// here I use same names as in the geekforgeeks article
// val - array of values
// K - cache for intermediate results,
// wt - array of weights
i = K.length - 1
j = W
while (i > 0) {
k = K(i)(j)
if (k != K(i - 1)(j)) {
println("selected item: val(i - 1)")
j -= wt(i - 1)
}
i -= 1
}
我在这里找到了这种方法:cs.cmu.edu