递归地解决背包问题,我想知道在提供最大价值的包中采取了哪些项目(项目的重量)。 到目前为止我有这个:
int MAX(int a, int b) { return (a > b) ? a : b ; }
int thief(int W, int weight[], int value[], int n)
{
int a,b,c;
//basecase:
if(n == 0 || weight <= 0) return 0;
// each item's weight can't be more than W:
if(weight[n-1] > W){
return thief(W, weight, value, n-1);}
a=value[n-1] + thief(W-weight[n-1], weight, value, n-1);// a: nth item included
b=thief(W, weight, value, n-1);// b:nth item not included
c= MAX(a,b);//answer is the maximum of situation a and b
if (c==a) { //if situation a occurs then nth item is included
cout<<weight[n]<<endl;
}
return c;
}
考虑n = 4且最大重量(W)= 30
重量为:30 10 20 5
和价值观:100 50 60 10
但此代码输出:20 5 20 10 5
我只想输出10和20
我也尝试定义一个默认值为false的bool数组,如果发生c == a,它的第n个元素会变为true,但这也不会给出正确的结果。
我应该递归地做。
答案 0 :(得分:2)
您的基本算法不起作用。在测试不同组合时无法进行打印。
但是,首先你必须修复一个错误:
cout<<weight[n-1]<<endl; // n-1 instead of n
您的算法执行此操作:
a = value[3] + thief(30-weight[3], weight, value, 3); // Use item 3
b = thief(30, weight, value, 3); // Don't use item 3
第二行将导致
a = value[2] + thief(30-weight[2], weight, value, 2); // Use item 2
b = thief(30, weight, value, 2); // Don't use item 2
第二行将导致
a = value[1] + thief(30-weight[1], weight, value, 1); // Use item 1
b = thief(30, weight, value, 1); // Don't use item 1
第二行将导致
a = value[0] + thief(30-weight[0], weight, value, 0); // Use item 0
b = thief(30, weight, value, 0); // Don't use item 0
这会导致
a = 30
b = 0
所以您的代码会选择item 0
并打印30
,但这是一个错误!
正如我在开始时所述:在测试不同组合时,您无法进行打印。
相反,您需要跟踪您在不同组合中使用的元素,并且只保留“最佳”。
我没有测试下面的代码,但我认为你可以这样做(假设你的代码正确计算出最佳组合):
#include <vector>
// The vector v is used for holding the index of the items selected.
// The caller must supply a vector containing the items included so far.
// This function will test whether item "n-1" shall be included or
// excluded. If item "n-1" is included the index is added to the vector.
int thief(int W, int weight[], int value[], int n, vector<int>& v) // Added vector
{
vector<int> v1, v2; // Vector to hold elements of the two combinations
int a,b,c;
//basecase:
if(n == 0 || weight <= 0) return 0;
// each item's weight can't be more than W:
if(weight[n-1] > W){
return thief(W, weight, value, n-1, v2);}
v1.push_back(n-1); // Put n-1 in vector v1 and pass the vector v1
a=value[n-1] + thief(W-weight[n-1], weight, value, n-1, v1);// a: nth item included
// Don't put anything in v2 but pass the vector v2
b=thief(W, weight, value, n-1, v2);// b:nth item not included
c= MAX(a,b);//answer is the maximum of situation a and b
if (c==a) { //if situation a occurs then nth item is included
// cout<<weight[n-1]<<endl;
// Copy elements from v1 to v
for (auto e : v1)
{
v.push_back(e);
}
}
else
{
// Copy elements from v2 to v
for (auto e : v2)
{
v.push_back(e);
}
}
return c;
}
int main() {
vector<int> v;
int weight[4] = {30, 10, 20, 5};
int value[4] = {100, 50, 60, 10};
cout << "result=" << thief(30, weight, value, 4, v) << endl;
// Print the elements used
for (auto e : v)
{
cout << "elem=" << e << endl;
}
return 0;
}
最后注意 - 你的强力方法在n的起始值的执行时间方面非常昂贵。有更好的方法可以解决这个问题。
答案 1 :(得分:0)
您可以用c编写代码吗? 我写了这个,但是没有用。 (我认为区别在于粗体)
int knapSack(int W, int wt[], int val[], int n, int arr[])
{
int x, y, c, j, arr1[50], arr2[50];
// 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, arr2);
// Return the maximum of two cases:
// x nth item included
// y not included
**arr1[n - 1] = val[n - 1];**
x = val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1, arr1);
//copyArr(arr, out, n);
y = knapSack(W, wt, val, n - 1, arr2);
if (x > y)
c = x;
else
c = y;
if (c == x)
for (j = 0; j < 50; j++)
arr[j] = arr1[j];
else
for (j = 0; j < 50; j++)
arr[j] = arr2[j];
return c;
}