查找背包中携带的物品

Posted

技术标签:

【中文标题】查找背包中携带的物品【英文标题】:Find which items are taken in knapsack 【发布时间】:2015-12-25 10:33:51 【问题描述】:

递归解决背包问题,我想知道袋子里拿了哪些物品(物品的重量)给出的最大值。 到目前为止,我有这个:

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,但这也不会给出正确的结果。 我应该递归地这样做。

【问题讨论】:

【参考方案1】:

您的基本算法不起作用。当您测试不同的组合时,您无法进行打印。

但是,首先你必须修复一个错误:

    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 的起始值很高。有很多更好的方法可以解决这个问题。

【讨论】:

【参考方案2】:

你能用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;

【讨论】:

欢迎来到 SO。哪个位是粗体?

以上是关于查找背包中携带的物品的主要内容,如果未能解决你的问题,请参考以下文章

动态规划-0-1背包

0-1 背包再访

Tao Tao要吃鸡

crash DP上下班路上学动态规划

怎么用ce查找单机游戏的物品代码

背包问题变体 - 将重量和价值最大化到极限